Skip to content

Commit

Permalink
more tests for logdb
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Jan 11, 2024
1 parent 6d72ede commit db18285
Showing 1 changed file with 197 additions and 51 deletions.
248 changes: 197 additions & 51 deletions logdb/logdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ func newReceipt() *tx.Receipt {
}
}

func newEventOnlyReceipt() *tx.Receipt {
return &tx.Receipt{
Outputs: []*tx.Output{
{
Events: tx.Events{{
Address: randAddress(),
Topics: []thor.Bytes32{randBytes32()},
Data: randBytes32().Bytes(),
}},
},
},
}
}

func newTransferOnlyReceipt() *tx.Receipt {
return &tx.Receipt{
Outputs: []*tx.Output{
{
Transfers: tx.Transfers{{
Sender: randAddress(),
Recipient: randAddress(),
Amount: new(big.Int).SetBytes(randAddress().Bytes()),
}},
},
},
}
}

type eventLogs []*logdb.Event

func (logs eventLogs) Filter(f func(ev *logdb.Event) bool) (ret eventLogs) {
Expand Down Expand Up @@ -161,6 +189,7 @@ func TestEvents(t *testing.T) {
want eventLogs
}{
{"query all events", &logdb.EventFilter{}, allEvents},
{"query all events with nil option", nil, allEvents},
{"query all events asc", &logdb.EventFilter{Order: logdb.ASC}, allEvents},
{"query all events desc", &logdb.EventFilter{Order: logdb.DESC}, allEvents.Reverse()},
{"query all events limit offset", &logdb.EventFilter{Options: &logdb.Options{Offset: 1, Limit: 10}}, allEvents[1:11]},
Expand Down Expand Up @@ -191,6 +220,7 @@ func TestEvents(t *testing.T) {
want transferLogs
}{
{"query all transfers", &logdb.TransferFilter{}, allTransfers},
{"query all transfers with nil option", nil, allTransfers},
{"query all transfers asc", &logdb.TransferFilter{Order: logdb.ASC}, allTransfers},
{"query all transfers desc", &logdb.TransferFilter{Order: logdb.DESC}, allTransfers.Reverse()},
{"query all transfers limit offset", &logdb.TransferFilter{Options: &logdb.Options{Offset: 1, Limit: 10}}, allTransfers[1:11]},
Expand All @@ -215,72 +245,188 @@ func TestEvents(t *testing.T) {
}
}

func TestNewestBlockID(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
b = new(block.Builder).
ParentID(b.Header().ID()).
Transaction(newTx()).
Build()
receipts := tx.Receipts{newReceipt()}

for i := 0; i < 100; i++ {
w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
t.Fatal(err)
}
if err := w.Commit(); err != nil {
t.Fatal(err)
}

b = new(block.Builder).
ParentID(b.Header().ID()).
Transaction(newTx()).
Transaction(newTx()).
Build()
receipts := tx.Receipts{newReceipt(), newReceipt()}
tests := []struct {
name string
prepare func() (thor.Bytes32, error)
}{
{
"newest block id",
func() (thor.Bytes32, error) {
return b.Header().ID(), nil
},
}, {
"add empty block, best should remain unchanged",
func() (thor.Bytes32, error) {
wanted := b.Header().ID()
b = new(block.Builder).ParentID(b.Header().ID()).Build()
receipts = tx.Receipts{}

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
return thor.Bytes32{}, nil
}
if err := w.Commit(); err != nil {
return thor.Bytes32{}, nil
}
return wanted, nil
},
},
{
"add both event and transfer, best should change",
func() (thor.Bytes32, error) {
b = new(block.Builder).
ParentID(b.Header().ID()).
Transaction(newTx()).
Build()
receipts := tx.Receipts{newReceipt()}

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
return thor.Bytes32{}, nil
}
if err := w.Commit(); err != nil {
return thor.Bytes32{}, nil
}
return b.Header().ID(), nil
},
},
{
"add event only, best should change",
func() (thor.Bytes32, error) {
b = new(block.Builder).
ParentID(b.Header().ID()).
Transaction(newTx()).
Build()
receipts := tx.Receipts{newEventOnlyReceipt()}

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
return thor.Bytes32{}, nil
}
if err := w.Commit(); err != nil {
return thor.Bytes32{}, nil
}
return b.Header().ID(), nil
},
},
{
"add transfer only, best should change",
func() (thor.Bytes32, error) {
b = new(block.Builder).
ParentID(b.Header().ID()).
Transaction(newTx()).
Build()
receipts := tx.Receipts{newTransferOnlyReceipt()}

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
return thor.Bytes32{}, nil
}
if err := w.Commit(); err != nil {
return thor.Bytes32{}, nil
}
return b.Header().ID(), nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
want, err := tt.prepare()
if err != nil {
t.Fatal(err)
}
got, err := db.NewestBlockID()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, want, got)
})
}
}

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,
})
func TestLogDB_HasBlockID(t *testing.T) {
db, err := logdb.NewMem()
if err != nil {
t.Fatal(err)
}
defer db.Close()

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,
})
}
b0 := new(block.Builder).Build()

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

w := db.NewWriter()
_ = w.Write(b, receipts)

b = new(block.Builder).
ParentID(b1).
Build()
b2 := b.Header().ID()
receipts = tx.Receipts{}
_ = w.Write(b, receipts)

b = new(block.Builder).
ParentID(b2).
Transaction(newTx()).
Build()
b3 := b.Header().ID()
receipts = tx.Receipts{newEventOnlyReceipt()}
_ = w.Write(b, receipts)

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

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
t.Fatal(err)
}
has, err := db.HasBlockID(b0.Header().ID())
if err != nil {
t.Fatal(err)
}
assert.False(t, has)

if err := w.Commit(); err != nil {
t.Fatal(err)
}
has, err = db.HasBlockID(b1)
if err != nil {
t.Fatal(err)
}
assert.True(t, has)

expectedNewestId = b.Header().ID()
has, err = db.HasBlockID(b2)
if err != nil {
t.Fatal(err)
}
assert.False(t, has)

has, err = db.HasBlockID(b3)
if err != nil {
t.Fatal(err)
}
assert.True(t, has)

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

0 comments on commit db18285

Please sign in to comment.