Skip to content

Commit

Permalink
Improve testing of last round
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos committed Feb 22, 2023
1 parent 88b7182 commit 689d686
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
48 changes: 32 additions & 16 deletions ledger/simulation/simulation_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func TestPayTxn(t *testing.T) {
return simulationTestCase{
input: []transactions.SignedTxn{txn},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -255,7 +256,8 @@ func TestPayTxn(t *testing.T) {
return simulationTestCase{
input: []transactions.SignedTxn{txn},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -304,7 +306,8 @@ func TestPayTxn(t *testing.T) {
input: []transactions.SignedTxn{txn},
expectedError: fmt.Sprintf("tried to spend {%d}", amount),
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -355,7 +358,8 @@ func TestWrongAuthorizerTxn(t *testing.T) {
input: []transactions.SignedTxn{stxn},
expectedError: fmt.Sprintf("should have been authorized by %s but was actually authorized by %s", sender.Addr, authority.Addr),
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -415,7 +419,8 @@ func TestRekey(t *testing.T) {
return simulationTestCase{
input: []transactions.SignedTxn{stxn1, stxn2},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -604,7 +609,8 @@ int 1`,
input: []transactions.SignedTxn{signedPayTxn, signedAppCallTxn},
expectedError: testCase.expectedError,
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -678,7 +684,8 @@ int 0
return simulationTestCase{
input: []transactions.SignedTxn{signedCreateTxn, signedCallTxn},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -750,7 +757,8 @@ int 0
input: []transactions.SignedTxn{signedCreateTxn},
expectedError: "transaction rejected by ApprovalProgram",
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -813,7 +821,8 @@ int 0
input: []transactions.SignedTxn{signedCreateTxn},
expectedError: "err opcode executed",
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -993,7 +1002,8 @@ int 1`,
signedCheckEndingBalanceTxn,
},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -1062,7 +1072,8 @@ func TestPartialMissingSignatures(t *testing.T) {
return simulationTestCase{
input: []transactions.SignedTxn{signedTxn1, signedTxn2},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -1125,7 +1136,8 @@ func TestPooledFeesAcrossSignedAndUnsigned(t *testing.T) {
return simulationTestCase{
input: []transactions.SignedTxn{signedPay1, signedPay2},
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -1225,7 +1237,8 @@ int 1`,
input: txgroup,
expectedError: "rejected by ApprovalProgram",
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -1319,7 +1332,8 @@ int 1`,
input: txgroup,
expectedError: "rejected by ApprovalProgram",
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -1408,7 +1422,8 @@ int 1`,
input: txgroup,
expectedError: "logic eval error: this transaction should be issued by the manager",
expected: simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
Txns: []simulation.TxnResult{
Expand Down Expand Up @@ -1487,7 +1502,8 @@ func TestMockTracerScenarios(t *testing.T) {
expectedFailedAt[0]++
}
expected := simulation.Result{
Version: 1,
Version: 1,
LastRound: txnInfo.LatestRound(),
TxnGroups: []simulation.TxnGroupResult{
{
FailedAt: expectedFailedAt,
Expand Down
27 changes: 25 additions & 2 deletions ledger/simulation/testing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package simulationtesting

import (
"math/rand"
"testing"

"github.com/algorand/go-algorand/agreement"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data"
Expand All @@ -45,6 +47,11 @@ type TxnInfo struct {
LatestHeader bookkeeping.BlockHeader
}

// LatestRound returns the round number of the most recently committed block
func (info TxnInfo) LatestRound() basics.Round {
return info.LatestHeader.Round
}

// CurrentProtocolParams returns the consensus parameters that the network is currently using
func (info TxnInfo) CurrentProtocolParams() config.ConsensusParams {
return config.Consensus[info.LatestHeader.CurrentProtocol]
Expand Down Expand Up @@ -101,9 +108,25 @@ func PrepareSimulatorTest(t *testing.T) (l *data.Ledger, accounts []Account, txn
i++
}

hdr, err := l.BlockHdr(l.Latest())
latest := l.Latest()
latestHeader, err := l.BlockHdr(latest)
require.NoError(t, err)
txnInfo = TxnInfo{hdr}

// append a random number of blocks to ensure simulation results have a valid LastRound field
numBlocks := rand.Intn(4)
for i := 0; i < numBlocks; i++ {
nextBlock := bookkeeping.MakeBlock(latestHeader)
err = l.AddBlock(nextBlock, agreement.Certificate{})
require.NoError(t, err)

// round has advanced by 1
require.Equal(t, latest+1, l.Latest())
latest += 1

latestHeader = nextBlock.BlockHeader
}

txnInfo = TxnInfo{latestHeader}

return
}

0 comments on commit 689d686

Please sign in to comment.