diff --git a/clients/ethereum/client.go b/clients/ethereum/client.go index b7261ac3..1417034b 100644 --- a/clients/ethereum/client.go +++ b/clients/ethereum/client.go @@ -147,7 +147,8 @@ func (c *client) GetBatch(ctx context.Context, nonce uint64) (*clients.TransferB return nil, err } if int(batch.DepositsCount) != len(deposits) { - return nil, errDepositsAndBatchDepositsCountDiffer + return nil, fmt.Errorf("%w, batch.DepositsCount: %d, fetched deposits len: %d", + errDepositsAndBatchDepositsCountDiffer, batch.DepositsCount, len(deposits)) } transferBatch := &clients.TransferBatch{ diff --git a/clients/ethereum/client_test.go b/clients/ethereum/client_test.go index 3a8bbf45..75227502 100644 --- a/clients/ethereum/client_test.go +++ b/clients/ethereum/client_test.go @@ -192,9 +192,9 @@ func TestClient_GetBatch(t *testing.T) { args := createMockEthereumClientArgs() c, _ := NewEthereumClient(args) + expectedErr := errors.New("expected error") t.Run("error while getting batch", func(t *testing.T) { - expectedErr := errors.New("expected error") c.clientWrapper = &bridgeTests.EthereumClientWrapperStub{ GetBatchCalled: func(ctx context.Context, batchNonce *big.Int) (contract.Batch, error) { return contract.Batch{}, expectedErr @@ -204,6 +204,60 @@ func TestClient_GetBatch(t *testing.T) { assert.Nil(t, batch) assert.Equal(t, expectedErr, err) }) + t.Run("error while getting deposits", func(t *testing.T) { + c.clientWrapper = &bridgeTests.EthereumClientWrapperStub{ + GetBatchCalled: func(ctx context.Context, batchNonce *big.Int) (contract.Batch, error) { + return contract.Batch{ + Nonce: batchNonce, + DepositsCount: 2, + }, nil + }, + GetBatchDepositsCalled: func(ctx context.Context, batchNonce *big.Int) ([]contract.Deposit, error) { + return nil, expectedErr + }, + } + batch, err := c.GetBatch(context.Background(), 1) + assert.Nil(t, batch) + assert.Equal(t, expectedErr, err) + }) + t.Run("deposits mismatch - with 0", func(t *testing.T) { + c.clientWrapper = &bridgeTests.EthereumClientWrapperStub{ + GetBatchCalled: func(ctx context.Context, batchNonce *big.Int) (contract.Batch, error) { + return contract.Batch{ + Nonce: batchNonce, + DepositsCount: 2, + }, nil + }, + GetBatchDepositsCalled: func(ctx context.Context, batchNonce *big.Int) ([]contract.Deposit, error) { + return make([]contract.Deposit, 0), nil + }, + } + batch, err := c.GetBatch(context.Background(), 1) + assert.Nil(t, batch) + assert.True(t, errors.Is(err, errDepositsAndBatchDepositsCountDiffer)) + assert.True(t, strings.Contains(err.Error(), "batch.DepositsCount: 2, fetched deposits len: 0")) + }) + t.Run("deposits mismatch - with non zero value", func(t *testing.T) { + c.clientWrapper = &bridgeTests.EthereumClientWrapperStub{ + GetBatchCalled: func(ctx context.Context, batchNonce *big.Int) (contract.Batch, error) { + return contract.Batch{ + Nonce: batchNonce, + DepositsCount: 2, + }, nil + }, + GetBatchDepositsCalled: func(ctx context.Context, batchNonce *big.Int) ([]contract.Deposit, error) { + return []contract.Deposit{ + { + Nonce: big.NewInt(22), + }, + }, nil + }, + } + batch, err := c.GetBatch(context.Background(), 1) + assert.Nil(t, batch) + assert.True(t, errors.Is(err, errDepositsAndBatchDepositsCountDiffer)) + assert.True(t, strings.Contains(err.Error(), "batch.DepositsCount: 2, fetched deposits len: 1")) + }) t.Run("returns batch should work", func(t *testing.T) { from1 := testsCommon.CreateRandomEthereumAddress() token1 := testsCommon.CreateRandomEthereumAddress() diff --git a/cmd/bridge/config/config.toml b/cmd/bridge/config/config.toml index 124276a8..bcae1550 100644 --- a/cmd/bridge/config/config.toml +++ b/cmd/bridge/config/config.toml @@ -24,6 +24,8 @@ MaxRetriesOnQuorumReached = 3 MaxRetriesOnWasTransferProposed = 3 ProxyCacherExpirationSeconds = 600 # the caching time in seconds + ProxyFinalityCheck = true + ProxyMaxNoncesDelta = 7 # the number of maximum blocks allowed to be "in front" of what the metachain has notarized [Elrond.GasMap] Sign = 8000000 ProposeTransferBase = 11000000 diff --git a/cmd/bridge/main.go b/cmd/bridge/main.go index 1d4b0650..cd994670 100644 --- a/cmd/bridge/main.go +++ b/cmd/bridge/main.go @@ -138,8 +138,15 @@ func startRelay(ctx *cli.Context, version string) error { return fmt.Errorf("empty Elrond.NetworkAddress in config file") } - proxy := blockchain.NewElrondProxy(cfg.Elrond.NetworkAddress, nil) - proxyWithCacher, err := blockchain.NewElrondProxyWithCache(proxy, time.Second*time.Duration(cfg.Elrond.ProxyCacherExpirationSeconds)) + argsProxy := blockchain.ArgsElrondProxy{ + ProxyURL: cfg.Elrond.NetworkAddress, + SameScState: false, + ShouldBeSynced: false, + FinalityCheck: cfg.Elrond.ProxyFinalityCheck, + AllowedDeltaToFinal: cfg.Elrond.ProxyMaxNoncesDelta, + CacheExpirationTime: time.Second * time.Duration(cfg.Elrond.ProxyCacherExpirationSeconds), + } + proxy, err := blockchain.NewElrondProxy(argsProxy) if err != nil { return err } @@ -204,7 +211,7 @@ func startRelay(ctx *cli.Context, version string) error { Configs: configs, Messenger: messenger, StatusStorer: statusStorer, - Proxy: proxyWithCacher, + Proxy: proxy, Erc20ContractsHolder: erc20ContractsHolder, ClientWrapper: clientWrapper, TimeForBootstrap: timeForBootstrap, diff --git a/cmd/priceFeeder/config/config.toml b/cmd/priceFeeder/config/config.toml index 37354fe2..9b20e828 100644 --- a/cmd/priceFeeder/config/config.toml +++ b/cmd/priceFeeder/config/config.toml @@ -9,6 +9,8 @@ MinResultsNum = 3 # min number of results waiting PollIntervalInSeconds = 2 # polling interval for fetchers AutoSendIntervalInSeconds = 10 # seconds before next send price when percent difference is not met + ProxyFinalityCheck = true + ProxyMaxNoncesDelta = 7 # the number of maximum blocks allowed to be "in front" of what the metachain has notarized [[Pairs]] Base = "ETH" diff --git a/cmd/priceFeeder/main.go b/cmd/priceFeeder/main.go index 658d3ab0..a107b0b6 100644 --- a/cmd/priceFeeder/main.go +++ b/cmd/priceFeeder/main.go @@ -51,8 +51,15 @@ func runApp() error { return fmt.Errorf("empty NetworkAddress in config file") } - proxy := blockchain.NewElrondProxy(cfg.GeneralConfig.NetworkAddress, nil) - proxyWithCacher, err := blockchain.NewElrondProxyWithCache(proxy, time.Second*time.Duration(cfg.GeneralConfig.ProxyCacherExpirationSeconds)) + args := blockchain.ArgsElrondProxy{ + ProxyURL: cfg.GeneralConfig.NetworkAddress, + SameScState: false, + ShouldBeSynced: false, + FinalityCheck: cfg.GeneralConfig.ProxyFinalityCheck, + AllowedDeltaToFinal: cfg.GeneralConfig.ProxyMaxNoncesDelta, + CacheExpirationTime: time.Second * time.Duration(cfg.GeneralConfig.ProxyCacherExpirationSeconds), + } + proxy, err := blockchain.NewElrondProxy(args) if err != nil { return err } @@ -76,7 +83,7 @@ func runApp() error { return err } - txNonceHandler, err := interactors.NewNonceTransactionHandler(proxyWithCacher, time.Second*time.Duration(cfg.GeneralConfig.IntervalToResendTxsInSeconds), true) + txNonceHandler, err := interactors.NewNonceTransactionHandler(proxy, time.Second*time.Duration(cfg.GeneralConfig.IntervalToResendTxsInSeconds), true) if err != nil { return err } @@ -99,7 +106,7 @@ func runApp() error { return err } argsElrondNotifee := notifees.ArgsElrondNotifee{ - Proxy: proxyWithCacher, + Proxy: proxy, TxBuilder: txBuilder, TxNonceHandler: txNonceHandler, ContractAddress: aggregatorAddress, diff --git a/config/config.go b/config/config.go index b22f1d68..cedab06c 100644 --- a/config/config.go +++ b/config/config.go @@ -145,6 +145,8 @@ type ElrondConfig struct { MaxRetriesOnQuorumReached uint64 MaxRetriesOnWasTransferProposed uint64 ProxyCacherExpirationSeconds uint64 + ProxyMaxNoncesDelta int + ProxyFinalityCheck bool } // ElrondGasMapConfig represents the gas limits for Elrond operations @@ -176,6 +178,8 @@ type GeneralNotifierConfig struct { MinResultsNum int PollIntervalInSeconds uint64 AutoSendIntervalInSeconds uint64 + ProxyMaxNoncesDelta int + ProxyFinalityCheck bool } // Pair parameters for a pair diff --git a/factory/ethElrondBridgeComponents_test.go b/factory/ethElrondBridgeComponents_test.go index 30c51361..3765604e 100644 --- a/factory/ethElrondBridgeComponents_test.go +++ b/factory/ethElrondBridgeComponents_test.go @@ -74,11 +74,16 @@ func createMockEthElrondBridgeArgs() ArgsEthereumToElrondBridge { }, } + argsProxy := blockchain.ArgsElrondProxy{ + ProxyURL: cfg.Elrond.NetworkAddress, + CacheExpirationTime: time.Minute, + } + proxy, _ := blockchain.NewElrondProxy(argsProxy) return ArgsEthereumToElrondBridge{ Configs: configs, Messenger: &p2pMocks.MessengerStub{}, StatusStorer: testsCommon.NewStorerMock(), - Proxy: blockchain.NewElrondProxy(cfg.Elrond.NetworkAddress, nil), + Proxy: proxy, Erc20ContractsHolder: &bridgeTests.ERC20ContractsHolderStub{}, ClientWrapper: &bridgeTests.EthereumClientWrapperStub{}, TimeForBootstrap: minTimeForBootstrap, diff --git a/go.mod b/go.mod index 076d2bed..a04d2aec 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/ElrondNetwork/elrond-go-core v1.1.14 github.com/ElrondNetwork/elrond-go-crypto v1.0.1 github.com/ElrondNetwork/elrond-go-logger v1.0.6 - github.com/ElrondNetwork/elrond-sdk-erdgo v1.0.15 + github.com/ElrondNetwork/elrond-sdk-erdgo v1.0.17 github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 github.com/ethereum/go-ethereum v1.10.16 github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a diff --git a/go.sum b/go.sum index 85f4bc23..f87c7b53 100644 --- a/go.sum +++ b/go.sum @@ -69,8 +69,8 @@ github.com/ElrondNetwork/elrond-go-logger v1.0.4/go.mod h1:e5D+c97lKUfFdAzFX7rrI github.com/ElrondNetwork/elrond-go-logger v1.0.5/go.mod h1:cBfgx0ST/CJx8jrxJSC5aiSrvkGzcnF7sK06RD8mFxQ= github.com/ElrondNetwork/elrond-go-logger v1.0.6 h1:FGbO2C/s9cfNHag9jaKSB+sjuWUo+k8E+8iAz08Woac= github.com/ElrondNetwork/elrond-go-logger v1.0.6/go.mod h1:cBfgx0ST/CJx8jrxJSC5aiSrvkGzcnF7sK06RD8mFxQ= -github.com/ElrondNetwork/elrond-sdk-erdgo v1.0.15 h1:p77Pkb0Y+dDT80bOsKpWYNgGjZcEvVytSadiXDn1n5E= -github.com/ElrondNetwork/elrond-sdk-erdgo v1.0.15/go.mod h1:J7HoBQq0ZaHoWtN/mR2Fd/m/m79IjaIjAp/RTvhvYBQ= +github.com/ElrondNetwork/elrond-sdk-erdgo v1.0.17 h1:3wegm0717WdlvqwfdJiNuqXLm6Krie4M+pzlkDRRFEY= +github.com/ElrondNetwork/elrond-sdk-erdgo v1.0.17/go.mod h1:J7HoBQq0ZaHoWtN/mR2Fd/m/m79IjaIjAp/RTvhvYBQ= github.com/ElrondNetwork/elrond-vm-common v1.1.0/go.mod h1:w3i6f8uiuRkE68Ie/gebRcLgTuHqvruJSYrFyZWuLrE= github.com/ElrondNetwork/elrond-vm-common v1.2.9/go.mod h1:B/Y8WiqHyDd7xsjNYsaYbVMp1jQgQ+z4jTJkFvj/EWI= github.com/ElrondNetwork/elrond-vm-common v1.3.2 h1:O/Wr5k7HXX7p0+U3ZsGdY5ydqfSABZvBSzwyV/xbu08=