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: ConfirmTxs returns list of chain.Result #1803

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions docs/tutorials/morpheusvm/4_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,19 @@ This step will consist of the following:
- If the test takes longer than 2 seconds, it will fail
- Calling `ConfirmTxs` with our TX being passed in

The function `ConfirmTXs` is useful as it checks that our TX was
sent and that, if finalized, our transaction has the expected outputs. We have
The function `ConfirmTxs` is useful as it checks that our TX was
sent and that, if finalized, it returns the txs results. We have
the following:

```go
timeoutCtx, timeoutCtxFnc := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer timeoutCtxFnc()

require.NoError(tn.ConfirmTxs(timeoutCtx, []*chain.Transaction{tx}))
results, err := tn.ConfirmTxs(timeoutCtx, []*chain.Transaction{tx})
require.NoError(err)
for _, result := range results {
require.True(result.Success)
}
```

## Registering our Tests
Expand Down
6 changes: 5 additions & 1 deletion examples/morpheusvm/tests/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ var _ = registry.Register(TestsRegistry, "Transfer Transaction", func(t ginkgo.F
timeoutCtx, timeoutCtxFnc := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer timeoutCtxFnc()

require.NoError(tn.ConfirmTxs(timeoutCtx, []*chain.Transaction{tx}))
results, err := tn.ConfirmTxs(timeoutCtx, []*chain.Transaction{tx})
require.NoError(err)
for _, result := range results {
require.True(result.Success)
}
})
30 changes: 23 additions & 7 deletions tests/e2e/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,47 @@ func (n *Network) URIs() []string {
return n.uris
}

func (n *Network) ConfirmTxs(ctx context.Context, txs []*chain.Transaction) error {
func (n *Network) ConfirmTxs(ctx context.Context, txs []*chain.Transaction) ([]*chain.Result, error) {
c := jsonrpc.NewJSONRPCClient(n.uris[0])
txIDs := []ids.ID{}
for _, tx := range txs {
txID, err := c.SubmitTx(ctx, tx.Bytes())
if err != nil {
return fmt.Errorf("unable to submit transaction : %w", err)
return nil, fmt.Errorf("unable to submit transaction : %w", err)
}
txIDs = append(txIDs, txID)
}

results := make([]*chain.Result, 0, len(txs))
indexerCli := indexer.NewClient(n.uris[0])
for _, txID := range txIDs {
success, _, err := indexerCli.WaitForTransaction(ctx, txCheckInterval, txID)
if err != nil {
return fmt.Errorf("error while waiting for transaction : %w", err)
return nil, fmt.Errorf("error while waiting for transaction : %w", err)
}
if !success {
return ErrUnableToConfirmTx
return nil, ErrUnableToConfirmTx
}
txResponse, _, err := indexerCli.GetTx(ctx, txID)
if err != nil {
return nil, fmt.Errorf("error while fetching transaction : %w", err)
}
outputs := make([][]byte, 0, len(txResponse.Outputs))
for _, output := range txResponse.Outputs {
outputs = append(outputs, []byte(output))
}
results = append(results, &chain.Result{
Success: txResponse.Success,
Error: []byte(txResponse.ErrorStr),
Fee: txResponse.Fee,
Units: txResponse.Units,
Outputs: outputs,
})
}

_, targetHeight, _, err := c.Accepted(ctx)
if err != nil {
return err
return nil, err
}
for _, uri := range n.uris[1:] {
if err := jsonrpc.Wait(ctx, txCheckInterval, func(ctx context.Context) (bool, error) {
Expand All @@ -89,10 +105,10 @@ func (n *Network) ConfirmTxs(ctx context.Context, txs []*chain.Transaction) erro
}
return nodeHeight >= targetHeight, nil
}); err != nil {
return err
return nil, err
}
}
return nil
return results, nil
}

func (n *Network) GenerateTx(ctx context.Context, actions []chain.Action, auth chain.AuthFactory) (*chain.Transaction, error) {
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ type Network struct {
uris []string
}

func (*Network) ConfirmTxs(ctx context.Context, txs []*chain.Transaction) error {
func (*Network) ConfirmTxs(ctx context.Context, txs []*chain.Transaction) ([]*chain.Result, error) {
err := instances[0].confirmTxs(ctx, txs)
if err != nil {
return err
return nil, err
}
lastAcceptedBlock := instances[0].vm.LastAcceptedStatefulBlock()
for i := 1; i < len(instances); i++ {
err = instances[i].applyBlk(ctx, lastAcceptedBlock)
if err != nil {
return err
return nil, err
}
}
return nil
return instances[0].vm.LastAcceptedBlockResult().Results, nil
}

func (*Network) GenerateTx(ctx context.Context, actions []chain.Action, auth chain.AuthFactory) (*chain.Transaction, error) {
Expand Down
2 changes: 1 addition & 1 deletion tests/workload/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type TestNetwork interface {
ConfirmTxs(context.Context, []*chain.Transaction) error
ConfirmTxs(context.Context, []*chain.Transaction) ([]*chain.Result, error)
GenerateTx(context.Context, []chain.Action, chain.AuthFactory) (*chain.Transaction, error)
URIs() []string
Configuration() TestNetworkConfiguration
Expand Down
Loading