Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat/protocol-evic…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
karimodm committed Dec 12, 2023
2 parents 67a1b36 + abf24f2 commit 542832e
Show file tree
Hide file tree
Showing 55 changed files with 989 additions and 551 deletions.
12 changes: 10 additions & 2 deletions components/debugapi/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ func prepareCommitmentGraph(g *graphviz.Graphviz, rootCommitment *protocol.Commi
}

func createNode(graph *cgraph.Graph, commitment *protocol.Commitment) (*cgraph.Node, error) {
node, err := graph.Node(fmt.Sprintf("%d: %s", commitment.ID().Slot(), commitment.ID().String()[:8]))
node, err := graph.CreateNode(fmt.Sprintf("%d-%s", commitment.ID().Slot(), commitment.ID().Identifier().String()[:8]))
if err != nil {
return nil, ierrors.Wrapf(err, "could not create node %s", commitment.ID().String()[:8])
return nil, ierrors.Wrapf(err, "could not retrieve node %s", commitment.ID().Identifier().String()[:8])
}
if node != nil {
return node, nil
}

node, err = graph.CreateNode(fmt.Sprintf("%d-%s", commitment.ID().Slot(), commitment.ID().Identifier().String()[:8]))
if err != nil {
return nil, ierrors.Wrapf(err, "could not create node %s", commitment.ID().Identifier().String()[:8])
}

return node, nil
Expand Down
3 changes: 2 additions & 1 deletion components/inx/server_commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/workerpool"
inx "github.com/iotaledger/inx/go"
"github.com/iotaledger/iota-core/pkg/model"
Expand Down Expand Up @@ -142,7 +143,7 @@ func (s *Server) ListenToCommitments(req *inx.SlotRangeRequest, srv inx.INX_List
case done:
cancel()
}
}).Unhook
}, event.WithWorkerPool(wp)).Unhook

<-ctx.Done()
unhook()
Expand Down
2 changes: 1 addition & 1 deletion components/inx/server_utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (s *Server) ListenToLedgerUpdates(req *inx.SlotRangeRequest, srv inx.INX_Li
case done:
cancel()
}
}).Unhook
}, event.WithWorkerPool(wp)).Unhook

<-ctx.Done()
unhook()
Expand Down
27 changes: 16 additions & 11 deletions components/restapi/core/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
}

var reward iotago.Mana
var actualStart, actualEnd iotago.EpochIndex
var firstRewardEpoch, lastRewardEpoch iotago.EpochIndex
switch utxoOutput.OutputType() {
case iotago.OutputAccount:
//nolint:forcetypeassert
Expand All @@ -198,42 +198,47 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
//nolint:forcetypeassert
stakingFeature := feature.(*iotago.StakingFeature)

apiForSlot := deps.Protocol.APIForSlot(slotIndex)
futureBoundedSlotIndex := slotIndex + apiForSlot.ProtocolParameters().MinCommittableAge()
claimingEpoch := apiForSlot.TimeProvider().EpochFromSlot(futureBoundedSlotIndex)

// check if the account is a validator
reward, actualStart, actualEnd, err = deps.Protocol.Engines.Main.Get().SybilProtection.ValidatorReward(
reward, firstRewardEpoch, lastRewardEpoch, err = deps.Protocol.Engines.Main.Get().SybilProtection.ValidatorReward(
accountOutput.AccountID,
stakingFeature.StakedAmount,
stakingFeature.StartEpoch,
stakingFeature.EndEpoch,
stakingFeature,
claimingEpoch,
)

case iotago.OutputDelegation:
//nolint:forcetypeassert
delegationOutput := utxoOutput.Output().(*iotago.DelegationOutput)
delegationEnd := delegationOutput.EndEpoch
apiForSlot := deps.Protocol.APIForSlot(slotIndex)
futureBoundedSlotIndex := slotIndex + apiForSlot.ProtocolParameters().MinCommittableAge()
claimingEpoch := apiForSlot.TimeProvider().EpochFromSlot(futureBoundedSlotIndex)
// If Delegation ID is zeroed, the output is in delegating state, which means its End Epoch is not set and we must use the
// "last epoch" for the rewards calculation.
// In this case the calculation must be consistent with the rewards calculation at execution time, so a client can specify
// a slot index explicitly, which should be equal to the slot it uses as the commitment input for the claiming transaction.
if delegationOutput.DelegationID.Empty() {
apiForSlot := deps.Protocol.APIForSlot(slotIndex)
futureBoundedSlotIndex := slotIndex + apiForSlot.ProtocolParameters().MinCommittableAge()
delegationEnd = apiForSlot.TimeProvider().EpochFromSlot(futureBoundedSlotIndex) - iotago.EpochIndex(1)
delegationEnd = claimingEpoch - iotago.EpochIndex(1)
}

reward, actualStart, actualEnd, err = deps.Protocol.Engines.Main.Get().SybilProtection.DelegatorReward(
reward, firstRewardEpoch, lastRewardEpoch, err = deps.Protocol.Engines.Main.Get().SybilProtection.DelegatorReward(
delegationOutput.ValidatorAddress.AccountID(),
delegationOutput.DelegatedAmount,
delegationOutput.StartEpoch,
delegationEnd,
claimingEpoch,
)
}
if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to calculate reward for output %s: %s", outputID.ToHex(), err)
}

return &api.ManaRewardsResponse{
StartEpoch: actualStart,
EndEpoch: actualEnd,
StartEpoch: firstRewardEpoch,
EndEpoch: lastRewardEpoch,
Rewards: reward,
}, nil
}
Expand Down
28 changes: 23 additions & 5 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package core

import (
"time"

"github.com/labstack/echo/v4"

"github.com/iotaledger/hive.go/ierrors"
Expand Down Expand Up @@ -84,12 +86,28 @@ func blockIssuance() (*api.IssuanceBlockHeaderResponse, error) {
return nil, ierrors.Wrap(echo.ErrServiceUnavailable, "no strong parents available")
}

// get the latest parent block issuing time
var latestParentBlockIssuingTime time.Time
for _, parentType := range []iotago.ParentsType{iotago.StrongParentType, iotago.WeakParentType, iotago.ShallowLikeParentType} {
for _, blockID := range references[parentType] {
block, exists := deps.Protocol.Engines.Main.Get().Block(blockID)
if !exists {
return nil, ierrors.Wrapf(echo.ErrNotFound, "no block found for parent, block ID: %s", blockID.ToHex())
}

if latestParentBlockIssuingTime.Before(block.ProtocolBlock().Header.IssuingTime) {
latestParentBlockIssuingTime = block.ProtocolBlock().Header.IssuingTime
}
}
}

resp := &api.IssuanceBlockHeaderResponse{
StrongParents: references[iotago.StrongParentType],
WeakParents: references[iotago.WeakParentType],
ShallowLikeParents: references[iotago.ShallowLikeParentType],
LatestFinalizedSlot: deps.Protocol.Engines.Main.Get().SyncManager.LatestFinalizedSlot(),
LatestCommitment: deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment().Commitment(),
StrongParents: references[iotago.StrongParentType],
WeakParents: references[iotago.WeakParentType],
ShallowLikeParents: references[iotago.ShallowLikeParentType],
LatestParentBlockIssuingTime: latestParentBlockIssuingTime,
LatestFinalizedSlot: deps.Protocol.Engines.Main.Get().SyncManager.LatestFinalizedSlot(),
LatestCommitment: deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment().Commitment(),
}

return resp, nil
Expand Down
30 changes: 30 additions & 0 deletions components/restapi/core/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,33 @@ func getUTXOChanges(commitmentID iotago.CommitmentID) (*api.UTXOChangesResponse,
ConsumedOutputs: consumedOutputs,
}, nil
}

func getUTXOChangesFull(commitmentID iotago.CommitmentID) (*api.UTXOChangesFullResponse, error) {
diffs, err := deps.Protocol.Engines.Main.Get().Ledger.SlotDiffs(commitmentID.Slot())
if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get slot diffs, commitmentID: %s, slot: %d, error: %w", commitmentID, commitmentID.Slot(), err)
}

createdOutputs := make([]*api.OutputWithID, len(diffs.Outputs))
consumedOutputs := make([]*api.OutputWithID, len(diffs.Spents))

for i, output := range diffs.Outputs {
createdOutputs[i] = &api.OutputWithID{
OutputID: output.OutputID(),
Output: output.Output(),
}
}

for i, output := range diffs.Spents {
consumedOutputs[i] = &api.OutputWithID{
OutputID: output.OutputID(),
Output: output.Output().Output(),
}
}

return &api.UTXOChangesFullResponse{
CommitmentID: commitmentID,
CreatedOutputs: createdOutputs,
ConsumedOutputs: consumedOutputs,
}, nil
}
39 changes: 39 additions & 0 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ func configure() error {
return responseByHeader(c, resp)
})

routeGroup.GET(api.EndpointWithEchoParameters(api.CoreEndpointCommitmentByIDUTXOChangesFull), func(c echo.Context) error {
commitmentID, err := httpserver.ParseCommitmentIDParam(c, api.ParameterCommitmentID)
if err != nil {
return err
}

// load the commitment to check if it matches the given commitmentID
commitment, err := getCommitmentByID(commitmentID)
if err != nil {
return err
}

resp, err := getUTXOChangesFull(commitment.ID())
if err != nil {
return err
}

return responseByHeader(c, resp)
})

routeGroup.GET(api.EndpointWithEchoParameters(api.CoreEndpointCommitmentBySlot), func(c echo.Context) error {
index, err := httpserver.ParseSlotParam(c, api.ParameterSlot)
if err != nil {
Expand Down Expand Up @@ -175,6 +195,25 @@ func configure() error {
return responseByHeader(c, resp)
})

routeGroup.GET(api.EndpointWithEchoParameters(api.CoreEndpointCommitmentBySlotUTXOChangesFull), func(c echo.Context) error {
slot, err := httpserver.ParseSlotParam(c, api.ParameterSlot)
if err != nil {
return err
}

commitment, err := getCommitmentBySlot(slot)
if err != nil {
return err
}

resp, err := getUTXOChangesFull(commitment.ID())
if err != nil {
return err
}

return responseByHeader(c, resp)
})

routeGroup.GET(api.EndpointWithEchoParameters(api.CoreEndpointOutput), func(c echo.Context) error {
resp, err := outputByID(c)
if err != nil {
Expand Down
36 changes: 18 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/iotaledger/hive.go/ads v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/app v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/constraints v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231205131244-472357435a39
github.com/iotaledger/hive.go/crypto v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/ds v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/ierrors v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/app v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/constraints v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/crypto v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/ds v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/ierrors v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/kvstore v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/lo v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/log v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/logger v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/runtime v0.0.0-20231205131244-472357435a39
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231205131244-472357435a39
github.com/iotaledger/hive.go/stringify v0.0.0-20231205131244-472357435a39
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d
github.com/iotaledger/hive.go/lo v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/log v0.0.0-20231206113509-4b4ff95ac61c
github.com/iotaledger/hive.go/logger v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/runtime v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231206114953-6a65a82e30ad
github.com/iotaledger/hive.go/stringify v0.0.0-20231206114953-6a65a82e30ad
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231206124511-b78dc962031f
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231206124145-f773dfe3927e
github.com/iotaledger/iota.go/v4 v4.0.0-20231206123921-2af411eef0b5
github.com/labstack/echo/v4 v4.11.3
github.com/labstack/gommon v0.4.1
github.com/libp2p/go-libp2p v0.32.0
Expand Down Expand Up @@ -140,7 +140,7 @@ require (
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/petermattis/goid v0.0.0-20231126143041-f558c26febf5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pokt-network/smt v0.6.1 // indirect
Expand All @@ -155,7 +155,7 @@ require (
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand All @@ -175,7 +175,7 @@ require (
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
Expand Down
Loading

0 comments on commit 542832e

Please sign in to comment.