Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Improve Ethereum client wrappers adoption #594

Merged
merged 2 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
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=
Expand Down
41 changes: 8 additions & 33 deletions pkg/chain/ethereum/connect.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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.
Expand All @@ -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{}
Expand All @@ -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 {
Expand All @@ -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]",
Expand All @@ -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(
Expand All @@ -146,29 +137,13 @@ func addClientWrappers(
)

return ethutil.WrapRateLimiting(
loggingBackend,
loggingClient,
&ethutil.RateLimiterConfig{
RequestsPerSecondLimit: config.RequestsPerSecondLimit,
ConcurrencyLimit: config.ConcurrencyLimit,
},
)
}

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
}
11 changes: 10 additions & 1 deletion pkg/chain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package ethereum

import (
"context"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -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
}