Skip to content

Commit

Permalink
Metadata refactor
Browse files Browse the repository at this point in the history
- All Metadata features are now `map[string][]byte` instead of `[]byte`
- Removed StateMetadata in favor of new GovernorMetadata feature
  • Loading branch information
muXxer committed Nov 8, 2023
1 parent c08a4ff commit 9684361
Show file tree
Hide file tree
Showing 20 changed files with 578 additions and 283 deletions.
28 changes: 27 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, GovernorMetadataFeature
ValidationMode: serializer.ArrayValidationModeNoDuplicates |
serializer.ArrayValidationModeLexicalOrdering |
serializer.ArrayValidationModeAtMostOneOfEachTypeByte,
Expand Down Expand Up @@ -340,9 +340,33 @@ func V3API(protoParams ProtocolParameters) API {
must(api.RegisterTypeSettings(IssuerFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureIssuer))),
)

must(api.RegisterTypeSettings(MetadataFeatureEntriesKey(""),
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithMinLen(1).WithMaxLen(64)),
)
must(api.RegisterTypeSettings(MetadataFeatureEntriesValue{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsUint16).WithMinLen(0).WithMaxLen(1000)),
)
must(api.RegisterTypeSettings(MetadataFeatureEntries{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithMinLen(1).WithMaxLen(64).WithMaxByteSize(8192)),
)
must(api.RegisterTypeSettings(MetadataFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureMetadata))),
)

must(api.RegisterTypeSettings(GovernorMetadataFeatureEntriesKey(""),
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithMinLen(1).WithMaxLen(64)),
)
must(api.RegisterTypeSettings(GovernorMetadataFeatureEntriesValue{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsUint16).WithMinLen(0).WithMaxLen(1000)),
)
must(api.RegisterTypeSettings(GovernorMetadataFeatureEntries{},
serix.TypeSettings{}.WithLengthPrefixType(serix.LengthPrefixTypeAsByte).WithMinLen(1).WithMaxLen(64).WithMaxByteSize(8192)),
)
must(api.RegisterTypeSettings(GovernorMetadataFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureMetadataGovernor))),
)

must(api.RegisterTypeSettings(TagFeature{},
serix.TypeSettings{}.WithObjectType(uint8(FeatureTag))),
)
Expand All @@ -358,6 +382,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), (*GovernorMetadataFeature)(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 +521,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), (*GovernorMetadataFeature)(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
45 changes: 22 additions & 23 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.stateCtrlReq = true

return builder
}

// GovernorMetadata sets/modifies an iotago.GovernorMetadataFeature on the output.
func (builder *AnchorOutputBuilder) GovernorMetadata(entries iotago.GovernorMetadataFeatureEntries) *AnchorOutputBuilder {
builder.output.Features.Upsert(&iotago.GovernorMetadataFeature{Entries: entries})
builder.govCtrlReq = 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()
// Metadata sets/modifies an iotago.MetadataFeature as a mutable feature on the output.
func (trans *AnchorStateTransition) Metadata(entries iotago.MetadataFeatureEntries) *AnchorStateTransition {
return trans.builder.Metadata(entries).StateTransition()
}

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

// 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()
// GovernorMetadata sets/modifies an iotago.GovernorMetadataFeature on the output.
func (trans *AnchorGovernanceTransition) GovernorMetadata(entries iotago.GovernorMetadataFeatureEntries) *AnchorGovernanceTransition {
return trans.builder.GovernorMetadata(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

0 comments on commit 9684361

Please sign in to comment.