From 90f4607a7905226670ffe1e3dfb53a3e7012803a Mon Sep 17 00:00:00 2001 From: Levente Pap Date: Wed, 7 Sep 2022 11:22:18 +0200 Subject: [PATCH 1/2] Add more native token test cases - input side exceeds max distinct native token count - output side exceeds max distinct native token count - most possible native tokens in a tx - native token count just exceeded --- transaction_test.go | 178 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) diff --git a/transaction_test.go b/transaction_test.go index a4d478782..f19d1bb20 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -2132,6 +2132,184 @@ func TestTxSemanticNativeTokens(t *testing.T) { wantErr: iotago.ErrMaxNativeTokensCountExceeded, } }(), + func() test { + numDistinctNTs := iotago.MaxNativeTokensCount + 1 + inputIDs := tpkg.RandOutputIDs(uint16(numDistinctNTs)) + + inputs := iotago.OutputSet{} + for i := 0; i < numDistinctNTs; i++ { + inputs[inputIDs[i]] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: tpkg.RandSortNativeTokens(1), + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + essence := &iotago.TransactionEssence{ + Inputs: inputIDs.UTXOInputs(), + Outputs: iotago.Outputs{ + &iotago.BasicOutput{ + Amount: 100 * uint64(numDistinctNTs), + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + }, + }, + } + + return test{ + name: "fail - too many on input side already", + svCtx: &iotago.SemanticValidationContext{ExtParas: &iotago.ExternalUnlockParameters{}}, + inputs: inputs, + tx: &iotago.Transaction{ + Essence: essence, + Unlocks: iotago.Unlocks{}, + }, + wantErr: iotago.ErrMaxNativeTokensCountExceeded, + } + }(), + func() test { + numDistinctNTs := iotago.MaxNativeTokensCount + 1 + tokens := tpkg.RandSortNativeTokens(numDistinctNTs) + inputIDs := tpkg.RandOutputIDs(uint16(numDistinctNTs)) + + inputs := iotago.OutputSet{} + for i := 0; i < numDistinctNTs; i++ { + inputs[inputIDs[i]] = &iotago.BasicOutput{ + Amount: 100, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + outs := make(iotago.Outputs, numDistinctNTs) + for i, _ := range outs { + outs[i] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: iotago.NativeTokens{tokens[i]}, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + essence := &iotago.TransactionEssence{ + Inputs: inputIDs.UTXOInputs(), + Outputs: outs, + } + + return test{ + name: "fail - too many on output side already", + svCtx: &iotago.SemanticValidationContext{ExtParas: &iotago.ExternalUnlockParameters{}}, + inputs: inputs, + tx: &iotago.Transaction{ + Essence: essence, + Unlocks: iotago.Unlocks{}, + }, + wantErr: iotago.ErrMaxNativeTokensCountExceeded, + } + }(), + func() test { + numDistinctNTs := iotago.MaxNativeTokensCount + tokens := tpkg.RandSortNativeTokens(numDistinctNTs) + inputIDs := tpkg.RandOutputIDs(iotago.MaxInputsCount) + + inputs := iotago.OutputSet{} + for i := 0; i < iotago.MaxInputsCount; i++ { + inputs[inputIDs[i]] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: tokens, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + outputs := make(iotago.Outputs, iotago.MaxOutputsCount) + for i, _ := range outputs { + outputs[i] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: tokens, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + essence := &iotago.TransactionEssence{ + Inputs: inputIDs.UTXOInputs(), + Outputs: outputs, + } + + return test{ + name: "ok - most possible tokens in a tx", + svCtx: &iotago.SemanticValidationContext{ExtParas: &iotago.ExternalUnlockParameters{}}, + inputs: inputs, + tx: &iotago.Transaction{ + Essence: essence, + Unlocks: iotago.Unlocks{}, + }, + wantErr: nil, + } + }(), + func() test { + numDistinctNTs := iotago.MaxNativeTokensCount + tokens := tpkg.RandSortNativeTokens(numDistinctNTs) + inputIDs := tpkg.RandOutputIDs(iotago.MaxInputsCount) + + inputs := iotago.OutputSet{} + for i := 0; i < iotago.MaxInputsCount; i++ { + inputs[inputIDs[i]] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: tokens, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + outputs := make(iotago.Outputs, iotago.MaxOutputsCount) + for i, _ := range outputs { + outputs[i] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: tokens, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + } + + // add one more distinct native token to the last output + oneMore := tokens.Clone() + oneMore[len(oneMore)-1] = tpkg.RandNativeToken() + + outputs[iotago.MaxOutputsCount-1] = &iotago.BasicOutput{ + Amount: 100, + NativeTokens: oneMore, + Conditions: iotago.UnlockConditions{ + &iotago.AddressUnlockCondition{Address: tpkg.RandEd25519Address()}, + }, + } + + essence := &iotago.TransactionEssence{ + Inputs: inputIDs.UTXOInputs(), + Outputs: outputs, + } + + return test{ + name: "fail - max nt count just exceeded", + svCtx: &iotago.SemanticValidationContext{ExtParas: &iotago.ExternalUnlockParameters{}}, + inputs: inputs, + tx: &iotago.Transaction{ + Essence: essence, + Unlocks: iotago.Unlocks{}, + }, + wantErr: iotago.ErrMaxNativeTokensCountExceeded, + } + }(), func() test { inputIDs := tpkg.RandOutputIDs(1) From 501e848ffb7f1f37803b8e39ced0d282a1c35b4b Mon Sep 17 00:00:00 2001 From: Levente Pap Date: Wed, 7 Sep 2022 11:31:31 +0200 Subject: [PATCH 2/2] Please the review dog --- transaction_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/transaction_test.go b/transaction_test.go index f19d1bb20..3f6bc776e 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -2186,7 +2186,7 @@ func TestTxSemanticNativeTokens(t *testing.T) { } outs := make(iotago.Outputs, numDistinctNTs) - for i, _ := range outs { + for i := range outs { outs[i] = &iotago.BasicOutput{ Amount: 100, NativeTokens: iotago.NativeTokens{tokens[i]}, @@ -2229,7 +2229,7 @@ func TestTxSemanticNativeTokens(t *testing.T) { } outputs := make(iotago.Outputs, iotago.MaxOutputsCount) - for i, _ := range outputs { + for i := range outputs { outputs[i] = &iotago.BasicOutput{ Amount: 100, NativeTokens: tokens, @@ -2272,7 +2272,7 @@ func TestTxSemanticNativeTokens(t *testing.T) { } outputs := make(iotago.Outputs, iotago.MaxOutputsCount) - for i, _ := range outputs { + for i := range outputs { outputs[i] = &iotago.BasicOutput{ Amount: 100, NativeTokens: tokens,