Skip to content

Commit

Permalink
fix: Filter EVM logs instead of subscriptions (#322)
Browse files Browse the repository at this point in the history
* Filter EVM logs instead of subscriptions
Signed-off-by: failfmi <[email protected]>
  • Loading branch information
failfmi authored Nov 2, 2021
1 parent 20d4adf commit eadbb27
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 310 deletions.
12 changes: 9 additions & 3 deletions app/clients/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ func (ec *Client) ValidateContractDeployedAt(contractAddress string) (*common.Ad
}

// GetBlockTimestamp retrieves the timestamp of the given block
func (ec *Client) GetBlockTimestamp(blockNumber *big.Int) (uint64, error) {
func (ec *Client) GetBlockTimestamp(blockNumber *big.Int) uint64 {
block, err := ec.BlockByNumber(context.Background(), blockNumber)

if err != nil {
return 0, err
ec.logger.Errorf("Failed to get block [%s]. Error: [%s]. Retrying...", blockNumber, err)
time.Sleep(5 * time.Second)
return ec.GetBlockTimestamp(blockNumber)
}

return block.Time(), nil
return block.Time()
}

// WaitForTransaction waits for transaction receipt and depending on receipt status calls one of the provided functions
Expand Down Expand Up @@ -148,6 +150,10 @@ func (ec *Client) GetPrivateKey() string {
return ec.config.PrivateKey
}

func (ec Client) BlockConfirmations() uint64 {
return ec.config.BlockConfirmations
}

func (ec *Client) WaitForConfirmations(raw types.Log) error {
target := raw.BlockNumber + ec.config.BlockConfirmations
for {
Expand Down
13 changes: 8 additions & 5 deletions app/clients/evm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,20 @@ func Test_GetBlockTimestamp(t *testing.T) {
mocks.MEVMCoreClient.On("BlockByNumber", context.Background(), blockNumber).Return(types.NewBlockWithHeader(
&types.Header{Time: now},
), nil)
ts, err := c.GetBlockTimestamp(blockNumber)
assert.Nil(t, err)
ts := c.GetBlockTimestamp(blockNumber)
assert.Equal(t, now, ts)
}

func Test_GetBlockTimestamp_Fails(t *testing.T) {
setup()
blockNumber := big.NewInt(1)
mocks.MEVMCoreClient.On("BlockByNumber", context.Background(), blockNumber).Return(nil, errors.New("some-error"))
_, err := c.GetBlockTimestamp(blockNumber)
assert.Error(t, errors.New("some-error"), err)
now := uint64(time.Now().Unix())
mocks.MEVMCoreClient.On("BlockByNumber", context.Background(), blockNumber).Return(nil, errors.New("some-error")).Once()
mocks.MEVMCoreClient.On("BlockByNumber", context.Background(), blockNumber).Return(types.NewBlockWithHeader(
&types.Header{Time: now},
), nil)
res := c.GetBlockTimestamp(blockNumber)
assert.Equal(t, now, res)
}

func Test_CheckTransactionReceipt(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion app/domain/client/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type EVM interface {
GetClient() Core
BlockNumber(ctx context.Context) (uint64, error)
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
GetBlockTimestamp(blockNumber *big.Int) (uint64, error)
GetBlockTimestamp(blockNumber *big.Int) uint64
ValidateContractDeployedAt(contractAddress string) (*common.Address, error)
// WaitForTransaction waits for transaction receipt and depending on receipt status calls one of the provided functions
// onSuccess is called once the TX is successfully mined
Expand All @@ -40,4 +40,5 @@ type EVM interface {
WaitForConfirmations(raw types.Log) error
// GetPrivateKey retrieves private key used for the specific EVM Client
GetPrivateKey() string
BlockConfirmations() uint64
}
2 changes: 2 additions & 0 deletions app/domain/service/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Contracts interface {
Address() common.Address
// GetMembers returns the array of bridge members currently set in the Bridge contract
GetMembers() []string
// ReloadMembers triggers to fetch all the members from the Router Contract
ReloadMembers()
// GetClient returns the Contracts Service corresponding EVM Client
GetClient() client.Core
// IsMember returns true/false depending on whether the provided address is a Bridge member or not
Expand Down
8 changes: 8 additions & 0 deletions app/helper/big-numbers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ func ToBigInt(value string) (*big.Int, error) {

return amount, nil
}

// Max returns the larger of x or y.
func Max(x, y uint64) uint64 {
if x < y {
return y
}
return x
}
Loading

0 comments on commit eadbb27

Please sign in to comment.