Skip to content

Commit

Permalink
Merge pull request #318 from iotaledger/feat/rewards
Browse files Browse the repository at this point in the history
Update performance tracker and rewards
  • Loading branch information
karimodm authored Sep 21, 2023
2 parents 376df37 + 02376ee commit 32eedbe
Show file tree
Hide file tree
Showing 22 changed files with 788 additions and 306 deletions.
18 changes: 9 additions & 9 deletions components/restapi/core/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"github.com/iotaledger/iota.go/v4/nodeclient/apimodels"
)

const (
DefaultPageSize = 50
RequestsMemoryCacheGranularity = 10
MaxRequestedSlotAge = 10
)

func congestionForAccountID(c echo.Context) (*apimodels.CongestionResponse, error) {
accountID, err := httpserver.ParseAccountIDParam(c, restapipkg.ParameterAccountID)
if err != nil {
Expand Down Expand Up @@ -79,13 +73,13 @@ func validators(c echo.Context) (*apimodels.ValidatorsResponse, error) {
}

// do not respond to really old requests
if requestedSlotIndex+MaxRequestedSlotAge < latestCommittedSlot {
if requestedSlotIndex+iotago.SlotIndex(restapi.ParamsRestAPI.MaxRequestedSlotAge) < latestCommittedSlot {
return nil, ierrors.Errorf("request is too old, request started at %d, latest committed slot index is %d", requestedSlotIndex, latestCommittedSlot)
}

nextEpoch := deps.Protocol.APIForSlot(latestCommittedSlot).TimeProvider().EpochFromSlot(latestCommittedSlot) + 1

slotRange := uint32(requestedSlotIndex / RequestsMemoryCacheGranularity)
slotRange := uint32(requestedSlotIndex) / restapi.ParamsRestAPI.RequestsMemoryCacheGranularity
registeredValidators, exists := deps.Protocol.MainEngineInstance().Retainer.RegisteredValidatorsCache(slotRange)
if !exists {
registeredValidators, err = deps.Protocol.MainEngineInstance().SybilProtection.OrderedRegisteredCandidateValidatorsList(nextEpoch)
Expand Down Expand Up @@ -175,11 +169,17 @@ func rewardsByOutputID(c echo.Context) (*apimodels.ManaRewardsResponse, error) {
case iotago.OutputDelegation:
//nolint:forcetypeassert
delegationOutput := utxoOutput.Output().(*iotago.DelegationOutput)
latestCommittedSlot := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment().Index()
stakingEnd := delegationOutput.EndEpoch
// the output is in delayed calaiming state if endEpoch is set, otherwise we use latest possible epoch
if delegationOutput.EndEpoch == 0 {
stakingEnd = deps.Protocol.APIForSlot(latestCommittedSlot).TimeProvider().EpochFromSlot(deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment().Index())
}
reward, actualStart, actualEnd, err = deps.Protocol.MainEngineInstance().SybilProtection.DelegatorReward(
delegationOutput.ValidatorID,
delegationOutput.DelegatedAmount,
delegationOutput.StartEpoch,
delegationOutput.EndEpoch,
stakingEnd,
)
}
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions components/restapi/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type ParametersRestAPI struct {
AllowIncompleteBlock bool `default:"false" usage:"whether the node allows to fill in incomplete block and issue it for user"`
// MaxPageSize defines the maximum number of results per page.
MaxPageSize uint32 `default:"100" usage:"the maximum number of results per page"`
// RequestsMemoryCacheGranularity defines per how many slots a cache is created for big API requests.
RequestsMemoryCacheGranularity uint32 `default:"10" usage:"defines per how many slots a cache is created for big API requests"`
// MaxRequestedSlotAge defines the maximum age of a request that will be processed.
MaxRequestedSlotAge uint32 `default:"10" usage:"the maximum age of a request that will be processed"`

JWTAuth struct {
// salt used inside the JWT tokens for the REST API. Change this to a different value to invalidate JWT tokens not matching this new value
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20230912172434-dc477e1f5140
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230919065227-618931c246c5
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230918132810-48814818bff9
github.com/iotaledger/iota.go/v4 v4.0.0-20230913143616-917572c7752d
github.com/iotaledger/iota.go/v4 v4.0.0-20230920081018-054aaef28ba2
github.com/labstack/echo/v4 v4.11.1
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.30.0
github.com/libp2p/go-libp2p-kad-dht v0.25.1
github.com/multiformats/go-multiaddr v0.11.0
github.com/multiformats/go-varint v0.0.7
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e
github.com/otiai10/copy v1.12.0
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98
Expand Down Expand Up @@ -134,7 +135,6 @@ require (
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/onsi/ginkgo/v2 v2.12.0 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/otiai10/copy v1.12.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230918132810-48814818bff9 h1:BJb6+f
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230918132810-48814818bff9/go.mod h1:B7gyJP6GshCSlEmY3CxEk5TZdsMs3UNz5U92hkFDdMs=
github.com/iotaledger/iota.go/v4 v4.0.0-20230913143616-917572c7752d h1:p9IchKq6kft758XDlnN/tAEXJMXGlmQPmbdxolba1gs=
github.com/iotaledger/iota.go/v4 v4.0.0-20230913143616-917572c7752d/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230920081018-054aaef28ba2 h1:N1w2b3d7r25Fj2WvHVeTyz1Imbw+dpHqrQ8p9GBWHhk=
github.com/iotaledger/iota.go/v4 v4.0.0-20230920081018-054aaef28ba2/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230920163540-ec641f64d740 h1:+/4iXhSsTfOgSqgFluciagiOrBN6GUfK4vhI4wHlRMU=
github.com/iotaledger/iota.go/v4 v4.0.0-20230920163540-ec641f64d740/go.mod h1:DWCa+mXRTGWBV0EHVuvToUxAEcICe2Pab9hBlxBamKo=
github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY=
github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down Expand Up @@ -511,6 +515,8 @@ github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e h1:s2RNOM/IGd
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e/go.mod h1:nBdnFKj15wFbf94Rwfq4m30eAcyY9V/IyKAGQFtqkW0=
github.com/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY=
github.com/otiai10/copy v1.12.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
github.com/otiai10/mint v1.5.1/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c h1:Gcce/r5tSQeprxswXXOwQ/RBU1bjQWVd9dB7QKoPXBE=
Expand Down
37 changes: 37 additions & 0 deletions pkg/model/validator_performance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package model

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

type ValidatorPerformance struct {
// works if ValidatorBlocksPerSlot is less than 32 because we use it as bit vector
SlotActivityVector uint32 `serix:"0"`
// can be uint8 because max count per slot is maximally ValidatorBlocksPerSlot + 1
BlockIssuedCount uint8 `serix:"1"`
HighestSupportedVersionAndHash VersionAndHash `serix:"2"`
}

func NewValidatorPerformance() *ValidatorPerformance {
return &ValidatorPerformance{
SlotActivityVector: 0,
BlockIssuedCount: 0,
HighestSupportedVersionAndHash: VersionAndHash{},
}
}

func ValidatorPerformanceFromBytes(decodeAPI iotago.API) func([]byte) (*ValidatorPerformance, int, error) {
return func(bytes []byte) (*ValidatorPerformance, int, error) {
validatorPerformance := new(ValidatorPerformance)
consumedBytes, err := decodeAPI.Decode(bytes, validatorPerformance)
if err != nil {
return nil, 0, err
}

return validatorPerformance, consumedBytes, nil
}
}

func (p *ValidatorPerformance) Bytes(api iotago.API) ([]byte, error) {
return api.Encode(p)
}
4 changes: 2 additions & 2 deletions pkg/model/version_and_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
const VersionAndHashSize = iotago.IdentifierLength + iotago.VersionLength

type VersionAndHash struct {
Version iotago.Version
Hash iotago.Identifier
Version iotago.Version `serix:"0"`
Hash iotago.Identifier `serix:"1"`
}

func (v VersionAndHash) Bytes() ([]byte, error) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/protocol/engine/notarization/slotnotarization/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ func (m *Manager) createCommitment(index iotago.SlotIndex) (success bool) {
return false
}

committeeRoot, rewardsRoot := m.sybilProtection.CommitSlot(index)
committeeRoot, rewardsRoot, err := m.sybilProtection.CommitSlot(index)
if err != nil {
m.errorHandler(ierrors.Wrap(err, "failed to commit sybil protection"))
return false
}
apiForSlot := m.apiProvider.APIForSlot(index)

protocolParametersAndVersionsHash, err := m.upgradeOrchestrator.Commit(index)
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/sybilprotection/sybilprotection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type SybilProtection interface {
// The actual used epoch range is returned, only until usedEnd the decay was applied.
DelegatorReward(validatorID iotago.AccountID, delegatedAmount iotago.BaseToken, epochStart, epochEnd iotago.EpochIndex) (delegatorsReward iotago.Mana, decayedStart, decayedEnd iotago.EpochIndex, err error)
SeatManager() seatmanager.SeatManager
CommitSlot(iotago.SlotIndex) (iotago.Identifier, iotago.Identifier)
CommitSlot(iotago.SlotIndex) (iotago.Identifier, iotago.Identifier, error)
Import(io.ReadSeeker) error
Export(io.WriteSeeker, iotago.SlotIndex) error

Expand Down
Loading

0 comments on commit 32eedbe

Please sign in to comment.