Skip to content

Commit

Permalink
Merge pull request #228 from ElrondNetwork/feat/dynamic-gas-elrond-et…
Browse files Browse the repository at this point in the history
…h-batch

[Feature] Dynamic gas for elrond-ethereum batches
  • Loading branch information
dragos-rebegea authored Apr 12, 2022
2 parents 44388ba + 0449661 commit 5f0e5b3
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 68 deletions.
3 changes: 2 additions & 1 deletion clients/elrond/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ func (c *client) ProposeSetStatus(ctx context.Context, batch *clients.TransferBa
txBuilder.ArgBytes([]byte{stat})
}

hash, err := c.txHandler.SendTransactionReturnHash(ctx, txBuilder, c.gasMapConfig.ProposeStatus)
gasLimit := c.gasMapConfig.ProposeStatusBase + uint64(len(batch.Deposits))*c.gasMapConfig.ProposeStatusForEach
hash, err := c.txHandler.SendTransactionReturnHash(ctx, txBuilder, gasLimit)
if err == nil {
c.log.Info("proposed set statuses"+batch.String(), "transaction hash", hash)
}
Expand Down
10 changes: 6 additions & 4 deletions clients/elrond/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func createMockClientArgs() ClientArgs {
Sign: 10,
ProposeTransferBase: 20,
ProposeTransferForEach: 30,
ProposeStatus: 40,
PerformActionBase: 50,
PerformActionForEach: 60,
ProposeStatusBase: 40,
ProposeStatusForEach: 50,
PerformActionBase: 60,
PerformActionForEach: 70,
},
Proxy: &interactors.ElrondProxyStub{},
Log: logger.GetOrCreate("test"),
Expand Down Expand Up @@ -379,7 +380,8 @@ func TestClient_ProposeSetStatus(t *testing.T) {

expectedDataField := strings.Join(expectedArgs, "@")
assert.Equal(t, expectedDataField, dataField)
assert.Equal(t, c.gasMapConfig.ProposeStatus, gasLimit)
expectedGasLimit := c.gasMapConfig.ProposeStatusBase + uint64(len(expectedStatus))*c.gasMapConfig.ProposeStatusForEach
assert.Equal(t, gasLimit, expectedGasLimit)

return expectedHash, nil
},
Expand Down
80 changes: 43 additions & 37 deletions clients/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,34 @@ type argListsBatch struct {

// ArgsEthereumClient is the DTO used in the ethereum's client constructor
type ArgsEthereumClient struct {
ClientWrapper ClientWrapper
Erc20ContractsHandler Erc20ContractsHolder
Log elrondCore.Logger
AddressConverter core.AddressConverter
Broadcaster Broadcaster
PrivateKey *ecdsa.PrivateKey
TokensMapper TokensMapper
SignatureHolder SignaturesHolder
SafeContractAddress common.Address
GasHandler GasHandler
TransferGasLimit uint64
ClientWrapper ClientWrapper
Erc20ContractsHandler Erc20ContractsHolder
Log elrondCore.Logger
AddressConverter core.AddressConverter
Broadcaster Broadcaster
PrivateKey *ecdsa.PrivateKey
TokensMapper TokensMapper
SignatureHolder SignaturesHolder
SafeContractAddress common.Address
GasHandler GasHandler
TransferGasLimitBase uint64
TransferGasLimitForEach uint64
}

type client struct {
clientWrapper ClientWrapper
erc20ContractsHandler Erc20ContractsHolder
log elrondCore.Logger
addressConverter core.AddressConverter
broadcaster Broadcaster
privateKey *ecdsa.PrivateKey
publicKey *ecdsa.PublicKey
tokensMapper TokensMapper
signatureHolder SignaturesHolder
safeContractAddress common.Address
gasHandler GasHandler
transferGasLimit uint64
clientWrapper ClientWrapper
erc20ContractsHandler Erc20ContractsHolder
log elrondCore.Logger
addressConverter core.AddressConverter
broadcaster Broadcaster
privateKey *ecdsa.PrivateKey
publicKey *ecdsa.PublicKey
tokensMapper TokensMapper
signatureHolder SignaturesHolder
safeContractAddress common.Address
gasHandler GasHandler
transferGasLimitBase uint64
transferGasLimitForEach uint64
}

// NewEthereumClient will create a new Ethereum client
Expand All @@ -72,18 +74,19 @@ func NewEthereumClient(args ArgsEthereumClient) (*client, error) {
}

c := &client{
clientWrapper: args.ClientWrapper,
erc20ContractsHandler: args.Erc20ContractsHandler,
log: args.Log,
addressConverter: args.AddressConverter,
broadcaster: args.Broadcaster,
privateKey: args.PrivateKey,
publicKey: publicKeyECDSA,
tokensMapper: args.TokensMapper,
signatureHolder: args.SignatureHolder,
safeContractAddress: args.SafeContractAddress,
gasHandler: args.GasHandler,
transferGasLimit: args.TransferGasLimit,
clientWrapper: args.ClientWrapper,
erc20ContractsHandler: args.Erc20ContractsHandler,
log: args.Log,
addressConverter: args.AddressConverter,
broadcaster: args.Broadcaster,
privateKey: args.PrivateKey,
publicKey: publicKeyECDSA,
tokensMapper: args.TokensMapper,
signatureHolder: args.SignatureHolder,
safeContractAddress: args.SafeContractAddress,
gasHandler: args.GasHandler,
transferGasLimitBase: args.TransferGasLimitBase,
transferGasLimitForEach: args.TransferGasLimitForEach,
}

c.log.Info("NewEthereumClient",
Expand Down Expand Up @@ -121,7 +124,10 @@ func checkArgs(args ArgsEthereumClient) error {
if check.IfNil(args.GasHandler) {
return errNilGasHandler
}
if args.TransferGasLimit == 0 {
if args.TransferGasLimitBase == 0 {
return errInvalidGasLimit
}
if args.TransferGasLimitForEach == 0 {
return errInvalidGasLimit
}

Expand Down Expand Up @@ -313,7 +319,7 @@ func (c *client) ExecuteTransfer(

auth.Nonce = big.NewInt(nonce)
auth.Value = big.NewInt(0)
auth.GasLimit = c.transferGasLimit
auth.GasLimit = c.transferGasLimitBase + uint64(len(batch.Deposits))*c.transferGasLimitForEach
auth.Context = ctx
auth.GasPrice = gasPrice

Expand Down
21 changes: 15 additions & 6 deletions clients/ethereum/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func createMockEthereumClientArgs() ArgsEthereumClient {
return append([]byte("ERC20"), sourceBytes...), nil
},
},
SignatureHolder: &testsCommon.SignaturesHolderStub{},
SafeContractAddress: testsCommon.CreateRandomEthereumAddress(),
GasHandler: &testsCommon.GasHandlerStub{},
TransferGasLimit: 100,
SignatureHolder: &testsCommon.SignaturesHolderStub{},
SafeContractAddress: testsCommon.CreateRandomEthereumAddress(),
GasHandler: &testsCommon.GasHandlerStub{},
TransferGasLimitBase: 50,
TransferGasLimitForEach: 20,
}
}

Expand Down Expand Up @@ -161,9 +162,17 @@ func TestNewEthereumClient(t *testing.T) {
assert.Equal(t, errNilGasHandler, err)
assert.True(t, check.IfNil(c))
})
t.Run("0 transfer gas limit", func(t *testing.T) {
t.Run("0 transfer gas limit base", func(t *testing.T) {
args := createMockEthereumClientArgs()
args.TransferGasLimit = 0
args.TransferGasLimitBase = 0
c, err := NewEthereumClient(args)

assert.Equal(t, errInvalidGasLimit, err)
assert.True(t, check.IfNil(c))
})
t.Run("0 transfer gas limit for each", func(t *testing.T) {
args := createMockEthereumClientArgs()
args.TransferGasLimitForEach = 0
c, err := NewEthereumClient(args)

assert.Equal(t, errInvalidGasLimit, err)
Expand Down
6 changes: 4 additions & 2 deletions cmd/bridge/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
MultisigContractAddress = "3009d97FfeD62E57d444e552A9eDF9Ee6Bc8644c" # the eth address for the bridge contract
SafeContractAddress = "A6504Cc508889bbDBd4B748aFf6EA6b5D0d2684c"
PrivateKeyFile = "keys/ethereum.sk" # the path to the file containing the relayer eth private key
GasLimit = 500000
GasLimitBase = 200000
GasLimitForEach = 30000
IntervalToWaitForTransferInSeconds = 600 #10 minutes
MaxRetriesOnQuorumReached = 3
[Eth.GasStation]
Expand All @@ -27,7 +28,8 @@
Sign = 8000000
ProposeTransferBase = 11000000
ProposeTransferForEach = 3000000
ProposeStatus = 30000000
ProposeStatusBase = 10000000
ProposeStatusForEach = 7000000
PerformActionBase = 25000000
PerformActionForEach = 2000000

Expand Down
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type EthereumConfig struct {
SafeContractAddress string
PrivateKeyFile string
IntervalToResendTxsInSeconds uint64
GasLimit uint64
GasLimitBase uint64
GasLimitForEach uint64
GasStation GasStationConfig
MaxRetriesOnQuorumReached uint64
IntervalToWaitForTransferInSeconds uint64
Expand Down Expand Up @@ -151,7 +152,8 @@ type ElrondGasMapConfig struct {
Sign uint64
ProposeTransferBase uint64
ProposeTransferForEach uint64
ProposeStatus uint64
ProposeStatusBase uint64
ProposeStatusForEach uint64
PerformActionBase uint64
PerformActionForEach uint64
}
Expand Down
23 changes: 12 additions & 11 deletions factory/ethElrondBridgeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,18 @@ func (components *ethElrondBridgeComponents) createEthereumClient(args ArgsEther
safeContractAddress := common.HexToAddress(ethereumConfigs.SafeContractAddress)

argsEthClient := ethereum.ArgsEthereumClient{
ClientWrapper: args.ClientWrapper,
Erc20ContractsHandler: args.Erc20ContractsHolder,
Log: core.NewLoggerWithIdentifier(logger.GetOrCreate(ethClientLogId), ethClientLogId),
AddressConverter: components.addressConverter,
Broadcaster: components.broadcaster,
PrivateKey: privateKey,
TokensMapper: tokensMapper,
SignatureHolder: signaturesHolder,
SafeContractAddress: safeContractAddress,
GasHandler: gs,
TransferGasLimit: ethereumConfigs.GasLimit,
ClientWrapper: args.ClientWrapper,
Erc20ContractsHandler: args.Erc20ContractsHolder,
Log: core.NewLoggerWithIdentifier(logger.GetOrCreate(ethClientLogId), ethClientLogId),
AddressConverter: components.addressConverter,
Broadcaster: components.broadcaster,
PrivateKey: privateKey,
TokensMapper: tokensMapper,
SignatureHolder: signaturesHolder,
SafeContractAddress: safeContractAddress,
GasHandler: gs,
TransferGasLimitBase: ethereumConfigs.GasLimitBase,
TransferGasLimitForEach: ethereumConfigs.GasLimitForEach,
}

components.ethClient, err = ethereum.NewEthereumClient(argsEthClient)
Expand Down
3 changes: 2 additions & 1 deletion factory/ethElrondBridgeComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func createMockEthElrondBridgeArgs() ArgsEthereumToElrondBridge {
SafeContractAddress: "5DdDe022a65F8063eE9adaC54F359CBF46166068",
PrivateKeyFile: "testdata/grace.sk",
IntervalToResendTxsInSeconds: 0,
GasLimit: 500000,
GasLimitBase: 200000,
GasLimitForEach: 30000,
GasStation: config.GasStationConfig{
Enabled: true,
URL: "",
Expand Down
3 changes: 2 additions & 1 deletion integrationTests/relayers/ethToElrond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ func createBridgeComponentsConfig(index int) config.Config {
MultisigContractAddress: "3009d97FfeD62E57d444e552A9eDF9Ee6Bc8644c",
PrivateKeyFile: fmt.Sprintf("testdata/ethereum%d.sk", index),
IntervalToResendTxsInSeconds: 10,
GasLimit: 500000,
GasLimitBase: 200000,
GasLimitForEach: 30000,
GasStation: config.GasStationConfig{
Enabled: false,
},
Expand Down
7 changes: 4 additions & 3 deletions testsCommon/gasMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ func CreateTestElrondGasMap() config.ElrondGasMapConfig {
Sign: 101,
ProposeTransferBase: 102,
ProposeTransferForEach: 103,
ProposeStatus: 104,
PerformActionBase: 105,
PerformActionForEach: 106,
ProposeStatusBase: 104,
ProposeStatusForEach: 105,
PerformActionBase: 106,
PerformActionForEach: 107,
}
}

0 comments on commit 5f0e5b3

Please sign in to comment.