Skip to content

Commit 3f20bf3

Browse files
committed
tapdb+address: allow querying for last event height by version
We'll need to know the last block height we processed for any event based on the address version.
1 parent 33a37af commit 3f20bf3

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

address/book.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ type Storage interface {
138138
SetAddrManaged(ctx context.Context, addr *AddrWithKeyInfo,
139139
managedFrom time.Time) error
140140

141+
// LastEventHeightByVersion returns the last event height for a given
142+
// address version.
143+
LastEventHeightByVersion(ctx context.Context,
144+
version Version) (uint32, error)
145+
141146
// InsertInternalKey inserts an internal key into the database to make
142147
// sure it is identified as a local key later on when importing proofs.
143148
// The key can be an internal key for an asset script key or the
@@ -701,6 +706,14 @@ func (b *Book) SetAddrManaged(ctx context.Context, addr *AddrWithKeyInfo,
701706
return b.cfg.Store.SetAddrManaged(ctx, addr, managedFrom)
702707
}
703708

709+
// LastEventHeightByVersion returns the last event height for a given address
710+
// version.
711+
func (b *Book) LastEventHeightByVersion(ctx context.Context,
712+
version Version) (uint32, error) {
713+
714+
return b.cfg.Store.LastEventHeightByVersion(ctx, version)
715+
}
716+
704717
// GetOrCreateEvent creates a new address event for the given status, address
705718
// and transaction. If an event for that address and transaction already exists,
706719
// then the status and transaction information is updated instead.

address/book_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func TestBook_QueryAssetInfo(t *testing.T) {
146146
expectsError: false,
147147
}}
148148

149-
for _, test := range tests {
150-
t.Run(test.name, func(t *testing.T) {
149+
for _, tc := range tests {
150+
t.Run(tc.name, func(t *testing.T) {
151151
mockStorage := &MockStorage{}
152152
mockSyncer := &MockAssetSyncer{}
153153
book := NewBook(BookConfig{
@@ -157,9 +157,9 @@ func TestBook_QueryAssetInfo(t *testing.T) {
157157
Chain: ChainParams{},
158158
StoreTimeout: time.Second,
159159
})
160-
test.setupMocks(mockStorage, mockSyncer)
161-
_, err := book.QueryAssetInfo(ctx, test.specifier)
162-
if test.expectsError {
160+
tc.setupMocks(mockStorage, mockSyncer)
161+
_, err := book.QueryAssetInfo(ctx, tc.specifier)
162+
if tc.expectsError {
163163
require.Error(t, err)
164164
} else {
165165
require.NoError(t, err)
@@ -182,6 +182,13 @@ func (m *MockStorage) FetchAllAssetMeta(
182182
return args.Get(0).(map[asset.ID]*proof.MetaReveal), args.Error(1)
183183
}
184184

185+
func (m *MockStorage) LastEventHeightByVersion(ctx context.Context,
186+
version Version) (uint32, error) {
187+
188+
args := m.Called(ctx, version)
189+
return args.Get(0).(uint32), args.Error(1)
190+
}
191+
185192
func (m *MockStorage) AddrByScriptKeyAndVersion(ctx context.Context,
186193
key *btcec.PublicKey, version Version) (*AddrWithKeyInfo, error) {
187194

tapdb/addrs.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ type AddrBook interface {
194194
// FetchAllAssetMeta fetches all asset metadata records from the
195195
// database.
196196
FetchAllAssetMeta(ctx context.Context) ([]AllAssetMetaRow, error)
197+
198+
// QueryLastEventHeight queries the last event height for a given
199+
// address version.
200+
QueryLastEventHeight(ctx context.Context,
201+
version int16) (int64, error)
197202
}
198203

199204
// AddrBookTxOptions defines the set of db txn options the AddrBook
@@ -1101,6 +1106,26 @@ func (t *TapAddressBook) CompleteEvent(ctx context.Context,
11011106
})
11021107
}
11031108

1109+
// LastEventHeightByVersion returns the last event height for a given address
1110+
// version.
1111+
func (t *TapAddressBook) LastEventHeightByVersion(ctx context.Context,
1112+
version address.Version) (uint32, error) {
1113+
1114+
var lastHeight int64
1115+
1116+
readOpts := NewAssetStoreReadTx()
1117+
err := t.db.ExecTx(ctx, &readOpts, func(db AddrBook) error {
1118+
var err error
1119+
lastHeight, err = db.QueryLastEventHeight(ctx, int16(version))
1120+
return err
1121+
})
1122+
if err != nil {
1123+
return 0, err
1124+
}
1125+
1126+
return uint32(lastHeight), nil
1127+
}
1128+
11041129
// QueryAssetGroup attempts to fetch an asset group by its asset ID. If the
11051130
// asset group cannot be found, then ErrAssetGroupUnknown is returned.
11061131
func (t *TapAddressBook) QueryAssetGroup(ctx context.Context,

tapdb/addrs_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ func TestAddrEventCreation(t *testing.T) {
431431

432432
ctx := context.Background()
433433

434+
// Before we start, the last height for any version should be 0, as
435+
// we don't have any events yet.
436+
height, err := addrBook.LastEventHeightByVersion(ctx, address.V0)
437+
require.NoError(t, err)
438+
require.EqualValues(t, 0, height)
439+
height, err = addrBook.LastEventHeightByVersion(ctx, address.V1)
440+
require.NoError(t, err)
441+
require.EqualValues(t, 0, height)
442+
434443
// Create 5 addresses and then events with unconfirmed transactions.
435444
const numAddrs = 5
436445
proofCourierAddr := address.RandProofCourierAddr(t)
@@ -498,18 +507,30 @@ func TestAddrEventCreation(t *testing.T) {
498507

499508
// Now we update the status of our event, make the transaction confirmed
500509
// and set the tapscript sibling to nil for all of them.
501-
for idx := range events {
510+
maxHeightByVersion := make(map[address.Version]int32)
511+
for idx, event := range events {
512+
txn := txns[idx]
502513
confirmTx(txns[idx])
503514
events[idx].Status = address.StatusTransactionConfirmed
504515
events[idx].ConfirmationHeight = uint32(txns[idx].BlockHeight)
505516

506517
actual, err := addrBook.GetOrCreateEvent(
507518
ctx, address.StatusTransactionConfirmed,
508-
events[idx].Addr, txns[idx], events[idx].Outpoint.Index,
519+
event.Addr, txns[idx], events[idx].Outpoint.Index,
509520
)
510521
require.NoError(t, err)
511522

512523
assertEqualAddrEvent(t, *events[idx], *actual)
524+
525+
if maxHeightByVersion[event.Addr.Version] < txn.BlockHeight {
526+
maxHeightByVersion[event.Addr.Version] = txn.BlockHeight
527+
}
528+
}
529+
530+
for version, maxHeight := range maxHeightByVersion {
531+
height, err := addrBook.LastEventHeightByVersion(ctx, version)
532+
require.NoError(t, err)
533+
require.EqualValues(t, maxHeight, height)
513534
}
514535
}
515536

tapdb/sqlc/addrs.sql.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/addrs.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,12 @@ WHERE addr_events.status >= @status_from
174174
AND COALESCE(@addr_taproot_key, addrs.taproot_output_key) = addrs.taproot_output_key
175175
AND addr_events.creation_time >= @created_after
176176
ORDER by addr_events.creation_time;
177+
178+
-- name: QueryLastEventHeight :one
179+
SELECT cast(coalesce(max(chain_txns.block_height), 0) AS BIGINT) AS last_height
180+
FROM addr_events
181+
JOIN chain_txns
182+
ON addr_events.chain_txn_id = chain_txns.txn_id
183+
JOIN addrs
184+
ON addr_events.addr_id = addrs.id
185+
WHERE addrs.version = $1;

0 commit comments

Comments
 (0)