Skip to content

Commit

Permalink
Add Anchor Output
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Oct 31, 2023
1 parent 426e751 commit fa89569
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231028104239-869296c43f26
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231028104044-69b02af0058d
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967
github.com/labstack/echo/v4 v4.11.2
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.31.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231028104239-869296c43f26 h1:ZZs7I
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231028104239-869296c43f26/go.mod h1:aFS0dN6QgKGgZakGgEv57NOLw+pLGdEiGcfDZ3h9GL0=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231028104044-69b02af0058d h1:0SVvkN04C+Ylc2puM/c77HuvRMmHRl0BkNjlZx1YWeA=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231028104044-69b02af0058d/go.mod h1:WFa5hHen6fi3RBX4K6r4fzhGpoh+7KJVIyFztZHdM84=
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a h1:WLW4iaJAx4N9Pujv+gzHklnnjCt5MPrtXyVcK3UXdNc=
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967 h1:qiBW4TiRdzVJshLu7RoWn9ur64SFLCOQ0oFtQWtz4bk=
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
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=
Expand Down
50 changes: 46 additions & 4 deletions pkg/protocol/engine/utxoledger/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ func TestAccountOutputSerialization(t *testing.T) {
txCreationSlot := utils.RandSlotIndex()
blockID := utils.RandBlockID()
aliasID := utils.RandAccountID()
stateController := utils.RandAccountID()
governor := utils.RandAddress(iotago.AddressEd25519).(*iotago.Ed25519Address)
address := utils.RandAccountID().ToAddress()
issuer := utils.RandNFTID()
sender := utils.RandAccountID()
amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken)
Expand All @@ -301,6 +300,49 @@ func TestAccountOutputSerialization(t *testing.T) {
Amount: amount,
AccountID: aliasID,
Conditions: iotago.AccountOutputUnlockConditions{
&iotago.AddressUnlockCondition{
Address: address,
},
},
Features: iotago.AccountOutputFeatures{
&iotago.SenderFeature{
Address: sender.ToAddress(),
},
},
ImmutableFeatures: iotago.AccountOutputImmFeatures{
&iotago.IssuerFeature{
Address: issuer.ToAddress(),
},
},
}

outputProof, err := iotago.NewOutputIDProof(iotago_tpkg.TestAPI, txCommitment, txCreationSlot, iotago.TxEssenceOutputs{iotaOutput}, 0)
require.NoError(t, err)

output := CreateOutputAndAssertSerialization(t, blockID, index, iotaOutput, outputProof)
spent := CreateSpentAndAssertSerialization(t, output)
outputID := output.OutputID()

require.ElementsMatch(t, byteutils.ConcatBytes([]byte{utxoledger.StoreKeyPrefixOutputUnspent}, outputID[:]), output.UnspentLookupKey())
AssertOutputUnspentAndSpentTransitions(t, output, spent)
}

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()
amount := iotago_tpkg.RandBaseToken(iotago.MaxBaseToken)
index := utils.RandSlotIndex()

iotaOutput := &iotago.AnchorOutput{
Amount: amount,
AnchorID: aliasID,
Conditions: iotago.AnchorOutputUnlockConditions{
&iotago.StateControllerAddressUnlockCondition{
Address: stateController.ToAddress(),
},
Expand All @@ -309,12 +351,12 @@ func TestAccountOutputSerialization(t *testing.T) {
},
},
StateMetadata: make([]byte, 0),
Features: iotago.AccountOutputFeatures{
Features: iotago.AnchorOutputFeatures{
&iotago.SenderFeature{
Address: sender.ToAddress(),
},
},
ImmutableFeatures: iotago.AccountOutputImmFeatures{
ImmutableFeatures: iotago.AnchorOutputImmFeatures{
&iotago.IssuerFeature{
Address: issuer.ToAddress(),
},
Expand Down
24 changes: 8 additions & 16 deletions pkg/testsuite/mock/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ func WithInputs(inputs utxoledger.Outputs) options.Option[builder.TransactionBui
}
}

func WithAccountInput(input *utxoledger.Output, governorTransition bool) options.Option[builder.TransactionBuilder] {
func WithAccountInput(input *utxoledger.Output) options.Option[builder.TransactionBuilder] {
return func(txBuilder *builder.TransactionBuilder) {
switch input.OutputType() {
case iotago.OutputAccount:
address := input.Output().UnlockConditionSet().StateControllerAddress().Address
if governorTransition {
address = input.Output().UnlockConditionSet().GovernorAddress().Address
}
address := input.Output().UnlockConditionSet().Address().Address

txBuilder.AddInput(&builder.TxInput{
UnlockTarget: address,
InputID: input.OutputID(),
Expand Down Expand Up @@ -145,22 +143,19 @@ func WithBlockIssuerFeature(keys iotago.BlockIssuerKeys, expirySlot iotago.SlotI

func WithAddBlockIssuerKey(key iotago.BlockIssuerKey) options.Option[builder.AccountOutputBuilder] {
return func(accountBuilder *builder.AccountOutputBuilder) {
transition := accountBuilder.GovernanceTransition()
transition.BlockIssuerTransition().AddKeys(key)
accountBuilder.BlockIssuerTransition().AddKeys(key)
}
}

func WithBlockIssuerKeys(keys iotago.BlockIssuerKeys) options.Option[builder.AccountOutputBuilder] {
return func(accountBuilder *builder.AccountOutputBuilder) {
transition := accountBuilder.GovernanceTransition()
transition.BlockIssuerTransition().Keys(keys)
accountBuilder.BlockIssuerTransition().Keys(keys)
}
}

func WithBlockIssuerExpirySlot(expirySlot iotago.SlotIndex) options.Option[builder.AccountOutputBuilder] {
return func(accountBuilder *builder.AccountOutputBuilder) {
transition := accountBuilder.GovernanceTransition()
transition.BlockIssuerTransition().ExpirySlot(expirySlot)
accountBuilder.BlockIssuerTransition().ExpirySlot(expirySlot)
}
}

Expand Down Expand Up @@ -209,12 +204,9 @@ func WithAccountConditions(conditions iotago.AccountOutputUnlockConditions) opti
return func(accountBuilder *builder.AccountOutputBuilder) {
for _, condition := range conditions.MustSet() {
switch condition.Type() {
case iotago.UnlockConditionStateControllerAddress:
//nolint:forcetypeassert
accountBuilder.StateController(condition.(*iotago.StateControllerAddressUnlockCondition).Address)
case iotago.UnlockConditionGovernorAddress:
case iotago.UnlockConditionAddress:
//nolint:forcetypeassert
accountBuilder.Governor(condition.(*iotago.GovernorAddressUnlockCondition).Address)
accountBuilder.Address(condition.(*iotago.AddressUnlockCondition).Address)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/testsuite/mock/wallet_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func (w *Wallet) CreateAccountFromInput(transactionName string, inputName string, recipientWallet *Wallet, creationSlot iotago.SlotIndex, opts ...options.Option[builder.AccountOutputBuilder]) *iotago.SignedTransaction {
input := w.Output(inputName)

accountOutput := options.Apply(builder.NewAccountOutputBuilder(recipientWallet.Address(), recipientWallet.Address(), input.BaseTokenAmount()).
accountOutput := options.Apply(builder.NewAccountOutputBuilder(recipientWallet.Address(), input.BaseTokenAmount()).
Mana(input.StoredMana()),
opts).MustBuild()

Expand Down Expand Up @@ -151,7 +151,7 @@ func (w *Wallet) TransitionAccount(transactionName string, inputName string, opt

signedTransaction := lo.PanicOnErr(w.createSignedTransactionWithOptions(
transactionName,
WithAccountInput(input, true),
WithAccountInput(input),
WithContextInputs(iotago.TxEssenceContextInputs{
&iotago.BlockIssuanceCreditInput{
AccountID: accountOutput.AccountID,
Expand Down Expand Up @@ -192,7 +192,7 @@ func (w *Wallet) DestroyAccount(transactionName string, inputName string, creati
CommitmentID: w.Node.Protocol.MainEngineInstance().Storage.Settings().LatestCommitment().Commitment().MustID(),
},
}),
WithAccountInput(input, true),
WithAccountInput(input),
WithOutputs(destructionOutputs),
WithSlotCreated(creationSlot),
))
Expand Down Expand Up @@ -250,7 +250,7 @@ func (w *Wallet) TransitionImplicitAccountToAccountOutput(transactionName string
panic(fmt.Sprintf("output with alias %s is not an implicit account", inputName))
}

accountOutput := options.Apply(builder.NewAccountOutputBuilder(w.Address(), w.Address(), MinIssuerAccountAmount).
accountOutput := options.Apply(builder.NewAccountOutputBuilder(w.Address(), MinIssuerAccountAmount).
AccountID(iotago.AccountIDFromOutputID(input.OutputID())),
opts).MustBuild()

Expand Down
3 changes: 1 addition & 2 deletions pkg/testsuite/snapshotcreator/snapshotcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ func createAccount(accountID iotago.AccountID, address iotago.Address, tokenAmou
Mana: mana,
AccountID: accountID,
Conditions: iotago.AccountOutputUnlockConditions{
&iotago.StateControllerAddressUnlockCondition{Address: address},
&iotago.GovernorAddressUnlockCondition{Address: address},
&iotago.AddressUnlockCondition{Address: address},
},
Features: iotago.AccountOutputFeatures{
&iotago.BlockIssuerFeature{
Expand Down
36 changes: 31 additions & 5 deletions pkg/utils/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ func RandNFTID() iotago.NFTID {
}

func RandAccountID() iotago.AccountID {
alias := iotago.AccountID{}
copy(alias[:], RandBytes(iotago.AccountIDLength))
accountID := iotago.AccountID{}
copy(accountID[:], RandBytes(iotago.AccountIDLength))

return alias
return accountID
}

func RandAnchorID() iotago.AnchorID {
anchorID := iotago.AnchorID{}
copy(anchorID[:], RandBytes(iotago.AnchorIDLength))

return anchorID
}

func RandSlotIndex() iotago.SlotIndex {
Expand Down Expand Up @@ -181,12 +188,27 @@ func RandOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.
},
Features: iotago.BasicOutputFeatures{},
}

case iotago.OutputAccount:
//nolint:forcetypeassert // we already checked the type
iotaOutput = &iotago.AccountOutput{
Amount: amount,
AccountID: RandAccountID(),
Conditions: 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(),
Conditions: iotago.AnchorOutputUnlockConditions{
&iotago.StateControllerAddressUnlockCondition{
Address: address,
},
Expand All @@ -195,9 +217,10 @@ func RandOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.
},
},
StateMetadata: make([]byte, 0),
Features: iotago.AccountOutputFeatures{},
ImmutableFeatures: iotago.AccountOutputImmFeatures{},
Features: iotago.AnchorOutputFeatures{},
ImmutableFeatures: iotago.AnchorOutputImmFeatures{},
}

case iotago.OutputFoundry:
if address.Type() != iotago.AddressAccount {
panic("not an alias address")
Expand All @@ -221,6 +244,7 @@ func RandOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.
Features: iotago.FoundryOutputFeatures{},
ImmutableFeatures: iotago.FoundryOutputImmFeatures{},
}

case iotago.OutputNFT:
//nolint:forcetypeassert // we already checked the type
iotaOutput = &iotago.NFTOutput{
Expand All @@ -234,6 +258,7 @@ func RandOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.
Features: iotago.NFTOutputFeatures{},
ImmutableFeatures: iotago.NFTOutputImmFeatures{},
}

case iotago.OutputDelegation:
//nolint:forcetypeassert // we already checked the type
iotaOutput = &iotago.DelegationOutput{
Expand All @@ -249,6 +274,7 @@ func RandOutputOnAddressWithAmount(outputType iotago.OutputType, address iotago.
},
},
}

default:
panic("unhandled output type")
}
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42 // indirect
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231028104239-869296c43f26 // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231028104044-69b02af0058d // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967 // 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
Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231028104239-869296c43f26 h1:ZZs7I
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231028104239-869296c43f26/go.mod h1:aFS0dN6QgKGgZakGgEv57NOLw+pLGdEiGcfDZ3h9GL0=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231028104044-69b02af0058d h1:0SVvkN04C+Ylc2puM/c77HuvRMmHRl0BkNjlZx1YWeA=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231028104044-69b02af0058d/go.mod h1:WFa5hHen6fi3RBX4K6r4fzhGpoh+7KJVIyFztZHdM84=
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a h1:WLW4iaJAx4N9Pujv+gzHklnnjCt5MPrtXyVcK3UXdNc=
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967 h1:qiBW4TiRdzVJshLu7RoWn9ur64SFLCOQ0oFtQWtz4bk=
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
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=
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-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967
github.com/mr-tron/base58 v1.2.0
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.14.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 @@ -52,8 +52,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292c580b/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g=
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42 h1:OlDhgvJ48bZxcvTeebJ1b96xtNnJAddejd2Q4rlH1mU=
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a h1:WLW4iaJAx4N9Pujv+gzHklnnjCt5MPrtXyVcK3UXdNc=
github.com/iotaledger/iota.go/v4 v4.0.0-20231028103644-b834fd54b02a/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967 h1:qiBW4TiRdzVJshLu7RoWn9ur64SFLCOQ0oFtQWtz4bk=
github.com/iotaledger/iota.go/v4 v4.0.0-20231031113109-5d7d59311967/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
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 fa89569

Please sign in to comment.