Skip to content

Commit

Permalink
Merge pull request #429 from iotaledger/fix/storage-deposit
Browse files Browse the repository at this point in the history
Fix MinStorageDeposit for return output calculation
  • Loading branch information
luca-moser authored May 22, 2023
2 parents d533cb3 + 6b84149 commit c78f869
Show file tree
Hide file tree
Showing 26 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion address_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (aliasAddr *AliasAddress) Clone() Address {
return cpy
}

func (aliasAddr *AliasAddress) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (aliasAddr *AliasAddress) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(AliasAddressSerializedBytesSize)
}

Expand Down
2 changes: 1 addition & 1 deletion address_ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (edAddr *Ed25519Address) Clone() Address {
return cpy
}

func (edAddr *Ed25519Address) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (edAddr *Ed25519Address) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(Ed25519AddressSerializedBytesSize)
}

Expand Down
2 changes: 1 addition & 1 deletion address_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (nftAddr *NFTAddress) Clone() Address {
return cpy
}

func (nftAddr *NFTAddress) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (nftAddr *NFTAddress) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(NFTAddressSerializedBytesSize)
}

Expand Down
4 changes: 2 additions & 2 deletions feat.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (f Features) Clone() Features {
return cpy
}

func (f Features) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
var sumCost uint64
func (f Features) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
var sumCost VBytes
for _, feat := range f {
sumCost += feat.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion feat_issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *IssuerFeature) Clone() Feature {
return &IssuerFeature{Address: s.Address.Clone()}
}

func (s *IssuerFeature) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *IssuerFeature) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize) +
s.Address.VBytes(rentStruct, nil)
}
Expand Down
4 changes: 2 additions & 2 deletions feat_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (s *MetadataFeature) Clone() Feature {
return &MetadataFeature{Data: append([]byte(nil), s.Data...)}
}

func (s *MetadataFeature) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
return rentStruct.VBFactorData.Multiply(uint64(serializer.SmallTypeDenotationByteSize + serializer.UInt16ByteSize + len(s.Data)))
func (s *MetadataFeature) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(VBytes(serializer.SmallTypeDenotationByteSize + serializer.UInt16ByteSize + len(s.Data)))
}

func (s *MetadataFeature) Equal(other Feature) bool {
Expand Down
2 changes: 1 addition & 1 deletion feat_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *SenderFeature) Clone() Feature {
return &SenderFeature{Address: s.Address.Clone()}
}

func (s *SenderFeature) VBytes(rentStruct *RentStructure, f VBytesFunc) uint64 {
func (s *SenderFeature) VBytes(rentStruct *RentStructure, f VBytesFunc) VBytes {
if f != nil {
return f(rentStruct)
}
Expand Down
4 changes: 2 additions & 2 deletions feat_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func (s *TagFeature) Clone() Feature {
return &TagFeature{Tag: append([]byte(nil), s.Tag...)}
}

func (s *TagFeature) VBytes(rentStruct *RentStructure, f VBytesFunc) uint64 {
func (s *TagFeature) VBytes(rentStruct *RentStructure, f VBytesFunc) VBytes {
if f != nil {
return f(rentStruct)
}
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize + serializer.OneByte + uint64(len(s.Tag)))
return rentStruct.VBFactorData.Multiply(VBytes(serializer.SmallTypeDenotationByteSize + serializer.OneByte + len(s.Tag)))
}

func (s *TagFeature) Equal(other Feature) bool {
Expand Down
6 changes: 3 additions & 3 deletions native_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func (n NativeTokens) Clone() NativeTokens {
return cpy
}

func (n NativeTokens) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (n NativeTokens) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
// length prefix + (native token count * static native token cost)
return rentStruct.VBFactorData.Multiply(uint64(serializer.OneByte + len(n)*NativeTokenVByteCost))
return rentStruct.VBFactorData.Multiply(VBytes(serializer.OneByte + len(n)*NativeTokenVByteCost))
}

func (n NativeTokens) ToSerializables() serializer.Serializables {
Expand Down Expand Up @@ -168,7 +168,7 @@ func (n *NativeToken) Clone() *NativeToken {
return cpy
}

func (n *NativeToken) VBytes(_ *RentStructure, _ VBytesFunc) uint64 {
func (n *NativeToken) VBytes(_ *RentStructure, _ VBytesFunc) VBytes {
return NativeTokenVByteCost
}

Expand Down
2 changes: 1 addition & 1 deletion output.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
)

// defines the default offset virtual byte costs for an output.
func outputOffsetVByteCost(rentStruct *RentStructure) uint64 {
func outputOffsetVByteCost(rentStruct *RentStructure) VBytes {
return rentStruct.VBFactorKey.Multiply(OutputIDLength) +
// included block id, conf ms index, conf ms ts
rentStruct.VBFactorData.Multiply(BlockIDLength+serializer.UInt32ByteSize+serializer.UInt32ByteSize)
Expand Down
4 changes: 2 additions & 2 deletions output_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,14 @@ func (a *AliasOutput) UnlockableBy(ident Address, next TransDepIdentOutput, extP
return outputUnlockable(a, next, ident, extParas)
}

func (a *AliasOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (a *AliasOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return outputOffsetVByteCost(rentStruct) +
// prefix + amount
rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt64ByteSize) +
a.NativeTokens.VBytes(rentStruct, nil) +
rentStruct.VBFactorData.Multiply(AliasIDLength) +
// state index, state meta length, state meta, foundry counter
rentStruct.VBFactorData.Multiply(uint64(serializer.UInt32ByteSize+serializer.UInt16ByteSize+len(a.StateMetadata)+serializer.UInt32ByteSize)) +
rentStruct.VBFactorData.Multiply(VBytes(serializer.UInt32ByteSize+serializer.UInt16ByteSize+len(a.StateMetadata)+serializer.UInt32ByteSize)) +
a.Conditions.VBytes(rentStruct, nil) +
a.Features.VBytes(rentStruct, nil) +
a.ImmutableFeatures.VBytes(rentStruct, nil)
Expand Down
2 changes: 1 addition & 1 deletion output_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (e *BasicOutput) UnlockableBy(ident Address, extParas *ExternalUnlockParame
return ok
}

func (e *BasicOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (e *BasicOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return outputOffsetVByteCost(rentStruct) +
// prefix + amount
rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt64ByteSize) +
Expand Down
2 changes: 1 addition & 1 deletion output_foundry.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (f *FoundryOutput) UnlockableBy(ident Address, extParas *ExternalUnlockPara
return ok
}

func (f *FoundryOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (f *FoundryOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return outputOffsetVByteCost(rentStruct) +
// prefix + amount
rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt64ByteSize) +
Expand Down
2 changes: 1 addition & 1 deletion output_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (n *NFTOutput) UnlockableBy(ident Address, extParas *ExternalUnlockParamete
return ok
}

func (n *NFTOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (n *NFTOutput) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return outputOffsetVByteCost(rentStruct) +
// prefix + amount
rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt64ByteSize) +
Expand Down
18 changes: 9 additions & 9 deletions output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func copyObject(t *testing.T, source serializer.Serializable, mutations fieldMut
func TestOutputsSyntacticalDepositAmount(t *testing.T) {
nonZeroCostParas := &iotago.ProtocolParameters{
RentStructure: iotago.RentStructure{
VByteCost: 1,
VByteCost: 100,
VBFactorData: iotago.VByteCostFactorData,
VBFactorKey: iotago.VByteCostFactorKey,
},
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestOutputsSyntacticalDepositAmount(t *testing.T) {
protoParas: nonZeroCostParas,
outputs: iotago.Outputs{
&iotago.BasicOutput{
Amount: 426, // min amount
Amount: 42600, // min amount
Conditions: iotago.UnlockConditions{&iotago.AddressUnlockCondition{Address: tpkg.RandAliasAddress()}},
},
},
Expand All @@ -197,14 +197,14 @@ func TestOutputsSyntacticalDepositAmount(t *testing.T) {
name: "ok - storage deposit return",
protoParas: nonZeroCostParas,
outputs: iotago.Outputs{
// min 444
// min 46800
&iotago.BasicOutput{
Amount: 1000,
Amount: 100000,
Conditions: iotago.UnlockConditions{
&iotago.AddressUnlockCondition{Address: tpkg.RandAliasAddress()},
&iotago.StorageDepositReturnUnlockCondition{
ReturnAddress: tpkg.RandAliasAddress(),
Amount: 566, // 1000 - 444
Amount: 42600,
},
},
},
Expand All @@ -216,12 +216,12 @@ func TestOutputsSyntacticalDepositAmount(t *testing.T) {
protoParas: nonZeroCostParas,
outputs: iotago.Outputs{
&iotago.BasicOutput{
Amount: 1000,
Amount: 100000,
Conditions: iotago.UnlockConditions{
&iotago.AddressUnlockCondition{Address: tpkg.RandAliasAddress()},
&iotago.StorageDepositReturnUnlockCondition{
ReturnAddress: tpkg.RandAliasAddress(),
Amount: 413, // off by 1
Amount: 42600 - 1, // off by 1
},
},
},
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestOutputsSyntacticalDepositAmount(t *testing.T) {
protoParas: nonZeroCostParas,
outputs: iotago.Outputs{
&iotago.BasicOutput{
Amount: 100,
Amount: 42600 - 1,
Conditions: iotago.UnlockConditions{
&iotago.AddressUnlockCondition{Address: tpkg.RandAliasAddress()},
},
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestOutputsSyntacticalDepositAmount(t *testing.T) {
runErr = err
}
}
require.ErrorIs(t, runErr, tt.wantErr)
require.ErrorIs(t, runErr, tt.wantErr, tt.name)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion output_treasury.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *TreasuryOutput) Clone() Output {
return &TreasuryOutput{Amount: t.Amount}
}

func (t *TreasuryOutput) VBytes(_ *RentStructure, _ VBytesFunc) uint64 {
func (t *TreasuryOutput) VBytes(_ *RentStructure, _ VBytesFunc) VBytes {
return 0
}

Expand Down
15 changes: 9 additions & 6 deletions rent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/iotaledger/hive.go/serializer/v2"
)

// VBytes defines the type of the virtual byte costs.
type VBytes uint64

// VByteCostFactor defines the type of the virtual byte cost factor.
type VByteCostFactor byte

Expand All @@ -27,8 +30,8 @@ var (
)

// Multiply multiplies in with this factor.
func (factor VByteCostFactor) Multiply(in uint64) uint64 {
return uint64(factor) * in
func (factor VByteCostFactor) Multiply(in VBytes) VBytes {
return VBytes(factor) * in
}

// With joins two factors with each other.
Expand Down Expand Up @@ -133,13 +136,13 @@ func (r *RentStructure) CoversStateRent(object NonEphemeralObject, rent uint64)

// MinRent returns the minimum rent to cover a given object.
func (r *RentStructure) MinRent(object NonEphemeralObject) uint64 {
return uint64(r.VByteCost) * object.VBytes(r, nil)
return uint64(r.VByteCost) * uint64(object.VBytes(r, nil))
}

// MinStorageDepositForReturnOutput returns the minimum renting costs for an BasicOutput which returns
// a StorageDepositReturnUnlockCondition amount back to the origin sender.
func (r *RentStructure) MinStorageDepositForReturnOutput(sender Address) uint64 {
return (&BasicOutput{Conditions: UnlockConditions{&AddressUnlockCondition{Address: sender}}, Amount: 0}).VBytes(r, nil)
return uint64(r.VByteCost) * uint64((&BasicOutput{Conditions: UnlockConditions{&AddressUnlockCondition{Address: sender}}, Amount: 0}).VBytes(r, nil))
}

// NonEphemeralObject is an object which can not be pruned by nodes as it
Expand All @@ -150,8 +153,8 @@ type NonEphemeralObject interface {
// virtual and physical space within the data set needed to implement the IOTA protocol.
// The override parameter acts as an escape hatch in case the cost needs to be adjusted
// according to some external properties outside the NonEphemeralObject.
VBytes(rentStruct *RentStructure, override VBytesFunc) uint64
VBytes(rentStruct *RentStructure, override VBytesFunc) VBytes
}

// VBytesFunc is a function which computes the virtual byte cost of a NonEphemeralObject.
type VBytesFunc func(rentStruct *RentStructure) uint64
type VBytesFunc func(rentStruct *RentStructure) VBytes
2 changes: 1 addition & 1 deletion token_scheme_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *SimpleTokenScheme) Clone() TokenScheme {
}
}

func (s *SimpleTokenScheme) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *SimpleTokenScheme) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.OneByte) +
// minted/melted supply, max. supply
rentStruct.VBFactorData.Multiply(Uint256ByteSize+Uint256ByteSize+Uint256ByteSize)
Expand Down
4 changes: 2 additions & 2 deletions unlock_cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ type UnlockCondition interface {
// UnlockConditions is a slice of UnlockCondition(s).
type UnlockConditions []UnlockCondition

func (f UnlockConditions) VBytes(rentStruct *RentStructure, override VBytesFunc) uint64 {
var sumCost uint64
func (f UnlockConditions) VBytes(rentStruct *RentStructure, override VBytesFunc) VBytes {
var sumCost VBytes
for _, unlockCond := range f {
sumCost += unlockCond.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *AddressUnlockCondition) Clone() UnlockCondition {
return &AddressUnlockCondition{Address: s.Address.Clone()}
}

func (s *AddressUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *AddressUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize) +
s.Address.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *ExpirationUnlockCondition) Clone() UnlockCondition {
}
}

func (s *ExpirationUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *ExpirationUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt32ByteSize) +
s.ReturnAddress.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_governor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *GovernorAddressUnlockCondition) Clone() UnlockCondition {
return &GovernorAddressUnlockCondition{Address: s.Address.Clone()}
}

func (s *GovernorAddressUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *GovernorAddressUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize) +
s.Address.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_imm_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *ImmutableAliasUnlockCondition) Clone() UnlockCondition {
return &ImmutableAliasUnlockCondition{Address: s.Address.Clone().(*AliasAddress)}
}

func (s *ImmutableAliasUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *ImmutableAliasUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize) +
s.Address.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_state_ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *StateControllerAddressUnlockCondition) Clone() UnlockCondition {
return &StateControllerAddressUnlockCondition{Address: s.Address.Clone()}
}

func (s *StateControllerAddressUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *StateControllerAddressUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize) +
s.Address.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_storage_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *StorageDepositReturnUnlockCondition) Clone() UnlockCondition {
}
}

func (s *StorageDepositReturnUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *StorageDepositReturnUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt64ByteSize) +
s.ReturnAddress.VBytes(rentStruct, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion unlock_cond_timelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *TimelockUnlockCondition) Clone() UnlockCondition {
}
}

func (s *TimelockUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) uint64 {
func (s *TimelockUnlockCondition) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize + serializer.UInt32ByteSize)
}

Expand Down

0 comments on commit c78f869

Please sign in to comment.