diff --git a/go.mod b/go.mod index 139d4d6e7..d064a5a96 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/google/gofuzz v1.1.0 github.com/ipfs/go-log v1.0.4 - github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 + github.com/keep-network/keep-common v1.2.1-0.20201103151750-c52f5d480c10 github.com/keep-network/keep-core v1.3.0 github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 226695f3d..c03839b0f 100644 --- a/go.sum +++ b/go.sum @@ -335,6 +335,8 @@ github.com/keep-network/keep-common v1.2.0 h1:hVd2tTd7vL+9CQP5Ntk5kjs+GYvkgrRNBc github.com/keep-network/keep-common v1.2.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 h1:CivupPSFswHACua5xZGKdeYxsCQ2cmRomTIBh8kfk70= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= +github.com/keep-network/keep-common v1.2.1-0.20201103151750-c52f5d480c10 h1:KuZP1ArRAK1X4PunfteJX+QnP/XSfvfmi/ZR2a6kXTU= +github.com/keep-network/keep-common v1.2.1-0.20201103151750-c52f5d480c10/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-core v1.3.0 h1:7Tb33EmO/ntHOEbOiYciRlBhqu5Ln6KemWCaYK0Z6LA= github.com/keep-network/keep-core v1.3.0/go.mod h1:1KsSSTQoN754TrFLW7kLy50pOG2CQ4BOfnJqdvEG7FA= github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 h1:/ZNMBY7y6hfzCYA8mgtHnspGO26OmWV3sDehyGnqRyY= diff --git a/pkg/chain/ethereum/connect.go b/pkg/chain/ethereum/connect.go index 830216269..cfc5e848d 100644 --- a/pkg/chain/ethereum/connect.go +++ b/pkg/chain/ethereum/connect.go @@ -1,14 +1,11 @@ package ethereum import ( - "context" "fmt" "math/big" "sync" "time" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/ethclient" @@ -37,12 +34,11 @@ var ( type EthereumChain struct { config *ethereum.Config accountKey *keystore.Key - client bind.ContractBackend + client ethutil.EthereumClient bondedECDSAKeepFactoryContract *contract.BondedECDSAKeepFactory blockCounter *blockcounter.EthereumBlockCounter miningWaiter *ethutil.MiningWaiter nonceManager *ethutil.NonceManager - blockTimestampFn blockTimestampFn // transactionMutex allows interested parties to forcibly serialize // transaction submission. @@ -67,10 +63,6 @@ func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain, return nil, err } - // TODO: Add rate limiting to (keep-ecdsa/pull/585#discussion_r513351032): - // - `createBlockTimestampFn` - // - `blockCounter` - // - `miningWaiter` wrappedClient := addClientWrappers(config, client) transactionMutex := &sync.Mutex{} @@ -91,7 +83,7 @@ func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain, logger.Infof("using [%v] mining check interval", checkInterval) logger.Infof("using [%v] wei max gas price", maxGasPrice) - miningWaiter := ethutil.NewMiningWaiter(client, checkInterval, maxGasPrice) + miningWaiter := ethutil.NewMiningWaiter(wrappedClient, checkInterval, maxGasPrice) bondedECDSAKeepFactoryContractAddress, err := config.ContractAddress(BondedECDSAKeepFactoryContractName) if err != nil { @@ -109,7 +101,7 @@ func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain, return nil, err } - blockCounter, err := blockcounter.CreateBlockCounter(client) + blockCounter, err := blockcounter.CreateBlockCounter(wrappedClient) if err != nil { return nil, fmt.Errorf( "failed to create Ethereum blockcounter: [%v]", @@ -126,15 +118,14 @@ func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain, nonceManager: nonceManager, miningWaiter: miningWaiter, transactionMutex: transactionMutex, - blockTimestampFn: createBlockTimestampFn(client), }, nil } func addClientWrappers( config *ethereum.Config, - backend bind.ContractBackend, -) bind.ContractBackend { - loggingBackend := ethutil.WrapCallLogging(logger, backend) + client ethutil.EthereumClient, +) ethutil.EthereumClient { + loggingClient := ethutil.WrapCallLogging(logger, client) if config.RequestsPerSecondLimit > 0 || config.ConcurrencyLimit > 0 { logger.Infof( @@ -146,7 +137,7 @@ func addClientWrappers( ) return ethutil.WrapRateLimiting( - loggingBackend, + loggingClient, ðutil.RateLimiterConfig{ RequestsPerSecondLimit: config.RequestsPerSecondLimit, ConcurrencyLimit: config.ConcurrencyLimit, @@ -154,21 +145,5 @@ func addClientWrappers( ) } - return loggingBackend -} - -type blockTimestampFn func(blockNumber *big.Int) (uint64, error) - -func createBlockTimestampFn(client *ethclient.Client) blockTimestampFn { - return func(blockNumber *big.Int) (uint64, error) { - ctx, cancelCtx := context.WithTimeout(context.Background(), 1*time.Minute) - defer cancelCtx() - - header, err := client.HeaderByNumber(ctx, blockNumber) - if err != nil { - return 0, err - } - - return header.Time, nil - } + return loggingClient } diff --git a/pkg/chain/ethereum/ethereum.go b/pkg/chain/ethereum/ethereum.go index 0e1db077a..580cbc43c 100644 --- a/pkg/chain/ethereum/ethereum.go +++ b/pkg/chain/ethereum/ethereum.go @@ -2,6 +2,7 @@ package ethereum import ( + "context" "fmt" "math/big" "sort" @@ -560,5 +561,13 @@ func (ec *EthereumChain) PastSignatureSubmittedEvents( // BlockTimestamp returns given block's timestamp. func (ec *EthereumChain) BlockTimestamp(blockNumber *big.Int) (uint64, error) { - return ec.blockTimestampFn(blockNumber) + ctx, cancelCtx := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancelCtx() + + header, err := ec.client.HeaderByNumber(ctx, blockNumber) + if err != nil { + return 0, err + } + + return header.Time, nil }