Skip to content

Commit

Permalink
cleanup: Reduce and normalize import path aliasing. (#6975)
Browse files Browse the repository at this point in the history
The code in the Tendermint repository makes heavy use of import aliasing.
This is made necessary by our extensive reuse of common base package names, and
by repetition of similar names across different subdirectories.

Unfortunately we have not been very consistent about which packages we alias in
various circumstances, and the aliases we use vary. In the spirit of the advice
in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports,
his change makes an effort to clean up and normalize import aliasing.

This change makes no API or behavioral changes. It is a pure cleanup intended
o help make the code more readable to developers (including myself) trying to
understand what is being imported where.

Only unexported names have been modified, and the changes were generated and
applied mechanically with gofmt -r and comby, respecting the lexical and
syntactic rules of Go.  Even so, I did not fix every inconsistency. Where the
changes would be too disruptive, I left it alone.

The principles I followed in this cleanup are:

- Remove aliases that restate the package name.
- Remove aliases where the base package name is unambiguous.
- Move overly-terse abbreviations from the import to the usage site.
- Fix lexical issues (remove underscores, remove capitalization).
- Fix import groupings to more closely match the style guide.
- Group blank (side-effecting) imports and ensure they are commented.
- Add aliases to multiple imports with the same base package name.
  • Loading branch information
M. J. Fromberger authored Sep 23, 2021
1 parent c9beef7 commit cf7537e
Show file tree
Hide file tree
Showing 127 changed files with 1,473 additions and 1,473 deletions.
14 changes: 7 additions & 7 deletions abci/example/kvstore/persistent_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"github.com/tendermint/tendermint/abci/example/code"
"github.com/tendermint/tendermint/abci/types"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/libs/log"
pc "github.com/tendermint/tendermint/proto/tendermint/crypto"
cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
)

const (
Expand All @@ -30,7 +30,7 @@ type PersistentKVStoreApplication struct {
// validator set
ValUpdates []types.ValidatorUpdate

valAddrToPubKeyMap map[string]pc.PublicKey
valAddrToPubKeyMap map[string]cryptoproto.PublicKey

logger log.Logger
}
Expand All @@ -46,7 +46,7 @@ func NewPersistentKVStoreApplication(dbDir string) *PersistentKVStoreApplication

return &PersistentKVStoreApplication{
app: &Application{state: state},
valAddrToPubKeyMap: make(map[string]pc.PublicKey),
valAddrToPubKeyMap: make(map[string]cryptoproto.PublicKey),
logger: log.NewNopLogger(),
}
}
Expand Down Expand Up @@ -194,8 +194,8 @@ func (app *PersistentKVStoreApplication) Validators() (validators []types.Valida
return
}

func MakeValSetChangeTx(pubkey pc.PublicKey, power int64) []byte {
pk, err := cryptoenc.PubKeyFromProto(pubkey)
func MakeValSetChangeTx(pubkey cryptoproto.PublicKey, power int64) []byte {
pk, err := encoding.PubKeyFromProto(pubkey)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon

// add, update, or remove a validator
func (app *PersistentKVStoreApplication) updateValidator(v types.ValidatorUpdate) types.ResponseDeliverTx {
pubkey, err := cryptoenc.PubKeyFromProto(v.PubKey)
pubkey, err := encoding.PubKeyFromProto(v.PubKey)
if err != nil {
panic(fmt.Errorf("can't decode public key: %w", err))
}
Expand Down
8 changes: 4 additions & 4 deletions abci/types/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
fmt "fmt"

"github.com/tendermint/tendermint/crypto/ed25519"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
)

func Ed25519ValidatorUpdate(pk []byte, power int64) ValidatorUpdate {
pke := ed25519.PubKey(pk)

pkp, err := cryptoenc.PubKeyToProto(pke)
pkp, err := encoding.PubKeyToProto(pke)
if err != nil {
panic(err)
}
Expand All @@ -29,7 +29,7 @@ func UpdateValidator(pk []byte, power int64, keyType string) ValidatorUpdate {
return Ed25519ValidatorUpdate(pk, power)
case secp256k1.KeyType:
pke := secp256k1.PubKey(pk)
pkp, err := cryptoenc.PubKeyToProto(pke)
pkp, err := encoding.PubKeyToProto(pke)
if err != nil {
panic(err)
}
Expand All @@ -39,7 +39,7 @@ func UpdateValidator(pk []byte, power int64, keyType string) ValidatorUpdate {
}
case sr25519.KeyType:
pke := sr25519.PubKey(pk)
pkp, err := cryptoenc.PubKeyToProto(pke)
pkp, err := encoding.PubKeyToProto(pke)
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/tendermint/commands/debug/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
Expand Down Expand Up @@ -65,9 +65,9 @@ func dumpCmdHandler(_ *cobra.Command, args []string) error {
}

home := viper.GetString(cli.HomeFlag)
conf := cfg.DefaultConfig()
conf := config.DefaultConfig()
conf = conf.SetRoot(home)
cfg.EnsureRoot(conf.RootDir)
config.EnsureRoot(conf.RootDir)

dumpDebugData(outDir, conf, rpc)

Expand All @@ -79,7 +79,7 @@ func dumpCmdHandler(_ *cobra.Command, args []string) error {
return nil
}

func dumpDebugData(outDir string, conf *cfg.Config, rpc *rpchttp.HTTP) {
func dumpDebugData(outDir string, conf *config.Config, rpc *rpchttp.HTTP) {
start := time.Now().UTC()

tmpDir, err := ioutil.TempDir(outDir, "tendermint_debug_tmp")
Expand Down
6 changes: 3 additions & 3 deletions cmd/tendermint/commands/debug/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
Expand Down Expand Up @@ -50,9 +50,9 @@ func killCmdHandler(cmd *cobra.Command, args []string) error {
}

home := viper.GetString(cli.HomeFlag)
conf := cfg.DefaultConfig()
conf := config.DefaultConfig()
conf = conf.SetRoot(home)
cfg.EnsureRoot(conf.RootDir)
config.EnsureRoot(conf.RootDir)

// Create a temporary directory which will contain all the state dumps and
// relevant files and directories that will be compressed into a file.
Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/debug/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path"
"path/filepath"

cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/config"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

Expand Down Expand Up @@ -48,7 +48,7 @@ func dumpConsensusState(rpc *rpchttp.HTTP, dir, filename string) error {

// copyWAL copies the Tendermint node's WAL file. It returns an error if the
// WAL file cannot be read or copied.
func copyWAL(conf *cfg.Config, dir string) error {
func copyWAL(conf *config.Config, dir string) error {
walPath := conf.Consensus.WalFile()
walFile := filepath.Base(walPath)

Expand Down
1 change: 0 additions & 1 deletion cmd/tendermint/commands/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/spf13/cobra"

dbm "github.com/tendermint/tm-db"

"github.com/tendermint/tendermint/libs/log"
Expand Down
19 changes: 10 additions & 9 deletions cmd/tendermint/commands/reindex_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/spf13/cobra"
tmdb "github.com/tendermint/tm-db"
dbm "github.com/tendermint/tm-db"

abcitypes "github.com/tendermint/tendermint/abci/types"
tmcfg "github.com/tendermint/tendermint/config"
Expand All @@ -15,7 +15,7 @@ import (
"github.com/tendermint/tendermint/internal/state/indexer"
"github.com/tendermint/tendermint/internal/state/indexer/sink/kv"
"github.com/tendermint/tendermint/internal/state/indexer/sink/psql"
ctypes "github.com/tendermint/tendermint/rpc/coretypes"
"github.com/tendermint/tendermint/rpc/coretypes"
"github.com/tendermint/tendermint/store"
"github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -129,17 +129,17 @@ func loadEventSinks(cfg *tmcfg.Config) ([]indexer.EventSink, error) {
}

func loadStateAndBlockStore(cfg *tmcfg.Config) (*store.BlockStore, state.Store, error) {
dbType := tmdb.BackendType(cfg.DBBackend)
dbType := dbm.BackendType(cfg.DBBackend)

// Get BlockStore
blockStoreDB, err := tmdb.NewDB("blockstore", dbType, cfg.DBDir())
blockStoreDB, err := dbm.NewDB("blockstore", dbType, cfg.DBDir())
if err != nil {
return nil, nil, err
}
blockStore := store.NewBlockStore(blockStoreDB)

// Get StateStore
stateDB, err := tmdb.NewDB("state", dbType, cfg.DBDir())
stateDB, err := dbm.NewDB("state", dbType, cfg.DBDir())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -221,14 +221,15 @@ func checkValidHeight(bs state.BlockStore) error {
}

if startHeight < base {
return fmt.Errorf("%s (requested start height: %d, base height: %d)", ctypes.ErrHeightNotAvailable, startHeight, base)
return fmt.Errorf("%s (requested start height: %d, base height: %d)",
coretypes.ErrHeightNotAvailable, startHeight, base)
}

height := bs.Height()

if startHeight > height {
return fmt.Errorf(
"%s (requested start height: %d, store height: %d)", ctypes.ErrHeightNotAvailable, startHeight, height)
"%s (requested start height: %d, store height: %d)", coretypes.ErrHeightNotAvailable, startHeight, height)
}

if endHeight == 0 || endHeight > height {
Expand All @@ -238,13 +239,13 @@ func checkValidHeight(bs state.BlockStore) error {

if endHeight < base {
return fmt.Errorf(
"%s (requested end height: %d, base height: %d)", ctypes.ErrHeightNotAvailable, endHeight, base)
"%s (requested end height: %d, base height: %d)", coretypes.ErrHeightNotAvailable, endHeight, base)
}

if endHeight < startHeight {
return fmt.Errorf(
"%s (requested the end height: %d is less than the start height: %d)",
ctypes.ErrInvalidRequest, startHeight, endHeight)
coretypes.ErrInvalidRequest, startHeight, endHeight)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/tendermint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

cmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/cmd/tendermint/commands/debug"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
nm "github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/node"
)

func main() {
Expand Down Expand Up @@ -42,12 +42,12 @@ func main() {
// * Provide their own DB implementation
// can copy this file and use something other than the
// node.NewDefault function
nodeFunc := nm.NewDefault
nodeFunc := node.NewDefault

// Create & start node
rootCmd.AddCommand(cmd.NewRunNodeCmd(nodeFunc))

cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv(filepath.Join("$HOME", cfg.DefaultTendermintDir)))
cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv(filepath.Join("$HOME", config.DefaultTendermintDir)))
if err := cmd.Execute(); err != nil {
panic(err)
}
Expand Down
11 changes: 6 additions & 5 deletions config/db.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

import (
dbm "github.com/tendermint/tm-db"

"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/service"
db "github.com/tendermint/tm-db"
)

// ServiceProvider takes a config and a logger and returns a ready to go Node.
Expand All @@ -16,11 +17,11 @@ type DBContext struct {
}

// DBProvider takes a DBContext and returns an instantiated DB.
type DBProvider func(*DBContext) (db.DB, error)
type DBProvider func(*DBContext) (dbm.DB, error)

// DefaultDBProvider returns a database using the DBBackend and DBDir
// specified in the Config.
func DefaultDBProvider(ctx *DBContext) (db.DB, error) {
dbType := db.BackendType(ctx.Config.DBBackend)
return db.NewDB(ctx.ID, dbType, ctx.Config.DBDir())
func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) {
dbType := dbm.BackendType(ctx.Config.DBBackend)
return dbm.NewDB(ctx.ID, dbType, ctx.Config.DBDir())
}
32 changes: 16 additions & 16 deletions crypto/encoding/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ import (
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
"github.com/tendermint/tendermint/libs/json"
pc "github.com/tendermint/tendermint/proto/tendermint/crypto"
cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
)

func init() {
json.RegisterType((*pc.PublicKey)(nil), "tendermint.crypto.PublicKey")
json.RegisterType((*pc.PublicKey_Ed25519)(nil), "tendermint.crypto.PublicKey_Ed25519")
json.RegisterType((*pc.PublicKey_Secp256K1)(nil), "tendermint.crypto.PublicKey_Secp256K1")
json.RegisterType((*cryptoproto.PublicKey)(nil), "tendermint.crypto.PublicKey")
json.RegisterType((*cryptoproto.PublicKey_Ed25519)(nil), "tendermint.crypto.PublicKey_Ed25519")
json.RegisterType((*cryptoproto.PublicKey_Secp256K1)(nil), "tendermint.crypto.PublicKey_Secp256K1")
}

// PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey
func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
var kp pc.PublicKey
func PubKeyToProto(k crypto.PubKey) (cryptoproto.PublicKey, error) {
var kp cryptoproto.PublicKey
switch k := k.(type) {
case ed25519.PubKey:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Ed25519{
kp = cryptoproto.PublicKey{
Sum: &cryptoproto.PublicKey_Ed25519{
Ed25519: k,
},
}
case secp256k1.PubKey:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Secp256K1{
kp = cryptoproto.PublicKey{
Sum: &cryptoproto.PublicKey_Secp256K1{
Secp256K1: k,
},
}
case sr25519.PubKey:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Sr25519{
kp = cryptoproto.PublicKey{
Sum: &cryptoproto.PublicKey_Sr25519{
Sr25519: k,
},
}
Expand All @@ -46,25 +46,25 @@ func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
}

// PubKeyFromProto takes a protobuf Pubkey and transforms it to a crypto.Pubkey
func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
func PubKeyFromProto(k cryptoproto.PublicKey) (crypto.PubKey, error) {
switch k := k.Sum.(type) {
case *pc.PublicKey_Ed25519:
case *cryptoproto.PublicKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeySize)
}
pk := make(ed25519.PubKey, ed25519.PubKeySize)
copy(pk, k.Ed25519)
return pk, nil
case *pc.PublicKey_Secp256K1:
case *cryptoproto.PublicKey_Secp256K1:
if len(k.Secp256K1) != secp256k1.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeySecp256k1. Got %d, expected %d",
len(k.Secp256K1), secp256k1.PubKeySize)
}
pk := make(secp256k1.PubKey, secp256k1.PubKeySize)
copy(pk, k.Secp256K1)
return pk, nil
case *pc.PublicKey_Sr25519:
case *cryptoproto.PublicKey_Sr25519:
if len(k.Sr25519) != sr25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeySr25519. Got %d, expected %d",
len(k.Sr25519), sr25519.PubKeySize)
Expand Down
3 changes: 1 addition & 2 deletions crypto/secp256k1/secp256k1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"math/big"
"testing"

underlyingSecp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"

underlyingSecp256k1 "github.com/btcsuite/btcd/btcec"
)

type keyData struct {
Expand Down
Loading

0 comments on commit cf7537e

Please sign in to comment.