From 8e00705de050b1b87a8845a4804cd80f104e14cf Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Wed, 24 Jul 2024 15:28:26 -0400 Subject: [PATCH 1/2] Cleanup wallet tests --- wallet/chain/p/builder_test.go | 266 +++++++++++++++++++-------------- 1 file changed, 157 insertions(+), 109 deletions(-) diff --git a/wallet/chain/p/builder_test.go b/wallet/chain/p/builder_test.go index 9f73b9e399b9..34c4df18e861 100644 --- a/wallet/chain/p/builder_test.go +++ b/wallet/chain/p/builder_test.go @@ -83,14 +83,17 @@ func TestBaseTx(t *testing.T) { require.NoError(err) // check UTXOs selection and fee financing - ins := utx.Ins outs := utx.Outs - require.Len(ins, 2) require.Len(outs, 2) - - expectedConsumed := testContext.BaseTxFee + outputsToMove[0].Out.Amount() - consumed := ins[0].In.Amount() + ins[1].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + require.Equal( + addAmounts( + addOutputAmounts(outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.BaseTxFee, + }, + ), + addInputAmounts(utx.Ins), + ) require.Equal(outputsToMove[0], outs[1]) } @@ -140,15 +143,16 @@ func TestAddSubnetValidatorTx(t *testing.T) { utx, err := builder.NewAddSubnetValidatorTx(subnetValidator) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 2) - require.Len(outs, 1) - - expectedConsumed := testContext.AddSubnetValidatorFee - consumed := ins[0].In.Amount() + ins[1].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.AddSubnetValidatorFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestRemoveSubnetValidatorTx(t *testing.T) { @@ -191,15 +195,16 @@ func TestRemoveSubnetValidatorTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 1) - require.Len(outs, 1) - - expectedConsumed := testContext.BaseTxFee - consumed := ins[0].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.BaseTxFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestCreateChainTx(t *testing.T) { @@ -250,15 +255,16 @@ func TestCreateChainTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 1) - require.Len(outs, 1) - - expectedConsumed := testContext.CreateBlockchainTxFee - consumed := ins[0].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.CreateBlockchainTxFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestCreateSubnetTx(t *testing.T) { @@ -298,15 +304,16 @@ func TestCreateSubnetTx(t *testing.T) { utx, err := builder.NewCreateSubnetTx(subnetOwner) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 1) - require.Len(outs, 1) - - expectedConsumed := testContext.CreateSubnetTxFee - consumed := ins[0].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.CreateSubnetTxFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestTransferSubnetOwnershipTx(t *testing.T) { @@ -349,15 +356,16 @@ func TestTransferSubnetOwnershipTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 1) - require.Len(outs, 1) - - expectedConsumed := testContext.BaseTxFee - consumed := ins[0].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.BaseTxFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestImportTx(t *testing.T) { @@ -397,17 +405,18 @@ func TestImportTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - importedIns := utx.ImportedInputs - require.Empty(ins) // we spend the imported input (at least partially) - require.Len(importedIns, 1) - require.Len(outs, 1) - - expectedConsumed := testContext.BaseTxFee - consumed := importedIns[0].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + require.Empty(utx.Ins) // we spend the imported input (at least partially) + + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.BaseTxFee, + }, + ), + addInputAmounts(utx.Ins, utx.ImportedInputs), + ) } func TestExportTx(t *testing.T) { @@ -447,16 +456,18 @@ func TestExportTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 2) - require.Len(outs, 1) - - expectedConsumed := testContext.BaseTxFee + exportedOutputs[0].Out.Amount() - consumed := ins[0].In.Amount() + ins[1].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) require.Equal(utx.ExportedOutputs, exportedOutputs) + + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs, utx.ExportedOutputs), + map[ids.ID]uint64{ + avaxAssetID: testContext.BaseTxFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestTransformSubnetTx(t *testing.T) { @@ -515,18 +526,17 @@ func TestTransformSubnetTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - outs := utx.Outs - require.Len(ins, 2) - require.Len(outs, 2) - - expectedConsumedSubnetAsset := maxSupply - initialSupply - consumedSubnetAsset := ins[0].In.Amount() - outs[1].Out.Amount() - require.Equal(expectedConsumedSubnetAsset, consumedSubnetAsset) - expectedConsumed := testContext.TransformSubnetTxFee - consumed := ins[1].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs), + map[ids.ID]uint64{ + avaxAssetID: testContext.TransformSubnetTxFee, + subnetAssetID: maxSupply - initialSupply, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestAddPermissionlessValidatorTx(t *testing.T) { @@ -583,20 +593,24 @@ func TestAddPermissionlessValidatorTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - staked := utx.StakeOuts - outs := utx.Outs - require.Len(ins, 4) - require.Len(staked, 2) - require.Len(outs, 2) + // check stake amount + require.Equal( + map[ids.ID]uint64{ + avaxAssetID: 2 * units.Avax, + }, + addOutputAmounts(utx.StakeOuts), + ) - expectedConsumedSubnetAsset := utx.Validator.Weight() - consumedSubnetAsset := staked[0].Out.Amount() + staked[1].Out.Amount() - require.Equal(expectedConsumedSubnetAsset, consumedSubnetAsset) - expectedConsumed := testContext.AddPrimaryNetworkValidatorFee - consumed := ins[1].In.Amount() + ins[3].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs, utx.StakeOuts), + map[ids.ID]uint64{ + avaxAssetID: testContext.AddPrimaryNetworkValidatorFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func TestAddPermissionlessDelegatorTx(t *testing.T) { @@ -641,20 +655,24 @@ func TestAddPermissionlessDelegatorTx(t *testing.T) { ) require.NoError(err) - // check UTXOs selection and fee financing - ins := utx.Ins - staked := utx.StakeOuts - outs := utx.Outs - require.Len(ins, 4) - require.Len(staked, 2) - require.Len(outs, 2) + // check stake amount + require.Equal( + map[ids.ID]uint64{ + avaxAssetID: 2 * units.Avax, + }, + addOutputAmounts(utx.StakeOuts), + ) - expectedConsumedSubnetAsset := utx.Validator.Weight() - consumedSubnetAsset := staked[0].Out.Amount() + staked[1].Out.Amount() - require.Equal(expectedConsumedSubnetAsset, consumedSubnetAsset) - expectedConsumed := testContext.AddPrimaryNetworkDelegatorFee - consumed := ins[1].In.Amount() + ins[3].In.Amount() - outs[0].Out.Amount() - require.Equal(expectedConsumed, consumed) + // check fee calculation + require.Equal( + addAmounts( + addOutputAmounts(utx.Outs, utx.StakeOuts), + map[ids.ID]uint64{ + avaxAssetID: testContext.AddPrimaryNetworkDelegatorFee, + }, + ), + addInputAmounts(utx.Ins), + ) } func makeTestUTXOs(utxosKey *secp256k1.PrivateKey) []*avax.UTXO { @@ -745,3 +763,33 @@ func makeTestUTXOs(utxosKey *secp256k1.PrivateKey) []*avax.UTXO { }, } } + +func addAmounts(allAmounts ...map[ids.ID]uint64) map[ids.ID]uint64 { + amounts := make(map[ids.ID]uint64) + for _, amountsToAdd := range allAmounts { + for assetID, amount := range amountsToAdd { + amounts[assetID] += amount + } + } + return amounts +} + +func addInputAmounts(inputSlices ...[]*avax.TransferableInput) map[ids.ID]uint64 { + consumed := make(map[ids.ID]uint64) + for _, inputs := range inputSlices { + for _, in := range inputs { + consumed[in.AssetID()] += in.In.Amount() + } + } + return consumed +} + +func addOutputAmounts(outputSlices ...[]*avax.TransferableOutput) map[ids.ID]uint64 { + produced := make(map[ids.ID]uint64) + for _, outputs := range outputSlices { + for _, out := range outputs { + produced[out.AssetID()] += out.Out.Amount() + } + } + return produced +} From d99f6962129725d3f7cf0d8f6c00356409a1b86d Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Wed, 24 Jul 2024 15:32:12 -0400 Subject: [PATCH 2/2] nit --- wallet/chain/p/builder_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/wallet/chain/p/builder_test.go b/wallet/chain/p/builder_test.go index 34c4df18e861..80164f052942 100644 --- a/wallet/chain/p/builder_test.go +++ b/wallet/chain/p/builder_test.go @@ -67,7 +67,7 @@ func TestBaseTx(t *testing.T) { builder = builder.New(set.Of(utxoAddr), testContext, backend) // data to build the transaction - outputsToMove = []*avax.TransferableOutput{{ + outputToMove = &avax.TransferableOutput{ Asset: avax.Asset{ID: avaxAssetID}, Out: &secp256k1fx.TransferOutput{ Amt: 7 * units.Avax, @@ -76,25 +76,25 @@ func TestBaseTx(t *testing.T) { Addrs: []ids.ShortID{utxoAddr}, }, }, - }} + } ) - utx, err := builder.NewBaseTx(outputsToMove) + utx, err := builder.NewBaseTx([]*avax.TransferableOutput{outputToMove}) require.NoError(err) - // check UTXOs selection and fee financing - outs := utx.Outs - require.Len(outs, 2) + // check that the output is included in the transaction + require.Contains(utx.Outs, outputToMove) + + // check fee calculation require.Equal( addAmounts( - addOutputAmounts(outs), + addOutputAmounts(utx.Outs), map[ids.ID]uint64{ avaxAssetID: testContext.BaseTxFee, }, ), addInputAmounts(utx.Ins), ) - require.Equal(outputsToMove[0], outs[1]) } func TestAddSubnetValidatorTx(t *testing.T) {