Skip to content

Commit

Permalink
itest: add re-org test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Aug 9, 2023
1 parent 9292ff1 commit 9cfba99
Show file tree
Hide file tree
Showing 3 changed files with 493 additions and 7 deletions.
97 changes: 90 additions & 7 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func assetAnchorCheck(txid, blockHash chainhash.Hash) assetCheck {

if a.ChainAnchor.AnchorTxid != txid.String() {
return fmt.Errorf("unexpected asset anchor TXID, got "+
"%x wanted %x", a.ChainAnchor.AnchorTxid,
"%v wanted %x", a.ChainAnchor.AnchorTxid,
txid[:])
}

if a.ChainAnchor.AnchorBlockHash != blockHash.String() {
return fmt.Errorf("unexpected asset anchor block "+
"hash, got %x wanted %x",
"hash, got %v wanted %x",
a.ChainAnchor.AnchorBlockHash, blockHash[:])
}

Expand Down Expand Up @@ -201,6 +201,39 @@ func commitmentKey(t *testing.T, rpcAsset *taprpc.Asset) [32]byte {
return asset.AssetCommitmentKey(assetID, scriptKey, groupKey == nil)
}

// waitForProofUpdate polls until the proof for the given asset has been
// updated, which is detected by checking the block height of the last proof.
func waitForProofUpdate(t *testing.T, tapd *tapdHarness, a *taprpc.Asset,
blockHeight int32) {

t.Helper()

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

require.Eventually(t, func() bool {
// Export the proof, then decode it.
exportResp, err := tapd.ExportProof(
ctxt, &taprpc.ExportProofRequest{
AssetId: a.AssetGenesis.AssetId,
ScriptKey: a.ScriptKey,
},
)
require.NoError(t, err)

f := &proof.File{}
require.NoError(
t, f.Decode(bytes.NewReader(exportResp.RawProof)),
)
lastProof, err := f.LastProof()
require.NoError(t, err)

// Check the block height of the proof.
return lastProof.BlockHeight == uint32(blockHeight)
}, defaultWaitTimeout, 200*time.Millisecond)
}

// assertAssetProofs makes sure the proofs for the given asset can be retrieved
// from the given daemon and can be fully validated.
func assertAssetProofs(t *testing.T, tapd *tapdHarness,
Expand Down Expand Up @@ -233,6 +266,34 @@ func assertAssetProofs(t *testing.T, tapd *tapdHarness,
return exportResp.RawProof
}

// assertAssetProofsInvalid makes sure the proofs for the given asset can be
// retrieved from the given daemon but fail to validate.
func assertAssetProofsInvalid(t *testing.T, tapd *tapdHarness,
a *taprpc.Asset) {

t.Helper()

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

exportResp, err := tapd.ExportProof(ctxt, &taprpc.ExportProofRequest{
AssetId: a.AssetGenesis.AssetId,
ScriptKey: a.ScriptKey,
})
require.NoError(t, err)

f := &proof.File{}
require.NoError(t, f.Decode(bytes.NewReader(exportResp.RawProof)))

// Also make sure that the RPC can verify the proof as well.
verifyResp, err := tapd.VerifyProof(ctxt, &taprpc.ProofFile{
RawProof: exportResp.RawProof,
})
require.NoError(t, err)
require.False(t, verifyResp.Valid)
}

// verifyProofBlob parses the given proof blob into a file, verifies it and
// returns the resulting last asset snapshot together with the parsed file.
func verifyProofBlob(t *testing.T, tapd *tapdHarness, a *taprpc.Asset,
Expand Down Expand Up @@ -287,7 +348,7 @@ func verifyProofBlob(t *testing.T, tapd *tapdHarness, a *taprpc.Asset,
expectedHash := hash
if heightHash != expectedHash {
return fmt.Errorf("block hash and block height "+
"mismatch; (height: %x, hashAtHeight: %s, "+
"mismatch; (height: %d, hashAtHeight: %s, "+
"expectedHash: %s)", height, heightHash,
expectedHash)
}
Expand Down Expand Up @@ -421,9 +482,10 @@ func assertAddrEventByStatus(t *testing.T, tapd *tapdHarness,
// with the node.
func confirmAndAssertOutboundTransfer(t *harnessTest, sender *tapdHarness,
sendResp *taprpc.SendAssetResponse, assetID []byte,
expectedAmounts []uint64, currentTransferIdx, numTransfers int) {
expectedAmounts []uint64, currentTransferIdx,
numTransfers int) *wire.MsgBlock {

confirmAndAssetOutboundTransferWithOutputs(
return confirmAndAssetOutboundTransferWithOutputs(
t, sender, sendResp, assetID, expectedAmounts,
currentTransferIdx, numTransfers, 2,
)
Expand All @@ -435,7 +497,7 @@ func confirmAndAssertOutboundTransfer(t *harnessTest, sender *tapdHarness,
func confirmAndAssetOutboundTransferWithOutputs(t *harnessTest,
sender *tapdHarness, sendResp *taprpc.SendAssetResponse,
assetID []byte, expectedAmounts []uint64, currentTransferIdx,
numTransfers, numOutputs int) {
numTransfers, numOutputs int) *wire.MsgBlock {

ctxb := context.Background()

Expand All @@ -459,7 +521,7 @@ func confirmAndAssetOutboundTransferWithOutputs(t *harnessTest,
t.Logf("Got response from sending assets: %v", sendRespJSON)

// Mine a block to force the send event to complete (confirm on-chain).
_ = mineBlocks(t, t.lndHarness, 1, 1)
newBlock := mineBlocks(t, t.lndHarness, 1, 1)[0]

// Confirm that we can externally view the transfer.
require.Eventually(t.t, func() bool {
Expand Down Expand Up @@ -494,6 +556,8 @@ func confirmAndAssetOutboundTransferWithOutputs(t *harnessTest,
transferRespJSON, err := formatProtoJSON(transferResp)
require.NoError(t.t, err)
t.Logf("Got response from list transfers: %v", transferRespJSON)

return newBlock
}

// assertNonInteractiveRecvComplete makes sure the given receiver has the
Expand Down Expand Up @@ -780,6 +844,25 @@ func assertListAssets(t *harnessTest, ctx context.Context, tapd *tapdHarness,
}
}

// assertUniverseRootEquality checks that the universe roots returned by two
// daemons are either equal or not, depending on the expectedEquality parameter.
func assertUniverseRootEquality(t *testing.T, a, b *tapdHarness,
expectedEquality bool) {

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

rootRequest := &unirpc.AssetRootRequest{}
universeRootsAlice, err := a.AssetRoots(ctxt, rootRequest)
require.NoError(t, err)
universeRootsBob, err := b.AssetRoots(ctxt, rootRequest)
require.NoError(t, err)
require.Equal(t, expectedEquality, assertUniverseRootsEqual(
universeRootsAlice, universeRootsBob,
))
}

func assertUniverseRoot(t *testing.T, tapd *tapdHarness, sum int,
assetID []byte, groupKey []byte) error {

Expand Down
Loading

0 comments on commit 9cfba99

Please sign in to comment.