From cfa2077968366689b7d2c92b8b4e4e331d411eae Mon Sep 17 00:00:00 2001 From: muXxer Date: Fri, 24 Nov 2023 15:53:01 +0100 Subject: [PATCH] Add tests for depositcalculator --- .../depositcalculator/depositcalculator.go | 10 +- .../depositcalculator_test.go | 151 ++++++++++++++++++ 2 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 pkg/testsuite/depositcalculator/depositcalculator_test.go diff --git a/pkg/testsuite/depositcalculator/depositcalculator.go b/pkg/testsuite/depositcalculator/depositcalculator.go index dbda2ad9f..ef379cb19 100644 --- a/pkg/testsuite/depositcalculator/depositcalculator.go +++ b/pkg/testsuite/depositcalculator/depositcalculator.go @@ -288,9 +288,13 @@ func MinDeposit(protocolParams iotago.ProtocolParameters, outputType iotago.Outp case iotago.OutputFoundry: dummyOutput = &iotago.FoundryOutput{ - Amount: 0, - SerialNumber: 0, - TokenScheme: &iotago.SimpleTokenScheme{}, + Amount: 0, + SerialNumber: 0, + TokenScheme: &iotago.SimpleTokenScheme{ + MintedTokens: big.NewInt(0), + MeltedTokens: big.NewInt(0), + MaximumSupply: big.NewInt(0), + }, UnlockConditions: getUnlockConditions[iotago.FoundryOutputUnlockCondition](outputType, depositCalculationOptions), Features: getFeatures[iotago.FoundryOutputFeature](depositCalculationOptions), ImmutableFeatures: getImmutableFeatures[iotago.FoundryOutputImmFeature](depositCalculationOptions), diff --git a/pkg/testsuite/depositcalculator/depositcalculator_test.go b/pkg/testsuite/depositcalculator/depositcalculator_test.go new file mode 100644 index 000000000..562e1831a --- /dev/null +++ b/pkg/testsuite/depositcalculator/depositcalculator_test.go @@ -0,0 +1,151 @@ +package depositcalculator_test + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/iotaledger/hive.go/runtime/options" + "github.com/iotaledger/iota-core/pkg/testsuite/depositcalculator" + iotago "github.com/iotaledger/iota.go/v4" +) + +func TestCalculate(t *testing.T) { + + protocolParams := iotago.NewV3ProtocolParameters(iotago.WithVersion(3)) + storageScoreStructure := iotago.NewStorageScoreStructure(protocolParams.StorageScoreParameters()) + + type test struct { + name string + outputType iotago.OutputType + options []options.Option[depositcalculator.Options] + targetOutput iotago.Output + targetErr error + } + + tests := []*test{ + { + name: "ok - basic output", + outputType: iotago.OutputBasic, + options: []options.Option[depositcalculator.Options]{ + depositcalculator.WithAddress(&iotago.Ed25519Address{}), + }, + targetOutput: &iotago.BasicOutput{ + UnlockConditions: iotago.BasicOutputUnlockConditions{ + &iotago.AddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + }, + }, + targetErr: nil, + }, + { + name: "ok - account output", + outputType: iotago.OutputAccount, + options: []options.Option[depositcalculator.Options]{ + depositcalculator.WithAddress(&iotago.Ed25519Address{}), + }, + targetOutput: &iotago.AccountOutput{ + UnlockConditions: iotago.AccountOutputUnlockConditions{ + &iotago.AddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + }, + }, + targetErr: nil, + }, + { + name: "ok - account output - 2 block issuer keys", + outputType: iotago.OutputAccount, + options: []options.Option[depositcalculator.Options]{ + depositcalculator.WithAddress(&iotago.Ed25519Address{}), + depositcalculator.WithBlockIssuerKeys(2), + }, + targetOutput: &iotago.AccountOutput{ + UnlockConditions: iotago.AccountOutputUnlockConditions{ + &iotago.AddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + }, + Features: iotago.AccountOutputFeatures{ + &iotago.BlockIssuerFeature{ + BlockIssuerKeys: iotago.BlockIssuerKeys{ + &iotago.Ed25519PublicKeyBlockIssuerKey{}, + &iotago.Ed25519PublicKeyBlockIssuerKey{}, + }, + ExpirySlot: 0, + }, + }, + }, + targetErr: nil, + }, + { + name: "ok - anchor output", + outputType: iotago.OutputAnchor, + options: []options.Option[depositcalculator.Options]{ + depositcalculator.WithStateControllerAddress(&iotago.Ed25519Address{}), + depositcalculator.WithGovernorAddress(&iotago.Ed25519Address{}), + }, + targetOutput: &iotago.AnchorOutput{ + UnlockConditions: iotago.AnchorOutputUnlockConditions{ + &iotago.StateControllerAddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + &iotago.GovernorAddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + }, + }, + targetErr: nil, + }, + { + name: "ok - foundry output", + outputType: iotago.OutputFoundry, + options: []options.Option[depositcalculator.Options]{}, + targetOutput: &iotago.FoundryOutput{ + UnlockConditions: iotago.FoundryOutputUnlockConditions{ + &iotago.ImmutableAccountUnlockCondition{Address: &iotago.AccountAddress{}}, + }, + TokenScheme: &iotago.SimpleTokenScheme{ + MintedTokens: big.NewInt(0), + MeltedTokens: big.NewInt(0), + MaximumSupply: big.NewInt(0), + }, + }, + targetErr: nil, + }, + { + name: "ok - NFT output", + outputType: iotago.OutputNFT, + options: []options.Option[depositcalculator.Options]{ + depositcalculator.WithAddress(&iotago.Ed25519Address{}), + }, + targetOutput: &iotago.NFTOutput{ + UnlockConditions: iotago.NFTOutputUnlockConditions{ + &iotago.AddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + }, + }, + targetErr: nil, + }, + { + name: "ok - Delegation output", + outputType: iotago.OutputDelegation, + options: []options.Option[depositcalculator.Options]{ + depositcalculator.WithAddress(&iotago.Ed25519Address{}), + }, + targetOutput: &iotago.DelegationOutput{ + UnlockConditions: iotago.DelegationOutputUnlockConditions{ + &iotago.AddressUnlockCondition{Address: &iotago.Ed25519Address{}}, + }, + }, + targetErr: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result, err := depositcalculator.MinDeposit(protocolParams, test.outputType, test.options...) + if test.targetErr != nil { + require.ErrorIs(t, err, test.targetErr) + return + } + require.NoError(t, err) + + targetDeposit, err := storageScoreStructure.MinDeposit(test.targetOutput) + require.NoError(t, err) + + require.Equal(t, targetDeposit, result) + }) + } +}