Skip to content

Commit

Permalink
test implicit account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberphysic4l committed Sep 19, 2023
1 parent 1a176a5 commit d564fbd
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20230906114834-b50190b9f9c2
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230829161228-3f4eb50a4d14
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230829160617-69b96c7c9f9b
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420
github.com/labstack/echo/v4 v4.11.1
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.30.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230829161228-3f4eb50a4d14 h1:BkDuQ
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230829161228-3f4eb50a4d14/go.mod h1:ADBXzdHXTldP0NB2Vf+KbhDxkYciGRjzQVXT6Rdne1g=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230829160617-69b96c7c9f9b h1:EPB/+iWeSx/WgJlzaXl8yjinxuD8CCOdi2ZPMLeeMVY=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230829160617-69b96c7c9f9b/go.mod h1:B7gyJP6GshCSlEmY3CxEk5TZdsMs3UNz5U92hkFDdMs=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784 h1:lb/8KbtTOqwIqGb84F70dbmqENRDHZZul4AaPNapfAU=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420 h1:WVaB6hOwT6UhnDtM+uQlL0OBw91OdUONiaFgOvAud1g=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY=
github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
8 changes: 8 additions & 0 deletions pkg/protocol/engine/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ func (a *AccountData) readFromReadSeeker(reader io.ReadSeeker) (int, error) {
}
bytesConsumed += bytesRead
blockIssuerKeys[i] = iotago.BlockIssuerKeyEd25519FromPublicKey(ed25519PublicKey)
case iotago.Ed25519BlockIssuerKeyAddress:
var implicitAccountCreationAddress iotago.ImplicitAccountCreationAddress
bytesRead, err = io.ReadFull(reader, implicitAccountCreationAddress[:])
if err != nil {
return bytesConsumed, ierrors.Wrapf(err, "unable to read address %d for accountID %s", i, a.ID)
}
bytesConsumed += bytesRead
blockIssuerKeys[i] = iotago.BlockIssuerKeyEd25519AddressFromAddress(&implicitAccountCreationAddress)
default:
return bytesConsumed, ierrors.Wrapf(err, "unsupported block issuer key type %d for accountID %s at offset %d", blockIssuerKeyType, a.ID, i)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestCommitmentFilter_NoAccount(t *testing.T) {
),
)
keyPairImplicitAccount := ed25519.GenerateKeyPair()
implicitAddress := iotago.Ed25519AddressFromPubKey(keyPairImplicitAccount.PublicKey[:])
implicitAddress := iotago.ImplicitAccountCreationAddressFromPubKey(keyPairImplicitAccount.PublicKey[:])
implicitAccountID := iotago.AccountID(implicitAddress[:])
tf.AddAccountData(
implicitAccountID,
Expand Down
22 changes: 16 additions & 6 deletions pkg/protocol/engine/ledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ledger

import (
"io"
"math"

"github.com/iotaledger/hive.go/core/safemath"
"github.com/iotaledger/hive.go/ds"
Expand Down Expand Up @@ -388,11 +389,19 @@ func (l *Ledger) prepareAccountDiffs(accountDiffs map[iotago.AccountID]*model.Ac
// case 1.
continue
}
accountDiff.NewOutputID = createdOutput.OutputID()
accountDiff.PreviousOutputID = consumedOutput.OutputID()

// case 2.
// created output can never be an implicit account as these can not be transitioned, but consumed output can be.
switch consumedOutput.Output().Type() {
case iotago.OutputAccount:
accountDiff.PreviousExpirySlot = consumedOutput.Output().FeatureSet().BlockIssuer().ExpirySlot
case iotago.OutputBasic:
accountDiff.PreviousExpirySlot = iotago.SlotIndex(math.MaxUint64)
}

accountDiff.PreviousOutputID = consumedOutput.OutputID()
accountDiff.NewOutputID = createdOutput.OutputID()
accountDiff.NewExpirySlot = createdOutput.Output().FeatureSet().BlockIssuer().ExpirySlot
accountDiff.PreviousExpirySlot = consumedOutput.Output().FeatureSet().BlockIssuer().ExpirySlot

oldPubKeysSet := accountData.BlockIssuerKeys
newPubKeysSet := ds.NewSet[iotago.BlockIssuerKey]()
Expand Down Expand Up @@ -437,22 +446,23 @@ func (l *Ledger) prepareAccountDiffs(accountDiffs map[iotago.AccountID]*model.Ac
// have some values from the allotment, so no need to set them explicitly.
accountDiff.NewOutputID = createdOutput.OutputID()
accountDiff.PreviousOutputID = iotago.EmptyOutputID
accountDiff.NewExpirySlot = createdOutput.Output().FeatureSet().BlockIssuer().ExpirySlot
accountDiff.PreviousExpirySlot = 0

switch createdOutput.Output().Type() {
// for account outputs, get block issuer keys from the block issuer feature, and check for staking info.
case iotago.OutputAccount:
accountDiff.BlockIssuerKeysAdded = createdOutput.Output().FeatureSet().BlockIssuer().BlockIssuerKeys
accountDiff.NewExpirySlot = createdOutput.Output().FeatureSet().BlockIssuer().ExpirySlot
if stakingFeature := createdOutput.Output().FeatureSet().Staking(); stakingFeature != nil {
accountDiff.ValidatorStakeChange = int64(stakingFeature.StakedAmount)
accountDiff.StakeEndEpochChange = int64(stakingFeature.EndEpoch)
accountDiff.FixedCostChange = int64(stakingFeature.FixedCost)
}
// for basic outputs (implicit accounts), get block issuer keys from the address in the unlock conditions.
case iotago.OutputBasic:
ed25519Address, _ := createdOutput.Output().UnlockConditionSet().Address().Address.(*iotago.Ed25519Address)
accountDiff.BlockIssuerKeysAdded = iotago.BlockIssuerKeys{iotago.BlockIssuerKeyEd25519AddressFromAddress(ed25519Address)}
address, _ := createdOutput.Output().UnlockConditionSet().Address().Address.(*iotago.ImplicitAccountCreationAddress)
accountDiff.BlockIssuerKeysAdded = iotago.BlockIssuerKeys{iotago.BlockIssuerKeyEd25519AddressFromAddress(address)}
accountDiff.NewExpirySlot = iotago.SlotIndex(math.MaxUint64)
}

}
Expand Down
37 changes: 36 additions & 1 deletion pkg/tests/accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tests

import (
"math"
"testing"

"github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/options"
Expand Down Expand Up @@ -276,7 +278,7 @@ func Test_TransitionAccount(t *testing.T) {

block4 := ts.IssueBlockAtSlotWithOptions("block4", slotIndexBlock4, node1.Protocol.MainEngineInstance().Storage.Settings().LatestCommitment().Commitment(), node1, tx4, blockfactory.WithStrongParents(latestParent.ID()))

_ = ts.CommitUntilSlot(slotIndexBlock4, activeNodes, block4)
latestParent = ts.CommitUntilSlot(slotIndexBlock4, activeNodes, block4)

ts.AssertAccountDiff(newAccountOutput.AccountID, slotIndexBlock4, &model.AccountDiff{
BICChange: 0,
Expand All @@ -303,6 +305,39 @@ func Test_TransitionAccount(t *testing.T) {
ValidatorStake: 10000,
}, ts.Nodes()...)

// CREATE IMPLICIT ACCOUNT FROM BASIC UTXO

keyPair := ed25519.GenerateKeyPair()
implicitAccountAddress := iotago.ImplicitAccountCreationAddressFromPubKey(keyPair.PublicKey[:])
inputForImplicitAccount, outputsForImplicitAccount, implicitWallet := ts.TransactionFramework.CreateImplicitAccountFromInput("TX1:3", implicitAccountAddress)

tx5 := lo.PanicOnErr(ts.TransactionFramework.CreateTransactionWithOptions("TX5", implicitWallet,
testsuite.WithInputs(inputForImplicitAccount),
testsuite.WithOutputs(outputsForImplicitAccount),
))

implicitAccountOutput := ts.TransactionFramework.Output("TX5:0")
implicitAccountOutputID := implicitAccountOutput.OutputID()
implicitAccountID := iotago.AccountIDFromOutputID(implicitAccountOutputID)

slotIndexBlock5 := latestParent.ID().Index()

block5 := ts.IssueBlockAtSlotWithOptions("block5", slotIndexBlock5, node1.Protocol.MainEngineInstance().Storage.Settings().LatestCommitment().Commitment(), node1, tx5, blockfactory.WithStrongParents(latestParent.ID()))

latestParent = ts.CommitUntilSlot(slotIndexBlock5, activeNodes, block5)

var implicitBlockIssuerKey iotago.BlockIssuerKey = iotago.BlockIssuerKeyEd25519AddressFromAddress(implicitAccountAddress)

ts.AssertAccountData(&accounts.AccountData{
ID: implicitAccountID,
Credits: accounts.NewBlockIssuanceCredits(0, slotIndexBlock5),
ExpirySlot: iotago.SlotIndex(math.MaxUint64),
OutputID: implicitAccountOutputID,
BlockIssuerKeys: ds.NewSet(implicitBlockIssuerKey),
}, ts.Nodes()...)

// TRANSITION IMPLICIT ACCOUNT TO ACCOUNT OUTPUT

ts.Wait(ts.Nodes()...)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/testsuite/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (t *TestSuite) AssertAccountData(accountData *accounts.AccountData, nodes .
}

if !cmp.Equal(accountData.BlockIssuerKeys.ToSlice(), actualAccountData.BlockIssuerKeys.ToSlice()) {
return ierrors.Errorf("AssertAccountData: %s: accountID %s expected pub keys %s, got %s", node.Name, accountData.ID, accountData.BlockIssuerKeys, actualAccountData.BlockIssuerKeys)
return ierrors.Errorf("AssertAccountData: %s: accountID %s expected pub keys %s, got %s", node.Name, accountData.ID, accountData.BlockIssuerKeys.ToSlice(), actualAccountData.BlockIssuerKeys.ToSlice())
}

if accountData.StakeEndEpoch != actualAccountData.StakeEndEpoch {
Expand Down
17 changes: 17 additions & 0 deletions pkg/testsuite/transactions_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ func (t *TransactionFramework) CreateAccountFromInput(inputAlias string, opts ..
return utxoledger.Outputs{input}, outputStates, []*mock.HDWallet{t.wallet}
}

// CreateImplicitAccountFromInput

Check failure on line 212 in pkg/testsuite/transactions_framework.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/testsuite/transactions_framework.go#L212

Comment should end in a period (godot)
Raw output
pkg/testsuite/transactions_framework.go:212:34: Comment should end in a period (godot)
// CreateImplicitAccountFromInput
                                 ^
func (t *TransactionFramework) CreateImplicitAccountFromInput(inputAlias string, address *iotago.ImplicitAccountCreationAddress) (utxoledger.Outputs, iotago.Outputs[iotago.Output], []*mock.HDWallet) {
input := t.Output(inputAlias)

basicOutput := &iotago.BasicOutput{
Amount: input.BaseTokenAmount(),
Mana: input.StoredMana(),
NativeTokens: iotago.NativeTokens{},
Conditions: iotago.BasicOutputUnlockConditions{
&iotago.AddressUnlockCondition{Address: address},
},
Features: iotago.BasicOutputFeatures{},
}

return utxoledger.Outputs{input}, iotago.Outputs[iotago.Output]{basicOutput}, []*mock.HDWallet{t.wallet}
}

// CreateDelegationFromInput creates a new DelegationOutput with given options from an input. If the remainder Output
// is not created, then StoredMana from the input is not passed and can potentially be burned.
// In order not to burn it, it needs to be assigned manually in another output in the transaction.
Expand Down
2 changes: 1 addition & 1 deletion tools/evil-spammer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/iotaledger/hive.go/runtime v0.0.0-20230906114834-b50190b9f9c2
github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000
github.com/iotaledger/iota-core/tools/genesis-snapshot v0.0.0-00010101000000-000000000000
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420
github.com/mr-tron/base58 v1.2.0
go.uber.org/atomic v1.11.0
)
Expand Down
4 changes: 2 additions & 2 deletions tools/evil-spammer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230912111751-d84fba0
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230912111751-d84fba02bb7c/go.mod h1:IJgaaxbgKCsNat18jlJJEAxCY2oVYR3F30B+M4vJ89I=
github.com/iotaledger/hive.go/stringify v0.0.0-20230906114834-b50190b9f9c2 h1:exATYMLT/d8fgMuVNO6kMDsFn9DUJEcyCuoBv9sP13g=
github.com/iotaledger/hive.go/stringify v0.0.0-20230906114834-b50190b9f9c2/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784 h1:lb/8KbtTOqwIqGb84F70dbmqENRDHZZul4AaPNapfAU=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420 h1:WVaB6hOwT6UhnDtM+uQlL0OBw91OdUONiaFgOvAud1g=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
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/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20230906114834-b50190b9f9c2 // indirect
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230829161228-3f4eb50a4d14 // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230829160617-69b96c7c9f9b // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784 // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420 // indirect
github.com/ipfs/boxo v0.10.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230829161228-3f4eb50a4d14 h1:BkDuQ
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230829161228-3f4eb50a4d14/go.mod h1:ADBXzdHXTldP0NB2Vf+KbhDxkYciGRjzQVXT6Rdne1g=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230829160617-69b96c7c9f9b h1:EPB/+iWeSx/WgJlzaXl8yjinxuD8CCOdi2ZPMLeeMVY=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230829160617-69b96c7c9f9b/go.mod h1:B7gyJP6GshCSlEmY3CxEk5TZdsMs3UNz5U92hkFDdMs=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784 h1:lb/8KbtTOqwIqGb84F70dbmqENRDHZZul4AaPNapfAU=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420 h1:WVaB6hOwT6UhnDtM+uQlL0OBw91OdUONiaFgOvAud1g=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY=
github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
2 changes: 1 addition & 1 deletion tools/genesis-snapshot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/iotaledger/hive.go/lo v0.0.0-20230906114834-b50190b9f9c2
github.com/iotaledger/hive.go/runtime v0.0.0-20230906114834-b50190b9f9c2
github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420
github.com/mr-tron/base58 v1.2.0
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.13.0
Expand Down
4 changes: 2 additions & 2 deletions tools/genesis-snapshot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230912111751-d84fba0
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230912111751-d84fba02bb7c/go.mod h1:IJgaaxbgKCsNat18jlJJEAxCY2oVYR3F30B+M4vJ89I=
github.com/iotaledger/hive.go/stringify v0.0.0-20230906114834-b50190b9f9c2 h1:exATYMLT/d8fgMuVNO6kMDsFn9DUJEcyCuoBv9sP13g=
github.com/iotaledger/hive.go/stringify v0.0.0-20230906114834-b50190b9f9c2/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784 h1:lb/8KbtTOqwIqGb84F70dbmqENRDHZZul4AaPNapfAU=
github.com/iotaledger/iota.go/v4 v4.0.0-20230915114908-97b965f76784/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420 h1:WVaB6hOwT6UhnDtM+uQlL0OBw91OdUONiaFgOvAud1g=
github.com/iotaledger/iota.go/v4 v4.0.0-20230919084223-31baf7634420/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
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=
Expand Down

0 comments on commit d564fbd

Please sign in to comment.