Skip to content

Commit

Permalink
fix unit tests (0xPolygonHermez#3063)
Browse files Browse the repository at this point in the history
* fix aggregator unit test

* linter

* synchronizer unit test

* fix AddVirtualBatch unit test
  • Loading branch information
ARR552 authored Jan 12, 2024
1 parent 1205c7f commit 4b0bcd3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
60 changes: 60 additions & 0 deletions aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,18 @@ func TestTryGenerateBatchProof(t *testing.T) {
},
).Return(nil).Once()
m.stateMock.On("GetBatchByNumber", mock.Anything, lastVerifiedBatchNum, nil).Return(&latestBatch, nil).Twice()
t := time.Now()
l1InfoRoot := common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
vb := state.VirtualBatch{
BatchNumber: lastVerifiedBatchNum + 1,
TxHash: common.Hash{},
Coinbase: common.Address{},
SequencerAddr: common.Address{},
BlockNumber: 0,
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
require.NoError(err)
m.proverMock.On("BatchProof", expectedInputProver).Return(nil, errBanana).Once()
Expand Down Expand Up @@ -816,6 +828,18 @@ func TestTryGenerateBatchProof(t *testing.T) {
},
).Return(nil).Once()
m.stateMock.On("GetBatchByNumber", mock.Anything, lastVerifiedBatchNum, nil).Return(&latestBatch, nil).Twice()
t := time.Now()
l1InfoRoot := common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
vb := state.VirtualBatch{
BatchNumber: lastVerifiedBatchNum + 1,
TxHash: common.Hash{},
Coinbase: common.Address{},
SequencerAddr: common.Address{},
BlockNumber: 0,
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
require.NoError(err)
m.proverMock.On("BatchProof", expectedInputProver).Return(&proofID, nil).Once()
Expand Down Expand Up @@ -846,6 +870,18 @@ func TestTryGenerateBatchProof(t *testing.T) {
},
).Return(nil).Once()
m.stateMock.On("GetBatchByNumber", mock.Anything, lastVerifiedBatchNum, nil).Return(&latestBatch, nil).Twice()
t := time.Now()
l1InfoRoot := common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
vb := state.VirtualBatch{
BatchNumber: lastVerifiedBatchNum + 1,
TxHash: common.Hash{},
Coinbase: common.Address{},
SequencerAddr: common.Address{},
BlockNumber: 0,
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
require.NoError(err)
m.proverMock.On("BatchProof", expectedInputProver).Return(&proofID, nil).Once()
Expand Down Expand Up @@ -876,6 +912,18 @@ func TestTryGenerateBatchProof(t *testing.T) {
},
).Return(nil).Once()
m.stateMock.On("GetBatchByNumber", mock.Anything, lastVerifiedBatchNum, nil).Return(&latestBatch, nil).Twice()
t := time.Now()
l1InfoRoot := common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
vb := state.VirtualBatch{
BatchNumber: lastVerifiedBatchNum + 1,
TxHash: common.Hash{},
Coinbase: common.Address{},
SequencerAddr: common.Address{},
BlockNumber: 0,
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
require.NoError(err)
m.proverMock.On("BatchProof", expectedInputProver).Return(&proofID, nil).Once()
Expand Down Expand Up @@ -920,6 +968,18 @@ func TestTryGenerateBatchProof(t *testing.T) {
},
).Return(nil).Once()
m.stateMock.On("GetBatchByNumber", mock.Anything, lastVerifiedBatchNum, nil).Return(&latestBatch, nil).Twice()
t := time.Now()
l1InfoRoot := common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757")
vb := state.VirtualBatch{
BatchNumber: lastVerifiedBatchNum + 1,
TxHash: common.Hash{},
Coinbase: common.Address{},
SequencerAddr: common.Address{},
BlockNumber: 0,
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
require.NoError(err)
m.proverMock.On("BatchProof", expectedInputProver).Return(&proofID, nil).Once()
Expand Down
7 changes: 6 additions & 1 deletion state/pgstatestorage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,15 @@ func (p *PostgresStorage) AddVirtualBatch(ctx context.Context, virtualBatch *sta
_, err := e.Exec(ctx, addVirtualBatchSQL, virtualBatch.BatchNumber, virtualBatch.TxHash.String(), virtualBatch.Coinbase.String(), virtualBatch.BlockNumber, virtualBatch.SequencerAddr.String())
return err
} else {
var l1InfoRoot *string
if virtualBatch.L1InfoRoot != nil {
l1IR := virtualBatch.L1InfoRoot.String()
l1InfoRoot = &l1IR
}
const addVirtualBatchSQL = "INSERT INTO state.virtual_batch (batch_num, tx_hash, coinbase, block_num, sequencer_addr, timestamp_batch_etrog, l1_info_root) VALUES ($1, $2, $3, $4, $5, $6, $7)"
e := p.getExecQuerier(dbTx)
_, err := e.Exec(ctx, addVirtualBatchSQL, virtualBatch.BatchNumber, virtualBatch.TxHash.String(), virtualBatch.Coinbase.String(), virtualBatch.BlockNumber, virtualBatch.SequencerAddr.String(),
virtualBatch.TimestampBatchEtrog.UTC(), virtualBatch.L1InfoRoot.String())
virtualBatch.TimestampBatchEtrog.UTC(), l1InfoRoot)
return err
}
}
Expand Down
25 changes: 22 additions & 3 deletions state/pgstatestorage/pgstatestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,19 +1212,38 @@ func TestGetVirtualBatchWithTstamp(t *testing.T) {
// add batch
_, err = testState.Exec(ctx, "INSERT INTO state.batch (batch_num, timestamp, wip) VALUES ($1,$2, false)", batchNumber, timestampBatch)
require.NoError(t, err)

virtualBatch := state.VirtualBatch{BlockNumber: blockNumber,
_, err = testState.Exec(ctx, "INSERT INTO state.batch (batch_num, timestamp, wip) VALUES ($1,$2, false)", batchNumber+1, timestampBatch)
require.NoError(t, err)
l1InfoRoot := common.HexToHash("0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f2")
virtualBatch := state.VirtualBatch{
BlockNumber: blockNumber,
BatchNumber: batchNumber,
Coinbase: addr,
SequencerAddr: addr,
TxHash: hash,
TimestampBatchEtrog: &virtualTimestampBatch}
TimestampBatchEtrog: &virtualTimestampBatch,
L1InfoRoot: &l1InfoRoot,
}
err = testState.AddVirtualBatch(ctx, &virtualBatch, dbTx)
require.NoError(t, err)

read, err := testState.GetVirtualBatch(ctx, batchNumber, dbTx)
require.NoError(t, err)
require.Equal(t, virtualBatch, *read)
virtualBatch2 := state.VirtualBatch{
BlockNumber: blockNumber,
BatchNumber: batchNumber+1,
Coinbase: addr,
SequencerAddr: addr,
TxHash: hash,
TimestampBatchEtrog: &virtualTimestampBatch,
}
err = testState.AddVirtualBatch(ctx, &virtualBatch2, dbTx)
require.NoError(t, err)

read, err = testState.GetVirtualBatch(ctx, batchNumber+1, dbTx)
require.NoError(t, err)
require.Equal(t, virtualBatch2, *read)
forcedForkId := uint64(state.FORKID_ETROG)
timeData, err := testState.GetBatchTimestamp(ctx, batchNumber, &forcedForkId, dbTx)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,14 @@ func TestForcedBatchEtrog(t *testing.T) {
Return(trustedBatch, nil).
Once()

var forcedGER common.Hash = sequencedBatch.ForcedGlobalExitRoot
virtualBatch := &state.VirtualBatch{
BatchNumber: sequencedBatch.BatchNumber,
TxHash: sequencedBatch.TxHash,
Coinbase: sequencedBatch.Coinbase,
BlockNumber: ethermanBlock.BlockNumber,
TimestampBatchEtrog: &t,
L1InfoRoot: &forcedGER,
}

m.State.
Expand Down

0 comments on commit 4b0bcd3

Please sign in to comment.