Skip to content

Commit

Permalink
Merge pull request #78 from 0xPolygon/CDK-189-dac-issue-65
Browse files Browse the repository at this point in the history
cdk-189 - fix for getting latest block number
  • Loading branch information
christophercampbell authored Apr 11, 2024
2 parents 2002ac3 + 5b956c5 commit 5398230
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
4 changes: 2 additions & 2 deletions synchronizer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func InitStartBlock(db db.DB, em etherman.Etherman, genesisBlock uint64, validiu
}

func findContractDeploymentBlock(ctx context.Context, em etherman.Etherman, contract common.Address) (*big.Int, error) {
latestBlock, err := em.BlockByNumber(ctx, nil)
latestHeader, err := em.HeaderByNumber(ctx, nil)
if err != nil {
return nil, err
}
firstBlock := findCode(ctx, em, contract, 0, latestBlock.Number().Int64())
firstBlock := findCode(ctx, em, contract, 0, latestHeader.Number.Int64())
return big.NewInt(firstBlock), nil
}

Expand Down
54 changes: 25 additions & 29 deletions synchronizer/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func Test_InitStartBlock(t *testing.T) {
storeLastProcessedBlockReturns []interface{}
commitReturns []interface{}
// eth client mocks
blockByNumberArgs []interface{}
blockByNumberReturns []interface{}
codeAtArgs [][]interface{}
codeAtReturns [][]interface{}
headerByNumberArgs []interface{}
headerByNumberReturns []interface{}
codeAtArgs [][]interface{}
codeAtReturns [][]interface{}

isErrorExpected bool
}
Expand Down Expand Up @@ -77,9 +77,9 @@ func Test_InitStartBlock(t *testing.T) {
returnArgs...).Once()
}

if config.blockByNumberArgs != nil && config.blockByNumberReturns != nil {
emMock.On("BlockByNumber", config.blockByNumberArgs...).Return(
config.blockByNumberReturns...).Once()
if config.headerByNumberArgs != nil && config.headerByNumberReturns != nil {
emMock.On("HeaderByNumber", config.headerByNumberArgs...).Return(
config.headerByNumberReturns...).Once()
}

if config.codeAtArgs != nil && config.codeAtReturns != nil {
Expand Down Expand Up @@ -131,57 +131,53 @@ func Test_InitStartBlock(t *testing.T) {
testFn(t, testConfig{
getLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask},
getLastProcessedBlockReturns: []interface{}{uint64(0), nil},
blockByNumberArgs: []interface{}{mock.Anything, mock.Anything},
blockByNumberReturns: []interface{}{nil, errors.New("error")},
headerByNumberArgs: []interface{}{mock.Anything, mock.Anything},
headerByNumberReturns: []interface{}{nil, errors.New("error")},
isErrorExpected: true,
})
})

t.Run("BeginStateTransaction fails", func(t *testing.T) {
t.Parallel()

block := ethTypes.NewBlockWithHeader(&ethTypes.Header{
Number: big.NewInt(0),
})

testFn(t, testConfig{
getLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask},
getLastProcessedBlockReturns: []interface{}{uint64(0), nil},
beginStateTransactionArgs: []interface{}{mock.Anything},
beginStateTransactionReturns: []interface{}{nil, errors.New("error")},
blockByNumberArgs: []interface{}{mock.Anything, mock.Anything},
blockByNumberReturns: []interface{}{block, nil},
isErrorExpected: true,
headerByNumberArgs: []interface{}{mock.Anything, mock.Anything},
headerByNumberReturns: []interface{}{ethTypes.NewBlockWithHeader(&ethTypes.Header{
Number: big.NewInt(0),
}).Header(), nil},
isErrorExpected: true,
})
})

t.Run("Store off-chain data fails", func(t *testing.T) {
t.Parallel()

block := ethTypes.NewBlockWithHeader(&ethTypes.Header{
Number: big.NewInt(0),
})

testFn(t, testConfig{
getLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask},
getLastProcessedBlockReturns: []interface{}{uint64(0), nil},
beginStateTransactionArgs: []interface{}{mock.Anything},
storeLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask, uint64(0), mock.Anything},
storeLastProcessedBlockReturns: []interface{}{errors.New("error")},
blockByNumberArgs: []interface{}{mock.Anything, mock.Anything},
blockByNumberReturns: []interface{}{block, nil},
isErrorExpected: true,
headerByNumberArgs: []interface{}{mock.Anything, mock.Anything},
headerByNumberReturns: []interface{}{ethTypes.NewBlockWithHeader(&ethTypes.Header{
Number: big.NewInt(0),
}).Header(), nil},
isErrorExpected: true,
})
})

t.Run("Commit fails", func(t *testing.T) {
t.Parallel()

testFn(t, testConfig{
blockByNumberArgs: []interface{}{mock.Anything, mock.Anything},
blockByNumberReturns: []interface{}{ethTypes.NewBlockWithHeader(&ethTypes.Header{
headerByNumberArgs: []interface{}{mock.Anything, mock.Anything},
headerByNumberReturns: []interface{}{ethTypes.NewBlockWithHeader(&ethTypes.Header{
Number: big.NewInt(0),
}), nil},
}).Header(), nil},
getLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask},
getLastProcessedBlockReturns: []interface{}{uint64(0), nil},
storeLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask, uint64(0), mock.Anything},
Expand All @@ -203,10 +199,10 @@ func Test_InitStartBlock(t *testing.T) {
storeLastProcessedBlockArgs: []interface{}{mock.Anything, L1SyncTask, uint64(2), mock.Anything},
storeLastProcessedBlockReturns: []interface{}{nil},
commitReturns: []interface{}{nil},
blockByNumberArgs: []interface{}{mock.Anything, mock.Anything},
blockByNumberReturns: []interface{}{ethTypes.NewBlockWithHeader(&ethTypes.Header{
headerByNumberArgs: []interface{}{mock.Anything, mock.Anything},
headerByNumberReturns: []interface{}{ethTypes.NewBlockWithHeader(&ethTypes.Header{
Number: big.NewInt(3),
}), nil},
}).Header(), nil},
codeAtArgs: [][]interface{}{
{mock.Anything, common.HexToAddress(l1Config.PolygonValidiumAddress), big.NewInt(1)},
{mock.Anything, common.HexToAddress(l1Config.PolygonValidiumAddress), big.NewInt(2)},
Expand Down

0 comments on commit 5398230

Please sign in to comment.