Skip to content

Commit

Permalink
Change block timestamp fetching to use header only (#821) (#822)
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Mindov <[email protected]>
  • Loading branch information
rokn authored Jan 25, 2023
1 parent a970f4f commit aa702a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/clients/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ func (ec *Client) ValidateContractDeployedAt(contractAddress string) (*common.Ad

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

if err != nil {
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()
return block.Time
}

// WaitForTransactionCallback waits for transaction receipt and depending on receipt status calls one of the provided functions
Expand Down
10 changes: 3 additions & 7 deletions app/clients/evm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func Test_GetBlockTimestamp(t *testing.T) {
setup()
now := uint64(time.Now().Unix())
blockNumber := big.NewInt(1)
mocks.MEVMCoreClient.On("BlockByNumber", context.Background(), blockNumber).Return(types.NewBlockWithHeader(
&types.Header{Time: now},
), nil)
mocks.MEVMCoreClient.On("HeaderByNumber", context.Background(), blockNumber).Return(&types.Header{Time: now}, nil)
ts := c.GetBlockTimestamp(blockNumber)
assert.Equal(t, now, ts)
}
Expand All @@ -122,10 +120,8 @@ func Test_GetBlockTimestamp_Fails(t *testing.T) {
setup()
blockNumber := big.NewInt(1)
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)
mocks.MEVMCoreClient.On("HeaderByNumber", context.Background(), blockNumber).Return(nil, errors.New("some-error")).Once()
mocks.MEVMCoreClient.On("HeaderByNumber", context.Background(), blockNumber).Return(&types.Header{Time: now}, nil)
res := c.GetBlockTimestamp(blockNumber)
assert.Equal(t, now, res)
}
Expand Down
13 changes: 12 additions & 1 deletion test/mocks/client/evm_core_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,19 @@ func (m *MockEVMCore) CallContract(ctx context.Context, call ethereum.CallMsg, b
return args[0].([]byte), args[1].(error)
}
func (m *MockEVMCore) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
panic("implement me")
args := m.Called(ctx, number)
if args[0] == nil && args[1] == nil {
return nil, nil
}
if args[0] == nil {
return nil, args[1].(error)
}
if args[1] == nil {
return args[0].(*types.Header), nil
}
return args[0].(*types.Header), args[1].(error)
}

func (m *MockEVMCore) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
args := m.Called(ctx, account)
if args[0] == nil && args[1] == nil {
Expand Down

0 comments on commit aa702a0

Please sign in to comment.