From b3e09522b2002460f40610a62fe0eb47f3f4fc4e Mon Sep 17 00:00:00 2001 From: muXxer Date: Fri, 1 Dec 2023 11:39:26 +0100 Subject: [PATCH] Move rand funcs to iota.go --- go.mod | 2 +- go.sum | 4 +- pkg/core/account/accounts_test.go | 12 +- .../accounts/accountsledger/testsuite_test.go | 3 +- .../engine/utxoledger/iteration_test.go | 47 ++- pkg/protocol/engine/utxoledger/output_test.go | 105 +++--- .../engine/utxoledger/slot_diff_test.go | 11 +- .../engine/utxoledger/snapshot_test.go | 53 ++-- pkg/protocol/engine/utxoledger/tpkg/random.go | 17 +- .../performance/testsuite_test.go | 2 +- .../performance/tracker_test.go | 12 +- pkg/tests/accounts_test.go | 12 +- pkg/tests/reward_test.go | 3 +- pkg/utils/rand.go | 298 ------------------ tools/gendoc/go.mod | 2 +- tools/gendoc/go.sum | 4 +- tools/genesis-snapshot/go.mod | 2 +- tools/genesis-snapshot/go.sum | 4 +- 18 files changed, 142 insertions(+), 451 deletions(-) delete mode 100644 pkg/utils/rand.go diff --git a/go.mod b/go.mod index e3b6dbe6d..8afa0b707 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231128163614-c82e1fa40733 github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231128162307-cc6b309e93ef - github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec + github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f github.com/labstack/echo/v4 v4.11.3 github.com/labstack/gommon v0.4.1 github.com/libp2p/go-libp2p v0.32.0 diff --git a/go.sum b/go.sum index 0f6592c84..8748b7e4a 100644 --- a/go.sum +++ b/go.sum @@ -307,8 +307,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231128163614-c82e1fa40733 h1:jdjFT github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231128163614-c82e1fa40733/go.mod h1:3ae5TFi3uaECV+F4d5ekKz+xcJQmVtCICcs/Rhvgn3w= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231128162307-cc6b309e93ef h1:RImO23W0kL3U9egdLoi5OTmOIhE5rHfR/dKM/6XhZeE= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231128162307-cc6b309e93ef/go.mod h1:bi0zndM3irf8FHYUg6MzG/Zk/t0fV8IwGXAdoT7wn98= -github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec h1:u9SAuKHygJwqwYxoEd+PyHCU4fTftcPdV2ZUGSRZe8o= -github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= +github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f h1:uT4ZKAyr/wk6lHcTQifAeEPb6WfBA5tdG7kEuyW7tDU= +github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI= github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= diff --git a/pkg/core/account/accounts_test.go b/pkg/core/account/accounts_test.go index 49639141d..282511d06 100644 --- a/pkg/core/account/accounts_test.go +++ b/pkg/core/account/accounts_test.go @@ -2,13 +2,13 @@ package account_test import ( "bytes" - "math/rand" "testing" "github.com/stretchr/testify/require" "github.com/iotaledger/iota-core/pkg/core/account" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/tpkg" ) func TestAccounts(t *testing.T) { @@ -49,7 +49,7 @@ func TestAccounts(t *testing.T) { }) // check "Get" for non existing IDs - _, exists := accounts.Get([32]byte{0, randByte()}) + _, exists := accounts.Get([32]byte{0, tpkg.RandByte()}) require.False(t, exists) for id, stake := range issuers { @@ -118,7 +118,7 @@ func TestAccounts(t *testing.T) { /* // Get a non existed account - _, exist = accounts.Get([32]byte{randByte()}) + _, exist = accounts.Get([32]byte{tpkg.RandByte()}) require.False(t, exist) */ @@ -131,7 +131,7 @@ func generateAccounts(count int) (map[iotago.AccountID]iotago.BaseToken, iotago. var totalStake iotago.BaseToken for i := 0; i < count; i++ { - id := iotago.AccountID([32]byte{randByte()}) + id := iotago.AccountID([32]byte{tpkg.RandByte()}) if _, exist := seenIDs[id]; exist { i-- continue @@ -145,7 +145,3 @@ func generateAccounts(count int) (map[iotago.AccountID]iotago.BaseToken, iotago. return issuers, totalStake } - -func randByte() byte { - return byte(rand.Intn(256)) -} diff --git a/pkg/protocol/engine/accounts/accountsledger/testsuite_test.go b/pkg/protocol/engine/accounts/accountsledger/testsuite_test.go index 08cfe1e99..51f2247bc 100644 --- a/pkg/protocol/engine/accounts/accountsledger/testsuite_test.go +++ b/pkg/protocol/engine/accounts/accountsledger/testsuite_test.go @@ -17,7 +17,6 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/accounts/accountsledger" "github.com/iotaledger/iota-core/pkg/protocol/engine/blocks" "github.com/iotaledger/iota-core/pkg/storage/prunable/slotstore" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/tpkg" ) @@ -326,7 +325,7 @@ func (t *TestSuite) BlockIssuerKey(alias string, createIfNotExists bool) iotago. panic(fmt.Sprintf("block issuer key with alias '%s' does not exist", alias)) } - t.blockIssuerKeys[alias] = utils.RandBlockIssuerKey() + t.blockIssuerKeys[alias] = tpkg.RandBlockIssuerKey() return t.blockIssuerKeys[alias] } diff --git a/pkg/protocol/engine/utxoledger/iteration_test.go b/pkg/protocol/engine/utxoledger/iteration_test.go index 85fd8cf59..12a3ab370 100644 --- a/pkg/protocol/engine/utxoledger/iteration_test.go +++ b/pkg/protocol/engine/utxoledger/iteration_test.go @@ -9,7 +9,6 @@ import ( "github.com/iotaledger/hive.go/kvstore/mapdb" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger/tpkg" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" iotago_tpkg "github.com/iotaledger/iota.go/v4/tpkg" ) @@ -17,18 +16,18 @@ import ( func TestUTXOComputeBalance(t *testing.T) { manager := utxoledger.New(mapdb.NewMapDB(), iotago.SingleVersionProvider(iotago_tpkg.TestAPI)) - initialOutput := tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputBasic, utils.RandAddress(iotago.AddressEd25519), 2_134_656_365) + initialOutput := tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressEd25519), 2_134_656_365) require.NoError(t, manager.AddGenesisUnspentOutput(initialOutput)) - require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputAccount, utils.RandAddress(iotago.AddressAccount), 56_549_524))) - require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputAnchor, utils.RandAddress(iotago.AddressAccount), 56_549_524))) - require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputFoundry, utils.RandAddress(iotago.AddressAccount), 25_548_858))) - require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputNFT, utils.RandAddress(iotago.AddressEd25519), 545_699_656))) - require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputBasic, utils.RandAddress(iotago.AddressAccount), 626_659_696))) + require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputAccount, iotago_tpkg.RandAddress(iotago.AddressAccount), 56_549_524))) + require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputAnchor, iotago_tpkg.RandAddress(iotago.AddressAccount), 56_549_524))) + require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputFoundry, iotago_tpkg.RandAddress(iotago.AddressAccount), 25_548_858))) + require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputNFT, iotago_tpkg.RandAddress(iotago.AddressEd25519), 545_699_656))) + require.NoError(t, manager.AddGenesisUnspentOutput(tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressAccount), 626_659_696))) index := iotago.SlotIndex(756) outputs := utxoledger.Outputs{ - tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputBasic, utils.RandAddress(iotago.AddressNFT), 2_134_656_365), + tpkg.RandLedgerStateOutputOnAddressWithAmount(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressNFT), 2_134_656_365), } spents := utxoledger.Spents{ @@ -55,22 +54,22 @@ func TestUTXOIteration(t *testing.T) { manager := utxoledger.New(mapdb.NewMapDB(), iotago.SingleVersionProvider(iotago_tpkg.TestAPI)) outputs := utxoledger.Outputs{ - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressEd25519)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressNFT)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressAccount)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressEd25519)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressNFT)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressAccount)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, utils.RandAddress(iotago.AddressEd25519)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, utils.RandAddress(iotago.AddressEd25519)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, utils.RandAddress(iotago.AddressAccount)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, utils.RandAddress(iotago.AddressNFT)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, utils.RandAddress(iotago.AddressAccount)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputAccount, utils.RandAddress(iotago.AddressEd25519)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputAnchor, utils.RandAddress(iotago.AddressEd25519)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputFoundry, utils.RandAddress(iotago.AddressAccount)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputFoundry, utils.RandAddress(iotago.AddressAccount)), - tpkg.RandLedgerStateOutputOnAddress(iotago.OutputFoundry, utils.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressEd25519)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressNFT)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressEd25519)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressNFT)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputBasic, iotago_tpkg.RandAddress(iotago.AddressEd25519)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, iotago_tpkg.RandAddress(iotago.AddressEd25519)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, iotago_tpkg.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, iotago_tpkg.RandAddress(iotago.AddressNFT)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputNFT, iotago_tpkg.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputAccount, iotago_tpkg.RandAddress(iotago.AddressEd25519)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputAnchor, iotago_tpkg.RandAddress(iotago.AddressEd25519)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputFoundry, iotago_tpkg.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputFoundry, iotago_tpkg.RandAddress(iotago.AddressAccount)), + tpkg.RandLedgerStateOutputOnAddress(iotago.OutputFoundry, iotago_tpkg.RandAddress(iotago.AddressAccount)), } index := iotago.SlotIndex(756) diff --git a/pkg/protocol/engine/utxoledger/output_test.go b/pkg/protocol/engine/utxoledger/output_test.go index 0a90ad8ab..65968b7b5 100644 --- a/pkg/protocol/engine/utxoledger/output_test.go +++ b/pkg/protocol/engine/utxoledger/output_test.go @@ -15,7 +15,6 @@ import ( "github.com/iotaledger/hive.go/serializer/v2/byteutils" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger/tpkg" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" iotago_tpkg "github.com/iotaledger/iota.go/v4/tpkg" ) @@ -108,7 +107,7 @@ func CreateOutputAndAssertSerialization(t *testing.T, blockID iotago.BlockID, in } func CreateSpentAndAssertSerialization(t *testing.T, output *utxoledger.Output) *utxoledger.Spent { - transactionID := utils.RandTransactionID() + transactionID := iotago_tpkg.RandTransactionID() indexSpent := iotago.SlotIndex(6788362) @@ -128,13 +127,13 @@ func CreateSpentAndAssertSerialization(t *testing.T, output *utxoledger.Output) func TestBasicOutputOnEd25519WithoutSpendConstraintsSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - address := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) - senderAddress := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) - tag := utils.RandBytes(23) + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + address := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + senderAddress := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + tag := iotago_tpkg.RandBytes(23) amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() iotaOutput := &iotago.BasicOutput{ Amount: amount, @@ -166,13 +165,13 @@ func TestBasicOutputOnEd25519WithoutSpendConstraintsSerialization(t *testing.T) func TestBasicOutputOnEd25519WithSpendConstraintsSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - address := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) - senderAddress := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + address := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + senderAddress := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() - timeLockUnlockSlot := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() + timeLockUnlockSlot := iotago_tpkg.RandSlot() iotaOutput := &iotago.BasicOutput{ Amount: amount, @@ -205,12 +204,12 @@ func TestBasicOutputOnEd25519WithSpendConstraintsSerialization(t *testing.T) { func TestNFTOutputSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - address := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) - nftID := utils.RandNFTID() + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + address := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + nftID := iotago_tpkg.RandNFTID() amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() iotaOutput := &iotago.NFTOutput{ Amount: amount, @@ -224,7 +223,7 @@ func TestNFTOutputSerialization(t *testing.T) { ImmutableFeatures: iotago.NFTOutputImmFeatures{ &iotago.MetadataFeature{ Entries: iotago.MetadataFeatureEntries{ - "data": utils.RandBytes(12), + "data": iotago_tpkg.RandBytes(12), }, }, }, @@ -243,14 +242,14 @@ func TestNFTOutputSerialization(t *testing.T) { func TestNFTOutputWithSpendConstraintsSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - address := utils.RandNFTID() - issuerAddress := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) - nftID := utils.RandNFTID() + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + address := iotago_tpkg.RandNFTID() + issuerAddress := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + nftID := iotago_tpkg.RandNFTID() amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() - expirationUnlockSlot := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() + expirationUnlockSlot := iotago_tpkg.RandSlot() iotaOutput := &iotago.NFTOutput{ Amount: amount, @@ -268,7 +267,7 @@ func TestNFTOutputWithSpendConstraintsSerialization(t *testing.T) { ImmutableFeatures: iotago.NFTOutputImmFeatures{ &iotago.MetadataFeature{ Entries: iotago.MetadataFeatureEntries{ - "data": utils.RandBytes(12), + "data": iotago_tpkg.RandBytes(12), }, }, &iotago.IssuerFeature{ @@ -290,14 +289,14 @@ func TestNFTOutputWithSpendConstraintsSerialization(t *testing.T) { func TestAccountOutputSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - aliasID := utils.RandAccountID() - address := utils.RandAccountID().ToAddress() - issuer := utils.RandNFTID() - sender := utils.RandAccountID() + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + aliasID := iotago_tpkg.RandAccountID() + address := iotago_tpkg.RandAccountID().ToAddress() + issuer := iotago_tpkg.RandNFTID() + sender := iotago_tpkg.RandAccountID() amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() iotaOutput := &iotago.AccountOutput{ Amount: amount, @@ -332,15 +331,15 @@ func TestAccountOutputSerialization(t *testing.T) { func TestAnchorOutputSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - aliasID := utils.RandAnchorID() - stateController := utils.RandAnchorID() - governor := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) - issuer := utils.RandNFTID() - sender := utils.RandAnchorID() + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + aliasID := iotago_tpkg.RandAnchorID() + stateController := iotago_tpkg.RandAnchorID() + governor := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + issuer := iotago_tpkg.RandNFTID() + sender := iotago_tpkg.RandAnchorID() amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() iotaOutput := &iotago.AnchorOutput{ Amount: amount, @@ -378,16 +377,16 @@ func TestAnchorOutputSerialization(t *testing.T) { func TestFoundryOutputSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - aliasID := utils.RandAccountID() + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + aliasID := iotago_tpkg.RandAccountID() amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() supply := new(big.Int).SetUint64(iotago_tpkg.RandUint64(math.MaxUint64)) iotaOutput := &iotago.FoundryOutput{ Amount: amount, - SerialNumber: utils.RandUint32(math.MaxUint32), + SerialNumber: iotago_tpkg.RandUint32(math.MaxUint32), TokenScheme: &iotago.SimpleTokenScheme{ MintedTokens: supply, MeltedTokens: new(big.Int).SetBytes([]byte{0}), @@ -415,17 +414,17 @@ func TestFoundryOutputSerialization(t *testing.T) { func TestDelegationOutputSerialization(t *testing.T) { txCommitment := iotago_tpkg.Rand32ByteArray() - txCreationSlot := utils.RandSlotIndex() - blockID := utils.RandBlockID() - address := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) + txCreationSlot := iotago_tpkg.RandSlot() + blockID := iotago_tpkg.RandBlockID() + address := iotago_tpkg.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address) amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken) - index := utils.RandSlotIndex() + index := iotago_tpkg.RandSlot() iotaOutput := &iotago.DelegationOutput{ Amount: amount, DelegatedAmount: amount, DelegationID: iotago_tpkg.RandDelegationID(), - ValidatorAddress: utils.RandAddress(iotago.AddressAccount).(*iotago.AccountAddress), + ValidatorAddress: iotago_tpkg.RandAddress(iotago.AddressAccount).(*iotago.AccountAddress), StartEpoch: iotago_tpkg.RandEpoch(), UnlockConditions: iotago.DelegationOutputUnlockConditions{ &iotago.AddressUnlockCondition{ diff --git a/pkg/protocol/engine/utxoledger/slot_diff_test.go b/pkg/protocol/engine/utxoledger/slot_diff_test.go index f23feaa5d..e39a3b510 100644 --- a/pkg/protocol/engine/utxoledger/slot_diff_test.go +++ b/pkg/protocol/engine/utxoledger/slot_diff_test.go @@ -13,7 +13,6 @@ import ( "github.com/iotaledger/hive.go/serializer/v2/byteutils" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger/tpkg" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" iotago_tpkg "github.com/iotaledger/iota.go/v4/tpkg" ) @@ -21,10 +20,10 @@ import ( func TestSimpleSlotDiffSerialization(t *testing.T) { indexBooked := iotago.SlotIndex(255975) - txID := utils.RandTransactionID() - outputID := utils.RandOutputID() - blockID := utils.RandBlockID() - address := utils.RandAddress(iotago.AddressEd25519) + txID := iotago_tpkg.RandTransactionID() + outputID := iotago_tpkg.RandOutputID() + blockID := iotago_tpkg.RandBlockID() + address := iotago_tpkg.RandAddress(iotago.AddressEd25519) amount := iotago.BaseToken(832493) iotaOutput := &iotago.BasicOutput{ Amount: amount, @@ -41,7 +40,7 @@ func TestSimpleSlotDiffSerialization(t *testing.T) { output := utxoledger.CreateOutput(iotago.SingleVersionProvider(iotago_tpkg.TestAPI), outputID, blockID, indexBooked, iotaOutput, outputProof) - transactionIDSpent := utils.RandTransactionID() + transactionIDSpent := iotago_tpkg.RandTransactionID() indexSpent := indexBooked + 1 diff --git a/pkg/protocol/engine/utxoledger/snapshot_test.go b/pkg/protocol/engine/utxoledger/snapshot_test.go index 5411094f6..ba1c5dd38 100644 --- a/pkg/protocol/engine/utxoledger/snapshot_test.go +++ b/pkg/protocol/engine/utxoledger/snapshot_test.go @@ -13,17 +13,16 @@ import ( "github.com/iotaledger/hive.go/serializer/v2/stream" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger/tpkg" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" iotago_tpkg "github.com/iotaledger/iota.go/v4/tpkg" ) func TestOutput_SnapshotBytes(t *testing.T) { - outputID := utils.RandOutputID(2) - blockID := utils.RandBlockID() - txID := utils.RandTransactionID() - slotBooked := utils.RandSlotIndex() - iotaOutput := utils.RandOutput(iotago.OutputBasic) + outputID := iotago_tpkg.RandOutputID(2) + blockID := iotago_tpkg.RandBlockID() + txID := iotago_tpkg.RandTransactionID() + slotBooked := iotago_tpkg.RandSlot() + iotaOutput := iotago_tpkg.RandOutput(iotago.OutputBasic) iotaOutputBytes, err := iotago_tpkg.TestAPI.Encode(iotaOutput) require.NoError(t, err) @@ -54,11 +53,11 @@ func TestOutput_SnapshotBytes(t *testing.T) { } func TestOutputFromSnapshotReader(t *testing.T) { - txID := utils.RandTransactionID() - outputID := utils.RandOutputID(2) - blockID := utils.RandBlockID() - slotBooked := utils.RandSlotIndex() - iotaOutput := utils.RandOutput(iotago.OutputBasic) + txID := iotago_tpkg.RandTransactionID() + outputID := iotago_tpkg.RandOutputID(2) + blockID := iotago_tpkg.RandBlockID() + slotBooked := iotago_tpkg.RandSlot() + iotaOutput := iotago_tpkg.RandOutput(iotago.OutputBasic) iotaOutputBytes, err := iotago_tpkg.TestAPI.Encode(iotaOutput) require.NoError(t, err) @@ -78,11 +77,11 @@ func TestOutputFromSnapshotReader(t *testing.T) { } func TestSpent_SnapshotBytes(t *testing.T) { - txID := utils.RandTransactionID() - outputID := utils.RandOutputID(2) - blockID := utils.RandBlockID() - slotBooked := utils.RandSlotIndex() - iotaOutput := utils.RandOutput(iotago.OutputBasic) + txID := iotago_tpkg.RandTransactionID() + outputID := iotago_tpkg.RandOutputID(2) + blockID := iotago_tpkg.RandBlockID() + slotBooked := iotago_tpkg.RandSlot() + iotaOutput := iotago_tpkg.RandOutput(iotago.OutputBasic) iotaOutputBytes, err := iotago_tpkg.TestAPI.Encode(iotaOutput) require.NoError(t, err) @@ -94,8 +93,8 @@ func TestSpent_SnapshotBytes(t *testing.T) { output := utxoledger.NewOutput(iotago.SingleVersionProvider(iotago_tpkg.TestAPI), outputID, blockID, slotBooked, iotaOutput, iotaOutputBytes, outputProof, outputProofBytes) outputSnapshotBytes := output.SnapshotBytes() - transactionID := utils.RandTransactionID() - slotSpent := utils.RandSlotIndex() + transactionID := iotago_tpkg.RandTransactionID() + slotSpent := iotago_tpkg.RandSlot() spent := utxoledger.NewSpent(output, transactionID, slotSpent) snapshotBytes := spent.SnapshotBytes() @@ -107,11 +106,11 @@ func TestSpent_SnapshotBytes(t *testing.T) { } func TestSpentFromSnapshotReader(t *testing.T) { - txID := utils.RandTransactionID() - outputID := utils.RandOutputID(2) - blockID := utils.RandBlockID() - slotBooked := utils.RandSlotIndex() - iotaOutput := utils.RandOutput(iotago.OutputBasic) + txID := iotago_tpkg.RandTransactionID() + outputID := iotago_tpkg.RandOutputID(2) + blockID := iotago_tpkg.RandBlockID() + slotBooked := iotago_tpkg.RandSlot() + iotaOutput := iotago_tpkg.RandOutput(iotago.OutputBasic) iotaOutputBytes, err := iotago_tpkg.TestAPI.Encode(iotaOutput) require.NoError(t, err) @@ -122,8 +121,8 @@ func TestSpentFromSnapshotReader(t *testing.T) { output := utxoledger.NewOutput(iotago.SingleVersionProvider(iotago_tpkg.TestAPI), outputID, blockID, slotBooked, iotaOutput, iotaOutputBytes, outputProof, outputProofBytes) - transactionID := utils.RandTransactionID() - slotSpent := utils.RandSlotIndex() + transactionID := iotago_tpkg.RandTransactionID() + slotSpent := iotago_tpkg.RandSlot() spent := utxoledger.NewSpent(output, transactionID, slotSpent) snapshotBytes := spent.SnapshotBytes() @@ -136,7 +135,7 @@ func TestSpentFromSnapshotReader(t *testing.T) { } func TestReadSlotDiffToSnapshotReader(t *testing.T) { - slot := utils.RandSlotIndex() + slot := iotago_tpkg.RandSlot() slotDiff := &utxoledger.SlotDiff{ Slot: slot, Outputs: utxoledger.Outputs{ @@ -164,7 +163,7 @@ func TestReadSlotDiffToSnapshotReader(t *testing.T) { } func TestWriteSlotDiffToSnapshotWriter(t *testing.T) { - slot := utils.RandSlotIndex() + slot := iotago_tpkg.RandSlot() slotDiff := &utxoledger.SlotDiff{ Slot: slot, Outputs: utxoledger.Outputs{ diff --git a/pkg/protocol/engine/utxoledger/tpkg/random.go b/pkg/protocol/engine/utxoledger/tpkg/random.go index 3df1f7e87..627516475 100644 --- a/pkg/protocol/engine/utxoledger/tpkg/random.go +++ b/pkg/protocol/engine/utxoledger/tpkg/random.go @@ -3,39 +3,38 @@ package tpkg import ( "github.com/iotaledger/hive.go/lo" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/tpkg" ) func RandLedgerStateOutput() *utxoledger.Output { - return RandLedgerStateOutputWithType(utils.RandOutputType()) + return RandLedgerStateOutputWithType(tpkg.RandOutputType()) } func RandLedgerStateOutputWithOutput(output iotago.Output) *utxoledger.Output { outputs := iotago.TxEssenceOutputs{output} - txID := utils.RandTransactionID() + txID := tpkg.RandTransactionID() proof := lo.PanicOnErr(iotago.NewOutputIDProof(tpkg.TestAPI, txID.Identifier(), txID.Slot(), outputs, 0)) - return utxoledger.CreateOutput(iotago.SingleVersionProvider(tpkg.TestAPI), utils.RandOutputID(), utils.RandBlockID(), utils.RandSlotIndex(), outputs[0], proof) + return utxoledger.CreateOutput(iotago.SingleVersionProvider(tpkg.TestAPI), tpkg.RandOutputID(), tpkg.RandBlockID(), tpkg.RandSlot(), outputs[0], proof) } func RandLedgerStateOutputWithType(outputType iotago.OutputType) *utxoledger.Output { - return RandLedgerStateOutputWithOutput(utils.RandOutput(outputType)) + return RandLedgerStateOutputWithOutput(tpkg.RandOutput(outputType)) } func RandLedgerStateOutputOnAddress(outputType iotago.OutputType, address iotago.Address) *utxoledger.Output { - return RandLedgerStateOutputWithOutput(utils.RandOutputOnAddress(outputType, address)) + return RandLedgerStateOutputWithOutput(tpkg.RandOutputOnAddress(outputType, address)) } func RandLedgerStateOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.Address, amount iotago.BaseToken) *utxoledger.Output { - return RandLedgerStateOutputWithOutput(utils.RandOutputOnAddressWithAmount(outputType, address, amount)) + return RandLedgerStateOutputWithOutput(tpkg.RandOutputOnAddressWithAmount(outputType, address, amount)) } func RandLedgerStateSpent(indexSpent iotago.SlotIndex) *utxoledger.Spent { - return utxoledger.NewSpent(RandLedgerStateOutput(), utils.RandTransactionID(), indexSpent) + return utxoledger.NewSpent(RandLedgerStateOutput(), tpkg.RandTransactionID(), indexSpent) } func RandLedgerStateSpentWithOutput(output *utxoledger.Output, indexSpent iotago.SlotIndex) *utxoledger.Spent { - return utxoledger.NewSpent(output, utils.RandTransactionID(), indexSpent) + return utxoledger.NewSpent(output, tpkg.RandTransactionID(), indexSpent) } diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go index a7f2fcddf..c79086778 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go @@ -295,7 +295,7 @@ func (t *TestSuite) applyPerformanceFactor(accountID iotago.AccountID, epoch iot } for i := uint64(0); i < validationBlocksSentPerSlot; i++ { - valBlock := tpkg.RandValidationBlock(t.api) + valBlock := tpkg.RandValidationBlockBody(t.api) block := tpkg.RandBlock(valBlock, t.api, 10) block.Header.IssuerID = accountID subslotIndex := i diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go index 731e89acc..6dca54628 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go @@ -96,17 +96,17 @@ func TestManager_Candidates(t *testing.T) { issuer2 := tpkg.RandAccountID() issuer3 := tpkg.RandAccountID() { - block1 := tpkg.RandBlock(tpkg.RandBasicBlock(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) + block1 := tpkg.RandBlock(tpkg.RandBasicBlockBody(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) block1.Header.IssuingTime = ts.api.TimeProvider().SlotStartTime(1) block1.Header.IssuerID = issuer1 - block2 := tpkg.RandBlock(tpkg.RandBasicBlock(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) + block2 := tpkg.RandBlock(tpkg.RandBasicBlockBody(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) block2.Header.IssuingTime = ts.api.TimeProvider().SlotStartTime(2) block2.Header.IssuerID = issuer2 - block3 := tpkg.RandBlock(tpkg.RandBasicBlock(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) + block3 := tpkg.RandBlock(tpkg.RandBasicBlockBody(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) block3.Header.IssuingTime = ts.api.TimeProvider().SlotStartTime(3) block3.Header.IssuerID = issuer3 @@ -117,16 +117,16 @@ func TestManager_Candidates(t *testing.T) { } { - block4 := tpkg.RandBlock(tpkg.RandBasicBlock(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) + block4 := tpkg.RandBlock(tpkg.RandBasicBlockBody(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) block4.Header.IssuingTime = ts.api.TimeProvider().SlotStartTime(4) block4.Header.IssuerID = issuer1 - block5 := tpkg.RandBlock(tpkg.RandBasicBlock(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) + block5 := tpkg.RandBlock(tpkg.RandBasicBlockBody(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) block5.Header.IssuingTime = ts.api.TimeProvider().SlotStartTime(5) block5.Header.IssuerID = issuer2 - block6 := tpkg.RandBlock(tpkg.RandBasicBlock(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) + block6 := tpkg.RandBlock(tpkg.RandBasicBlockBody(ts.api, iotago.PayloadCandidacyAnnouncement), ts.api, 0) block6.Header.IssuingTime = ts.api.TimeProvider().SlotStartTime(6) block6.Header.IssuerID = issuer3 diff --git a/pkg/tests/accounts_test.go b/pkg/tests/accounts_test.go index bc560555e..39a6deb69 100644 --- a/pkg/tests/accounts_test.go +++ b/pkg/tests/accounts_test.go @@ -13,12 +13,12 @@ import ( "github.com/iotaledger/iota-core/pkg/testsuite/depositcalculator" "github.com/iotaledger/iota-core/pkg/testsuite/mock" "github.com/iotaledger/iota-core/pkg/testsuite/snapshotcreator" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/tpkg" ) func Test_TransitionAndDestroyAccount(t *testing.T) { - oldGenesisOutputKey := utils.RandBlockIssuerKey() + oldGenesisOutputKey := tpkg.RandBlockIssuerKey() ts := testsuite.NewTestSuite(t, testsuite.WithProtocolParametersOptions( @@ -97,7 +97,7 @@ func Test_TransitionAndDestroyAccount(t *testing.T) { }, ts.Nodes()...) // MODIFY EXISTING GENESIS ACCOUNT - newGenesisOutputKey := utils.RandBlockIssuerKey() + newGenesisOutputKey := tpkg.RandBlockIssuerKey() var block1Slot iotago.SlotIndex = 1 // set the expiry of the genesis account to be the block slot + max committable age. newExpirySlot := block1Slot + ts.API.ProtocolParameters().MaxCommittableAge() @@ -217,7 +217,7 @@ func Test_StakeDelegateAndDelayedClaim(t *testing.T) { }, ts.Nodes()...) // CREATE NEW ACCOUNT WITH BLOCK ISSUER AND STAKING FEATURES FROM BASIC UTXO - newAccountBlockIssuerKey := utils.RandBlockIssuerKey() + newAccountBlockIssuerKey := tpkg.RandBlockIssuerKey() // set the expiry slot of the transitioned genesis account to the latest committed + MaxCommittableAge newAccountExpirySlot := node1.Protocol.MainEngineInstance().Storage.Settings().LatestCommitment().Slot() + ts.API.ProtocolParameters().MaxCommittableAge() @@ -430,7 +430,7 @@ func Test_ImplicitAccounts(t *testing.T) { // TRANSITION IMPLICIT ACCOUNT TO ACCOUNT OUTPUT. // USE IMPLICIT ACCOUNT AS BLOCK ISSUER. - fullAccountBlockIssuerKey := utils.RandBlockIssuerKey() + fullAccountBlockIssuerKey := tpkg.RandBlockIssuerKey() block2Slot := ts.CurrentSlot() tx2 := newUserWallet.TransitionImplicitAccountToAccountOutput( @@ -746,7 +746,7 @@ func Test_NegativeBIC_AccountOutput(t *testing.T) { }, ts.Nodes()...) // MODIFY EXISTING GENESIS ACCOUNT - newWallet1IssuerKey := utils.RandBlockIssuerKey() + newWallet1IssuerKey := tpkg.RandBlockIssuerKey() var block1Slot iotago.SlotIndex = 1 var latestParents []iotago.BlockID ts.SetCurrentSlot(block1Slot) diff --git a/pkg/tests/reward_test.go b/pkg/tests/reward_test.go index 36aa76bab..67af8e176 100644 --- a/pkg/tests/reward_test.go +++ b/pkg/tests/reward_test.go @@ -8,7 +8,6 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/accounts" "github.com/iotaledger/iota-core/pkg/testsuite" "github.com/iotaledger/iota-core/pkg/testsuite/mock" - "github.com/iotaledger/iota-core/pkg/utils" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/tpkg" @@ -160,7 +159,7 @@ func Test_Account_RemoveStakingFeatureWithoutRewards(t *testing.T) { fixedCost := iotago.Mana(421) stakedAmount := mock.MinValidatorAccountAmount(ts.API.ProtocolParameters()) - blockIssuerFeatKey := utils.RandBlockIssuerKey() + blockIssuerFeatKey := tpkg.RandBlockIssuerKey() // Set the expiry slot beyond the end epoch of the staking feature so we don't have to remove the feature. blockIssuerFeatExpirySlot := ts.API.TimeProvider().EpochEnd(claimingEpoch) diff --git a/pkg/utils/rand.go b/pkg/utils/rand.go deleted file mode 100644 index c3f8da1b9..000000000 --- a/pkg/utils/rand.go +++ /dev/null @@ -1,298 +0,0 @@ -package utils - -import ( - "crypto/rand" - "encoding/binary" - "math" - "math/big" - "time" - - "github.com/iotaledger/hive.go/crypto/ed25519" - iotago "github.com/iotaledger/iota.go/v4" - "github.com/iotaledger/iota.go/v4/tpkg" -) - -func RandomRead(p []byte) (n int, err error) { - return rand.Read(p) -} - -func RandomIntn(n int) int { - result, err := rand.Int(rand.Reader, big.NewInt(int64(n))) - if err != nil { - panic(err) - } - - return int(result.Int64()) -} - -func RandomInt31n(n int32) int32 { - result, err := rand.Int(rand.Reader, big.NewInt(int64(n))) - if err != nil { - panic(err) - } - - return int32(result.Int64()) -} - -func RandomInt63n(n int64) int64 { - result, err := rand.Int(rand.Reader, big.NewInt(n)) - if err != nil { - panic(err) - } - - return result.Int64() -} - -// RandBytes returns length amount random bytes. -func RandBytes(length int) []byte { - var b []byte - for i := 0; i < length; i++ { - b = append(b, byte(RandomIntn(256))) - } - - return b -} - -func RandString(length int) string { - return string(RandBytes(length)) -} - -// RandUint16 returns a random uint16. -func RandUint16(max uint16) uint16 { - return uint16(RandomInt31n(int32(max))) -} - -// RandUint32 returns a random uint32. -func RandUint32(max uint32) uint32 { - return uint32(RandomInt63n(int64(max))) -} - -// RandUint64 returns a random uint64. -func RandUint64(max uint64) uint64 { - return uint64(RandomInt63n(int64(uint32(max)))) -} - -func RandOutputID(index ...uint16) iotago.OutputID { - idx := RandUint16(126) - if len(index) > 0 { - idx = index[0] - } - - var outputID iotago.OutputID - _, err := RandomRead(outputID[:iotago.TransactionIDLength]) - if err != nil { - panic(err) - } - - binary.LittleEndian.PutUint16(outputID[iotago.TransactionIDLength:], idx) - - return outputID -} - -func RandBlockID() iotago.BlockID { - blockID := iotago.BlockID{} - copy(blockID[:], RandBytes(iotago.BlockIDLength)) - - return blockID -} - -func RandTransactionID() iotago.TransactionID { - transactionID := iotago.TransactionID{} - copy(transactionID[:], RandBytes(iotago.TransactionIDLength)) - - return transactionID -} - -func RandNFTID() iotago.NFTID { - nft := iotago.NFTID{} - copy(nft[:], RandBytes(iotago.NFTIDLength)) - - return nft -} - -func RandAccountID() iotago.AccountID { - accountID := iotago.AccountID{} - copy(accountID[:], RandBytes(iotago.AccountIDLength)) - - return accountID -} - -func RandAnchorID() iotago.AnchorID { - anchorID := iotago.AnchorID{} - copy(anchorID[:], RandBytes(iotago.AnchorIDLength)) - - return anchorID -} - -func RandSlotIndex() iotago.SlotIndex { - return iotago.SlotIndex(RandUint32(uint32(iotago.MaxSlotIndex))) -} - -func RandTimestamp() time.Time { - return time.Unix(int64(RandUint32(math.MaxUint32)), 0) -} - -func RandAddress(addressType iotago.AddressType) iotago.Address { - switch addressType { - case iotago.AddressEd25519: - address := &iotago.Ed25519Address{} - addressBytes := RandBytes(32) - copy(address[:], addressBytes) - - return address - - case iotago.AddressNFT: - return RandNFTID().ToAddress() - - case iotago.AddressAccount: - return RandAccountID().ToAddress() - - default: - panic("unknown address type") - } -} - -func RandOutputType() iotago.OutputType { - outputTypes := []iotago.OutputType{iotago.OutputBasic, iotago.OutputAccount, iotago.OutputAnchor, iotago.OutputFoundry, iotago.OutputNFT, iotago.OutputDelegation} - - return outputTypes[RandomIntn(len(outputTypes)-1)] -} - -func RandOutput(outputType iotago.OutputType) iotago.Output { - var addr iotago.Address - if outputType == iotago.OutputFoundry { - addr = RandAddress(iotago.AddressAccount) - } else { - addr = RandAddress(iotago.AddressEd25519) - } - - return RandOutputOnAddress(outputType, addr) -} - -func RandOutputOnAddress(outputType iotago.OutputType, address iotago.Address) iotago.Output { - return RandOutputOnAddressWithAmount(outputType, address, tpkg.RandBaseToken(iotago.MaxBaseToken)) -} - -func RandOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.Address, amount iotago.BaseToken) iotago.Output { - var iotaOutput iotago.Output - - switch outputType { - case iotago.OutputBasic: - //nolint:forcetypeassert // we already checked the type - iotaOutput = &iotago.BasicOutput{ - Amount: amount, - UnlockConditions: iotago.BasicOutputUnlockConditions{ - &iotago.AddressUnlockCondition{ - Address: address, - }, - }, - Features: iotago.BasicOutputFeatures{}, - } - - case iotago.OutputAccount: - //nolint:forcetypeassert // we already checked the type - iotaOutput = &iotago.AccountOutput{ - Amount: amount, - AccountID: RandAccountID(), - UnlockConditions: iotago.AccountOutputUnlockConditions{ - &iotago.AddressUnlockCondition{ - Address: address, - }, - }, - Features: iotago.AccountOutputFeatures{}, - ImmutableFeatures: iotago.AccountOutputImmFeatures{}, - } - - case iotago.OutputAnchor: - //nolint:forcetypeassert // we already checked the type - iotaOutput = &iotago.AnchorOutput{ - Amount: amount, - AnchorID: RandAnchorID(), - UnlockConditions: iotago.AnchorOutputUnlockConditions{ - &iotago.StateControllerAddressUnlockCondition{ - Address: address, - }, - &iotago.GovernorAddressUnlockCondition{ - Address: address, - }, - }, - Features: iotago.AnchorOutputFeatures{}, - ImmutableFeatures: iotago.AnchorOutputImmFeatures{}, - } - - case iotago.OutputFoundry: - if address.Type() != iotago.AddressAccount { - panic("not an alias address") - } - supply := new(big.Int).SetUint64(tpkg.RandUint64(math.MaxUint64)) - - //nolint:forcetypeassert // we already checked the type - iotaOutput = &iotago.FoundryOutput{ - Amount: amount, - SerialNumber: 0, - TokenScheme: &iotago.SimpleTokenScheme{ - MintedTokens: supply, - MeltedTokens: new(big.Int).SetBytes([]byte{0}), - MaximumSupply: supply, - }, - UnlockConditions: iotago.FoundryOutputUnlockConditions{ - &iotago.ImmutableAccountUnlockCondition{ - Address: address.(*iotago.AccountAddress), - }, - }, - Features: iotago.FoundryOutputFeatures{}, - ImmutableFeatures: iotago.FoundryOutputImmFeatures{}, - } - - case iotago.OutputNFT: - //nolint:forcetypeassert // we already checked the type - iotaOutput = &iotago.NFTOutput{ - Amount: amount, - NFTID: RandNFTID(), - UnlockConditions: iotago.NFTOutputUnlockConditions{ - &iotago.AddressUnlockCondition{ - Address: address, - }, - }, - Features: iotago.NFTOutputFeatures{}, - ImmutableFeatures: iotago.NFTOutputImmFeatures{}, - } - - case iotago.OutputDelegation: - //nolint:forcetypeassert // we already checked the type - iotaOutput = &iotago.DelegationOutput{ - Amount: amount, - DelegatedAmount: amount, - DelegationID: tpkg.RandDelegationID(), - ValidatorAddress: tpkg.RandAccountAddress(), - StartEpoch: tpkg.RandEpoch(), - EndEpoch: iotago.MaxEpochIndex, - UnlockConditions: iotago.DelegationOutputUnlockConditions{ - &iotago.AddressUnlockCondition{ - Address: address, - }, - }, - } - - default: - panic("unhandled output type") - } - - return iotaOutput -} - -func RandBlockIssuerKey() iotago.BlockIssuerKey { - return iotago.Ed25519PublicKeyBlockIssuerKeyFromPublicKey(ed25519.PublicKey(RandBytes(32))) -} - -func RandBlockIssuerKeys() iotago.BlockIssuerKeys { - // We always generate at least one key. - length := RandomIntn(10) + 1 - - blockIssuerKeys := iotago.NewBlockIssuerKeys() - for i := 0; i < length; i++ { - blockIssuerKeys.Add(RandBlockIssuerKey()) - } - - return blockIssuerKeys -} diff --git a/tools/gendoc/go.mod b/tools/gendoc/go.mod index 3da0794a6..874fe97f1 100644 --- a/tools/gendoc/go.mod +++ b/tools/gendoc/go.mod @@ -72,7 +72,7 @@ require ( github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe // indirect github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231128163614-c82e1fa40733 // indirect github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231128162307-cc6b309e93ef // indirect - github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec // indirect + github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f // indirect github.com/ipfs/boxo v0.13.1 // indirect github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect diff --git a/tools/gendoc/go.sum b/tools/gendoc/go.sum index 44394639e..3d4892dc3 100644 --- a/tools/gendoc/go.sum +++ b/tools/gendoc/go.sum @@ -311,8 +311,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231128163614-c82e1fa40733 h1:jdjFT github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231128163614-c82e1fa40733/go.mod h1:3ae5TFi3uaECV+F4d5ekKz+xcJQmVtCICcs/Rhvgn3w= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231128162307-cc6b309e93ef h1:RImO23W0kL3U9egdLoi5OTmOIhE5rHfR/dKM/6XhZeE= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231128162307-cc6b309e93ef/go.mod h1:bi0zndM3irf8FHYUg6MzG/Zk/t0fV8IwGXAdoT7wn98= -github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec h1:u9SAuKHygJwqwYxoEd+PyHCU4fTftcPdV2ZUGSRZe8o= -github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= +github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f h1:uT4ZKAyr/wk6lHcTQifAeEPb6WfBA5tdG7kEuyW7tDU= +github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI= github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= diff --git a/tools/genesis-snapshot/go.mod b/tools/genesis-snapshot/go.mod index 664f83bcb..b910a2cbe 100644 --- a/tools/genesis-snapshot/go.mod +++ b/tools/genesis-snapshot/go.mod @@ -10,7 +10,7 @@ require ( github.com/iotaledger/hive.go/lo v0.0.0-20231128121006-331a9e522dfe github.com/iotaledger/hive.go/runtime v0.0.0-20231128121006-331a9e522dfe github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000 - github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec + github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f github.com/mr-tron/base58 v1.2.0 github.com/spf13/pflag v1.0.5 golang.org/x/crypto v0.16.0 diff --git a/tools/genesis-snapshot/go.sum b/tools/genesis-snapshot/go.sum index c4e36dc03..1a30bb675 100644 --- a/tools/genesis-snapshot/go.sum +++ b/tools/genesis-snapshot/go.sum @@ -52,8 +52,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231127134220-90b88e3 github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231127134220-90b88e35bdb2/go.mod h1:FoH3T6yKlZJp8xm8K+zsQiibSynp32v21CpWx8xkek8= github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe h1:RcFUqhnJ+86+sA0XMrZ0q+086ULrdWQkWrjUt2OnJK4= github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec h1:u9SAuKHygJwqwYxoEd+PyHCU4fTftcPdV2ZUGSRZe8o= -github.com/iotaledger/iota.go/v4 v4.0.0-20231128162016-23f1b4e12cec/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= +github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f h1:uT4ZKAyr/wk6lHcTQifAeEPb6WfBA5tdG7kEuyW7tDU= +github.com/iotaledger/iota.go/v4 v4.0.0-20231201103607-03a45ba3707f/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=