Add config flag to enable/disable meddler logs

- In config, add Debug.MeddlerLogs to enable meddler debug logs.  This only
  affects the node started via cli, tests swill ran with `meddler.Debug = true`
- Remove unused ethereum client functions and config parameters
- Document all the config parameters
This commit is contained in:
Eduard S
2020-12-30 17:02:38 +01:00
parent 5c037e0a8f
commit f5d81ca8f8
6 changed files with 142 additions and 152 deletions

View File

@@ -40,45 +40,31 @@ type EthereumInterface interface {
var (
// ErrAccountNil is used when the calls can not be made because the account is nil
ErrAccountNil = fmt.Errorf("Authorized calls can't be made when the account is nil")
// ErrReceiptStatusFailed is used when receiving a failed transaction
ErrReceiptStatusFailed = fmt.Errorf("receipt status is failed")
// ErrReceiptNotReceived is used when unable to retrieve a transaction
ErrReceiptNotReceived = fmt.Errorf("receipt not available")
// ErrBlockHashMismatchEvent is used when there's a block hash mismatch
// beetween different events of the same block
ErrBlockHashMismatchEvent = fmt.Errorf("block hash mismatch in event log")
)
const (
errStrDeploy = "deployment of %s failed: %w"
errStrWaitReceipt = "wait receipt of %s deploy failed: %w"
// default values
defaultCallGasLimit = 300000
defaultDeployGasLimit = 1000000
defaultGasPriceDiv = 100
defaultReceiptTimeout = 60
defaultIntervalReceiptLoop = 200
defaultCallGasLimit = 300000
defaultGasPriceDiv = 100
)
// EthereumConfig defines the configuration parameters of the EthereumClient
type EthereumConfig struct {
CallGasLimit uint64
DeployGasLimit uint64
GasPriceDiv uint64
ReceiptTimeout time.Duration
IntervalReceiptLoop time.Duration
CallGasLimit uint64
GasPriceDiv uint64
}
// EthereumClient is an ethereum client to call Smart Contract methods and check blockchain information.
type EthereumClient struct {
client *ethclient.Client
chainID *big.Int
account *accounts.Account
ks *ethKeystore.KeyStore
ReceiptTimeout time.Duration
config *EthereumConfig
opts *bind.CallOpts
client *ethclient.Client
chainID *big.Int
account *accounts.Account
ks *ethKeystore.KeyStore
config *EthereumConfig
opts *bind.CallOpts
}
// NewEthereumClient creates a EthereumClient instance. The account is not mandatory (it can
@@ -86,20 +72,16 @@ type EthereumClient struct {
func NewEthereumClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, config *EthereumConfig) (*EthereumClient, error) {
if config == nil {
config = &EthereumConfig{
CallGasLimit: defaultCallGasLimit,
DeployGasLimit: defaultDeployGasLimit,
GasPriceDiv: defaultGasPriceDiv,
ReceiptTimeout: defaultReceiptTimeout,
IntervalReceiptLoop: defaultIntervalReceiptLoop,
CallGasLimit: defaultCallGasLimit,
GasPriceDiv: defaultGasPriceDiv,
}
}
c := &EthereumClient{
client: client,
account: account,
ks: ks,
ReceiptTimeout: config.ReceiptTimeout * time.Second,
config: config,
opts: newCallOpts(),
client: client,
account: account,
ks: ks,
config: config,
opts: newCallOpts(),
}
chainID, err := c.EthChainID()
if err != nil {
@@ -180,93 +162,16 @@ type ContractData struct {
Receipt *types.Receipt
}
// Deploy a smart contract. `name` is used to log deployment information. fn
// is a wrapper to the deploy function generated by abigen. In case of error,
// the returned `ContractData` may have some parameters filled depending on the
// kind of error that occurred.
func (c *EthereumClient) Deploy(name string,
fn func(c *ethclient.Client, auth *bind.TransactOpts) (ethCommon.Address, *types.Transaction, interface{}, error)) (ContractData, error) {
var contractData ContractData
log.Infow("Deploying", "contract", name)
tx, err := c.CallAuth(
c.config.DeployGasLimit,
func(client *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
addr, tx, _, err := fn(client, auth)
if err != nil {
return nil, tracerr.Wrap(err)
}
contractData.Address = addr
return tx, nil
},
)
if err != nil {
return contractData, tracerr.Wrap(fmt.Errorf(errStrDeploy, name, err))
}
log.Infow("Waiting receipt", "tx", tx.Hash().Hex(), "contract", name)
contractData.Tx = tx
receipt, err := c.WaitReceipt(tx)
if err != nil {
return contractData, tracerr.Wrap(fmt.Errorf(errStrWaitReceipt, name, err))
}
contractData.Receipt = receipt
return contractData, nil
}
// Call performs a read only Smart Contract method call.
func (c *EthereumClient) Call(fn func(*ethclient.Client) error) error {
return fn(c.client)
}
// WaitReceipt will block until a transaction is confirmed. Internally it
// polls the state every 200 milliseconds.
func (c *EthereumClient) WaitReceipt(tx *types.Transaction) (*types.Receipt, error) {
return c.waitReceipt(context.TODO(), tx, c.ReceiptTimeout)
}
// GetReceipt will check if a transaction is confirmed and return
// immediately, waiting at most 1 second and returning error if the transaction
// is still pending.
func (c *EthereumClient) GetReceipt(tx *types.Transaction) (*types.Receipt, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
defer cancel()
return c.waitReceipt(ctx, tx, 0)
}
// EthTransactionReceipt returns the transaction receipt of the given txHash
func (c *EthereumClient) EthTransactionReceipt(ctx context.Context, txHash ethCommon.Hash) (*types.Receipt, error) {
return c.client.TransactionReceipt(ctx, txHash)
}
func (c *EthereumClient) waitReceipt(ctx context.Context, tx *types.Transaction, timeout time.Duration) (*types.Receipt, error) {
var err error
var receipt *types.Receipt
txHash := tx.Hash()
log.Debugw("Waiting for receipt", "tx", txHash.Hex())
start := time.Now()
for {
receipt, err = c.client.TransactionReceipt(ctx, txHash)
if receipt != nil || time.Since(start) >= timeout {
break
}
time.Sleep(c.config.IntervalReceiptLoop * time.Millisecond)
}
if receipt != nil && receipt.Status == types.ReceiptStatusFailed {
log.Errorw("Failed transaction", "tx", txHash.Hex())
return receipt, tracerr.Wrap(ErrReceiptStatusFailed)
}
if receipt == nil {
log.Debugw("Pendingtransaction / Wait receipt timeout", "tx", txHash.Hex(), "lasterr", err)
return receipt, tracerr.Wrap(ErrReceiptNotReceived)
}
log.Debugw("Successful transaction", "tx", txHash.Hex())
return receipt, tracerr.Wrap(err)
}
// EthLastBlock returns the last block number in the blockchain
func (c *EthereumClient) EthLastBlock() (int64, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)