Skip to content

Commit

Permalink
Merge pull request #471 from iotaledger/feat/vbytes-factor-issuer-val…
Browse files Browse the repository at this point in the history
…idator

Introduce VBytes factor for block issuer keys and staking.
  • Loading branch information
piotrm50 authored Aug 4, 2023
2 parents 5a196b9 + 59ff31d commit 7e29753
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 30 deletions.
4 changes: 3 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func TestProtocolParametersJSONMarshalling(t *testing.T) {
6,
7,
8,
9,
10,
),
iotago.WithWorkScoreOptions(
1,
Expand Down Expand Up @@ -120,7 +122,7 @@ func TestProtocolParametersJSONMarshalling(t *testing.T) {
iotago.WithVersionSignalingOptions(3, 4, 1),
)

protoParamsJSON := `{"type":0,"version":3,"networkName":"xxxNetwork","bech32Hrp":"xxx","rentStructure":{"vByteCost":6,"vByteFactorData":7,"vByteFactorKey":8},"workScoreStructure":{"dataByte":1,"block":2,"missingParent":3,"input":4,"contextInput":5,"output":6,"nativeToken":7,"staking":8,"blockIssuer":9,"allotment":10,"signatureEd25519":11,"minStrongParentsThreshold":12},"tokenSupply":"1234567890987654321","genesisUnixTimestamp":"1681373293","slotDurationInSeconds":10,"slotsPerEpochExponent":13,"manaGenerationRate":1,"manaGenerationRateExponent":27,"manaDecayFactors":[10,20],"manaDecayFactorsExponent":32,"manaDecayFactorEpochsSum":1337,"manaDecayFactorEpochsSumExponent":20,"stakingUnbondingPeriod":"11","livenessThreshold":"3","minCommittableAge":"10","maxCommittableAge":"20","epochNearingThreshold":"24","versionSignaling":{"windowSize":3,"windowTargetRatio":4,"activationOffset":1}}`
protoParamsJSON := `{"type":0,"version":3,"networkName":"xxxNetwork","bech32Hrp":"xxx","rentStructure":{"vByteCost":6,"vByteFactorData":7,"vByteFactorKey":8,"vByteFactorIssuerKeys":9,"vByteFactorStakingFeature":10},"workScoreStructure":{"dataByte":1,"block":2,"missingParent":3,"input":4,"contextInput":5,"output":6,"nativeToken":7,"staking":8,"blockIssuer":9,"allotment":10,"signatureEd25519":11,"minStrongParentsThreshold":12},"tokenSupply":"1234567890987654321","genesisUnixTimestamp":"1681373293","slotDurationInSeconds":10,"slotsPerEpochExponent":13,"manaGenerationRate":1,"manaGenerationRateExponent":27,"manaDecayFactors":[10,20],"manaDecayFactorsExponent":32,"manaDecayFactorEpochsSum":1337,"manaDecayFactorEpochsSumExponent":20,"stakingUnbondingPeriod":"11","livenessThreshold":"3","minCommittableAge":"10","maxCommittableAge":"20","epochNearingThreshold":"24","versionSignaling":{"windowSize":3,"windowTargetRatio":4,"activationOffset":1}}`

jsonProtoParams, err := tpkg.TestAPI.JSONEncode(protoParams)
require.NoError(t, err)
Expand Down
12 changes: 7 additions & 5 deletions api_v3_protocol_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewV3ProtocolParameters(opts ...options.Option[V3ProtocolParameters]) *V3Pr
append([]options.Option[V3ProtocolParameters]{
WithVersion(apiV3Version),
WithNetworkOptions("testnet", PrefixTestnet),
WithSupplyOptions(1813620509061365, 100, 1, 10),
WithSupplyOptions(1813620509061365, 100, 1, 10, 100, 100),
WithWorkScoreOptions(1, 100, 500, 20, 20, 20, 20, 100, 100, 100, 200, 4),
WithTimeProviderOptions(time.Now().Unix(), 10, 13),
// TODO: add sane default values
Expand Down Expand Up @@ -163,13 +163,15 @@ func WithNetworkOptions(networkName string, bech32HRP NetworkPrefix) options.Opt
}
}

func WithSupplyOptions(totalSupply BaseToken, vByteCost uint32, vBFactorData VByteCostFactor, vBFactorKey VByteCostFactor) options.Option[V3ProtocolParameters] {
func WithSupplyOptions(totalSupply BaseToken, vByteCost uint32, vBFactorData, vBFactorKey, vBFactorIssuerKeys, vBFactorStakingFeature VByteCostFactor) options.Option[V3ProtocolParameters] {
return func(p *V3ProtocolParameters) {
p.basicProtocolParameters.TokenSupply = totalSupply
p.basicProtocolParameters.RentStructure = RentStructure{
VByteCost: vByteCost,
VBFactorData: vBFactorData,
VBFactorKey: vBFactorKey,
VByteCost: vByteCost,
VBFactorData: vBFactorData,
VBFactorKey: vBFactorKey,
VBFactorIssuerKeys: vBFactorIssuerKeys,
VBFactorStakingFeature: vBFactorStakingFeature,
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions feat_blockissuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ func (s *BlockIssuerFeature) Clone() Feature {
}

func (s *BlockIssuerFeature) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
// TODO: add factor for block issuer keys (higher than regular keys factor).
// VBFactorData: type prefix + expiry slot + keys length
// VBFactorKey: numKeys * pubKeyLength
// VBFactorIssuerKeys: numKeys * pubKeyLength
return rentStruct.VBFactorData.Multiply(serializer.SmallTypeDenotationByteSize+serializer.UInt64ByteSize+serializer.OneByte) +
rentStruct.VBFactorKey.Multiply(VBytes(len(s.BlockIssuerKeys))*(ed25519.PublicKeySize))
rentStruct.VBFactorIssuerKeys.Multiply(VBytes(len(s.BlockIssuerKeys))*(ed25519.PublicKeySize))
}

func (s *BlockIssuerFeature) WorkScore(workScoreStructure *WorkScoreStructure) (WorkScore, error) {
Expand Down
9 changes: 5 additions & 4 deletions feat_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func (s *StakingFeature) Clone() Feature {
return &StakingFeature{StakedAmount: s.StakedAmount, FixedCost: s.FixedCost, StartEpoch: s.StartEpoch, EndEpoch: s.EndEpoch}
}

func (s *StakingFeature) VBytes(rentStruct *RentStructure, _ VBytesFunc) VBytes {
vbytes := s.Size()
func (s *StakingFeature) VBytes(rentStruct *RentStructure, f VBytesFunc) VBytes {
if f != nil {
return f(rentStruct)
}

// TODO: Introduce another vbyte factor for the staking feature.
return rentStruct.VBFactorData.Multiply(VBytes(vbytes))
return rentStruct.VBFactorStakingFeature.Multiply(VBytes(s.Size()))
}

func (s *StakingFeature) WorkScore(workScoreStructure *WorkScoreStructure) (WorkScore, error) {
Expand Down
2 changes: 1 addition & 1 deletion nodeclient/apimodels/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Test_InfoResponse(t *testing.T) {
jsonResponse, err := api.JSONEncode(response)
require.NoError(t, err)

expected := "{\"name\":\"test\",\"version\":\"2.0.0\",\"status\":{\"isHealthy\":false,\"acceptedTangleTime\":\"2023-08-01T08:45:05Z\",\"relativeAcceptedTangleTime\":\"2023-08-01T08:45:05Z\",\"confirmedTangleTime\":\"2023-08-01T08:45:05Z\",\"relativeConfirmedTangleTime\":\"2023-08-01T08:45:05Z\",\"latestCommitmentId\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"latestFinalizedSlot\":\"1\",\"latestAcceptedBlockSlot\":\"2\",\"latestConfirmedBlockSlot\":\"3\",\"pruningSlot\":\"4\"},\"metrics\":{\"blocksPerSecond\":\"1.1E+00\",\"confirmedBlocksPerSecond\":\"2.2E+00\",\"confirmationRate\":\"3.3E+00\"},\"protocolParameters\":[{\"startEpoch\":\"0\",\"parameters\":{\"type\":0,\"version\":3,\"networkName\":\"testnet\",\"bech32Hrp\":\"rms\",\"rentStructure\":{\"vByteCost\":100,\"vByteFactorData\":1,\"vByteFactorKey\":10},\"workScoreStructure\":{\"dataByte\":1,\"block\":100,\"missingParent\":500,\"input\":20,\"contextInput\":20,\"output\":20,\"nativeToken\":20,\"staking\":100,\"blockIssuer\":100,\"allotment\":100,\"signatureEd25519\":200,\"minStrongParentsThreshold\":4},\"tokenSupply\":\"1813620509061365\",\"genesisUnixTimestamp\":\"1690879505\",\"slotDurationInSeconds\":10,\"slotsPerEpochExponent\":13,\"manaGenerationRate\":1,\"manaGenerationRateExponent\":0,\"manaDecayFactors\":[10,20],\"manaDecayFactorsExponent\":0,\"manaDecayFactorEpochsSum\":0,\"manaDecayFactorEpochsSumExponent\":0,\"stakingUnbondingPeriod\":\"10\",\"livenessThreshold\":\"3\",\"minCommittableAge\":\"10\",\"maxCommittableAge\":\"20\",\"epochNearingThreshold\":\"24\",\"versionSignaling\":{\"windowSize\":7,\"windowTargetRatio\":5,\"activationOffset\":7}}}],\"baseToken\":{\"name\":\"Shimmer\",\"tickerSymbol\":\"SMR\",\"unit\":\"SMR\",\"subunit\":\"glow\",\"decimals\":6,\"useMetricPrefix\":false},\"features\":[\"test\"]}"
expected := "{\"name\":\"test\",\"version\":\"2.0.0\",\"status\":{\"isHealthy\":false,\"acceptedTangleTime\":\"2023-08-01T08:45:05Z\",\"relativeAcceptedTangleTime\":\"2023-08-01T08:45:05Z\",\"confirmedTangleTime\":\"2023-08-01T08:45:05Z\",\"relativeConfirmedTangleTime\":\"2023-08-01T08:45:05Z\",\"latestCommitmentId\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"latestFinalizedSlot\":\"1\",\"latestAcceptedBlockSlot\":\"2\",\"latestConfirmedBlockSlot\":\"3\",\"pruningSlot\":\"4\"},\"metrics\":{\"blocksPerSecond\":\"1.1E+00\",\"confirmedBlocksPerSecond\":\"2.2E+00\",\"confirmationRate\":\"3.3E+00\"},\"protocolParameters\":[{\"startEpoch\":\"0\",\"parameters\":{\"type\":0,\"version\":3,\"networkName\":\"testnet\",\"bech32Hrp\":\"rms\",\"rentStructure\":{\"vByteCost\":100,\"vByteFactorData\":1,\"vByteFactorKey\":10,\"vByteFactorIssuerKeys\":100,\"vByteFactorStakingFeature\":100},\"workScoreStructure\":{\"dataByte\":1,\"block\":100,\"missingParent\":500,\"input\":20,\"contextInput\":20,\"output\":20,\"nativeToken\":20,\"staking\":100,\"blockIssuer\":100,\"allotment\":100,\"signatureEd25519\":200,\"minStrongParentsThreshold\":4},\"tokenSupply\":\"1813620509061365\",\"genesisUnixTimestamp\":\"1690879505\",\"slotDurationInSeconds\":10,\"slotsPerEpochExponent\":13,\"manaGenerationRate\":1,\"manaGenerationRateExponent\":0,\"manaDecayFactors\":[10,20],\"manaDecayFactorsExponent\":0,\"manaDecayFactorEpochsSum\":0,\"manaDecayFactorEpochsSumExponent\":0,\"stakingUnbondingPeriod\":\"10\",\"livenessThreshold\":\"3\",\"minCommittableAge\":\"10\",\"maxCommittableAge\":\"20\",\"epochNearingThreshold\":\"24\",\"versionSignaling\":{\"windowSize\":7,\"windowTargetRatio\":5,\"activationOffset\":7}}}],\"baseToken\":{\"name\":\"Shimmer\",\"tickerSymbol\":\"SMR\",\"unit\":\"SMR\",\"subunit\":\"glow\",\"decimals\":6,\"useMetricPrefix\":false},\"features\":[\"test\"]}"
require.Equal(t, expected, string(jsonResponse))

decoded := new(apimodels.InfoResponse)
Expand Down
2 changes: 1 addition & 1 deletion nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
var (
protoParams = iotago.NewV3ProtocolParameters(
iotago.WithNetworkOptions("alphanet", "atoi"),
iotago.WithSupplyOptions(tpkg.TestTokenSupply, 500, 1, 10),
iotago.WithSupplyOptions(tpkg.TestTokenSupply, 500, 1, 10, 100, 100),
)

mockAPI = iotago.V3API(protoParams)
Expand Down
2 changes: 1 addition & 1 deletion output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestOutputsDeSerialize(t *testing.T) {

func TestOutputsSyntacticalDepositAmount(t *testing.T) {
nonZeroCostParams := iotago.NewV3ProtocolParameters(
iotago.WithSupplyOptions(tpkg.TestTokenSupply, 100, iotago.VByteCostFactorData, iotago.VByteCostFactorKey),
iotago.WithSupplyOptions(tpkg.TestTokenSupply, 100, 1, 10, 10, 10),
)

tests := []struct {
Expand Down
13 changes: 5 additions & 8 deletions rent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ type VBytes uint64
// VByteCostFactor defines the type of the virtual byte cost factor.
type VByteCostFactor byte

const (
// VByteCostFactorData defines the multiplier for data fields.
VByteCostFactorData VByteCostFactor = 1
// VByteCostFactorKey defines the multiplier for fields which can act as keys for lookups.
VByteCostFactorKey VByteCostFactor = 10
)

var (
// ErrVByteDepositNotCovered gets returned when a NonEphemeralObject does not cover the minimum deposit
// which is calculated from its virtual byte costs.
Expand All @@ -43,8 +36,12 @@ type RentStructure struct {
VByteCost uint32 `serix:"0,mapKey=vByteCost"`
// Defines the factor to be used for data only fields.
VBFactorData VByteCostFactor `serix:"1,mapKey=vByteFactorData"`
// defines the factor to be used for key/lookup generating fields.
// Defines the factor to be used for key/lookup generating fields.
VBFactorKey VByteCostFactor `serix:"2,mapKey=vByteFactorKey"`
// Defines the factor to be used for block issuer feature public keys.
VBFactorIssuerKeys VByteCostFactor `serix:"3,mapKey=vByteFactorIssuerKeys"`
// Defines the factor to be used for staking feature.
VBFactorStakingFeature VByteCostFactor `serix:"4,mapKey=vByteFactorStakingFeature"`
}

// CoversMinDeposit tells whether given this NonEphemeralObject, the base token amount fulfills the deposit requirements
Expand Down
10 changes: 6 additions & 4 deletions rent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ func TestRentStructure_DeSerialize(t *testing.T) {

func TestRentStructureJSONMarshalling(t *testing.T) {
rentStructure := &iotago.RentStructure{
VByteCost: 500,
VBFactorData: 1,
VBFactorKey: 10,
VByteCost: 500,
VBFactorData: 1,
VBFactorKey: 10,
VBFactorIssuerKeys: 50,
VBFactorStakingFeature: 100,
}
rentStructureJSON := `{"vByteCost":500,"vByteFactorData":1,"vByteFactorKey":10}`
rentStructureJSON := `{"vByteCost":500,"vByteFactorData":1,"vByteFactorKey":10,"vByteFactorIssuerKeys":50,"vByteFactorStakingFeature":100}`

j, err := tpkg.TestAPI.JSONEncode(rentStructure)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion tpkg/test_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
var TestAPI = iotago.V3API(
iotago.NewV3ProtocolParameters(
iotago.WithNetworkOptions("TestJungle", "tgl"),
iotago.WithSupplyOptions(TestTokenSupply, 0, 0, 0),
iotago.WithSupplyOptions(TestTokenSupply, 0, 0, 0, 0, 0),
),
)

Expand Down
2 changes: 2 additions & 0 deletions tpkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ func RandProtocolParameters() iotago.ProtocolParameters {
RandUint32(math.MaxUint32),
iotago.VByteCostFactor(RandUint8(math.MaxUint8)),
iotago.VByteCostFactor(RandUint8(math.MaxUint8)),
iotago.VByteCostFactor(RandUint8(math.MaxUint8)),
iotago.VByteCostFactor(RandUint8(math.MaxUint8)),
),
iotago.WithWorkScoreOptions(
RandWorkScore(math.MaxUint32),
Expand Down
2 changes: 1 addition & 1 deletion vm/stardust/vm_stardust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

testProtoParams = iotago.NewV3ProtocolParameters(
iotago.WithNetworkOptions("test", "test"),
iotago.WithSupplyOptions(tpkg.TestTokenSupply, 100, 1, 10),
iotago.WithSupplyOptions(tpkg.TestTokenSupply, 100, 1, 10, 100, 100),
iotago.WithWorkScoreOptions(1, 100, 500, 20, 20, 20, 20, 100, 100, 100, 200, 4),
iotago.WithTimeProviderOptions(100, slotDurationSeconds, slotsPerEpochExponent),
iotago.WithManaOptions(generationRate,
Expand Down

0 comments on commit 7e29753

Please sign in to comment.