Skip to content

Commit

Permalink
Merge pull request #548 from lightninglabs/use-64bit-primary-keys
Browse files Browse the repository at this point in the history
Use 64bit primary keys in DB
  • Loading branch information
Roasbeef authored Oct 4, 2023
2 parents f22fa1a + 6993be2 commit affe4f9
Show file tree
Hide file tree
Showing 27 changed files with 477 additions and 413 deletions.
2 changes: 1 addition & 1 deletion address/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type EventQueryParams struct {
// multiple events.
type Event struct {
// ID is the database primary key ID of the address event.
ID int32
ID int64

// CreationTime is the time the event was first created.
CreationTime time.Time
Expand Down
6 changes: 3 additions & 3 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3177,10 +3177,10 @@ func (r *rpcServer) ListFederationServers(ctx context.Context,
}, nil
}

func unmarshalUniverseServer(server *unirpc.UniverseFederationServer,
) universe.ServerAddr {
func unmarshalUniverseServer(
server *unirpc.UniverseFederationServer) universe.ServerAddr {

return universe.NewServerAddr(uint32(server.Id), server.Host)
return universe.NewServerAddr(int64(server.Id), server.Host)
}

// AddFederationServer adds a new server to the federation of the local
Expand Down
22 changes: 11 additions & 11 deletions tapdb/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,35 @@ type AddrBook interface {
arg []byte) (AddrByTaprootOutput, error)

// InsertAddr inserts a new address into the database.
InsertAddr(ctx context.Context, arg NewAddr) (int32, error)
InsertAddr(ctx context.Context, arg NewAddr) (int64, error)

// UpsertInternalKey inserts a new or updates an existing internal key
// into the database and returns the primary key.
UpsertInternalKey(ctx context.Context, arg InternalKey) (int32, error)
UpsertInternalKey(ctx context.Context, arg InternalKey) (int64, error)

// UpsertScriptKey inserts a new script key on disk into the DB.
UpsertScriptKey(context.Context, NewScriptKey) (int32, error)
UpsertScriptKey(context.Context, NewScriptKey) (int64, error)

// SetAddrManaged sets an address as being managed by the internal
// wallet.
SetAddrManaged(ctx context.Context, arg AddrManaged) error

// UpsertManagedUTXO inserts a new or updates an existing managed UTXO
// to disk and returns the primary key.
UpsertManagedUTXO(ctx context.Context, arg RawManagedUTXO) (int32,
UpsertManagedUTXO(ctx context.Context, arg RawManagedUTXO) (int64,
error)

// UpsertChainTx inserts a new or updates an existing chain tx into the
// DB.
UpsertChainTx(ctx context.Context, arg ChainTxParams) (int32, error)
UpsertChainTx(ctx context.Context, arg ChainTxParams) (int64, error)

// UpsertAddrEvent inserts a new or updates an existing address event
// and returns the primary key.
UpsertAddrEvent(ctx context.Context, arg UpsertAddrEvent) (int32, error)
UpsertAddrEvent(ctx context.Context, arg UpsertAddrEvent) (int64, error)

// FetchAddrEvent returns a single address event based on its primary
// key.
FetchAddrEvent(ctx context.Context, id int32) (AddrEvent, error)
FetchAddrEvent(ctx context.Context, id int64) (AddrEvent, error)

// QueryEventIDs returns a list of event IDs and their corresponding
// address IDs that match the given query parameters.
Expand Down Expand Up @@ -193,7 +193,7 @@ func NewTapAddressBook(db BatchedAddrBook, params *address.ChainParams,
// insertInternalKey inserts a new internal key into the DB and returns the
// primary key of the internal key.
func insertInternalKey(ctx context.Context, a AddrBook,
desc keychain.KeyDescriptor) (int32, error) {
desc keychain.KeyDescriptor) (int64, error) {

return a.UpsertInternalKey(ctx, InternalKey{
RawKey: desc.PubKey.SerializeCompressed(),
Expand Down Expand Up @@ -844,7 +844,7 @@ func (t *TapAddressBook) QueryAddrEvents(

// fetchEvent fetches a single address event identified by its primary ID and
// address.
func fetchEvent(ctx context.Context, db AddrBook, eventID int32,
func fetchEvent(ctx context.Context, db AddrBook, eventID int64,
addr *address.AddrWithKeyInfo) (*address.Event, error) {

dbEvent, err := db.FetchAddrEvent(ctx, eventID)
Expand Down Expand Up @@ -901,8 +901,8 @@ func (t *TapAddressBook) CompleteEvent(ctx context.Context,
Status: int16(status),
Txid: anchorPoint.Hash[:],
ChainTxnOutputIndex: int32(anchorPoint.Index),
AssetProofID: sqlInt32(proofData.ProofID),
AssetID: sqlInt32(proofData.AssetID),
AssetProofID: sqlInt64(proofData.ProofID),
AssetID: sqlInt64(proofData.AssetID),
})
return err
})
Expand Down
30 changes: 15 additions & 15 deletions tapdb/asset_minting.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ type PendingAssetStore interface {

// FetchSeedlingID is used to look up the ID of a specific seedling
// in a batch.
FetchSeedlingID(ctx context.Context, arg AssetSeedlingTuple) (int32,
FetchSeedlingID(ctx context.Context, arg AssetSeedlingTuple) (int64,
error)

// FetchSeedlingByID is used to look up a specific seedling.
FetchSeedlingByID(ctx context.Context,
seedlingID int32) (AssetSeedling, error)
seedlingID int64) (AssetSeedling, error)

// BindMintingBatchWithTx adds the minting transaction to an existing
// batch.
Expand All @@ -183,7 +183,7 @@ type PendingAssetStore interface {

// UpsertManagedUTXO inserts a new or updates an existing managed UTXO
// to disk and returns the primary key.
UpsertManagedUTXO(ctx context.Context, arg RawManagedUTXO) (int32,
UpsertManagedUTXO(ctx context.Context, arg RawManagedUTXO) (int64,
error)

// AnchorPendingAssets associated an asset on disk with the transaction
Expand All @@ -192,7 +192,7 @@ type PendingAssetStore interface {

// UpsertChainTx inserts a new or updates an existing chain tx into the
// DB.
UpsertChainTx(ctx context.Context, arg ChainTxParams) (int32, error)
UpsertChainTx(ctx context.Context, arg ChainTxParams) (int64, error)

// ConfirmChainTx confirms an existing chain tx.
ConfirmChainTx(ctx context.Context, arg ChainTxConf) error
Expand Down Expand Up @@ -334,7 +334,7 @@ func (a *AssetMintingStore) CommitMintingBatch(ctx context.Context,
return err
}

dbSeedling.GroupGenesisID = sqlInt32(genesisID)
dbSeedling.GroupGenesisID = sqlInt64(genesisID)
}

// If this seedling is being issued to a group being
Expand All @@ -349,7 +349,7 @@ func (a *AssetMintingStore) CommitMintingBatch(ctx context.Context,
return err
}

dbSeedling.GroupAnchorID = sqlInt32(anchorID)
dbSeedling.GroupAnchorID = sqlInt64(anchorID)
}

err = q.InsertAssetSeedling(ctx, dbSeedling)
Expand Down Expand Up @@ -407,7 +407,7 @@ func (a *AssetMintingStore) AddSeedlingsToBatch(ctx context.Context,
return err
}

dbSeedling.GroupGenesisID = sqlInt32(genesisID)
dbSeedling.GroupGenesisID = sqlInt64(genesisID)
}

// If this seedling is being issued to a group being
Expand All @@ -422,7 +422,7 @@ func (a *AssetMintingStore) AddSeedlingsToBatch(ctx context.Context,
return err
}

dbSeedling.GroupAnchorID = sqlInt32(anchorID)
dbSeedling.GroupAnchorID = sqlInt64(anchorID)
}

err = q.InsertAssetSeedlingIntoBatch(ctx, dbSeedling)
Expand All @@ -439,7 +439,7 @@ func (a *AssetMintingStore) AddSeedlingsToBatch(ctx context.Context,
// fetchSeedlingID attempts to fetch the ID for a seedling from a specific
// batch. This is performed within the context of a greater DB transaction.
func fetchSeedlingID(ctx context.Context, q PendingAssetStore,
batchKey []byte, seedlingName string) (int32, error) {
batchKey []byte, seedlingName string) (int64, error) {

seedlingParams := AssetSeedlingTuple{
SeedlingName: seedlingName,
Expand Down Expand Up @@ -479,7 +479,7 @@ func fetchAssetSeedlings(ctx context.Context, q PendingAssetStore,
// Fetch the group info for seedlings with a specific group.
// There can only be one group per genesis.
if dbSeedling.GroupGenesisID.Valid {
genID := extractSqlInt32[int32](
genID := extractSqlInt64[int64](
dbSeedling.GroupGenesisID,
)
seedlingGroup, err := fetchGroupByGenesis(ctx, q, genID)
Expand All @@ -496,7 +496,7 @@ func fetchAssetSeedlings(ctx context.Context, q PendingAssetStore,

// Fetch the group anchor for seedlings with a group anchor set.
if dbSeedling.GroupAnchorID.Valid {
anchorID := extractSqlInt32[int32](
anchorID := extractSqlInt64[int64](
dbSeedling.GroupAnchorID,
)
seedlingAnchor, err := q.FetchSeedlingByID(ctx, anchorID)
Expand Down Expand Up @@ -982,7 +982,7 @@ func (a *AssetMintingStore) AddSproutsToBatch(ctx context.Context,
ChangeOutputIndex: sqlInt32(
genesisPacket.ChangeOutputIndex,
),
GenesisID: sqlInt32(genesisPointID),
GenesisID: sqlInt64(genesisPointID),
})
if err != nil {
return fmt.Errorf("unable to add batch tx: %w", err)
Expand Down Expand Up @@ -1093,7 +1093,7 @@ func (a *AssetMintingStore) CommitSignedGenesisTx(ctx context.Context,
// managed UTXO.
err = q.AnchorPendingAssets(ctx, AssetAnchor{
PrevOut: genesisOutpoint,
AnchorUtxoID: sqlInt32(utxoID),
AnchorUtxoID: sqlInt64(utxoID),
})
if err != nil {
return fmt.Errorf("unable to anchor pending assets: %v", err)
Expand All @@ -1103,7 +1103,7 @@ func (a *AssetMintingStore) CommitSignedGenesisTx(ctx context.Context,
// transaction we inserted above.
if err := q.AnchorGenesisPoint(ctx, GenesisPointAnchor{
PrevOut: genesisOutpoint,
AnchorTxID: sqlInt32(chainTXID),
AnchorTxID: sqlInt64(chainTXID),
}); err != nil {
return fmt.Errorf("unable to anchor genesis tx: %w", err)
}
Expand Down Expand Up @@ -1167,7 +1167,7 @@ func (a *AssetMintingStore) MarkBatchConfirmed(ctx context.Context,
// FetchGroupByGenesis fetches the asset group created by the genesis referenced
// by the given ID.
func (a *AssetMintingStore) FetchGroupByGenesis(ctx context.Context,
genesisID int32) (*asset.AssetGroup, error) {
genesisID int64) (*asset.AssetGroup, error) {

var (
dbGroup *asset.AssetGroup
Expand Down
16 changes: 8 additions & 8 deletions tapdb/asset_minting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,22 +656,22 @@ func TestCommitBatchChainActions(t *testing.T) {
// above, we'll use that to confirm that the managed UTXO has been
// updated accordingly.
managedUTXO, err := db.FetchManagedUTXO(ctx, sqlc.FetchManagedUTXOParams{
TxnID: sqlInt32(dbGenTx.TxnID),
TxnID: sqlInt64(dbGenTx.TxnID),
})
require.NoError(t, err)
require.Equal(t, scriptRoot, managedUTXO.MerkleRoot)

// Next, we'll confirm that all the assets inserted previously now are
// able to be queried according to the anchor UTXO primary key.
anchoredAssets, err := db.FetchAssetsByAnchorTx(
ctx, sqlInt32(managedUTXO.UtxoID),
ctx, sqlInt64(managedUTXO.UtxoID),
)
require.NoError(t, err)
require.Equal(t, numSeedlings, len(anchoredAssets))

// Finally, we'll verify that the genesis point also points to the
// inserted chain transaction.
_, err = db.FetchGenesisPointByAnchorTx(ctx, sqlInt32(dbGenTx.TxnID))
_, err = db.FetchGenesisPointByAnchorTx(ctx, sqlInt64(dbGenTx.TxnID))
require.NoError(t, err)

// For each asset created above, we'll make a fake proof file for it.
Expand Down Expand Up @@ -889,7 +889,7 @@ func TestGroupStore(t *testing.T) {
for i, groupInfo := range mintGroups {
genID, err := fetchGenesisID(ctx, q, *groupInfo.Genesis)
require.NoError(t, err)
expectedID := int32(i + 1)
expectedID := int64(i + 1)
require.Equal(t, expectedID, genID)
}

Expand All @@ -914,16 +914,16 @@ func TestGroupStore(t *testing.T) {
// We should also be able to look up each asset group with a genesis ID.
fetchGroupByGenID := func(q PendingAssetStore) error {
for i, groupInfo := range mintGroups {
genID := int32(i + 1)
genID := int64(i + 1)
dbGroup, err := fetchGroupByGenesis(ctx, q, genID)
require.NoError(t, err)
assertGroupEqual(t, groupInfo, dbGroup)
}

// The returned group for the group anchor asset and reissued
// asset should be the same.
anchorGenID := int32(1)
reissueGenID := int32(3)
anchorGenID := int64(1)
reissueGenID := int64(3)
anchorGroup, err := fetchGroupByGenesis(ctx, q, anchorGenID)
require.NoError(t, err)
reissueGroup, err := fetchGroupByGenesis(ctx, q, reissueGenID)
Expand All @@ -948,7 +948,7 @@ func TestGroupStore(t *testing.T) {
_ = assetStore.db.ExecTx(ctx, &writeTxOpts, fetchGroupByGenID)

// Lookup of an invalid genesis ID should return a wrapped error.
invalidGenID := int32(len(mintGroups) + 1)
invalidGenID := int64(len(mintGroups) + 1)
fetchInvalidGenID := func(q PendingAssetStore) error {
dbGroup, err := fetchGroupByGenesis(ctx, q, invalidGenID)
require.Nil(t, dbGroup)
Expand Down
Loading

0 comments on commit affe4f9

Please sign in to comment.