Skip to content

Commit

Permalink
sqlite: extend test to check ids, fix event sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Feb 21, 2024
1 parent b6078b7 commit 182983a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ func TestEphemeralBalance(t *testing.T) {

expectedPayout := cm.TipState().BlockReward()
maturityHeight := cm.TipState().MaturityHeight() + 1
block := mineBlock(cm.TipState(), nil, addr)
minerPayoutID := block.ID().MinerOutputID(0)
// mine a block sending the payout to the wallet
if err := cm.AddBlocks([]types.Block{mineBlock(cm.TipState(), nil, addr)}); err != nil {
if err := cm.AddBlocks([]types.Block{block}); err != nil {
t.Fatal(err)
}

Expand All @@ -339,6 +341,8 @@ func TestEphemeralBalance(t *testing.T) {
t.Fatalf("expected 1 event, got %v", len(events))
} else if events[0].Data.EventType() != wallet.EventTypeMinerPayout {
t.Fatalf("expected payout event, got %v", events[0].Data.EventType())
} else if events[0].ID != types.Hash256(minerPayoutID) {
t.Fatalf("expected %v, got %v", minerPayoutID, events[0].ID)
}

// mine until the payout matures
Expand Down Expand Up @@ -419,6 +423,24 @@ func TestEphemeralBalance(t *testing.T) {
t.Fatalf("expected 0, got %v", balance.Siacoins)
}

// check that both transactions were added
events, err = db.WalletEvents("test", 0, 100)
if err != nil {
t.Fatal(err)
} else if len(events) != 3 { // 1 payout, 2 transactions
t.Fatalf("expected 3 events, got %v", len(events))
} else if events[2].Data.EventType() != wallet.EventTypeMinerPayout {
t.Fatalf("expected miner payout event, got %v", events[2].Data.EventType())
} else if events[1].Data.EventType() != wallet.EventTypeTransaction {
t.Fatalf("expected transaction event, got %v", events[1].Data.EventType())
} else if events[0].Data.EventType() != wallet.EventTypeTransaction {
t.Fatalf("expected transaction event, got %v", events[0].Data.EventType())
} else if events[1].ID != types.Hash256(parentTxn.ID()) { // parent txn first
t.Fatalf("expected %v, got %v", parentTxn.ID(), events[1].ID)
} else if events[0].ID != types.Hash256(txn.ID()) { // child txn second
t.Fatalf("expected %v, got %v", txn.ID(), events[0].ID)
}

// trigger a reorg
var blocks []types.Block
state := revertState
Expand Down
2 changes: 1 addition & 1 deletion persist/sqlite/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func getWalletEvents(tx *txn, walletID string, offset, limit int) (events []wall
FROM events ev
INNER JOIN chain_indices ci ON (ev.index_id = ci.id)
WHERE ev.id IN (SELECT event_id FROM event_addresses WHERE address_id IN (SELECT address_id FROM wallet_addresses WHERE wallet_id=$1))
ORDER BY ev.maturity_height DESC
ORDER BY ev.maturity_height DESC, ev.id DESC
LIMIT $2 OFFSET $3`

rows, err := tx.Query(query, walletID, limit, offset)
Expand Down

0 comments on commit 182983a

Please sign in to comment.