Skip to content

Commit

Permalink
libs/common: Refactor libs/common 5 (tendermint#4240)
Browse files Browse the repository at this point in the history
* libs/common: Refactor libs/common 5

- move mathematical functions and types out of `libs/common` to math pkg
- move net functions out of `libs/common` to net pkg
- move string functions out of `libs/common` to strings pkg
- move async functions out of `libs/common` to async pkg
- move bit functions out of `libs/common` to bits pkg
- move cmap functions out of `libs/common` to cmap pkg
- move os functions out of `libs/common` to os pkg

Signed-off-by: Marko Baricevic <[email protected]>

* fix testing issues

* fix tests

closes #41417

woooooooooooooooooo kill the cmn pkg

Signed-off-by: Marko Baricevic <[email protected]>

* add changelog entry

* fix goimport issues

* run gofmt
  • Loading branch information
tac0turtle authored Dec 12, 2019
1 parent 7e02211 commit 7b52f51
Show file tree
Hide file tree
Showing 83 changed files with 293 additions and 471 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ program](https://hackerone.com/tendermint).
- [libs/common] \#4237 Move byte functions from `libs/common` into pkg `bytes`
- [libs/common] \#4237 Move throttletimer functions from `libs/common` into pkg `timer`
- [libs/common] \#4237 Move tempfile functions from `libs/common` into pkg `tempfile`
- [libs/common] \#4240 Move os functions from `libs/common` into pkg `os`
- [libs/common] \#4240 Move net functions from `libs/common` into pkg `net`
- [libs/common] \#4240 Move mathematical functions and types out of `libs/common` to `math` pkg
- [libs/common] \#4240 Move string functions out of `libs/common` to `strings` pkg
- [libs/common] \#4240 Move async functions out of `libs/common` to `async` pkg
- [libs/common] \#4240 Move bit functions out of `libs/common` to `bits` pkg
- [libs/common] \#4240 Move cmap functions out of `libs/common` to `cmap` pkg
- Blockchain Protocol
Expand Down
4 changes: 2 additions & 2 deletions abci/client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"google.golang.org/grpc"

"github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmnet "github.com/tendermint/tendermint/libs/net"
"github.com/tendermint/tendermint/libs/service"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ func NewGRPCClient(addr string, mustConnect bool) *grpcClient {
}

func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
return cmn.Connect(addr)
return tmnet.Connect(addr)
}

func (cli *grpcClient) OnStart() error {
Expand Down
4 changes: 2 additions & 2 deletions abci/client/socket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmnet "github.com/tendermint/tendermint/libs/net"
"github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/libs/timer"
)
Expand Down Expand Up @@ -62,7 +62,7 @@ func (cli *socketClient) OnStart() error {
var conn net.Conn
RETRY_LOOP:
for {
conn, err = cmn.Connect(cli.addr)
conn, err = tmnet.Connect(cli.addr)
if err != nil {
if cli.mustConnect {
return err
Expand Down
6 changes: 3 additions & 3 deletions abci/cmd/abci-cli/abci-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

"github.com/spf13/cobra"

cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"

abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/example/code"
Expand Down Expand Up @@ -640,7 +640,7 @@ func cmdCounter(cmd *cobra.Command, args []string) error {
}

// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
tmos.TrapSignal(logger, func() {
// Cleanup
srv.Stop()
})
Expand Down Expand Up @@ -672,7 +672,7 @@ func cmdKVStore(cmd *cobra.Command, args []string) error {
}

// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
tmos.TrapSignal(logger, func() {
// Cleanup
srv.Stop()
})
Expand Down
4 changes: 2 additions & 2 deletions abci/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"golang.org/x/net/context"

cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
tmnet "github.com/tendermint/tendermint/libs/net"

abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/example/code"
Expand Down Expand Up @@ -108,7 +108,7 @@ func testStream(t *testing.T, app types.Application) {
// test grpc

func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
return cmn.Connect(addr)
return tmnet.Connect(addr)
}

func testGRPCSync(t *testing.T, app types.ABCIApplicationServer) {
Expand Down
4 changes: 2 additions & 2 deletions abci/server/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"google.golang.org/grpc"

"github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmnet "github.com/tendermint/tendermint/libs/net"
"github.com/tendermint/tendermint/libs/service"
)

Expand All @@ -23,7 +23,7 @@ type GRPCServer struct {

// NewGRPCServer returns a new gRPC ABCI server
func NewGRPCServer(protoAddr string, app types.ABCIApplicationServer) service.Service {
proto, addr := cmn.ProtocolAndAddress(protoAddr)
proto, addr := tmnet.ProtocolAndAddress(protoAddr)
s := &GRPCServer{
proto: proto,
addr: addr,
Expand Down
4 changes: 2 additions & 2 deletions abci/server/socket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"

"github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmnet "github.com/tendermint/tendermint/libs/net"
"github.com/tendermint/tendermint/libs/service"
)

Expand All @@ -30,7 +30,7 @@ type SocketServer struct {
}

func NewSocketServer(protoAddr string, app types.Application) service.Service {
proto, addr := cmn.ProtocolAndAddress(protoAddr)
proto, addr := tmnet.ProtocolAndAddress(protoAddr)
s := &SocketServer{
proto: proto,
addr: addr,
Expand Down
4 changes: 2 additions & 2 deletions abci/tests/benchmarks/parallel/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"log"

"github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmnet "github.com/tendermint/tendermint/libs/net"
)

func main() {

conn, err := cmn.Connect("unix://test.sock")
conn, err := tmnet.Connect("unix://test.sock")
if err != nil {
log.Fatal(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions abci/tests/benchmarks/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"reflect"

"github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmnet "github.com/tendermint/tendermint/libs/net"
)

func main() {

conn, err := cmn.Connect("unix://test.sock")
conn, err := tmnet.Connect("unix://test.sock")
if err != nil {
log.Fatal(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions blockchain/v1/reactor_fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

"github.com/stretchr/testify/assert"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
tmmath "github.com/tendermint/tendermint/libs/math"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/types"
Expand Down Expand Up @@ -736,7 +736,7 @@ func makeCorrectTransitionSequence(startingHeight int64, numBlocks int64, numPee
continue
}
if randomPeerHeights {
peerHeights[i] = int64(cmn.MaxInt(rand.RandIntn(int(numBlocks)), int(startingHeight)+1))
peerHeights[i] = int64(tmmath.MaxInt(rand.RandIntn(int(numBlocks)), int(startingHeight)+1))
} else {
peerHeights[i] = numBlocks
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/priv_val_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"time"

"github.com/tendermint/tendermint/crypto/ed25519"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
tmnet "github.com/tendermint/tendermint/libs/net"
tmos "github.com/tendermint/tendermint/libs/os"

"github.com/tendermint/tendermint/privval"
)
Expand Down Expand Up @@ -36,7 +37,7 @@ func main() {
pv := privval.LoadFilePV(*privValKeyPath, *privValStatePath)

var dialer privval.SocketDialer
protocol, address := cmn.ProtocolAndAddress(*addr)
protocol, address := tmnet.ProtocolAndAddress(*addr)
switch protocol {
case "unix":
dialer = privval.DialUnixFn(address)
Expand All @@ -57,7 +58,7 @@ func main() {
}

// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
tmos.TrapSignal(logger, func() {
err := ss.Stop()
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/gen_node_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

cmn "github.com/tendermint/tendermint/libs/common"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/p2p"
)

Expand All @@ -19,7 +19,7 @@ var GenNodeKeyCmd = &cobra.Command{

func genNodeKey(cmd *cobra.Command, args []string) error {
nodeKeyFile := config.NodeKeyFile()
if cmn.FileExists(nodeKeyFile) {
if tmos.FileExists(nodeKeyFile) {
return fmt.Errorf("node key at %s already exists", nodeKeyFile)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/tendermint/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config"
cmn "github.com/tendermint/tendermint/libs/common"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval"
Expand All @@ -29,7 +29,7 @@ func initFilesWithConfig(config *cfg.Config) error {
privValKeyFile := config.PrivValidatorKeyFile()
privValStateFile := config.PrivValidatorStateFile()
var pv *privval.FilePV
if cmn.FileExists(privValKeyFile) {
if tmos.FileExists(privValKeyFile) {
pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
logger.Info("Found private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
Expand All @@ -41,7 +41,7 @@ func initFilesWithConfig(config *cfg.Config) error {
}

nodeKeyFile := config.NodeKeyFile()
if cmn.FileExists(nodeKeyFile) {
if tmos.FileExists(nodeKeyFile) {
logger.Info("Found node key", "path", nodeKeyFile)
} else {
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
Expand All @@ -52,7 +52,7 @@ func initFilesWithConfig(config *cfg.Config) error {

// genesis file
genFile := config.GenesisFile()
if cmn.FileExists(genFile) {
if tmos.FileExists(genFile) {
logger.Info("Found genesis file", "path", genFile)
} else {
genDoc := types.GenesisDoc{
Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/lite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

cmn "github.com/tendermint/tendermint/libs/common"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/lite/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"
)
Expand Down Expand Up @@ -65,7 +65,7 @@ func EnsureAddrHasSchemeOrDefaultToTCP(addr string) (string, error) {

func runProxy(cmd *cobra.Command, args []string) error {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
tmos.TrapSignal(logger, func() {
// TODO: close up shop
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/reset_priv_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/spf13/cobra"

cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/privval"
)

Expand Down Expand Up @@ -58,7 +58,7 @@ func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logg
logger.Error("Error removing all blockchain history", "dir", dbDir, "err", err)
}
// recreate the dbDir since the privVal state needs to live there
cmn.EnsureDir(dbDir, 0700)
tmos.EnsureDir(dbDir, 0700)
resetFilePV(privValKeyFile, privValStateFile, logger)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
cmn "github.com/tendermint/tendermint/libs/common"
tmos "github.com/tendermint/tendermint/libs/os"
)

var (
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestRootConfig(t *testing.T) {

// XXX: path must match cfg.defaultConfigPath
configFilePath := filepath.Join(defaultRoot, "config")
err := cmn.EnsureDir(configFilePath, 0700)
err := tmos.EnsureDir(configFilePath, 0700)
require.Nil(t, err)

// write the non-defaults to a different path
Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/run_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"

cfg "github.com/tendermint/tendermint/config"
cmn "github.com/tendermint/tendermint/libs/common"
tmos "github.com/tendermint/tendermint/libs/os"
nm "github.com/tendermint/tendermint/node"
)

Expand Down Expand Up @@ -105,7 +105,7 @@ func NewRunNodeCmd(nodeProvider nm.Provider) *cobra.Command {
}

// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
tmos.TrapSignal(logger, func() {
if n.IsRunning() {
n.Stop()
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tendermint/commands/show_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

cmn "github.com/tendermint/tendermint/libs/common"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/privval"
)

Expand All @@ -19,7 +19,7 @@ var ShowValidatorCmd = &cobra.Command{

func showValidator(cmd *cobra.Command, args []string) error {
keyFilePath := config.PrivValidatorKeyFile()
if !cmn.FileExists(keyFilePath) {
if !tmos.FileExists(keyFilePath) {
return fmt.Errorf("private validator file %s does not exist", keyFilePath)
}

Expand Down
Loading

0 comments on commit 7b52f51

Please sign in to comment.