Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata refactor #595

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion api_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var (

anchorOutputV3FeatBlocksArrRules = &serix.ArrayRules{
Min: 0, // Min: -
Max: 2, // Max: SenderFeature, MetadataFeature
Max: 3, // Max: SenderFeature, MetadataFeature, StateMetadataFeature
ValidationMode: serializer.ArrayValidationModeNoDuplicates |
serializer.ArrayValidationModeLexicalOrdering |
serializer.ArrayValidationModeAtMostOneOfEachTypeByte,
Expand Down Expand Up @@ -340,9 +340,47 @@ func V3API(protoParams ProtocolParameters) API {
must(api.RegisterTypeSettings(IssuerFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureIssuer))),
)

must(api.RegisterTypeSettings(MetadataFeatureEntriesKey(""),
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte)),
)
must(api.RegisterValidators(MetadataFeatureEntriesKey(""), nil, func(ctx context.Context, key MetadataFeatureEntriesKey) error {
if err := checkASCIIString(string(key)); err != nil {
return ierrors.Join(ErrInvalidMetadataKey, err)
}

return nil
}))
must(api.RegisterTypeSettings(MetadataFeatureEntriesValue{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsUint16)),
)
must(api.RegisterTypeSettings(MetadataFeatureEntries{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithMinLen(1).WithMaxByteSize(8192)),
)
must(api.RegisterTypeSettings(MetadataFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureMetadata))),
)

must(api.RegisterTypeSettings(StateMetadataFeatureEntriesKey(""),
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte)),
)
must(api.RegisterValidators(StateMetadataFeatureEntriesKey(""), nil, func(ctx context.Context, key StateMetadataFeatureEntriesKey) error {
if err := checkASCIIString(string(key)); err != nil {
return ierrors.Join(ErrInvalidStateMetadataKey, err)
}

return nil
}))
must(api.RegisterTypeSettings(StateMetadataFeatureEntriesValue{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsUint16)),
)
must(api.RegisterTypeSettings(StateMetadataFeatureEntries{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithMinLen(1).WithMaxByteSize(8192)),
)
must(api.RegisterTypeSettings(StateMetadataFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureStateMetadata))),
)

must(api.RegisterTypeSettings(TagFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureTag))),
)
Expand All @@ -358,6 +396,7 @@ func V3API(protoParams ProtocolParameters) API {
must(api.RegisterInterfaceObjects((*Feature)(nil), (*SenderFeature)(nil)))
must(api.RegisterInterfaceObjects((*Feature)(nil), (*IssuerFeature)(nil)))
must(api.RegisterInterfaceObjects((*Feature)(nil), (*MetadataFeature)(nil)))
must(api.RegisterInterfaceObjects((*Feature)(nil), (*StateMetadataFeature)(nil)))
must(api.RegisterInterfaceObjects((*Feature)(nil), (*TagFeature)(nil)))
must(api.RegisterInterfaceObjects((*Feature)(nil), (*NativeTokenFeature)(nil)))
must(api.RegisterInterfaceObjects((*Feature)(nil), (*BlockIssuerFeature)(nil)))
Expand Down Expand Up @@ -496,6 +535,7 @@ func V3API(protoParams ProtocolParameters) API {

must(api.RegisterInterfaceObjects((*anchorOutputFeature)(nil), (*SenderFeature)(nil)))
must(api.RegisterInterfaceObjects((*anchorOutputFeature)(nil), (*MetadataFeature)(nil)))
must(api.RegisterInterfaceObjects((*anchorOutputFeature)(nil), (*StateMetadataFeature)(nil)))

must(api.RegisterTypeSettings(AnchorOutputImmFeatures{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithArrayRules(anchorOutputV3ImmFeatBlocksArrRules),
Expand Down
8 changes: 4 additions & 4 deletions builder/output_builder_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ func (builder *AccountOutputBuilder) ImmutableSender(senderAddr iotago.Address)
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *AccountOutputBuilder) Metadata(data []byte) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *AccountOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.AccountOutput.
func (builder *AccountOutputBuilder) ImmutableMetadata(data []byte) *AccountOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *AccountOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *AccountOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand Down
43 changes: 21 additions & 22 deletions builder/output_builder_anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
// NewAnchorOutputBuilder creates a new AnchorOutputBuilder with the required state controller/governor addresses and base token amount.
func NewAnchorOutputBuilder(stateCtrl iotago.Address, govAddr iotago.Address, amount iotago.BaseToken) *AnchorOutputBuilder {
return &AnchorOutputBuilder{output: &iotago.AnchorOutput{
Amount: amount,
Mana: 0,
AnchorID: iotago.EmptyAnchorID,
StateIndex: 0,
StateMetadata: []byte{},
Amount: amount,
Mana: 0,
AnchorID: iotago.EmptyAnchorID,
StateIndex: 0,
UnlockConditions: iotago.AnchorOutputUnlockConditions{
&iotago.StateControllerAddressUnlockCondition{Address: stateCtrl},
&iotago.GovernorAddressUnlockCondition{Address: govAddr},
Expand Down Expand Up @@ -62,14 +61,6 @@ func (builder *AnchorOutputBuilder) AnchorID(anchorID iotago.AnchorID) *AnchorOu
return builder
}

// StateMetadata sets the state metadata of the output.
func (builder *AnchorOutputBuilder) StateMetadata(data []byte) *AnchorOutputBuilder {
builder.output.StateMetadata = data
builder.stateCtrlReq = true

return builder
}

// StateController sets the iotago.StateControllerAddressUnlockCondition of the output.
func (builder *AnchorOutputBuilder) StateController(stateCtrl iotago.Address) *AnchorOutputBuilder {
builder.output.UnlockConditions.Upsert(&iotago.StateControllerAddressUnlockCondition{Address: stateCtrl})
Expand Down Expand Up @@ -103,17 +94,25 @@ func (builder *AnchorOutputBuilder) ImmutableSender(senderAddr iotago.Address) *
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *AnchorOutputBuilder) Metadata(data []byte) *AnchorOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *AnchorOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *AnchorOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})
builder.govCtrlReq = true

return builder
}

// StateMetadata sets/modifies an iotago.StateMetadataFeature on the output.
func (builder *AnchorOutputBuilder) StateMetadata(entries iotago.StateMetadataFeatureEntries) *AnchorOutputBuilder {
builder.output.Features.Upsert(&iotago.StateMetadataFeature{Entries: entries})
builder.stateCtrlReq = true

return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.AnchorOutput.
func (builder *AnchorOutputBuilder) ImmutableMetadata(data []byte) *AnchorOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *AnchorOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *AnchorOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand Down Expand Up @@ -171,9 +170,9 @@ func (trans *AnchorStateTransition) Mana(mana iotago.Mana) *AnchorStateTransitio
return trans.builder.Mana(mana).StateTransition()
}

// StateMetadata sets the state metadata of the output.
func (trans *AnchorStateTransition) StateMetadata(data []byte) *AnchorStateTransition {
return trans.builder.StateMetadata(data).StateTransition()
// StateMetadata sets/modifies an iotago.StateMetadataFeature on the output.
func (trans *AnchorStateTransition) StateMetadata(entries iotago.StateMetadataFeatureEntries) *AnchorStateTransition {
return trans.builder.StateMetadata(entries).StateTransition()
}

// Sender sets/modifies an iotago.SenderFeature as a mutable feature on the output.
Expand Down Expand Up @@ -213,8 +212,8 @@ func (trans *AnchorGovernanceTransition) Sender(senderAddr iotago.Address) *Anch
}

// Metadata sets/modifies an iotago.MetadataFeature as a mutable feature on the output.
func (trans *AnchorGovernanceTransition) Metadata(data []byte) *AnchorGovernanceTransition {
return trans.builder.Metadata(data).GovernanceTransition()
func (trans *AnchorGovernanceTransition) Metadata(entries iotago.MetadataFeatureEntries) *AnchorGovernanceTransition {
return trans.builder.Metadata(entries).GovernanceTransition()
}

// Builder returns the AnchorOutputBuilder.
Expand Down
4 changes: 2 additions & 2 deletions builder/output_builder_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (builder *BasicOutputBuilder) Sender(senderAddr iotago.Address) *BasicOutpu
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *BasicOutputBuilder) Metadata(data []byte) *BasicOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *BasicOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *BasicOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand Down
10 changes: 5 additions & 5 deletions builder/output_builder_foundry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ func (builder *FoundryOutputBuilder) NativeToken(nt *iotago.NativeTokenFeature)
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *FoundryOutputBuilder) Metadata(data []byte) *FoundryOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *FoundryOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *FoundryOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.AccountOutput.
func (builder *FoundryOutputBuilder) ImmutableMetadata(data []byte) *FoundryOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Data: data})
// Only call this function on a new iotago.FoundryOutput.
func (builder *FoundryOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *FoundryOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand Down
8 changes: 4 additions & 4 deletions builder/output_builder_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ func (builder *NFTOutputBuilder) Sender(senderAddr iotago.Address) *NFTOutputBui
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *NFTOutputBuilder) Metadata(data []byte) *NFTOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *NFTOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *NFTOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.NFTOutput.
func (builder *NFTOutputBuilder) ImmutableMetadata(data []byte) *NFTOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Data: data})
func (builder *NFTOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *NFTOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand Down
Loading
Loading