Skip to content

Commit

Permalink
Merge pull request #538 from iotaledger/fix/congestionapi
Browse files Browse the repository at this point in the history
Fix Congestion endpoint to use AccountAddress
  • Loading branch information
alexsporn authored Nov 21, 2023
2 parents 0e57f8b + f4c0817 commit 909a664
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
34 changes: 25 additions & 9 deletions components/restapi/core/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ import (
"github.com/iotaledger/iota.go/v4/nodeclient/apimodels"
)

func congestionForAccountID(c echo.Context) (*apimodels.CongestionResponse, error) {
accountID, err := httpserver.ParseAccountIDParam(c, restapipkg.ParameterAccountID)
if err != nil {
return nil, err
}

func congestionByAccountAddress(c echo.Context) (*apimodels.CongestionResponse, error) {
commitmentID, err := httpserver.ParseCommitmentIDQueryParam(c, restapipkg.ParameterCommitmentID)
if err != nil {
return nil, err
Expand All @@ -36,6 +31,18 @@ func congestionForAccountID(c echo.Context) (*apimodels.CongestionResponse, erro
}
}

hrp := deps.Protocol.CommittedAPI().ProtocolParameters().Bech32HRP()
address, err := httpserver.ParseBech32AddressParam(c, hrp, restapipkg.ParameterBech32Address)
if err != nil {
return nil, err
}

accountAddress, ok := address.(*iotago.AccountAddress)
if !ok {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "address %s is not an account address", c.Param(restapipkg.ParameterBech32Address))
}

accountID := accountAddress.AccountID()
acc, exists, err := deps.Protocol.MainEngineInstance().Ledger.Account(accountID, commitment.Slot())
if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get account %s from the Ledger: %s", accountID.ToHex(), err)
Expand Down Expand Up @@ -107,20 +114,29 @@ func validators(c echo.Context) (*apimodels.ValidatorsResponse, error) {
return resp, nil
}

func validatorByAccountID(c echo.Context) (*apimodels.ValidatorResponse, error) {
accountID, err := httpserver.ParseAccountIDParam(c, restapipkg.ParameterAccountID)
func validatorByAccountAddress(c echo.Context) (*apimodels.ValidatorResponse, error) {
hrp := deps.Protocol.CommittedAPI().ProtocolParameters().Bech32HRP()
address, err := httpserver.ParseBech32AddressParam(c, hrp, restapipkg.ParameterBech32Address)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse account ID %s", c.Param(restapipkg.ParameterAccountID))
return nil, err
}

accountAddress, ok := address.(*iotago.AccountAddress)
if !ok {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "address %s is not an account address", c.Param(restapipkg.ParameterBech32Address))
}

latestCommittedSlot := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment().Slot()

accountID := accountAddress.AccountID()
accountData, exists, err := deps.Protocol.MainEngineInstance().Ledger.Account(accountID, latestCommittedSlot)
if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get account %s from the Ledger: %s", accountID.ToHex(), err)
}
if !exists {
return nil, ierrors.Wrapf(echo.ErrNotFound, "account %s not found for latest committedSlot %d", accountID.ToHex(), latestCommittedSlot)
}

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

active, err := deps.Protocol.MainEngineInstance().SybilProtection.IsCandidateActive(accountID, nextEpoch)
Expand Down
12 changes: 6 additions & 6 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,22 @@ const (
RouteCommitmentByIndexUTXOChanges = "/commitments/by-index/:" + restapipkg.ParameterSlotIndex + "/utxo-changes"

// RouteCongestion is the route for getting the current congestion state and all account related useful details as block issuance credits.
// GET returns the congestion state related to the specified account. (optional query parameters: "commitmentID" to specify the used commitment)
// GET returns the congestion state related to the specified account address. (optional query parameters: "commitmentID" to specify the used commitment)
// MIMEApplicationJSON => json.
// MIMEApplicationVendorIOTASerializerV2 => bytes.
RouteCongestion = "/accounts/:" + restapipkg.ParameterAccountID + "/congestion"
RouteCongestion = "/accounts/:" + restapipkg.ParameterBech32Address + "/congestion"

// RouteValidators is the route for getting informations about the current validators.
// GET returns the paginated response with the list of validators.
// MIMEApplicationJSON => json.
// MIMEApplicationVendorIOTASerializerV2 => bytes.
RouteValidators = "/validators"

// RouteValidatorsAccount is the route for getting details about the validator by its accountID.
// RouteValidatorsAccount is the route for getting details about the validator by its account address.
// GET returns the validator details.
// MIMEApplicationJSON => json.
// MIMEApplicationVendorIOTASerializerV2 => bytes.
RouteValidatorsAccount = "/validators/:" + restapipkg.ParameterAccountID
RouteValidatorsAccount = "/validators/:" + restapipkg.ParameterBech32Address

// RouteRewards is the route for getting the rewards for staking or delegation based on staking account or delegation output.
// Rewards are decayed up to returned epochEnd index.
Expand Down Expand Up @@ -345,7 +345,7 @@ func configure() error {
}, checkNodeSynced())

routeGroup.GET(RouteCongestion, func(c echo.Context) error {
resp, err := congestionForAccountID(c)
resp, err := congestionByAccountAddress(c)
if err != nil {
return err
}
Expand All @@ -363,7 +363,7 @@ func configure() error {
}, checkNodeSynced())

routeGroup.GET(RouteValidatorsAccount, func(c echo.Context) error {
resp, err := validatorByAccountID(c)
resp, err := validatorByAccountAddress(c)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/restapi/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const (
// ParameterCommitmentID is used to identify a slot commitment by its ID.
ParameterCommitmentID = "commitmentID"

// ParameterAccountID is used to identify an account by its ID.
ParameterAccountID = "accountID"
// ParameterBech32Address is used to to represent bech32 address.
ParameterBech32Address = "bech32Address"

// ParameterPeerID is used to identify a peer.
ParameterPeerID = "peerID"
Expand Down

0 comments on commit 909a664

Please sign in to comment.