Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit a29a425

Browse files
committed
Cleanup and add more comments
1 parent 8e13a4d commit a29a425

File tree

2 files changed

+109
-112
lines changed

2 files changed

+109
-112
lines changed

pkg/tests/combined_account_transition_test.go

Lines changed: 100 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,85 @@ import (
77
"github.com/iotaledger/iota-core/pkg/model"
88
"github.com/iotaledger/iota-core/pkg/protocol/engine/accounts"
99
"github.com/iotaledger/iota-core/pkg/testsuite"
10-
"github.com/iotaledger/iota-core/pkg/testsuite/depositcalculator"
1110
"github.com/iotaledger/iota-core/pkg/testsuite/mock"
1211
iotago "github.com/iotaledger/iota.go/v4"
1312
"github.com/iotaledger/iota.go/v4/tpkg"
14-
"github.com/stretchr/testify/require"
1513
)
1614

15+
// Test_AccountStateTransition follows the account state transition flow described in:
16+
// https://github.com/iotaledger/iota-core/issues/660#issuecomment-1892596243
17+
func Test_AccountStateTransition(t *testing.T) {
18+
ts := testsuite.NewTestSuite(t,
19+
testsuite.WithProtocolParametersOptions(
20+
iotago.WithTimeProviderOptions(
21+
0,
22+
testsuite.GenesisTimeWithOffsetBySlots(200, testsuite.DefaultSlotDurationInSeconds),
23+
testsuite.DefaultSlotDurationInSeconds,
24+
testsuite.DefaultSlotsPerEpochExponent,
25+
),
26+
),
27+
)
28+
defer ts.Shutdown()
29+
30+
node1 := ts.AddValidatorNode("node1")
31+
node2 := ts.AddValidatorNode("node2")
32+
wallet := ts.AddDefaultWallet(node1)
33+
34+
ts.Run(true)
35+
36+
node1.Protocol.SetLogLevel(log.LevelTrace)
37+
38+
// split genesis output into 4 outputs for the further usage.
39+
tx1 := wallet.CreateBasicOutputsEquallyFromInput("TX1", 4, "Genesis:0")
40+
ts.IssueBasicBlockWithOptions("block0", wallet, tx1)
41+
42+
// Issue some more blocks to make transaction accepted
43+
{
44+
ts.IssueValidationBlockWithHeaderOptions("vblock0", node2, mock.WithStrongParents(ts.BlockID("block0")))
45+
ts.IssueValidationBlockWithHeaderOptions("vblock1", node1, mock.WithStrongParents(ts.BlockID("vblock0")))
46+
ts.IssueValidationBlockWithHeaderOptions("vblock2", node2, mock.WithStrongParents(ts.BlockID("vblock1")))
47+
48+
ts.AssertTransactionsInCacheAccepted(wallet.Transactions("TX1"), true, node1, node2)
49+
}
50+
51+
// create the account1 from TX1:0 with wallet "first"
52+
// generated (block1, TX2)
53+
ts.AddWallet("first", node1, iotago.EmptyAccountID)
54+
createFullAccount(ts)
55+
56+
// create the account2, from implicit to full account from TX1:1 with wallet "second"
57+
// generated (block2, TX3), (block3, TX4)
58+
ts.AddWallet("second", node1, iotago.EmptyAccountID)
59+
implicitAccID := createImplicitToFullAccount(ts)
60+
61+
// send funds to account2, with TX1:2
62+
// generated (block4, TX5)
63+
sendfunds(ts)
64+
65+
// allot 1000 mana to account2 with TX1:3
66+
// generated (block5, TX6)
67+
allotManaTo(ts, implicitAccID)
68+
69+
// create native token from "TX5:0" and account2 (TX4:0)
70+
// generated (block6, TX7)
71+
createNativetoken(ts)
72+
}
73+
1774
func createFullAccount(ts *testsuite.TestSuite) iotago.AccountID {
1875
node1 := ts.Node("node1")
76+
newUserWallet := ts.Wallet("first")
1977

20-
newUserWallet := ts.AddWallet("first", node1, iotago.EmptyAccountID)
21-
// CREATE NEW ACCOUNT WITH BLOCK ISSUER AND STAKING FEATURES FROM BASIC UTXO
78+
// CREATE NEW ACCOUNT WITH BLOCK ISSUER FROM BASIC UTXO
2279
newAccountBlockIssuerKey := tpkg.RandBlockIssuerKey()
2380
// set the expiry slot of the transitioned genesis account to the latest committed + MaxCommittableAge
2481
newAccountExpirySlot := node1.Protocol.Engines.Main.Get().Storage.Settings().LatestCommitment().Slot() + ts.API.ProtocolParameters().MaxCommittableAge()
2582

26-
stakedAmount := iotago.BaseToken(10000)
27-
28-
validatorAccountAmount, err := depositcalculator.MinDeposit(ts.API.ProtocolParameters(), iotago.OutputAccount,
29-
depositcalculator.WithAddress(&iotago.Ed25519Address{}),
30-
depositcalculator.WithBlockIssuerKeys(1),
31-
depositcalculator.WithStakedAmount(stakedAmount),
32-
)
33-
require.NoError(ts.Testing, err)
34-
3583
tx1 := ts.DefaultWallet().CreateAccountFromInput(
3684
"TX2",
3785
"TX1:0",
3886
newUserWallet,
3987
mock.WithBlockIssuerFeature(iotago.BlockIssuerKeys{newAccountBlockIssuerKey}, newAccountExpirySlot),
40-
mock.WithStakingFeature(stakedAmount, 421, 0, 10),
41-
mock.WithAccountAmount(validatorAccountAmount),
88+
mock.WithAccountAmount(mock.MinIssuerAccountAmount(ts.API.ProtocolParameters())),
4289
mock.WithAccountMana(mock.MaxBlockManaCost(ts.DefaultWallet().Node.Protocol.CommittedAPI().ProtocolParameters())),
4390
)
4491

@@ -59,9 +106,9 @@ func createFullAccount(ts *testsuite.TestSuite) iotago.AccountID {
59106
PreviousOutputID: iotago.EmptyOutputID,
60107
BlockIssuerKeysAdded: iotago.NewBlockIssuerKeys(newAccountBlockIssuerKey),
61108
BlockIssuerKeysRemoved: iotago.NewBlockIssuerKeys(),
62-
ValidatorStakeChange: int64(stakedAmount),
63-
StakeEndEpochChange: 10,
64-
FixedCostChange: 421,
109+
ValidatorStakeChange: 0,
110+
StakeEndEpochChange: 0,
111+
FixedCostChange: 0,
65112
DelegationStakeChange: 0,
66113
}, false, ts.Nodes()...)
67114

@@ -71,21 +118,16 @@ func createFullAccount(ts *testsuite.TestSuite) iotago.AccountID {
71118
ExpirySlot: newAccountExpirySlot,
72119
OutputID: newAccount.OutputID(),
73120
BlockIssuerKeys: iotago.NewBlockIssuerKeys(newAccountBlockIssuerKey),
74-
StakeEndEpoch: 10,
75-
FixedCost: 421,
76-
DelegationStake: 0,
77-
ValidatorStake: stakedAmount,
78121
}, ts.Nodes()...)
79122

80123
return newAccountOutput.AccountID
81124
}
82125

83126
func createImplicitToFullAccount(ts *testsuite.TestSuite) iotago.AccountID {
84127
node1 := ts.Node("node1")
128+
newUserWallet := ts.Wallet("second")
85129

86130
// CREATE IMPLICIT ACCOUNT FROM GENESIS BASIC UTXO, SENT TO A NEW USER WALLET.
87-
// this wallet is not registered in the ledger yet.
88-
newUserWallet := ts.AddWallet("second", node1, iotago.EmptyAccountID)
89131
// a default wallet, already registered in the ledger, will issue the transaction and block.
90132
tx3 := ts.DefaultWallet().CreateImplicitAccountFromInput(
91133
"TX3",
@@ -158,115 +200,70 @@ func createImplicitToFullAccount(ts *testsuite.TestSuite) iotago.AccountID {
158200
return implicitAccountID
159201
}
160202

161-
func allotManaTo(ts *testsuite.TestSuite, to iotago.AccountID) {
162-
wallet := ts.DefaultWallet()
203+
func sendfunds(ts *testsuite.TestSuite) {
163204
node1 := ts.Node("node1")
164205
node2 := ts.Node("node2")
206+
wallet := ts.DefaultWallet()
207+
secondWallet := ts.Wallet("second")
165208

166-
tx6 := wallet.AllotManaFromInputs("TX5",
167-
iotago.Allotments{&iotago.Allotment{
168-
AccountID: to,
169-
Mana: iotago.Mana(1000),
170-
}}, "TX1:2")
171-
commitment := node1.Protocol.Engines.Main.Get().Storage.Settings().LatestCommitment().Commitment()
172-
ts.IssueBasicBlockWithOptions("block4", wallet, tx6, mock.WithSlotCommitment(commitment))
209+
// send funds from defaultWallet to secondWallet
210+
tx := wallet.SendFundsToWallet("TX5", secondWallet, "TX1:2")
211+
ts.IssueBasicBlockWithOptions("block4", wallet, tx)
212+
213+
ts.AssertTransactionsExist(wallet.Transactions("TX5"), true, node1)
214+
ts.AssertTransactionsInCacheBooked(wallet.Transactions("TX5"), true, node1)
173215

174216
// Issue some more blocks to make transaction accepted
175217
{
176-
ts.IssueValidationBlockWithHeaderOptions("vblock6", node2, mock.WithStrongParents(ts.BlockID("block4")))
177-
ts.IssueValidationBlockWithHeaderOptions("vblock7", node1, mock.WithStrongParents(ts.BlockID("vblock6")))
178-
ts.IssueValidationBlockWithHeaderOptions("vblock8", node2, mock.WithStrongParents(ts.BlockID("vblock7")))
218+
ts.IssueValidationBlockWithHeaderOptions("vblock9", node2, mock.WithStrongParents(ts.BlockID("block4")))
219+
ts.IssueValidationBlockWithHeaderOptions("vblock10", node1, mock.WithStrongParents(ts.BlockID("vblock9")))
220+
ts.IssueValidationBlockWithHeaderOptions("vblock11", node2, mock.WithStrongParents(ts.BlockID("vblock10")))
179221

180222
ts.AssertTransactionsInCacheAccepted(wallet.Transactions("TX5"), true, node1, node2)
181223
}
182224
}
183225

184-
func sendfunds(ts *testsuite.TestSuite) {
226+
func allotManaTo(ts *testsuite.TestSuite, to iotago.AccountID) {
227+
wallet := ts.DefaultWallet()
185228
node1 := ts.Node("node1")
186229
node2 := ts.Node("node2")
187-
wallet := ts.DefaultWallet()
188-
secondWallet := ts.Wallet("second")
189-
190-
tx := wallet.SendFundsToWallet("TX6", secondWallet, "TX1:3")
191-
ts.IssueBasicBlockWithOptions("block5", wallet, tx)
192230

193-
ts.AssertTransactionsExist(wallet.Transactions("TX6"), true, node1)
194-
ts.AssertTransactionsInCacheBooked(wallet.Transactions("TX6"), true, node1)
231+
tx6 := wallet.AllotManaFromInputs("TX6",
232+
iotago.Allotments{&iotago.Allotment{
233+
AccountID: to,
234+
Mana: iotago.Mana(1000),
235+
}}, "TX1:3")
236+
commitment := node1.Protocol.Engines.Main.Get().Storage.Settings().LatestCommitment().Commitment()
237+
ts.IssueBasicBlockWithOptions("block5", wallet, tx6, mock.WithSlotCommitment(commitment))
195238

196239
// Issue some more blocks to make transaction accepted
197240
{
198-
ts.IssueValidationBlockWithHeaderOptions("vblock9", node2, mock.WithStrongParents(ts.BlockID("block5")))
199-
ts.IssueValidationBlockWithHeaderOptions("vblock10", node1, mock.WithStrongParents(ts.BlockID("vblock9")))
200-
ts.IssueValidationBlockWithHeaderOptions("vblock11", node2, mock.WithStrongParents(ts.BlockID("vblock10")))
241+
ts.IssueValidationBlockWithHeaderOptions("vblock6", node2, mock.WithStrongParents(ts.BlockID("block5")))
242+
ts.IssueValidationBlockWithHeaderOptions("vblock7", node1, mock.WithStrongParents(ts.BlockID("vblock6")))
243+
ts.IssueValidationBlockWithHeaderOptions("vblock8", node2, mock.WithStrongParents(ts.BlockID("vblock7")))
201244

202-
ts.AssertTransactionsInCacheAccepted(wallet.Transactions("TX5"), true, node1, node2)
245+
ts.AssertTransactionsInCacheAccepted(wallet.Transactions("TX6"), true, node1, node2)
203246
}
204-
205247
}
206248

207-
func createNativetoken(ts *testsuite.TestSuite, acc iotago.AccountID) {
249+
// createNativetoken creates a native token from the given input and account.
250+
func createNativetoken(ts *testsuite.TestSuite) {
208251
wallet := ts.Wallet("second")
209252
node1 := ts.Node("node1")
253+
node2 := ts.Node("node2")
210254

211-
accOutput := ts.Wallet("second").Output("TX4:0")
212-
tx := wallet.CreateNativeTokenFromInput("TX7", "TX6:0", accOutput)
213-
ts.IssueBasicBlockWithOptions("block5", wallet, tx)
255+
tx := wallet.CreateNativeTokenFromInput("TX7", "TX5:0", "TX4:0")
256+
ts.IssueBasicBlockWithOptions("block6", wallet, tx)
214257

215258
ts.AssertTransactionsExist(wallet.Transactions("TX7"), true, node1)
216259
ts.AssertTransactionsInCacheBooked(wallet.Transactions("TX7"), true, node1)
217-
}
218-
219-
func Test_AccountStateTransition(t *testing.T) {
220-
ts := testsuite.NewTestSuite(t,
221-
testsuite.WithProtocolParametersOptions(
222-
iotago.WithTimeProviderOptions(
223-
0,
224-
testsuite.GenesisTimeWithOffsetBySlots(200, testsuite.DefaultSlotDurationInSeconds),
225-
testsuite.DefaultSlotDurationInSeconds,
226-
testsuite.DefaultSlotsPerEpochExponent,
227-
),
228-
),
229-
)
230-
defer ts.Shutdown()
231-
232-
// Add a validator node to the network. This will add a validator account to the snapshot.
233-
node1 := ts.AddValidatorNode("node1")
234-
node2 := ts.AddValidatorNode("node2")
235-
// Add a default block issuer to the network. This will add another block issuer account to the snapshot.
236-
wallet := ts.AddDefaultWallet(node1)
237-
238-
ts.Run(true)
239-
240-
node1.Protocol.SetLogLevel(log.LevelTrace)
241-
242-
// split genesis output into two outputs for the 2 accounts creation, 1 as faucet funds
243-
tx1 := wallet.CreateBasicOutputsEquallyFromInput("TX1", 4, "Genesis:0")
244-
ts.IssueBasicBlockWithOptions("block0", wallet, tx1)
245260

246261
// Issue some more blocks to make transaction accepted
247262
{
248-
ts.IssueValidationBlockWithHeaderOptions("vblock0", node2, mock.WithStrongParents(ts.BlockID("block0")))
249-
ts.IssueValidationBlockWithHeaderOptions("vblock1", node1, mock.WithStrongParents(ts.BlockID("vblock0")))
250-
ts.IssueValidationBlockWithHeaderOptions("vblock2", node2, mock.WithStrongParents(ts.BlockID("vblock1")))
263+
ts.IssueValidationBlockWithHeaderOptions("vblock12", node2, mock.WithStrongParents(ts.BlockID("block6")))
264+
ts.IssueValidationBlockWithHeaderOptions("vblock13", node1, mock.WithStrongParents(ts.BlockID("vblock12")))
265+
ts.IssueValidationBlockWithHeaderOptions("vblock14", node2, mock.WithStrongParents(ts.BlockID("vblock13")))
251266

252-
ts.AssertTransactionsInCacheAccepted(wallet.Transactions("TX1"), true, node1, node2)
267+
ts.AssertTransactionsInCacheAccepted(wallet.Transactions("TX7"), true, node1, node2)
253268
}
254-
255-
// create the first account with TX1:0
256-
// generated (block1, TX2)
257-
createFullAccount(ts)
258-
259-
// create the second account, from implicit to full account with TX1:1
260-
// generated (block2, TX3), (block3, TX4)
261-
implicitAccID := createImplicitToFullAccount(ts)
262-
263-
// allot 1000 mana to implicit account with TX1:2
264-
// generated (block4, TX5)
265-
allotManaTo(ts, implicitAccID)
266-
267-
// send funds to account 2, with TX1:3
268-
sendfunds(ts)
269-
270-
// create native token
271-
createNativetoken(ts, implicitAccID)
272269
}

pkg/testsuite/mock/wallet_transactions.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func (w *Wallet) SendFundsToWallet(transactionName string, receiverWallet *Walle
398398
)
399399

400400
receiverWallet.registerOutputs(transactionName, signedTransaction.Transaction)
401-
fmt.Println("here:", lo.Keys(w.outputs))
401+
fmt.Println(lo.Keys(w.outputs))
402402

403403
return signedTransaction
404404
}
@@ -665,31 +665,31 @@ func (w *Wallet) CreateNFTFromInput(transactionName string, inputName string, op
665665
)
666666
}
667667

668-
func (w *Wallet) CreateNativeTokenFromInput(transactionName string, inputName string, accountOutput *utxoledger.Output, opts ...options.Option[builder.FoundryOutputBuilder]) *iotago.SignedTransaction {
668+
func (w *Wallet) CreateNativeTokenFromInput(transactionName string, inputName string, accountOutputName string) *iotago.SignedTransaction {
669669
input := w.Output(inputName)
670+
accountOutput := w.AccountOutput(accountOutputName)
670671
mintedAmount := input.BaseTokenAmount()
671672

672-
// transition account output
673+
// transition account output, increase foundry counter by 1, the amount of account stays the same
673674
accID := accountOutput.Output().(*iotago.AccountOutput).AccountID
674675
accAddr := accID.ToAddress().(*iotago.AccountAddress)
675676
accTransitionOutput := builder.NewAccountOutputBuilderFromPrevious(accountOutput.Output().(*iotago.AccountOutput)).
676677
FoundriesToGenerate(1).MustBuild()
677678

678-
// foundry output
679+
// build foundry output, consume all amount from the UTXO output
679680
foundryID, _ := iotago.FoundryIDFromAddressAndSerialNumberAndTokenScheme(accAddr, accTransitionOutput.FoundryCounter, iotago.TokenSchemeSimple)
680681
tokenScheme := &iotago.SimpleTokenScheme{
681682
MintedTokens: big.NewInt(int64(mintedAmount)),
682-
MaximumSupply: big.NewInt(int64(input.BaseTokenAmount())),
683+
MaximumSupply: big.NewInt(int64(mintedAmount)),
683684
MeltedTokens: big.NewInt(0),
684685
}
685686

686-
foundryOutput := options.Apply(builder.NewFoundryOutputBuilder(accAddr, tokenScheme, mintedAmount).
687+
foundryOutput := builder.NewFoundryOutputBuilder(accAddr, tokenScheme, mintedAmount).
687688
NativeToken(&iotago.NativeTokenFeature{
688689
ID: foundryID,
689690
Amount: big.NewInt(int64(mintedAmount)),
690-
}),
691-
opts).MustBuild()
692-
foundryOutput.SerialNumber = accTransitionOutput.FoundryCounter
691+
}).
692+
SerialNumber(accTransitionOutput.FoundryCounter).MustBuild()
693693

694694
return w.createSignedTransactionWithOptions(
695695
transactionName,

0 commit comments

Comments
 (0)