Skip to content

Commit 4073831

Browse files
committed
tapdb: add SparseBlockHeight helper function
1 parent 51cba34 commit 4073831

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tapdb/sqlutils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tapdb
22

33
import (
4+
"bytes"
45
"context"
56
"database/sql"
67
"encoding/binary"
@@ -14,7 +15,9 @@ import (
1415
"github.com/btcsuite/btcd/wire"
1516
"github.com/lightninglabs/taproot-assets/fn"
1617
"github.com/lightninglabs/taproot-assets/internal/test"
18+
"github.com/lightninglabs/taproot-assets/proof"
1719
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
20+
lfn "github.com/lightningnetwork/lnd/fn/v2"
1821
"github.com/stretchr/testify/require"
1922
"golang.org/x/exp/constraints"
2023
)
@@ -94,6 +97,25 @@ func sqlStr(s string) sql.NullString {
9497
}
9598
}
9699

100+
// SparseDecodeBlockHeight sparse decodes a proof to extract the block height.
101+
func SparseDecodeBlockHeight(rawProof []byte) (lfn.Option[uint32], error) {
102+
var blockHeightVal uint32
103+
err := proof.SparseDecode(
104+
bytes.NewReader(rawProof),
105+
proof.BlockHeightRecord(&blockHeightVal),
106+
)
107+
if err != nil {
108+
return lfn.None[uint32](), fmt.Errorf("unable to "+
109+
"sparse decode proof: %w", err)
110+
}
111+
112+
if blockHeightVal == 0 {
113+
return lfn.None[uint32](), nil
114+
}
115+
116+
return lfn.Some(blockHeightVal), nil
117+
}
118+
97119
// extractSqlInt64 turns a NullInt64 into a numerical type. This can be useful
98120
// when reading directly from the database, as this function handles extracting
99121
// the inner value from the "option"-like struct.

0 commit comments

Comments
 (0)