diff --git a/blockstore/blockstore.go b/blockstore/blockstore.go deleted file mode 100644 index 4e9ce4983..000000000 --- a/blockstore/blockstore.go +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package blockstore - -import ( - "fmt" - "io/ioutil" - "math/big" - "os" - "path/filepath" - - "github.com/ChainSafe/chainbridge-utils/msg" -) - -const PathPostfix = ".chainbridge/blockstore" - -type Blockstorer interface { - StoreBlock(*big.Int) error -} - -var _ Blockstorer = &EmptyStore{} -var _ Blockstorer = &Blockstore{} - -// Dummy store for testing only -type EmptyStore struct{} - -func (s *EmptyStore) StoreBlock(_ *big.Int) error { return nil } - -// Blockstore implements Blockstorer. -type Blockstore struct { - path string // Path excluding filename - fullPath string - chain msg.ChainId - relayer string -} - -func NewBlockstore(path string, chain msg.ChainId, relayer string) (*Blockstore, error) { - fileName := getFileName(chain, relayer) - if path == "" { - def, err := getDefaultPath() - if err != nil { - return nil, err - } - path = def - } - - return &Blockstore{ - path: path, - fullPath: filepath.Join(path, fileName), - chain: chain, - relayer: relayer, - }, nil -} - -// StoreBlock writes the block number to disk. -func (b *Blockstore) StoreBlock(block *big.Int) error { - // Create dir if it does not exist - if _, err := os.Stat(b.path); os.IsNotExist(err) { - errr := os.MkdirAll(b.path, os.ModePerm) - if errr != nil { - return errr - } - } - - // Write bytes to file - data := []byte(block.String()) - err := ioutil.WriteFile(b.fullPath, data, 0600) - if err != nil { - return err - } - return nil -} - -// TryLoadLatestBlock will attempt to load the latest block for the chain/relayer pair, returning 0 if not found. -// Passing an empty string for path will cause it to use the home directory. -func (b *Blockstore) TryLoadLatestBlock() (*big.Int, error) { - // If it exists, load and return - exists, err := fileExists(b.fullPath) - if err != nil { - return nil, err - } - if exists { - dat, err := ioutil.ReadFile(b.fullPath) - if err != nil { - return nil, err - } - block, _ := big.NewInt(0).SetString(string(dat), 10) - return block, nil - } - // Otherwise just return 0 - return big.NewInt(0), nil -} - -func getFileName(chain msg.ChainId, relayer string) string { - return fmt.Sprintf("%s-%d.block", relayer, chain) -} - -// getHomePath returns the home directory joined with PathPostfix -func getDefaultPath() (string, error) { - home, err := os.UserHomeDir() - if err != nil { - return "", err - } - - return filepath.Join(home, PathPostfix), nil -} - -func fileExists(fileName string) (bool, error) { - _, err := os.Stat(fileName) - if os.IsNotExist(err) { - return false, nil - } else if err != nil { - return false, err - } - return true, nil -} diff --git a/blockstore/blockstore_test.go b/blockstore/blockstore_test.go deleted file mode 100644 index aa375a712..000000000 --- a/blockstore/blockstore_test.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package blockstore - -import ( - "io/ioutil" - "math/big" - "os" - "testing" - - "github.com/ChainSafe/chainbridge-utils/keystore" - "github.com/ChainSafe/chainbridge-utils/msg" -) - -func TestSaveAndLoad(t *testing.T) { - dir, err := ioutil.TempDir(os.TempDir(), "blockstore") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) - - chain := msg.ChainId(10) - relayer := keystore.AliceSr25519.Address() - - bs, err := NewBlockstore(dir, chain, relayer) - if err != nil { - t.Fatal(err) - } - // Load non-existent dir/file - block, err := bs.TryLoadLatestBlock() - if err != nil { - t.Fatal(err) - } - - if block.Uint64() != uint64(0) { - t.Fatalf("Expected: %d got: %d", 0, block.Uint64()) - } - - // Save block number - block = big.NewInt(999) - err = bs.StoreBlock(block) - if err != nil { - t.Fatal(err) - } - - // Load block number - latest, err := bs.TryLoadLatestBlock() - if err != nil { - t.Fatal(err) - } - - if block.Uint64() != latest.Uint64() { - t.Fatalf("Expected: %d got: %d", block.Uint64(), latest.Uint64()) - } - - // Save block number again - block = big.NewInt(1234) - err = bs.StoreBlock(block) - if err != nil { - t.Fatal(err) - } - - // Load block number - latest, err = bs.TryLoadLatestBlock() - if err != nil { - t.Fatal(err) - } - - if block.Uint64() != latest.Uint64() { - t.Fatalf("Expected: %d got: %d", block.Uint64(), latest.Uint64()) - } -} diff --git a/chains/ethereum/chain.go b/chains/ethereum/chain.go index a24aeb525..7cb4638c3 100644 --- a/chains/ethereum/chain.go +++ b/chains/ethereum/chain.go @@ -28,13 +28,12 @@ import ( erc20Handler "github.com/ChainSafe/ChainBridge/bindings/ERC20Handler" erc721Handler "github.com/ChainSafe/ChainBridge/bindings/ERC721Handler" "github.com/ChainSafe/ChainBridge/bindings/GenericHandler" - "github.com/ChainSafe/ChainBridge/blockstore" connection "github.com/ChainSafe/ChainBridge/connections/ethereum" - "github.com/ChainSafe/ChainBridge/core" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" - "github.com/ChainSafe/ChainBridge/router" + "github.com/ChainSafe/chainbridge-utils/blockstore" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/crypto/secp256k1" "github.com/ChainSafe/chainbridge-utils/keystore" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -178,7 +177,7 @@ func InitializeChain(chainCfg *core.ChainConfig, logger log15.Logger, sysErr cha }, nil } -func (c *Chain) SetRouter(r *router.Router) { +func (c *Chain) SetRouter(r *core.Router) { r.Listen(c.cfg.Id, c.writer) c.listener.setRouter(r) } diff --git a/chains/ethereum/chain_test.go b/chains/ethereum/chain_test.go index 780df2f25..fde0157cb 100644 --- a/chains/ethereum/chain_test.go +++ b/chains/ethereum/chain_test.go @@ -8,9 +8,8 @@ import ( "testing" "time" - "github.com/ChainSafe/ChainBridge/core" - "github.com/ChainSafe/ChainBridge/router" ethtest "github.com/ChainSafe/ChainBridge/shared/ethereum/testing" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/keystore" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ethereum/go-ethereum/common" @@ -103,7 +102,7 @@ func TestChain_WriterShutdownOnFailure(t *testing.T) { t.Fatal(err) } - r := router.NewRouter(TestLogger) + r := core.NewRouter(TestLogger) chain.SetRouter(r) err = chain.Start() diff --git a/chains/ethereum/config.go b/chains/ethereum/config.go index 9965aa72b..fc85d8a6b 100644 --- a/chains/ethereum/config.go +++ b/chains/ethereum/config.go @@ -8,8 +8,8 @@ import ( "fmt" "math/big" - "github.com/ChainSafe/ChainBridge/core" utils "github.com/ChainSafe/ChainBridge/shared/ethereum" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ethereum/go-ethereum/common" ) diff --git a/chains/ethereum/config_test.go b/chains/ethereum/config_test.go index 90915c3f3..6b922fe5e 100644 --- a/chains/ethereum/config_test.go +++ b/chains/ethereum/config_test.go @@ -8,7 +8,7 @@ import ( "reflect" "testing" - "github.com/ChainSafe/ChainBridge/core" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ethereum/go-ethereum/common" ) diff --git a/chains/ethereum/listener.go b/chains/ethereum/listener.go index e447f6db6..764e3cfb6 100644 --- a/chains/ethereum/listener.go +++ b/chains/ethereum/listener.go @@ -14,10 +14,10 @@ import ( "github.com/ChainSafe/ChainBridge/bindings/ERC20Handler" "github.com/ChainSafe/ChainBridge/bindings/ERC721Handler" "github.com/ChainSafe/ChainBridge/bindings/GenericHandler" - "github.com/ChainSafe/ChainBridge/blockstore" "github.com/ChainSafe/ChainBridge/chains" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" utils "github.com/ChainSafe/ChainBridge/shared/ethereum" + "github.com/ChainSafe/chainbridge-utils/blockstore" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" eth "github.com/ethereum/go-ethereum" diff --git a/chains/ethereum/listener_test.go b/chains/ethereum/listener_test.go index 8d209cf25..8b9cbc607 100644 --- a/chains/ethereum/listener_test.go +++ b/chains/ethereum/listener_test.go @@ -14,9 +14,9 @@ import ( "github.com/ChainSafe/ChainBridge/bindings/ERC20Handler" "github.com/ChainSafe/ChainBridge/bindings/ERC721Handler" "github.com/ChainSafe/ChainBridge/bindings/GenericHandler" - "github.com/ChainSafe/ChainBridge/blockstore" utils "github.com/ChainSafe/ChainBridge/shared/ethereum" ethtest "github.com/ChainSafe/ChainBridge/shared/ethereum/testing" + "github.com/ChainSafe/chainbridge-utils/blockstore" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" "github.com/ethereum/go-ethereum/common" diff --git a/chains/ethereum/writer.go b/chains/ethereum/writer.go index 0a48bb7a7..ec00d61dd 100644 --- a/chains/ethereum/writer.go +++ b/chains/ethereum/writer.go @@ -5,13 +5,13 @@ package ethereum import ( "github.com/ChainSafe/ChainBridge/bindings/Bridge" - "github.com/ChainSafe/ChainBridge/chains" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" + "github.com/ChainSafe/chainbridge-utils/core" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" ) -var _ chains.Writer = &writer{} +var _ core.Writer = &writer{} // https://github.com/ChainSafe/chainbridge-solidity/blob/b5ed13d9798feb7c340e737a726dd415b8815366/contracts/Bridge.sol#L20 var PassedStatus uint8 = 2 diff --git a/chains/interfaces.go b/chains/interfaces.go index 73d21c0d8..294319144 100644 --- a/chains/interfaces.go +++ b/chains/interfaces.go @@ -11,6 +11,6 @@ type Router interface { Send(message msg.Message) error } -type Writer interface { - ResolveMessage(message msg.Message) bool -} +//type Writer interface { +// ResolveMessage(message msg.Message) bool +//} diff --git a/chains/substrate/chain.go b/chains/substrate/chain.go index ff83f69fa..6b0861906 100644 --- a/chains/substrate/chain.go +++ b/chains/substrate/chain.go @@ -24,12 +24,11 @@ As the writer receives messages from the router, it constructs proposals. If a p package substrate import ( - "github.com/ChainSafe/ChainBridge/blockstore" - "github.com/ChainSafe/ChainBridge/core" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" - "github.com/ChainSafe/ChainBridge/router" + "github.com/ChainSafe/chainbridge-utils/blockstore" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/crypto/sr25519" "github.com/ChainSafe/chainbridge-utils/keystore" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" ) @@ -128,7 +127,7 @@ func (c *Chain) Start() error { return nil } -func (c *Chain) SetRouter(r *router.Router) { +func (c *Chain) SetRouter(r *core.Router) { r.Listen(c.cfg.Id, c.writer) c.listener.setRouter(r) } diff --git a/chains/substrate/config.go b/chains/substrate/config.go index 84f40591b..2cb104b42 100644 --- a/chains/substrate/config.go +++ b/chains/substrate/config.go @@ -6,7 +6,7 @@ package substrate import ( "strconv" - "github.com/ChainSafe/ChainBridge/core" + "github.com/ChainSafe/chainbridge-utils/core" ) func parseStartBlock(cfg *core.ChainConfig) uint64 { diff --git a/chains/substrate/config_test.go b/chains/substrate/config_test.go index 0cb106c5e..43cd24346 100644 --- a/chains/substrate/config_test.go +++ b/chains/substrate/config_test.go @@ -6,7 +6,7 @@ package substrate import ( "testing" - "github.com/ChainSafe/ChainBridge/core" + "github.com/ChainSafe/chainbridge-utils/core" ) func TestParseStartBlock(t *testing.T) { diff --git a/chains/substrate/listener.go b/chains/substrate/listener.go index 8aebcc524..f98e2386e 100644 --- a/chains/substrate/listener.go +++ b/chains/substrate/listener.go @@ -9,10 +9,10 @@ import ( "math/big" "time" - "github.com/ChainSafe/ChainBridge/blockstore" "github.com/ChainSafe/ChainBridge/chains" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" utils "github.com/ChainSafe/ChainBridge/shared/substrate" + "github.com/ChainSafe/chainbridge-utils/blockstore" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" "github.com/centrifuge/go-substrate-rpc-client/types" diff --git a/chains/substrate/listener_test.go b/chains/substrate/listener_test.go index 245f85fe7..9063f5954 100644 --- a/chains/substrate/listener_test.go +++ b/chains/substrate/listener_test.go @@ -10,9 +10,9 @@ import ( "testing" "time" - "github.com/ChainSafe/ChainBridge/blockstore" utils "github.com/ChainSafe/ChainBridge/shared/substrate" subtest "github.com/ChainSafe/ChainBridge/shared/substrate/testing" + "github.com/ChainSafe/chainbridge-utils/blockstore" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/centrifuge/go-substrate-rpc-client/types" ) diff --git a/chains/substrate/writer.go b/chains/substrate/writer.go index c985954a7..b88f6ad66 100644 --- a/chains/substrate/writer.go +++ b/chains/substrate/writer.go @@ -9,15 +9,16 @@ import ( "fmt" "time" - "github.com/ChainSafe/ChainBridge/chains" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" + "github.com/ChainSafe/chainbridge-utils/core" + utils "github.com/ChainSafe/ChainBridge/shared/substrate" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" "github.com/centrifuge/go-substrate-rpc-client/types" ) -var _ chains.Writer = &writer{} +var _ core.Writer = &writer{} var AcknowledgeProposal utils.Method = utils.BridgePalletName + ".acknowledge_proposal" var TerminatedError = errors.New("terminated") diff --git a/cmd/chainbridge/account_test.go b/cmd/chainbridge/account_test.go index 9739aae90..703de0115 100644 --- a/cmd/chainbridge/account_test.go +++ b/cmd/chainbridge/account_test.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "sort" + "strconv" "strings" "testing" @@ -106,7 +107,7 @@ func newTestContext(description string, flags []string, values []interface{}) (* return nil, fmt.Errorf("failed to set cli flag: %T", flags[i]) } case uint: - err := ctx.Set(flags[i], string(v)) + err := ctx.Set(flags[i], strconv.Itoa(int(v))) if err != nil { return nil, fmt.Errorf("failed to set cli flag: %T", flags[i]) } diff --git a/cmd/chainbridge/main.go b/cmd/chainbridge/main.go index a70f65e39..8b7947a3c 100644 --- a/cmd/chainbridge/main.go +++ b/cmd/chainbridge/main.go @@ -17,9 +17,9 @@ import ( "github.com/ChainSafe/ChainBridge/chains/ethereum" "github.com/ChainSafe/ChainBridge/chains/substrate" "github.com/ChainSafe/ChainBridge/config" - "github.com/ChainSafe/ChainBridge/core" - "github.com/ChainSafe/ChainBridge/metrics/health" - metrics "github.com/ChainSafe/ChainBridge/metrics/types" + "github.com/ChainSafe/chainbridge-utils/core" + "github.com/ChainSafe/chainbridge-utils/metrics/health" + metrics "github.com/ChainSafe/chainbridge-utils/metrics/types" "github.com/ChainSafe/chainbridge-utils/msg" log "github.com/ChainSafe/log15" "github.com/prometheus/client_golang/prometheus/promhttp" diff --git a/core/chain.go b/core/chain.go deleted file mode 100644 index 765e39d20..000000000 --- a/core/chain.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package core - -import ( - metrics "github.com/ChainSafe/ChainBridge/metrics/types" - "github.com/ChainSafe/ChainBridge/router" - "github.com/ChainSafe/chainbridge-utils/msg" -) - -type Chain interface { - Start() error // Start chain - SetRouter(*router.Router) - Id() msg.ChainId - Name() string - LatestBlock() metrics.LatestBlock - Stop() -} - -type ChainConfig struct { - Name string // Human-readable chain name - Id msg.ChainId // ChainID - Endpoint string // url for rpc endpoint - From string // address of key to use - KeystorePath string // Location of key files - Insecure bool // Indicated whether the test keyring should be used - BlockstorePath string // Location of blockstore - FreshStart bool // If true, blockstore is ignored at start. - LatestBlock bool // If true, overrides blockstore or latest block in config and starts from current block - Opts map[string]string // Per chain options -} diff --git a/core/core.go b/core/core.go deleted file mode 100644 index 045e0ce55..000000000 --- a/core/core.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package core - -import ( - "fmt" - "os" - "os/signal" - "syscall" - - "github.com/ChainSafe/ChainBridge/router" - "github.com/ChainSafe/log15" -) - -type Core struct { - Registry []Chain - route *router.Router - log log15.Logger - sysErr <-chan error -} - -func NewCore(sysErr <-chan error) *Core { - return &Core{ - Registry: make([]Chain, 0), - route: router.NewRouter(log15.New("system", "router")), - log: log15.New("system", "core"), - sysErr: sysErr, - } -} - -// AddChain registers the chain in the Registry and calls Chain.SetRouter() -func (c *Core) AddChain(chain Chain) { - c.Registry = append(c.Registry, chain) - chain.SetRouter(c.route) -} - -// Start will call all registered chains' Start methods and block forever (or until signal is received) -func (c *Core) Start() { - for _, chain := range c.Registry { - err := chain.Start() - if err != nil { - c.log.Error( - "failed to start chain", - "chain", chain.Id(), - "err", err, - ) - return - } - c.log.Info(fmt.Sprintf("Started %s chain", chain.Name())) - } - - sigc := make(chan os.Signal, 1) - signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM) - defer signal.Stop(sigc) - - // Block here and wait for a signal - select { - case err := <-c.sysErr: - c.log.Error("FATAL ERROR. Shutting down.", "err", err) - case <-sigc: - c.log.Warn("Interrupt received, shutting down now.") - } - - // Signal chains to shutdown - for _, chain := range c.Registry { - chain.Stop() - } -} - -func (c *Core) Errors() <-chan error { - return c.sysErr -} diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index ebe480148..07dc675c9 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -13,7 +13,6 @@ import ( ethChain "github.com/ChainSafe/ChainBridge/chains/ethereum" subChain "github.com/ChainSafe/ChainBridge/chains/substrate" - "github.com/ChainSafe/ChainBridge/core" eth "github.com/ChainSafe/ChainBridge/e2e/ethereum" sub "github.com/ChainSafe/ChainBridge/e2e/substrate" "github.com/ChainSafe/ChainBridge/shared" @@ -21,6 +20,7 @@ import ( ethtest "github.com/ChainSafe/ChainBridge/shared/ethereum/testing" subutils "github.com/ChainSafe/ChainBridge/shared/substrate" subtest "github.com/ChainSafe/ChainBridge/shared/substrate/testing" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/msg" log "github.com/ChainSafe/log15" "github.com/centrifuge/go-substrate-rpc-client/types" diff --git a/e2e/ethereum/ethereum.go b/e2e/ethereum/ethereum.go index 27b588401..a0cdcdd37 100644 --- a/e2e/ethereum/ethereum.go +++ b/e2e/ethereum/ethereum.go @@ -13,9 +13,9 @@ import ( bridge "github.com/ChainSafe/ChainBridge/bindings/Bridge" "github.com/ChainSafe/ChainBridge/chains/ethereum" - "github.com/ChainSafe/ChainBridge/core" utils "github.com/ChainSafe/ChainBridge/shared/ethereum" ethtest "github.com/ChainSafe/ChainBridge/shared/ethereum/testing" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/crypto/secp256k1" "github.com/ChainSafe/chainbridge-utils/keystore" "github.com/ChainSafe/chainbridge-utils/msg" diff --git a/e2e/substrate/substrate.go b/e2e/substrate/substrate.go index 7f19bb8ed..9716c4a04 100644 --- a/e2e/substrate/substrate.go +++ b/e2e/substrate/substrate.go @@ -9,8 +9,8 @@ import ( "testing" "time" - "github.com/ChainSafe/ChainBridge/core" utils "github.com/ChainSafe/ChainBridge/shared/substrate" + "github.com/ChainSafe/chainbridge-utils/core" "github.com/ChainSafe/chainbridge-utils/keystore" "github.com/ChainSafe/chainbridge-utils/msg" "github.com/ChainSafe/log15" diff --git a/go.mod b/go.mod index bf060ec1c..bbd25e562 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e - github.com/ChainSafe/chainbridge-utils v1.0.1 + github.com/ChainSafe/chainbridge-utils v1.0.2 github.com/ChainSafe/log15 v1.0.0 github.com/aristanetworks/goarista v0.0.0-20200609010056-95bcf8053598 // indirect github.com/btcsuite/btcd v0.20.1-beta // indirect @@ -18,4 +18,4 @@ require ( github.com/urfave/cli/v2 v2.2.0 golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect -) +) \ No newline at end of file diff --git a/go.sum b/go.sum index dd9a94f9d..2102e70d9 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532 github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e/go.mod h1:H5fNH57wn/j1oLifOnWEqYbfJZcOWzr7jZjKKrUckSQ= github.com/ChainSafe/chainbridge-utils v1.0.1 h1:qxg0So8/XsiixiHCeZ2tn69IM7aGevhwRNby+xAfWGs= github.com/ChainSafe/chainbridge-utils v1.0.1/go.mod h1:jX+LT1patbRwukvjQl/4oJsKZvx3ujZubdXz8ZWlMXo= +github.com/ChainSafe/chainbridge-utils v1.0.2 h1:4HaFV0H7oiIB1TlV1AjH/kYu+3GBYgNsvovpNstI3iI= +github.com/ChainSafe/chainbridge-utils v1.0.2/go.mod h1:T5cOZhxdY4x0DrE0EqOnMwAPE8d4bNGqiPfN16o1rzc= github.com/ChainSafe/log15 v1.0.0 h1:vRDVtWtVwIH5uSCBvgTTZh6FA58UBJ6+QiiypaZfBf8= github.com/ChainSafe/log15 v1.0.0/go.mod h1:5v1+ALHtdW0NfAeeoYyKmzCAMcAeqkdhIg4uxXWIgOg= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= diff --git a/metrics/health/health.go b/metrics/health/health.go deleted file mode 100644 index 845b916a8..000000000 --- a/metrics/health/health.go +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package health - -import ( - "encoding/json" - "fmt" - "math/big" - "net/http" - "time" - - "github.com/ChainSafe/ChainBridge/core" - "github.com/ChainSafe/chainbridge-utils/msg" - log "github.com/ChainSafe/log15" -) - -// After this duration with no changes a chain will return an error -const BlockTimeout = 20 - -type httpMetricServer struct { - port int - timeDelay int - chains []core.Chain - stats []ChainInfo -} - -type httpResponse struct { - Chains []ChainInfo `json:"chains,omitempty"` - Error string `json:"error,omitempty"` -} - -type ChainInfo struct { - ChainId msg.ChainId `json:"chainId"` - Height *big.Int `json:"height"` - LastUpdated time.Time `json:"lastUpdated"` -} - -func NewHealthServer(port int, chains []core.Chain) *httpMetricServer { - return &httpMetricServer{ - port: port, - chains: chains, - timeDelay: BlockTimeout, - stats: make([]ChainInfo, len(chains)), - } -} - -// healthStatus is a catch-all update that grabs the latest updates on the running chains -// It assumes that the configuration was set correctly, therefore the relevant chains are -// only those that are in the core.Core registry. -func (s httpMetricServer) HealthStatus(w http.ResponseWriter, _ *http.Request) { - w.Header().Set("Content-Type", "application/json") - - // Iterate through their block heads and update the cache accordingly - for i, chain := range s.chains { - current := chain.LatestBlock() - prev := s.stats[i] - if s.stats[i].Height == nil { - // First time we've received a block for this chain - s.stats[chain.Id()] = ChainInfo{ - ChainId: chain.Id(), - Height: current.Height, - LastUpdated: current.LastUpdated, - } - } else { - now := time.Now() - timeDiff := now.Sub(prev.LastUpdated) - // If block has changed, update it - if current.Height.Cmp(prev.Height) == 1 { - s.stats[chain.Id()].LastUpdated = current.LastUpdated - s.stats[chain.Id()].Height = current.Height - } else if int(timeDiff.Seconds()) >= s.timeDelay { // Error if we exceeded the time limit - response := &httpResponse{ - Chains: []ChainInfo{}, - Error: fmt.Sprintf("chain %d height hasn't changed for %f seconds. Current Height: %s", prev.ChainId, timeDiff.Seconds(), current.Height), - } - w.WriteHeader(http.StatusInternalServerError) - err := json.NewEncoder(w).Encode(response) - if err != nil { - log.Error("Failed to write metrics", "err", err) - } - return - } else if current.Height != nil && prev.Height != nil && current.Height.Cmp(prev.Height) == -1 { // Error for having a smaller blockheight than previous - response := &httpResponse{ - Chains: []ChainInfo{}, - Error: fmt.Sprintf("unexpected block height. previous = %s current = %s", prev.Height, current.Height), - } - w.WriteHeader(http.StatusInternalServerError) - err := json.NewEncoder(w).Encode(response) - if err != nil { - log.Error("Failed to write metrics", "err", err) - } - return - } - } - } - - response := &httpResponse{ - Chains: s.stats, - Error: "", - } - w.WriteHeader(http.StatusOK) - err := json.NewEncoder(w).Encode(response) - if err != nil { - log.Error("Failed to serve metrics") - } -} diff --git a/metrics/types/prometheus.go b/metrics/types/prometheus.go deleted file mode 100644 index 5b6512747..000000000 --- a/metrics/types/prometheus.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package types - -import ( - "fmt" - - "github.com/prometheus/client_golang/prometheus" -) - -type ChainMetrics struct { - BlocksProcessed prometheus.Counter - VotesSubmitted prometheus.Counter -} - -func NewChainMetrics(chain string) *ChainMetrics { - metrics := &ChainMetrics{ - BlocksProcessed: prometheus.NewCounter(prometheus.CounterOpts{ - Name: fmt.Sprintf("%s_latest_block", chain), - Help: "Number of blocks processed by the chain's listener", - }), - VotesSubmitted: prometheus.NewCounter(prometheus.CounterOpts{ - Name: fmt.Sprintf("%s_votes_submitted", chain), - Help: "Number of votes submitted to chain", - }), - } - - prometheus.MustRegister(metrics.BlocksProcessed) - prometheus.MustRegister(metrics.VotesSubmitted) - - return metrics -} diff --git a/metrics/types/types.go b/metrics/types/types.go deleted file mode 100644 index a80b69ebf..000000000 --- a/metrics/types/types.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package types - -import ( - "math/big" - "time" -) - -// LatestBlock is used to track the health of a chain -type LatestBlock struct { - Height *big.Int - LastUpdated time.Time -} diff --git a/router/router.go b/router/router.go deleted file mode 100644 index 7821fc654..000000000 --- a/router/router.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package router - -import ( - "fmt" - "sync" - - "github.com/ChainSafe/ChainBridge/chains" - "github.com/ChainSafe/chainbridge-utils/msg" - log "github.com/ChainSafe/log15" -) - -// Router forwards messages from their source to their destination -type Router struct { - registry map[msg.ChainId]chains.Writer - lock *sync.RWMutex - log log.Logger -} - -func NewRouter(log log.Logger) *Router { - return &Router{ - registry: make(map[msg.ChainId]chains.Writer), - lock: &sync.RWMutex{}, - log: log, - } -} - -// Send passes a message to the destination Writer if it exists -func (r *Router) Send(msg msg.Message) error { - r.lock.Lock() - defer r.lock.Unlock() - - r.log.Trace("Routing message", "src", msg.Source, "dest", msg.Destination, "nonce", msg.DepositNonce, "rId", msg.ResourceId.Hex()) - w := r.registry[msg.Destination] - if w == nil { - return fmt.Errorf("unknown destination chainId: %d", msg.Destination) - } - - go w.ResolveMessage(msg) - return nil -} - -// Listen registers a Writer with a ChainId which Router.Send can then use to propagate messages -func (r *Router) Listen(id msg.ChainId, w chains.Writer) { - r.lock.Lock() - defer r.lock.Unlock() - r.log.Debug("Registering new chain in router", "id", id) - r.registry[id] = w -} diff --git a/router/router_test.go b/router/router_test.go deleted file mode 100644 index 44b076b82..000000000 --- a/router/router_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -package router - -import ( - "reflect" - "testing" - "time" - - "github.com/ChainSafe/chainbridge-utils/msg" - "github.com/ChainSafe/log15" -) - -type mockWriter struct { - msgs []msg.Message -} - -func (w *mockWriter) Start() error { return nil } -func (w *mockWriter) Stop() error { return nil } - -func (w *mockWriter) ResolveMessage(msg msg.Message) bool { - w.msgs = append(w.msgs, msg) - return true -} - -func TestRouter(t *testing.T) { - tLog := log15.New("test_router") - tLog.SetHandler(log15.LvlFilterHandler(log15.LvlTrace, tLog.GetHandler())) - router := NewRouter(tLog) - - ethW := &mockWriter{msgs: *new([]msg.Message)} - router.Listen(msg.ChainId(0), ethW) - - ctfgW := &mockWriter{msgs: *new([]msg.Message)} - router.Listen(msg.ChainId(1), ctfgW) - - msgEthToCtfg := msg.Message{ - Source: msg.ChainId(0), - Destination: msg.ChainId(1), - } - - msgCtfgToEth := msg.Message{ - Source: msg.ChainId(1), - Destination: msg.ChainId(0), - } - - err := router.Send(msgCtfgToEth) - if err != nil { - t.Fatal(err) - } - err = router.Send(msgEthToCtfg) - if err != nil { - t.Fatal(err) - } - - time.Sleep(time.Second) - - if !reflect.DeepEqual(ethW.msgs[0], msgCtfgToEth) { - t.Error("Unexpected message") - } - - if !reflect.DeepEqual(ctfgW.msgs[0], msgEthToCtfg) { - t.Error("Unexpected message") - } -}