Skip to content

Commit

Permalink
Merge branch 'develop' into feat/block-issuance-api
Browse files Browse the repository at this point in the history
  • Loading branch information
daria305 committed Oct 19, 2023
2 parents 2efd494 + bdd60ae commit 4720279
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 72 deletions.
63 changes: 35 additions & 28 deletions api/epoch_based_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"sync"
"time"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
Expand All @@ -19,16 +20,16 @@ type EpochBasedProvider struct {
latestAPIMutex sync.RWMutex
latestAPI iotago.API

currentAPIMutex sync.RWMutex
currentAPI iotago.API
committedAPIMutex sync.RWMutex
committedAPI iotago.API

currentSlotMutex sync.RWMutex
currentSlot iotago.SlotIndex
committedSlotMutex sync.RWMutex
committedSlot iotago.SlotIndex

optsAPIForMissingVersionCallback func(version iotago.Version) (iotago.API, error)
optsAPIForMissingVersionCallback func(protocolParameters iotago.ProtocolParameters) (iotago.API, error)
}

func WithAPIForMissingVersionCallback(callback func(version iotago.Version) (iotago.API, error)) options.Option[EpochBasedProvider] {
func WithAPIForMissingVersionCallback(callback func(protocolParameters iotago.ProtocolParameters) (iotago.API, error)) options.Option[EpochBasedProvider] {
return func(provider *EpochBasedProvider) {
provider.optsAPIForMissingVersionCallback = callback
}
Expand All @@ -42,17 +43,17 @@ func NewEpochBasedProvider(opts ...options.Option[EpochBasedProvider]) *EpochBas
}, opts)
}

func (e *EpochBasedProvider) SetCurrentSlot(slot iotago.SlotIndex) {
e.currentSlotMutex.Lock()
e.currentSlot = slot
e.currentSlotMutex.Unlock()
func (e *EpochBasedProvider) SetCommittedSlot(slot iotago.SlotIndex) {
e.committedSlotMutex.Lock()
e.committedSlot = slot
e.committedSlotMutex.Unlock()

e.updateCurrentAPI(slot)
e.updateCommittedAPI(slot)
}

func (e *EpochBasedProvider) updateCurrentAPI(slot iotago.SlotIndex) {
e.currentAPIMutex.Lock()
defer e.currentAPIMutex.Unlock()
func (e *EpochBasedProvider) updateCommittedAPI(slot iotago.SlotIndex) {
e.committedAPIMutex.Lock()
defer e.committedAPIMutex.Unlock()

latestAPI := e.LatestAPI()
if latestAPI == nil {
Expand All @@ -62,8 +63,8 @@ func (e *EpochBasedProvider) updateCurrentAPI(slot iotago.SlotIndex) {
epoch := latestAPI.TimeProvider().EpochFromSlot(slot)
version := e.VersionForEpoch(epoch)

if e.currentAPI == nil || version > e.currentAPI.ProtocolParameters().Version() {
e.currentAPI = lo.PanicOnErr(e.apiForVersion(version))
if e.committedAPI == nil || version > e.committedAPI.ProtocolParameters().Version() {
e.committedAPI = lo.PanicOnErr(e.apiForVersion(version))
}
}

Expand Down Expand Up @@ -91,10 +92,10 @@ func (e *EpochBasedProvider) AddVersion(version iotago.Version, epoch iotago.Epo
e.protocolVersions.Add(version, epoch)
e.mutex.Unlock()

e.currentSlotMutex.Lock()
defer e.currentSlotMutex.Unlock()
e.committedSlotMutex.Lock()
defer e.committedSlotMutex.Unlock()

e.updateCurrentAPI(e.currentSlot)
e.updateCommittedAPI(e.committedSlot)
}

func (e *EpochBasedProvider) AddFutureVersion(version iotago.Version, protocolParamsHash iotago.Identifier, epoch iotago.EpochIndex) {
Expand All @@ -118,7 +119,7 @@ func (e *EpochBasedProvider) apiForVersion(version iotago.Version) (iotago.API,
}

if e.optsAPIForMissingVersionCallback != nil {
return e.optsAPIForMissingVersionCallback(version)
return e.optsAPIForMissingVersionCallback(protocolParams)
}

return nil, ierrors.Errorf("no api available for parameters with version %d", protocolParams.Version())
Expand All @@ -131,6 +132,16 @@ func (e *EpochBasedProvider) APIForVersion(version iotago.Version) (iotago.API,
return e.apiForVersion(version)
}

func (e *EpochBasedProvider) APIForTime(t time.Time) iotago.API {
e.mutex.RLock()
defer e.mutex.RUnlock()

slot := e.latestAPI.TimeProvider().SlotFromTime(t)
epoch := e.latestAPI.TimeProvider().EpochFromSlot(slot)

return lo.PanicOnErr(e.apiForVersion(e.protocolVersions.VersionForEpoch(epoch)))
}

func (e *EpochBasedProvider) APIForSlot(slot iotago.SlotIndex) iotago.API {
e.mutex.RLock()
defer e.mutex.RUnlock()
Expand All @@ -154,11 +165,11 @@ func (e *EpochBasedProvider) LatestAPI() iotago.API {
return e.latestAPI
}

func (e *EpochBasedProvider) CurrentAPI() iotago.API {
e.currentAPIMutex.RLock()
defer e.currentAPIMutex.RUnlock()
func (e *EpochBasedProvider) CommittedAPI() iotago.API {
e.committedAPIMutex.RLock()
defer e.committedAPIMutex.RUnlock()

return e.currentAPI
return e.committedAPI
}

func (e *EpochBasedProvider) VersionsAndProtocolParametersHash() (iotago.Identifier, error) {
Expand Down Expand Up @@ -238,7 +249,3 @@ func (e *EpochBasedProvider) VersionForSlot(slot iotago.SlotIndex) iotago.Versio

return e.protocolVersions.VersionForEpoch(epoch)
}

func (e *EpochBasedProvider) IsFutureVersion(version iotago.Version) bool {
return e.CurrentAPI().Version() < version
}
8 changes: 7 additions & 1 deletion api/single_version_provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"time"

iotago "github.com/iotaledger/iota.go/v4"
)

Expand All @@ -16,6 +18,10 @@ func (t *singleVersionProvider) APIForVersion(iotago.Version) (iotago.API, error
return t.api, nil
}

func (t *singleVersionProvider) APIForTime(time.Time) iotago.API {
return t.api
}

func (t *singleVersionProvider) APIForSlot(iotago.SlotIndex) iotago.API {
return t.api
}
Expand All @@ -28,6 +34,6 @@ func (t *singleVersionProvider) LatestAPI() iotago.API {
return t.api
}

func (t *singleVersionProvider) CurrentAPI() iotago.API {
func (t *singleVersionProvider) CommittedAPI() iotago.API {
return t.api
}
9 changes: 7 additions & 2 deletions api_provider.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package iotago

import "time"

type APIProvider interface {
// APIForVersion returns the API for the given version.
APIForVersion(Version) (API, error)

// APIForTime returns the API for the given time.
APIForTime(time.Time) API

// APIForSlot returns the API for the given slot.
APIForSlot(SlotIndex) API

// APIForEpoch returns the API for the given epoch.
APIForEpoch(EpochIndex) API

// CurrentAPI returns the API for the current slot.
CurrentAPI() API
// CommittedAPI returns the API for the last committed slot.
CommittedAPI() API

// LatestAPI returns the API for the latest supported protocol version.
LatestAPI() API
Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestProtocolParametersJSONMarshalling(t *testing.T) {
iotago.WithRewardsOptions(8, 8, 31, 1154, 2, 1),
)

protoParamsJSON := `{"type":0,"version":3,"networkName":"xxxNetwork","bech32Hrp":"xxx","storageScoreParameters":{"storageCost":"6","factorData":7,"offsetOutput":"8","offsetEd25519BlockIssuerKey":"9","offsetStakingFeature":"10","offsetDelegation":"10"},"workScoreParameters":{"dataByte":1,"block":2,"input":3,"contextInput":4,"output":5,"nativeToken":6,"staking":7,"blockIssuer":8,"allotment":9,"signatureEd25519":10},"tokenSupply":"1234567890987654321","genesisUnixTimestamp":"1681373293","slotDurationInSeconds":10,"slotsPerEpochExponent":13,"manaParameters":{"bitsCount":1,"generationRate":1,"generationRateExponent":27,"decayFactors":[10,20],"decayFactorsExponent":32,"decayFactorEpochsSum":1337,"decayFactorEpochsSumExponent":20},"stakingUnbondingPeriod":11,"validationBlocksPerSlot":10,"punishmentEpochs":9,"livenessThresholdLowerBound":15,"livenessThresholdUpperBound":30,"minCommittableAge":10,"maxCommittableAge":20,"epochNearingThreshold":24,"congestionControlParameters":{"minReferenceManaCost":"500","increase":"500","decrease":"500","increaseThreshold":800000,"decreaseThreshold":500000,"schedulerRate":100000,"maxBufferSize":1000,"maxValidationBufferSize":100},"versionSignaling":{"windowSize":3,"windowTargetRatio":4,"activationOffset":1},"rewardsParameters":{"profitMarginExponent":8,"bootstrappingDuration":1154,"manaShareCoefficient":"2","decayBalancingConstantExponent":8,"decayBalancingConstant":"1","poolCoefficientExponent":31}}`
protoParamsJSON := `{"type":0,"version":3,"networkName":"xxxNetwork","bech32Hrp":"xxx","storageScoreParameters":{"storageCost":"6","factorData":7,"offsetOutputOverhead":"8","offsetEd25519BlockIssuerKey":"9","offsetStakingFeature":"10","offsetDelegation":"10"},"workScoreParameters":{"dataByte":1,"block":2,"input":3,"contextInput":4,"output":5,"nativeToken":6,"staking":7,"blockIssuer":8,"allotment":9,"signatureEd25519":10},"tokenSupply":"1234567890987654321","genesisUnixTimestamp":"1681373293","slotDurationInSeconds":10,"slotsPerEpochExponent":13,"manaParameters":{"bitsCount":1,"generationRate":1,"generationRateExponent":27,"decayFactors":[10,20],"decayFactorsExponent":32,"decayFactorEpochsSum":1337,"decayFactorEpochsSumExponent":20},"stakingUnbondingPeriod":11,"validationBlocksPerSlot":10,"punishmentEpochs":9,"livenessThresholdLowerBound":15,"livenessThresholdUpperBound":30,"minCommittableAge":10,"maxCommittableAge":20,"epochNearingThreshold":24,"congestionControlParameters":{"minReferenceManaCost":"500","increase":"500","decrease":"500","increaseThreshold":800000,"decreaseThreshold":500000,"schedulerRate":100000,"maxBufferSize":1000,"maxValidationBufferSize":100},"versionSignaling":{"windowSize":3,"windowTargetRatio":4,"activationOffset":1},"rewardsParameters":{"profitMarginExponent":8,"bootstrappingDuration":1154,"manaShareCoefficient":"2","decayBalancingConstantExponent":8,"decayBalancingConstant":"1","poolCoefficientExponent":31}}`

jsonProtoParams, err := tpkg.TestAPI.JSONEncode(protoParams)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions api_v3_protocol_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ func WithNetworkOptions(networkName string, bech32HRP NetworkPrefix) options.Opt
}
}

func WithSupplyOptions(totalSupply BaseToken, storageCost BaseToken, factorData StorageScoreFactor, offsetOutput, offsetEd25519BlockIssuerKey, offsetStakingFeature, offsetDelegation StorageScore) options.Option[V3ProtocolParameters] {
func WithSupplyOptions(totalSupply BaseToken, storageCost BaseToken, factorData StorageScoreFactor, offsetOutputOverhead, offsetEd25519BlockIssuerKey, offsetStakingFeature, offsetDelegation StorageScore) options.Option[V3ProtocolParameters] {
return func(p *V3ProtocolParameters) {
p.basicProtocolParameters.TokenSupply = totalSupply
p.basicProtocolParameters.StorageScoreParameters = StorageScoreParameters{
StorageCost: storageCost,
FactorData: factorData,
OffsetOutput: offsetOutput,
OffsetOutputOverhead: offsetOutputOverhead,
OffsetEd25519BlockIssuerKey: offsetEd25519BlockIssuerKey,
OffsetStakingFeature: offsetStakingFeature,
OffsetDelegation: offsetDelegation,
Expand Down
6 changes: 3 additions & 3 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func createBlockAtSlotWithPayload(t *testing.T, blockIndex, commitmentIndex iota
func TestProtocolBlock_ProtocolVersionSyntactical(t *testing.T) {
apiProvider := api.NewEpochBasedProvider(
api.WithAPIForMissingVersionCallback(
func(version iotago.Version) (iotago.API, error) {
return iotago.V3API(iotago.NewV3ProtocolParameters(iotago.WithVersion(version))), nil
func(parameters iotago.ProtocolParameters) (iotago.API, error) {
return iotago.V3API(iotago.NewV3ProtocolParameters(iotago.WithVersion(parameters.Version()))), nil
},
),
)
apiProvider.AddProtocolParametersAtEpoch(iotago.NewV3ProtocolParameters(), 0)
apiProvider.AddProtocolParametersAtEpoch(iotago.NewV3ProtocolParameters(iotago.WithVersion(4)), 3)

timeProvider := apiProvider.CurrentAPI().TimeProvider()
timeProvider := apiProvider.CommittedAPI().TimeProvider()

require.ErrorIs(t, createBlockAtSlotWithVersion(t, timeProvider.EpochStart(1), 2, apiProvider), iotago.ErrInvalidBlockVersion)

Expand Down
Loading

0 comments on commit 4720279

Please sign in to comment.