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

tests: cross-sdk heartbeat tests #676

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 45 additions & 1 deletion test/algodclientv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"

"github.com/algorand/go-algorand-sdk/v2/client/v2/algod"
"github.com/algorand/go-algorand-sdk/v2/client/v2/common"
"github.com/algorand/go-algorand-sdk/v2/client/v2/common/models"
modelsV2 "github.com/algorand/go-algorand-sdk/v2/client/v2/common/models"
"github.com/algorand/go-algorand-sdk/v2/types"

Expand Down Expand Up @@ -65,6 +67,7 @@ func AlgodClientV2Context(s *godog.ScenarioContext) {
s.Step(`^we make a LedgerStateDeltaForTransactionGroupResponse call for ID "([^"]*)"$`, weMakeALedgerStateDeltaForTransactionGroupResponseCallForID)
s.Step(`^we make a TransactionGroupLedgerStateDeltaForRoundResponse call for round (\d+)$`, weMakeATransactionGroupLedgerStateDeltaForRoundResponseCallForRound)
s.Step(`^we make a GetBlockTxids call against block number (\d+)$`, weMakeAGetBlockTxidsCallAgainstBlockNumber)
s.Step(`^the parsed Get Block response should have heartbeat address "([^"]*)"$`, theParsedGetBlockResponseShouldHaveHeartbeatAddress)

s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
globalErrForExamination = nil
Expand Down Expand Up @@ -104,6 +107,34 @@ func weMakeAnyAccountInformationCall() error {
return weMakeAnyCallTo("algod", "AccountInformation")
}

type mockBlock struct {
c *algod.Client
round uint64
p algod.BlockParams
}

// This is a mock implementation of the do method in the Block struct in the algod package.
// in order to support both JSON and MSGPACK formats depending on the mock file.
func (s *mockBlock) do(ctx context.Context, headers ...*common.Header) (result types.Block, err error) {
gmalouf marked this conversation as resolved.
Show resolved Hide resolved
var response models.BlockResponse

val := responseRing.Value.([]byte)
if len(val) > 0 && val[0] == '{' {
s.p.Format = "json"
err = (*common.Client)(s.c).Get(ctx, &response, fmt.Sprintf("/v2/blocks/%d", s.round), s.p, headers)
} else {
s.p.Format = "msgpack"
err = (*common.Client)(s.c).GetRawMsgpack(ctx, &response, fmt.Sprintf("/v2/blocks/%d", s.round), s.p, headers)
}

if err != nil {
return
}

result = response.Block
return
}

var blockResponse types.Block

func weMakeAnyGetBlockCall() error {
Expand All @@ -113,7 +144,9 @@ func weMakeAnyGetBlockCall() error {
if err != nil {
return err
}
blockResponse, globalErrForExamination = algodClient.Block(0).Do(context.Background())
req := &mockBlock{c: algodClient, round: 0}
blockResponse, globalErrForExamination = req.do(context.Background())

return nil
}

Expand All @@ -129,6 +162,17 @@ func theParsedGetBlockResponseShouldHaveRewardsPool(pool string) error {
return nil
}

func theParsedGetBlockResponseShouldHaveHeartbeatAddress(hbAddress string) error {
if len(blockResponse.Payset) == 0 {
return fmt.Errorf("response has no payset")
}
addr := blockResponse.Payset[0].Txn.HeartbeatTxnFields.HbAddress.String()
if addr != hbAddress {
return fmt.Errorf("response heartbeat address %s mismatched expected address %s", addr, hbAddress)
}
return nil
}

func weMakeAnySuggestedTransactionParametersCall() error {
return weMakeAnyCallTo("algod", "TransactionParams")
}
Expand Down
20 changes: 20 additions & 0 deletions test/indexer_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"github.com/algorand/go-algorand-sdk/v2/client/v2/common/models"
"github.com/algorand/go-algorand-sdk/v2/client/v2/indexer"
"github.com/cucumber/godog"
)
Expand Down Expand Up @@ -56,6 +57,7 @@ func IndexerUnitTestContext(s *godog.ScenarioContext) {
s.Step(`^we make a Lookup Block call against round (\d+) and header "([^"]*)"$`, weMakeALookupBlockCallAgainstRoundAndHeader)
s.Step(`^we make a LookupApplicationBoxByIDandName call with applicationID (\d+) with encoded box name "([^"]*)"$`, weMakeALookupApplicationBoxByIDandName)
s.Step(`^we make a SearchForApplicationBoxes call with applicationID (\d+) with max (\d+) nextToken "([^"]*)"$`, weMakeASearchForApplicationBoxes)
s.Step(`^the parsed SearchForTransactions response should be valid on round (\d+) and the array should be of len (\d+) and the element at index (\d+) should have hbaddress "([^"]*)"$`, theParsedSearchForTransactionsResponseHasHeartbeatAddress)
s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
globalErrForExamination = nil
return ctx, nil
Expand Down Expand Up @@ -394,3 +396,21 @@ func weMakeASearchForApplicationBoxes(appId int, limit int, next string) error {
indexerClient.SearchForApplicationBoxes(uint64(appId)).Limit(uint64(limit)).Next(next).Do(context.Background())
return nil
}

func theParsedSearchForTransactionsResponseHasHeartbeatAddress(round int, length int, index int, hbAddress string) error {
resp := response.(models.TransactionsResponse)

if resp.CurrentRound != uint64(round) {
return fmt.Errorf("Expected round %d, got %d", round, resp.CurrentRound)
}
if len(resp.Transactions) != length {
return fmt.Errorf("Expected %d transactions, got %d", length, len(resp.Transactions))
}

hbTxn := resp.Transactions[index]
if hbTxn.HeartbeatTransaction.HbAddress != hbAddress {
return fmt.Errorf("Expected heartbeat address %s, got %s", hbAddress, hbTxn.HeartbeatTransaction.HbAddress)
}

return nil
}
3 changes: 3 additions & 0 deletions test/unit.tags
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
@unit.abijson.byname
@unit.algod
@unit.algod.ledger_refactoring
@unit.algod.heartbeat
@unit.algod.heartbeat.msgp
@unit.applications
@unit.applications.boxes
@unit.atomic_transaction_composer
Expand All @@ -14,6 +16,7 @@
@unit.indexer.ledger_refactoring
@unit.indexer.logs
@unit.indexer.rekey
@unit.indexer.heartbeat
@unit.offline
@unit.program_sanity_check
@unit.ready
Expand Down
Loading