Skip to content

Commit fece1d1

Browse files
committed
tapdb: add test db helper func for server addresses and proof leaves
This commit adds a helper method for inserting server addresses into a unit test db. It also adds a helper method for inserting a proof leaf into a unit test db. These helper methods will be used in a subsequent commit.
1 parent 473ae23 commit fece1d1

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
lines changed

tapdb/sqlutils_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"database/sql"
7+
"fmt"
78
"math/rand"
89
"testing"
910
"time"
@@ -15,6 +16,7 @@ import (
1516
"github.com/lightninglabs/taproot-assets/internal/test"
1617
"github.com/lightninglabs/taproot-assets/proof"
1718
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
19+
"github.com/lightninglabs/taproot-assets/universe"
1820
"github.com/lightningnetwork/lnd/clock"
1921
"github.com/stretchr/testify/require"
2022
)
@@ -158,6 +160,67 @@ func (d *DbHandler) AddRandomAssetProof(t *testing.T) (*asset.Asset,
158160
return testAsset, annotatedProof
159161
}
160162

163+
// AddUniProofLeaf generates a universe proof leaf and inserts it into the test
164+
// database.
165+
func (d *DbHandler) AddUniProofLeaf(t *testing.T, testAsset *asset.Asset,
166+
annotatedProof *proof.AnnotatedProof) *universe.Proof {
167+
168+
ctx := context.Background()
169+
170+
// Insert proof into the multiverse/universe store. This step will
171+
// populate the universe root and universe leaves tables.
172+
uniId := universe.NewUniIDFromAsset(*testAsset)
173+
174+
leafKey := universe.LeafKey{
175+
OutPoint: annotatedProof.AssetSnapshot.OutPoint,
176+
ScriptKey: &testAsset.ScriptKey,
177+
}
178+
179+
leaf := universe.Leaf{
180+
GenesisWithGroup: universe.GenesisWithGroup{
181+
Genesis: testAsset.Genesis,
182+
GroupKey: testAsset.GroupKey,
183+
},
184+
RawProof: annotatedProof.Blob,
185+
Asset: testAsset,
186+
Amt: testAsset.Amount,
187+
}
188+
189+
uniProof, err := d.MultiverseStore.UpsertProofLeaf(
190+
ctx, uniId, leafKey, &leaf, nil,
191+
)
192+
require.NoError(t, err)
193+
194+
return uniProof
195+
}
196+
197+
// AddRandomServerAddrs is a helper function that will create server addresses
198+
// and add them to the database.
199+
func (d *DbHandler) AddRandomServerAddrs(t *testing.T,
200+
numServers int) []universe.ServerAddr {
201+
202+
var (
203+
ctx = context.Background()
204+
fedDB = d.UniverseFederationStore
205+
)
206+
207+
addrs := make([]universe.ServerAddr, 0, numServers)
208+
for i := 0; i < numServers; i++ {
209+
portOffset := i + 10_000
210+
hostStr := fmt.Sprintf("localhost:%v", portOffset)
211+
212+
addr := universe.NewServerAddr(int64(i+1), hostStr)
213+
addrs = append(addrs, addr)
214+
}
215+
216+
// With the set of addrs created, we'll now insert them all into the
217+
// database.
218+
err := fedDB.AddServers(ctx, addrs...)
219+
require.NoError(t, err)
220+
221+
return addrs
222+
}
223+
161224
// NewDbHandle creates a new store and query handle to the test database.
162225
func NewDbHandle(t *testing.T) *DbHandler {
163226
// Create a new test database.

tapdb/universe_federation_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tapdb
33
import (
44
"context"
55
"database/sql"
6-
"fmt"
76
"testing"
87
"time"
98

@@ -35,10 +34,12 @@ func newTestFederationDb(t *testing.T,
3534
func TestUniverseFederationCRUD(t *testing.T) {
3635
t.Parallel()
3736

38-
testClock := clock.NewTestClock(time.Now())
39-
fedDB, _ := newTestFederationDb(t, testClock)
37+
var (
38+
ctx = context.Background()
4039

41-
ctx := context.Background()
40+
db = NewDbHandle(t)
41+
fedDB = db.UniverseFederationStore
42+
)
4243

4344
// If we try to list the set of servers without any added, we should
4445
// get the error we expect.
@@ -47,19 +48,7 @@ func TestUniverseFederationCRUD(t *testing.T) {
4748
require.Empty(t, dbServers)
4849

4950
// Next, we'll try to add a new series of servers to the DB.
50-
const numServers = 10
51-
addrs := make([]universe.ServerAddr, 0, numServers)
52-
for i := int64(0); i < numServers; i++ {
53-
portOffset := i + 10_000
54-
hostStr := fmt.Sprintf("localhost:%v", portOffset)
55-
56-
addrs = append(addrs, universe.NewServerAddr(i+1, hostStr))
57-
}
58-
59-
// With the set of addrs created, we'll now insert them all into the
60-
// database.
61-
err = fedDB.AddServers(ctx, addrs...)
62-
require.NoError(t, err)
51+
addrs := db.AddRandomServerAddrs(t, 10)
6352

6453
// If we try to insert them all again, then we should get an error as
6554
// we ensure the host names are unique.

universe/interface.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ func (i *Identifier) StringForLog() string {
8484
i.String(), i.AssetID[:], groupKey, i.ProofType)
8585
}
8686

87+
// NewUniIDFromAsset creates a new universe ID from an asset.
88+
func NewUniIDFromAsset(a asset.Asset) Identifier {
89+
proofType := ProofTypeTransfer
90+
if a.IsGenesisAsset() {
91+
proofType = ProofTypeIssuance
92+
}
93+
94+
if a.GroupKey != nil {
95+
return Identifier{
96+
GroupKey: &a.GroupKey.GroupPubKey,
97+
ProofType: proofType,
98+
}
99+
}
100+
101+
return Identifier{
102+
AssetID: a.ID(),
103+
ProofType: proofType,
104+
}
105+
}
106+
87107
// NewUniIDFromRawArgs creates a new universe ID from the raw arguments. The
88108
// asset ID bytes and group key bytes are mutually exclusive. If the group key
89109
// bytes are set, then the asset ID bytes will be ignored.

0 commit comments

Comments
 (0)