Skip to content

Commit

Permalink
Merge pull request #232 from ElrondNetwork/proxy-with-finality-check-…
Browse files Browse the repository at this point in the history
…integration

Integrated the new proxy implementation with finality check
  • Loading branch information
iulianpascalau authored May 2, 2022
2 parents 5f0e5b3 + 352d666 commit 0025cef
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 13 deletions.
3 changes: 2 additions & 1 deletion clients/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
56 changes: 55 additions & 1 deletion clients/ethereum/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions cmd/bridge/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions cmd/bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions cmd/priceFeeder/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 11 additions & 4 deletions cmd/priceFeeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -99,7 +106,7 @@ func runApp() error {
return err
}
argsElrondNotifee := notifees.ArgsElrondNotifee{
Proxy: proxyWithCacher,
Proxy: proxy,
TxBuilder: txBuilder,
TxNonceHandler: txNonceHandler,
ContractAddress: aggregatorAddress,
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -176,6 +178,8 @@ type GeneralNotifierConfig struct {
MinResultsNum int
PollIntervalInSeconds uint64
AutoSendIntervalInSeconds uint64
ProxyMaxNoncesDelta int
ProxyFinalityCheck bool
}

// Pair parameters for a pair
Expand Down
7 changes: 6 additions & 1 deletion factory/ethElrondBridgeComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 0025cef

Please sign in to comment.