Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test REST API with the Docker test framework #817

Merged
merged 28 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
736003a
Add core api tests
daria305 Feb 28, 2024
e90c626
Merge branch 'develop' into test/rest-api
daria305 Mar 1, 2024
d932aac
Add more assertions, and fixes
daria305 Mar 4, 2024
2323eda
Merge branch 'develop' into test/rest-api
daria305 Mar 5, 2024
aa0a1d6
Corrections for HTTP error codes
daria305 Mar 6, 2024
b91cc8a
Merge branch 'develop' into test/rest-api
daria305 Mar 6, 2024
d0aca23
Clean up and fix linter in docker tests
daria305 Mar 6, 2024
c030d36
Fix UTXO diffs and congestion tests
daria305 Mar 7, 2024
4431d0b
Finish up rewards, and validator API
daria305 Mar 7, 2024
3d5fde4
Merge branch 'develop' into test/rest-api
daria305 Mar 7, 2024
485b340
Increase created assets num
daria305 Mar 7, 2024
94e00d9
Add status codes test
daria305 Mar 7, 2024
8e47059
Update iota.go
daria305 Mar 7, 2024
7f8287e
Merge branch 'develop' of github.com:iotaledger/iota-core into test/r…
muXxer Mar 7, 2024
99c816f
Fix check synced
daria305 Mar 7, 2024
cd7d007
Apply suggestions for errors
daria305 Mar 7, 2024
ef94cd5
Revert to InternalServerErrors
daria305 Mar 7, 2024
ea88925
Add forgotten Test_OutputWithMetadata
daria305 Mar 7, 2024
f89a1c0
Merge branch 'develop' into test/rest-api
daria305 Mar 11, 2024
0ed1b30
Remove incorrect validators check
daria305 Mar 11, 2024
8c489ab
Await finalisation instead of commitment
daria305 Mar 11, 2024
f75e926
Issue reattachment, and check the first included block
daria305 Mar 11, 2024
126b4f4
Move basic output tx creation to wallet, add todo
daria305 Mar 12, 2024
9f05399
Fix assert
daria305 Mar 12, 2024
109f4ae
Move checks before getting commitment
daria305 Mar 12, 2024
6c96755
Rename
daria305 Mar 12, 2024
41146f0
Better check for StatusNotFound
daria305 Mar 12, 2024
6a74c6b
Please the linter
daria305 Mar 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions components/restapi/core/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func congestionByAccountAddress(c echo.Context) (*api.CongestionResponse, error) {
commitmentID, err := httpserver.ParseCommitmentIDQueryParam(c, api.ParameterCommitmentID)
queryCommitmentID, err := httpserver.ParseCommitmentIDQueryParam(c, api.ParameterCommitmentID)
if err != nil {
return nil, err
}
Expand All @@ -27,7 +27,7 @@ func congestionByAccountAddress(c echo.Context) (*api.CongestionResponse, error)
workScores = append(workScores, workScore)
}

commitment, err := deps.RequestHandler.GetCommitmentByID(commitmentID)
commitment, err := deps.RequestHandler.GetCommitmentByID(queryCommitmentID)
daria305 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand All @@ -43,6 +43,16 @@ func congestionByAccountAddress(c echo.Context) (*api.CongestionResponse, error)
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "address %s is not an account address", c.Param(api.ParameterBech32Address))
}

maxCommittableAge := deps.RequestHandler.CommittedAPI().ProtocolParameters().MaxCommittableAge()
latestCommittedSlot := deps.RequestHandler.GetLatestCommitment().Slot()
if latestCommittedSlot >= maxCommittableAge && commitment.Slot()+maxCommittableAge < latestCommittedSlot && queryCommitmentID != iotago.EmptyCommitmentID {
return nil, ierrors.Wrapf(echo.ErrBadRequest, "invalid commitmentID, target slot index older than allowed (%d<%d)", commitment.Slot(), latestCommittedSlot-maxCommittableAge)
}

if commitment.Slot() > latestCommittedSlot && queryCommitmentID != iotago.EmptyCommitmentID {
return nil, ierrors.Wrapf(echo.ErrBadRequest, "invalid commitmentID, slot %d is not committed yet, latest committed slot: %d", commitment.Slot(), latestCommittedSlot)
}

daria305 marked this conversation as resolved.
Show resolved Hide resolved
return deps.RequestHandler.CongestionByAccountAddress(accountAddress, commitment, workScores...)
}

Expand All @@ -61,10 +71,6 @@ func validators(c echo.Context) (*api.ValidatorsResponse, error) {
}
}

// do not respond to really old requests
if requestedSlot+iotago.SlotIndex(restapi.ParamsRestAPI.MaxRequestedSlotAge) < latestCommittedSlot {
return nil, ierrors.Wrapf(echo.ErrBadRequest, "request is too old, request started at %d, latest committed slot index is %d", requestedSlot, latestCommittedSlot)
}
slotRange := uint32(requestedSlot) / restapi.ParamsRestAPI.RequestsMemoryCacheGranularity

return deps.RequestHandler.Validators(slotRange, cursorIndex, pageSize)
Expand All @@ -86,14 +92,14 @@ func validatorByAccountAddress(c echo.Context) (*api.ValidatorResponse, error) {
}

func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
var err error
outputID, err := httpserver.ParseOutputIDParam(c, api.ParameterOutputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(api.ParameterOutputID))
}

var slot iotago.SlotIndex
if len(c.QueryParam(api.ParameterSlot)) > 0 {
var err error
slot, err = httpserver.ParseSlotQueryParam(c, api.ParameterSlot)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse slot index %s", c.Param(api.ParameterSlot))
Expand All @@ -113,6 +119,7 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {

func selectedCommittee(c echo.Context) (*api.CommitteeResponse, error) {
var epoch iotago.EpochIndex

if len(c.QueryParam(api.ParameterEpoch)) == 0 {
// by default we return current epoch
epoch = deps.RequestHandler.CommittedAPI().TimeProvider().CurrentEpoch()
Expand All @@ -122,6 +129,10 @@ func selectedCommittee(c echo.Context) (*api.CommitteeResponse, error) {
if err != nil {
return nil, err
}
currentEpoch := deps.RequestHandler.CommittedAPI().TimeProvider().CurrentEpoch()
if epoch > currentEpoch {
return nil, ierrors.Wrapf(echo.ErrBadRequest, "provided epoch %d is from the future, current epoch: %d", epoch, currentEpoch)
}
}

return deps.RequestHandler.SelectedCommittee(epoch)
Expand Down
4 changes: 2 additions & 2 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func configure() error {
}

return responseByHeader(c, resp)
})
}, checkNodeSynced())

routeGroup.GET(api.EndpointWithEchoParameters(api.CoreEndpointOutputWithMetadata), func(c echo.Context) error {
resp, err := outputWithMetadataFromOutputID(c)
Expand All @@ -231,7 +231,7 @@ func configure() error {
}

return responseByHeader(c, resp)
})
}, checkNodeSynced())

routeGroup.GET(api.EndpointWithEchoParameters(api.CoreEndpointTransactionsIncludedBlock), func(c echo.Context) error {
block, err := blockFromTransactionID(c)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20240307102857-7e23a3c59bd2
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022
github.com/iotaledger/iota.go/v4 v4.0.0-20240307091827-db3c503615a6
github.com/iotaledger/iota.go/v4 v4.0.0-20240307175623-0904c71fcb38
github.com/labstack/echo/v4 v4.11.4
github.com/labstack/gommon v0.4.2
github.com/libp2p/go-libp2p v0.33.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 h1:I178Sa
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022/go.mod h1:jTFxIWiMUdAwO263jlJCSWcNLqEkgYEVOFXfjp5aNJM=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240216103559-27ca8dffd1e7 h1:t6k4MqiUov0FrBb2o2JhKlOVSdlPbIQWM8ivYHL0G0g=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240216103559-27ca8dffd1e7/go.mod h1:do+N3LpeDEi9qselEC4XcjqGoRc7cWGiqBtIeBOKEMs=
github.com/iotaledger/iota.go/v4 v4.0.0-20240307091827-db3c503615a6 h1:dzOaHlQDJecS3SDqwaUDQfTYjZDvwqRdn1/6bCjFpgM=
github.com/iotaledger/iota.go/v4 v4.0.0-20240307091827-db3c503615a6/go.mod h1:8UQOTI7CC5R/3TurawUFuBZbkb37RzW8m4q8Hp7ct30=
github.com/iotaledger/iota.go/v4 v4.0.0-20240307175623-0904c71fcb38 h1:NizJ3CALLCcJowtAtkNuDlpE4gd4qjaWZkp/kTZfeYk=
github.com/iotaledger/iota.go/v4 v4.0.0-20240307175623-0904c71fcb38/go.mod h1:8UQOTI7CC5R/3TurawUFuBZbkb37RzW8m4q8Hp7ct30=
github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw=
github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
5 changes: 4 additions & 1 deletion pkg/requesthandler/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,19 @@ func (r *RequestHandler) RewardsByOutputID(outputID iotago.OutputID, slot iotago
delegationEnd,
claimingEpoch,
)
default:
return nil, ierrors.Wrapf(echo.ErrBadRequest, "output %s is neither a delegation output nor account", outputID.ToHex())
}

if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to calculate reward for output %s: %s", outputID.ToHex(), err)
}

latestCommittedEpochPoolRewards, poolRewardExists, err := r.protocol.Engines.Main.Get().SybilProtection.PoolRewardsForAccount(stakingPoolValidatorAccountID)

if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to retrieve pool rewards for account %s: %s", stakingPoolValidatorAccountID.ToHex(), err)
}

if !poolRewardExists {
latestCommittedEpochPoolRewards = 0
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/requesthandler/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (r *RequestHandler) GetCommitmentBySlot(slot iotago.SlotIndex) (*model.Comm

commitment, err := r.protocol.Engines.Main.Get().Storage.Commitments().Load(slot)
if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load commitment, slot: %d, error: %w", slot, err)
return nil, ierrors.Join(echo.ErrInternalServerError, ierrors.Wrapf(err, "failed to load commitment, slot: %d", slot))
}

return commitment, nil
Expand All @@ -38,11 +38,11 @@ func (r *RequestHandler) GetCommitmentByID(commitmentID iotago.CommitmentID) (*m

commitment, err := r.protocol.Engines.Main.Get().Storage.Commitments().Load(commitmentID.Slot())
if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load commitment, commitmentID: %s, slot: %d, error: %w", commitmentID, commitmentID.Slot(), err)
return nil, ierrors.Join(echo.ErrInternalServerError, ierrors.Wrapf(err, "failed to load commitment, commitmentID: %s, slot: %d", commitmentID, commitmentID.Slot()))
}

if commitment.ID() != commitmentID {
return nil, ierrors.Wrapf(echo.ErrBadRequest, "commitment in the store for slot %d does not match the given commitmentID (%s != %s)", commitmentID.Slot(), commitment.ID(), commitmentID)
return nil, ierrors.Join(echo.ErrBadRequest, ierrors.Wrapf(err, "commitment in the store for slot %d does not match the given commitmentID (%s != %s)", commitmentID.Slot(), commitment.ID(), commitmentID))
}

return commitment, nil
Expand All @@ -55,7 +55,7 @@ func (r *RequestHandler) GetLatestCommitment() *model.Commitment {
func (r *RequestHandler) GetUTXOChanges(commitmentID iotago.CommitmentID) (*api.UTXOChangesResponse, error) {
diffs, err := r.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)
return nil, ierrors.Join(echo.ErrInternalServerError, ierrors.Wrapf(err, "failed to get slot diffs, commitmentID: %s, slot: %d", commitmentID, commitmentID.Slot()))
}

createdOutputs := make(iotago.OutputIDs, len(diffs.Outputs))
Expand All @@ -79,7 +79,7 @@ func (r *RequestHandler) GetUTXOChanges(commitmentID iotago.CommitmentID) (*api.
func (r *RequestHandler) GetUTXOChangesFull(commitmentID iotago.CommitmentID) (*api.UTXOChangesFullResponse, error) {
diffs, err := r.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)
return nil, ierrors.Join(echo.ErrInternalServerError, ierrors.Wrapf(err, "failed to get slot diffs, commitmentID: %s, slot: %d", commitmentID, commitmentID.Slot()))
}

createdOutputs := make([]*api.OutputWithID, len(diffs.Outputs))
Expand Down
8 changes: 4 additions & 4 deletions tools/docker-network/tests/committeerotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ func Test_Delegation(t *testing.T) {
account := d.CreateAccount()

// delegate all faucet funds to V2, V2 should replace V3
delegationStartEpoch := d.DelegateToValidator(account.ID, d.Node("V2"))
d.AssertCommittee(delegationStartEpoch+1, d.AccountsFromNodes(d.Nodes("V1", "V2", "V4")...))
_, delegationOutput := d.DelegateToValidator(account.ID, d.Node("V2").AccountAddress(t))
d.AssertCommittee(delegationOutput.StartEpoch+1, d.AccountsFromNodes(d.Nodes("V1", "V2", "V4")...))

// delegate all faucet funds to V3, V3 should replace V1
delegationStartEpoch = d.DelegateToValidator(account.ID, d.Node("V3"))
d.AssertCommittee(delegationStartEpoch+1, d.AccountsFromNodes(d.Nodes("V2", "V3", "V4")...))
_, delegationOutput = d.DelegateToValidator(account.ID, d.Node("V3").AccountAddress(t))
d.AssertCommittee(delegationOutput.StartEpoch+1, d.AccountsFromNodes(d.Nodes("V2", "V3", "V4")...))
}
Loading
Loading