Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Feb 4, 2024
1 parent fd7572f commit 5ca23eb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 21 deletions.
2 changes: 1 addition & 1 deletion common/testdata/blockTrace_04.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"receiptsRoot": "0x7ad169feb178baf74f7c0a12a28570bd69bd10e616acad2caea09a55fd1fb541",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x2",
"number": "0xd",
"number": "0x3",
"gasLimit": "0x7a1200",
"gasUsed": "0x5dc0",
"timestamp": "0x646b6e13",
Expand Down
2 changes: 1 addition & 1 deletion rollup/internal/controller/watcher/chunk_proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func testChunkProposerLimits(t *testing.T) {

if len(chunks) > 0 {
blockOrm := orm.NewL2Block(db)
blocks, err := blockOrm.GetL2Blocks(context.Background(), map[string]interface{}{}, []string{"number ASC"}, tt.expectedBlocksInFirstChunk)
blocks, err := blockOrm.GetL2Blocks(context.Background(), nil, nil, 0)
assert.NoError(t, err)
assert.Len(t, blocks, tt.expectedBlocksInFirstChunk)
for _, block := range blocks {
Expand Down
74 changes: 74 additions & 0 deletions rollup/internal/orm/l2_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (o *L2Block) GetL2WrappedBlocksGEHeight(ctx context.Context, height uint64,

// GetL2Blocks retrieves selected L2Blocks from the database.
// The returned L2Blocks are sorted in ascending order by their block number.
// only used for unit tests.
func (o *L2Block) GetL2Blocks(ctx context.Context, fields map[string]interface{}, orderByList []string, limit int) ([]*L2Block, error) {
db := o.db.WithContext(ctx)
db = db.Model(&L2Block{})
Expand Down Expand Up @@ -299,6 +300,7 @@ type TransactionData struct {
S *hexutil.Big `json:"s"`
}

// This is used for backward compatibility.
func decodeTransactionDataJSON(encodedTx []byte) ([]*gethTypes.Transaction, error) {
var txData []*TransactionData
if jsonErr := json.Unmarshal(encodedTx, &txData); jsonErr != nil {
Expand Down Expand Up @@ -346,3 +348,75 @@ func decodeTransactionDataJSON(encodedTx []byte) ([]*gethTypes.Transaction, erro

return transactions, nil
}

// newTransactionData returns a transaction that will serialize to the trace
// representation, with the given location metadata set (if available).
// only used for unit tests.
func newTransactionData(tx *gethTypes.Transaction) *TransactionData {
v, r, s := tx.RawSignatureValues()

nonce := tx.Nonce()
if tx.IsL1MessageTx() {
nonce = tx.L1MessageQueueIndex()
}

result := &TransactionData{
Type: tx.Type(),
Nonce: nonce,
Gas: tx.Gas(),
GasPrice: (*hexutil.Big)(tx.GasPrice()),
To: tx.To(),
Value: (*hexutil.Big)(tx.Value()),
Data: hexutil.Encode(tx.Data()),
V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
}

if tx.IsL1MessageTx() {
result.From = tx.AsL1MessageTx().Sender
}

return result
}

// only used for unit tests.
func (o *L2Block) updateTransactions(ctx context.Context) error {
l2Blocks, err := o.GetL2Blocks(ctx, nil, nil, 0)
if err != nil {
return fmt.Errorf("failed to get L2Blocks: %w", err)
}

for _, block := range l2Blocks {
var transactions []*gethTypes.Transaction
err := rlp.DecodeBytes(block.TransactionsRLP, &transactions)
if err != nil {
return fmt.Errorf("L2Block.GetL2BlocksInRange: failed to decode transactions_rlp, err: %w", err)
}

var txDataSlice []*TransactionData
for _, tx := range transactions {
txData := newTransactionData(tx)
txDataSlice = append(txDataSlice, txData)
}

txDataJSON, err := json.Marshal(txDataSlice)
if err != nil {
return fmt.Errorf("failed to JSON encode transactions for block %d: %w", block.Number, err)
}

updateMap := map[string]interface{}{
"transactions": string(txDataJSON),
"transactions_rlp": "",
}

db := o.db.WithContext(ctx)
db = db.Model(&L2Block{})
db = db.Where("hash", block.Hash)
if err := db.Updates(updateMap).Error; err != nil {
return fmt.Errorf("failed to update transactions JSON and clear transactions for block %d: %w", block.Number, err)
}
}

return nil
}
51 changes: 32 additions & 19 deletions rollup/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func setupEnv(t *testing.T) {
err = json.Unmarshal(templateBlockTrace, wrappedBlock1)
assert.NoError(t, err)

templateBlockTrace, err = os.ReadFile("../../../common/testdata/blockTrace_03.json")
templateBlockTrace, err = os.ReadFile("../../../common/testdata/blockTrace_04.json")
assert.NoError(t, err)
wrappedBlock2 = &rollupTypes.WrappedBlock{}
err = json.Unmarshal(templateBlockTrace, wrappedBlock2)
Expand Down Expand Up @@ -152,38 +152,51 @@ func TestL2BlockOrm(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(3), height)

blocks, err := l2BlockOrm.GetL2Blocks(context.Background(), map[string]interface{}{}, []string{}, 0)
blocks, err := l2BlockOrm.GetL2Blocks(context.Background(), nil, nil, 0)
assert.NoError(t, err)
assert.Len(t, blocks, 2)
assert.Equal(t, "", blocks[0].ChunkHash)
assert.Equal(t, "", blocks[1].ChunkHash)

wrappedBlocks, err := l2BlockOrm.GetL2BlocksInRange(context.Background(), 2, 3)
wrappedBlocks1, err := l2BlockOrm.GetL2BlocksInRange(context.Background(), 2, 3)
assert.NoError(t, err)
assert.Len(t, blocks, 2)
assert.Equal(t, wrappedBlock1.Header, wrappedBlocks[0].Header)
assert.Equal(t, wrappedBlock1.RowConsumption, wrappedBlocks[0].RowConsumption)
assert.Equal(t, wrappedBlock1.WithdrawRoot, wrappedBlocks[0].WithdrawRoot)
assert.Equal(t, len(wrappedBlock1.Transactions), len(wrappedBlocks[0].Transactions))
for i := range wrappedBlock1.Transactions {
assert.Equal(t, wrappedBlock1.Transactions[i].Hash(), wrappedBlocks[0].Transactions[i].Hash())
}
assert.Equal(t, wrappedBlock2.Header, wrappedBlocks[1].Header)
assert.Equal(t, wrappedBlock2.RowConsumption, wrappedBlocks[1].RowConsumption)
assert.Equal(t, wrappedBlock2.WithdrawRoot, wrappedBlocks[1].WithdrawRoot)
assert.Equal(t, len(wrappedBlock2.Transactions), len(wrappedBlocks[1].Transactions))
for i := range wrappedBlock2.Transactions {
assert.Equal(t, wrappedBlock2.Transactions[i].Hash(), wrappedBlocks[1].Transactions[i].Hash())
}
assertWrappedBlockEquality(t, []*rollupTypes.WrappedBlock{wrappedBlock1, wrappedBlock2}, wrappedBlocks1)

wrappedBlocks2, err := l2BlockOrm.GetL2WrappedBlocksGEHeight(context.Background(), 0, 2)
assert.NoError(t, err)
assertWrappedBlockEquality(t, []*rollupTypes.WrappedBlock{wrappedBlock1, wrappedBlock2}, wrappedBlocks2)

err = l2BlockOrm.UpdateChunkHashInRange(context.Background(), 2, 2, "test hash")
assert.NoError(t, err)

blocks, err = l2BlockOrm.GetL2Blocks(context.Background(), map[string]interface{}{}, []string{}, 0)
blocks, err = l2BlockOrm.GetL2Blocks(context.Background(), nil, nil, 0)
assert.NoError(t, err)
assert.Len(t, blocks, 2)
assert.Equal(t, "test hash", blocks[0].ChunkHash)
assert.Equal(t, "", blocks[1].ChunkHash)

err = l2BlockOrm.updateTransactions(context.Background())
assert.NoError(t, err)

wrappedBlocks1, err = l2BlockOrm.GetL2BlocksInRange(context.Background(), 2, 3)
assertWrappedBlockEquality(t, []*rollupTypes.WrappedBlock{wrappedBlock1, wrappedBlock2}, wrappedBlocks1)

wrappedBlocks2, err = l2BlockOrm.GetL2WrappedBlocksGEHeight(context.Background(), 0, 2)
assert.NoError(t, err)
assertWrappedBlockEquality(t, []*rollupTypes.WrappedBlock{wrappedBlock1, wrappedBlock2}, wrappedBlocks2)
}

func assertWrappedBlockEquality(t *testing.T, expected, actual []*rollupTypes.WrappedBlock) {
assert.Len(t, actual, len(expected))
for i := range expected {
assert.Equal(t, expected[i].Header, actual[i].Header)
assert.Equal(t, expected[i].RowConsumption, actual[i].RowConsumption)
assert.Equal(t, expected[i].WithdrawRoot, actual[i].WithdrawRoot)
assert.Equal(t, len(expected[i].Transactions), len(actual[i].Transactions))
for j := range expected[i].Transactions {
assert.Equal(t, expected[i].Transactions[j].Hash(), actual[i].Transactions[j].Hash())
}
}
}

func TestChunkOrm(t *testing.T) {
Expand Down

0 comments on commit 5ca23eb

Please sign in to comment.