Skip to content

Commit 8c70b0b

Browse files
committed
tapdb+tapgarden: refactor RandSeedlingMintingBatch
Refactor RandSeedlingMintingBatch for generalization: - Rename to RandMintingBatch. - Change argument to `options...` pattern for extendability. The next commit will introduce additional options, enabling the creation of more specific mint batches.
1 parent 01f23a0 commit 8c70b0b

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

tapdb/asset_minting_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ func TestCommitMintingBatchSeedlings(t *testing.T) {
499499
// First, we'll write a new minting batch to disk, including an
500500
// internal key and a set of seedlings. One random seedling will
501501
// be a reissuance into a specific group.
502-
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, numSeedlings)
502+
mintingBatch := tapgarden.RandMintingBatch(
503+
t, tapgarden.WithTotalSeedlings(numSeedlings),
504+
)
503505
_, randGroup, _ := addRandGroupToBatch(
504506
t, assetStore, ctx, mintingBatch.Seedlings,
505507
)
@@ -600,7 +602,9 @@ func TestCommitMintingBatchSeedlings(t *testing.T) {
600602

601603
// Insert another normal batch into the database. We should get this
602604
// batch back if we query for the set of non-final batches.
603-
mintingBatch = tapgarden.RandSeedlingMintingBatch(t, numSeedlings)
605+
mintingBatch = tapgarden.RandMintingBatch(
606+
t, tapgarden.WithTotalSeedlings(numSeedlings),
607+
)
604608
err = assetStore.CommitMintingBatch(ctx, mintingBatch)
605609
require.NoError(t, err)
606610
mintingBatches = noError1(t, assetStore.FetchNonFinalBatches, ctx)
@@ -791,7 +795,9 @@ func TestAddSproutsToBatch(t *testing.T) {
791795

792796
// First, we'll create a new batch, then add some sample seedlings.
793797
// One random seedling will be a reissuance into a specific group.
794-
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, numSeedlings)
798+
mintingBatch := tapgarden.RandMintingBatch(
799+
t, tapgarden.WithTotalSeedlings(numSeedlings),
800+
)
795801
_, seedlingGroups, _ := addRandGroupToBatch(
796802
t, assetStore, ctx, mintingBatch.Seedlings,
797803
)
@@ -889,7 +895,9 @@ type randAssetCtx struct {
889895
func addRandAssets(t *testing.T, ctx context.Context,
890896
assetStore *AssetMintingStore, numAssets int) randAssetCtx {
891897

892-
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, numAssets)
898+
mintingBatch := tapgarden.RandMintingBatch(
899+
t, tapgarden.WithTotalSeedlings(numAssets),
900+
)
893901
genAmt, seedlingGroups, group := addRandGroupToBatch(
894902
t, assetStore, ctx, mintingBatch.Seedlings,
895903
)
@@ -1397,7 +1405,9 @@ func TestGroupAnchors(t *testing.T) {
13971405
// internal key and a set of seedlings. One random seedling will
13981406
// be a reissuance into a specific group. Two other seedlings will form
13991407
// a multi-asset group.
1400-
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, numSeedlings)
1408+
mintingBatch := tapgarden.RandMintingBatch(
1409+
t, tapgarden.WithTotalSeedlings(numSeedlings),
1410+
)
14011411
_, seedlingGroups, _ := addRandGroupToBatch(
14021412
t, assetStore, ctx, mintingBatch.Seedlings,
14031413
)
@@ -1834,7 +1844,9 @@ func TestUpsertMintAnchorUniCommitment(t *testing.T) {
18341844
assetStore, _, _ := newAssetStore(t)
18351845

18361846
// Create a new batch with one asset group seedling.
1837-
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, 1)
1847+
mintingBatch := tapgarden.RandMintingBatch(
1848+
t, tapgarden.WithTotalSeedlings(1),
1849+
)
18381850
mintingBatch.UniverseCommitments = true
18391851

18401852
_, _, group := addRandGroupToBatch(

tapgarden/mock.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,43 @@ func RandSeedlings(t testing.TB, numSeedlings int) map[string]*Seedling {
5656
return seedlings
5757
}
5858

59-
// RandSeedlingMintingBatch creates a new minting batch with only random
60-
// seedlings populated for testing.
61-
func RandSeedlingMintingBatch(t testing.TB, numSeedlings int) *MintingBatch {
59+
// MintBatchOptions is a set of options for creating a new minting batch.
60+
type MintBatchOptions struct {
61+
// totalSeedlings specifies the number of seedlings to generate in this
62+
// minting batch. The seedlings are randomly assigned as grouped or
63+
// ungrouped.
64+
totalSeedlings int
65+
}
66+
67+
// MintBatchOption is a functional option for creating a new minting batch.
68+
type MintBatchOption func(*MintBatchOptions)
69+
70+
// DefaultMintBatchOptions returns a new set of default minting batch options.
71+
func DefaultMintBatchOptions() MintBatchOptions {
72+
return MintBatchOptions{}
73+
}
74+
75+
// WithTotalSeedlings sets the total number of seedlings to populate in the
76+
// minting batch.
77+
func WithTotalSeedlings(count int) MintBatchOption {
78+
return func(options *MintBatchOptions) {
79+
options.totalSeedlings = count
80+
}
81+
}
82+
83+
// RandMintingBatch creates a new minting batch with only random seedlings
84+
// populated for testing.
85+
func RandMintingBatch(t testing.TB, opts ...MintBatchOption) *MintingBatch {
86+
// Construct options.
87+
options := DefaultMintBatchOptions()
88+
for _, opt := range opts {
89+
opt(&options)
90+
}
91+
6292
batchKey, _ := test.RandKeyDesc(t)
6393
batch := MintingBatch{
6494
BatchKey: batchKey,
65-
Seedlings: RandSeedlings(t, numSeedlings),
95+
Seedlings: RandSeedlings(t, options.totalSeedlings),
6696
HeightHint: test.RandInt[uint32](),
6797
CreationTime: time.Now(),
6898
}

0 commit comments

Comments
 (0)