Skip to content

Commit

Permalink
fix: add a unit test to ensure NewestBlockID is working
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Jan 9, 2024
1 parent 0b9f930 commit 4e3c7b0
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions logdb/logdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,73 @@ func TestEvents(t *testing.T) {
}
}
}

func TestLogDB_NewestBlockID(t *testing.T) {
db, err := logdb.NewMem()
if err != nil {
t.Fatal(err)
}
defer db.Close()

b := new(block.Builder).Build()
expectedNewestId := b.Header().ID()

var allEvents eventLogs
var allTransfers transferLogs

for i := 0; i < 100; i++ {

b = new(block.Builder).
ParentID(b.Header().ID()).
Transaction(newTx()).
Transaction(newTx()).
Build()
receipts := tx.Receipts{newReceipt(), newReceipt()}

for j := 0; j < len(receipts); j++ {
tx := b.Transactions()[j]
receipt := receipts[j]
origin, _ := tx.Origin()
allEvents = append(allEvents, &logdb.Event{
BlockNumber: b.Header().Number(),
Index: uint32(j),
BlockID: b.Header().ID(),
BlockTime: b.Header().Timestamp(),
TxID: tx.ID(),
TxOrigin: origin,
ClauseIndex: 0,
Address: receipt.Outputs[0].Events[0].Address,
Topics: [5]*thor.Bytes32{&receipt.Outputs[0].Events[0].Topics[0]},
Data: receipt.Outputs[0].Events[0].Data,
})

allTransfers = append(allTransfers, &logdb.Transfer{
BlockNumber: b.Header().Number(),
Index: uint32(j),
BlockID: b.Header().ID(),
BlockTime: b.Header().Timestamp(),
TxID: tx.ID(),
TxOrigin: origin,
ClauseIndex: 0,
Sender: receipt.Outputs[0].Transfers[0].Sender,
Recipient: receipt.Outputs[0].Transfers[0].Recipient,
Amount: receipt.Outputs[0].Transfers[0].Amount,
})
}

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
t.Fatal(err)
}

if err := w.Commit(); err != nil {
t.Fatal(err)
}

expectedNewestId = b.Header().ID()
}

newestBlockID, err := db.NewestBlockID()
assert.Nil(t, err)
assert.Equal(t, expectedNewestId.Bytes(), newestBlockID.Bytes())
}

0 comments on commit 4e3c7b0

Please sign in to comment.