Skip to content

Commit

Permalink
Merge pull request lightninglabs#811 from lightninglabs/flake-fix
Browse files Browse the repository at this point in the history
Fix unit and integration test flakes
  • Loading branch information
guggero authored Feb 23, 2024
2 parents f756868 + 14940ca commit 8eff420
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 22 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,15 @@ endif

flakehunter: build-itest
@$(call print, "Flake hunting ${backend} integration tests.")
while [ $$? -eq 0 ]; do make itest-only; done
while [ $$? -eq 0 ]; do make itest-only-trace; done

flake-unit:
@$(call print, "Flake hunting unit tests.")
while [ $$? -eq 0 ]; do '$(GOLIST) | $(XARGS) env $(GOTEST) -test.timeout=20m -count=1'; done
while [ $$? -eq 0 ]; do make unit nocache=1; done

flake-unit-trace:
@$(call print, "Flake hunting unit tests in debug mode.")
while [ $$? -eq 0 ]; do make unit-trace nocache=1; done

flake-unit-race:
@$(call print, "Flake hunting races in unit tests.")
Expand Down
28 changes: 28 additions & 0 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,34 @@ func AssertUniverseRootEquality(t *testing.T,
))
}

// AssertUniverseRootEqualityEventually checks that the universe roots returned
// by two daemons are either equal eventually.
func AssertUniverseRootEqualityEventually(t *testing.T,
clientA, clientB unirpc.UniverseClient) {

ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
defer cancel()

err := wait.NoError(func() error {
rootRequest := &unirpc.AssetRootRequest{}
universeRootsAlice, err := clientA.AssetRoots(ctxt, rootRequest)
require.NoError(t, err)
universeRootsBob, err := clientB.AssetRoots(ctxt, rootRequest)
require.NoError(t, err)

if !AssertUniverseRootsEqual(
universeRootsAlice, universeRootsBob,
) {

return fmt.Errorf("roots not equal")
}

return nil
}, defaultWaitTimeout)
require.NoError(t, err)
}

// AssertUniverseRoot makes sure the given universe root exists with the given
// sum, either identified by the asset ID or group key.
func AssertUniverseRoot(t *testing.T, client unirpc.UniverseClient,
Expand Down
8 changes: 8 additions & 0 deletions itest/multi_asset_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func testMultiAssetGroupSend(t *harnessTest) {
collectibleGroupMembers + 1,
})

AssertUniverseRootEqualityEventually(
t.t, t.tapd, t.universeServer.service,
)

// We'll make a second node now that'll be the receiver of all the
// assets made above.
secondTapd := setupTapdHarness(
Expand All @@ -326,6 +330,10 @@ func testMultiAssetGroupSend(t *harnessTest) {
require.NoError(t.t, secondTapd.stop(!*noDelete))
}()

AssertUniverseRootEqualityEventually(
t.t, secondTapd, t.universeServer.service,
)

// Send 5 of the assets to Bob, and verify that they are received.
numUnits := issuableAsset.Asset.Amount
assetType := issuableAsset.Asset.AssetType
Expand Down
24 changes: 23 additions & 1 deletion proof/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,29 @@ func (m *MockProofCourier) ReceiveProof(_ context.Context,
return nil, ErrProofNotFound
}

return proof, nil
return &AnnotatedProof{
Locator: Locator{
AssetID: proof.Locator.AssetID,
GroupKey: proof.Locator.GroupKey,
ScriptKey: proof.Locator.ScriptKey,
OutPoint: proof.Locator.OutPoint,
},
Blob: proof.Blob,
AssetSnapshot: &AssetSnapshot{
Asset: proof.AssetSnapshot.Asset,
OutPoint: proof.AssetSnapshot.OutPoint,
AnchorBlockHash: proof.AssetSnapshot.AnchorBlockHash,
AnchorBlockHeight: proof.AssetSnapshot.AnchorBlockHeight,
AnchorTxIndex: proof.AssetSnapshot.AnchorTxIndex,
AnchorTx: proof.AssetSnapshot.AnchorTx,
OutputIndex: proof.AssetSnapshot.OutputIndex,
InternalKey: proof.AssetSnapshot.InternalKey,
ScriptRoot: proof.AssetSnapshot.ScriptRoot,
TapscriptSibling: proof.AssetSnapshot.TapscriptSibling,
SplitAsset: proof.AssetSnapshot.SplitAsset,
MetaReveal: proof.AssetSnapshot.MetaReveal,
},
}, nil
}

// SetSubscribers sets the set of subscribers that will be notified
Expand Down
12 changes: 7 additions & 5 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3873,7 +3873,7 @@ func (r *rpcServer) QueryProof(ctx context.Context,
}

rpcsLog.Tracef("[QueryProof]: fetching proof at (universeID=%v, "+
"leafKey=%x)", universeID, leafKey.UniverseKey())
"leafKey=%x)", universeID.StringForLog(), leafKey.UniverseKey())

// Retrieve proof export config for the given universe.
syncConfigs, err := r.cfg.UniverseFederation.QuerySyncConfigs(ctx)
Expand Down Expand Up @@ -3937,7 +3937,8 @@ func (r *rpcServer) QueryProof(ctx context.Context,

rpcsLog.Debugf("[QueryProof]: error querying for "+
"proof at (universeID=%v, leafKey=%x)",
universeID, leafKey.UniverseKey())
universeID.StringForLog(),
leafKey.UniverseKey())
return nil, err
}

Expand All @@ -3953,12 +3954,13 @@ func (r *rpcServer) QueryProof(ctx context.Context,

// TODO(roasbeef): query may return multiple proofs, if allow key to
// not be fully specified
proof := proofs[0]
firstProof := proofs[0]

rpcsLog.Tracef("[QueryProof]: found proof at (universeID=%v, "+
"leafKey=%x)", universeID, leafKey.UniverseKey())
"leafKey=%x)", universeID.StringForLog(),
leafKey.UniverseKey())

return r.marshalUniverseProofLeaf(ctx, req, proof)
return r.marshalUniverseProofLeaf(ctx, req, firstProof)
}

// unmarshalAssetLeaf unmarshals an asset leaf from the RPC form.
Expand Down
5 changes: 3 additions & 2 deletions tapdb/asset_minting.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,11 @@ func (a *AssetMintingStore) CommitSignedGenesisTx(ctx context.Context,
AnchorUtxoID: sqlInt64(utxoID),
})
if err != nil {
return fmt.Errorf("unable to anchor pending assets: %w", err)
return fmt.Errorf("unable to anchor pending assets: %w",
err)
}

// Next, we'll anchor the genesis point to point to the chain
// Next, we'll anchor the genesis point-to-point to the chain
// transaction we inserted above.
if err := q.AnchorGenesisPoint(ctx, GenesisPointAnchor{
PrevOut: genesisOutpoint,
Expand Down
12 changes: 5 additions & 7 deletions tapdb/asset_minting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestCommitMintingBatchSeedlings(t *testing.T) {
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, numSeedlings)
addRandGroupToBatch(t, assetStore, ctx, mintingBatch.Seedlings)
err := assetStore.CommitMintingBatch(ctx, mintingBatch)
require.NoError(t, err, "unable to write batch: %v", err)
require.NoError(t, err)

batchKey := mintingBatch.BatchKey.PubKey

Expand All @@ -267,11 +267,9 @@ func TestCommitMintingBatchSeedlings(t *testing.T) {
// Pick a random seedling and give it a specific group.
addRandGroupToBatch(t, assetStore, ctx, seedlings)
mintingBatch.Seedlings = mergeMap(mintingBatch.Seedlings, seedlings)
require.NoError(t,
assetStore.AddSeedlingsToBatch(
ctx, batchKey, maps.Values(seedlings)...,
), "unable to write seedlings: %v", err,
)
require.NoError(t, assetStore.AddSeedlingsToBatch(
ctx, batchKey, maps.Values(seedlings)...,
))

// If we read the batch from disk again, then we should have 10 total
// seedlings, and the batch still matches what we wrote to disk.
Expand Down Expand Up @@ -1088,7 +1086,7 @@ func TestGroupAnchors(t *testing.T) {
)
addMultiAssetGroupToBatch(t, mintingBatch.Seedlings)
err := assetStore.CommitMintingBatch(ctx, mintingBatch)
require.NoError(t, err, "unable to write batch: %v", err)
require.NoError(t, err)

batchKey := mintingBatch.BatchKey.PubKey

Expand Down
2 changes: 1 addition & 1 deletion tapdb/multiverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func namespaceForProof(proofType universe.ProofType) (string, error) {
return transferMultiverseNS, nil

default:
return "", fmt.Errorf("unknown proof type: %v", int(proofType))
return "", fmt.Errorf("unknown proof type: %d", int(proofType))
}
}

Expand Down
2 changes: 1 addition & 1 deletion tapdb/sqlutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func parseCoalesceNumericType[T constraints.Integer](value any) (T, error) {
parsedValue, err := strconv.ParseInt(typedValue, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse value '%v' as "+
"number: %v", value, err)
"number: %w", value, err)
}

return T(parsedValue), nil
Expand Down
11 changes: 10 additions & 1 deletion tapgarden/custodian.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ func (c *Custodian) watchInboundAssets() {
continue
}

// If this event is not yet confirmed, we don't yet expect a
// proof to be delivered. We'll wait for the confirmation to
// come in, and then we'll launch a goroutine to use the
// ProofCourier to import the proof into our local DB.
if event.ConfirmationHeight == 0 {
continue
}

// If we didn't find a proof, we'll launch a goroutine to use
// the ProofCourier to import the proof into our local DB.
c.Wg.Add(1)
Expand Down Expand Up @@ -530,7 +538,8 @@ func (c *Custodian) receiveProof(addr *address.Tap, op wire.OutPoint) error {
ctx, headerVerifier, c.cfg.GroupVerifier, false, addrProof,
)
if err != nil {
return fmt.Errorf("unable to import proofs: %w", err)
return fmt.Errorf("unable to import proofs script_key=%x, "+
"asset_id=%x: %w", scriptKeyBytes, assetID[:], err)
}

// The proof is now verified and in our local archive. We will now
Expand Down
3 changes: 1 addition & 2 deletions tapgarden/planter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"testing"
"time"

"github.com/lightninglabs/taproot-assets/tapsend"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
Expand All @@ -30,6 +28,7 @@ import (
_ "github.com/lightninglabs/taproot-assets/tapdb" // Register relevant drivers.
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/tapscript"
"github.com/lightninglabs/taproot-assets/tapsend"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntest/wait"
Expand Down

0 comments on commit 8eff420

Please sign in to comment.