Skip to content

Commit 9f81a92

Browse files
Fix overriding consensus parameters in evaluator. (#2811)
Fix overriding consensus parameters in evaluator for indexer
1 parent 390edd1 commit 9f81a92

File tree

6 files changed

+121
-9
lines changed

6 files changed

+121
-9
lines changed

ledger/appcow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func MakeDebugBalances(l ledgerForCowBase, round basics.Round, proto protocol.Co
476476
UpgradeState: bookkeeping.UpgradeState{CurrentProtocol: proto},
477477
}
478478
hint := 2
479-
cb := makeRoundCowState(base, hdr, prevTimestamp, hint)
479+
cb := makeRoundCowState(base, hdr, config.Consensus[proto], prevTimestamp, hint)
480480
return cb
481481
}
482482

ledger/appcow_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ func TestCowStorage(t *testing.T) {
195195
ml := emptyLedger{}
196196
var bh bookkeeping.BlockHeader
197197
bh.CurrentProtocol = protocol.ConsensusCurrentVersion
198-
cow := makeRoundCowState(&ml, bh, 0, 0)
198+
proto, ok := config.Consensus[bh.CurrentProtocol]
199+
require.True(t, ok)
200+
cow := makeRoundCowState(&ml, bh, proto, 0, 0)
199201
allSptrs, allAddrs := randomAddrApps(10)
200202

201203
st := makeStateTracker()

ledger/cow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ type roundCowState struct {
7878
trackedCreatables map[int]basics.CreatableIndex
7979
}
8080

81-
func makeRoundCowState(b roundCowParent, hdr bookkeeping.BlockHeader, prevTimestamp int64, hint int) *roundCowState {
81+
func makeRoundCowState(b roundCowParent, hdr bookkeeping.BlockHeader, proto config.ConsensusParams, prevTimestamp int64, hint int) *roundCowState {
8282
cb := roundCowState{
8383
lookupParent: b,
8484
commitParent: nil,
85-
proto: config.Consensus[hdr.CurrentProtocol],
85+
proto: proto,
8686
mods: ledgercore.MakeStateDelta(&hdr, prevTimestamp, hint, 0),
8787
sdeltas: make(map[basics.Address]map[storagePtr]*storageDelta),
8888
trackedCreatables: make(map[int]basics.CreatableIndex),

ledger/cow_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import (
2121

2222
"github.com/stretchr/testify/require"
2323

24+
"github.com/algorand/go-algorand/config"
2425
"github.com/algorand/go-algorand/data/basics"
2526
"github.com/algorand/go-algorand/data/bookkeeping"
2627
"github.com/algorand/go-algorand/data/transactions"
2728
"github.com/algorand/go-algorand/ledger/ledgercore"
29+
"github.com/algorand/go-algorand/protocol"
2830
"github.com/algorand/go-algorand/test/partitiontest"
2931
)
3032

@@ -112,7 +114,9 @@ func TestCowBalance(t *testing.T) {
112114
accts0 := randomAccounts(20, true)
113115
ml := mockLedger{balanceMap: accts0}
114116

115-
c0 := makeRoundCowState(&ml, bookkeeping.BlockHeader{}, 0, 0)
117+
c0 := makeRoundCowState(
118+
&ml, bookkeeping.BlockHeader{}, config.Consensus[protocol.ConsensusCurrentVersion],
119+
0, 0)
116120
checkCow(t, c0, accts0)
117121

118122
c1 := c0.child(0)

ledger/eval.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func startEvaluator(l ledgerForEvaluator, hdr bookkeeping.BlockHeader, proto con
468468
eval.block.BlockHeader.RewardsState = eval.prevHeader.NextRewardsState(hdr.Round, proto, incentivePoolData.MicroAlgos, prevTotals.RewardUnits())
469469
}
470470
// set the eval state with the current header
471-
eval.state = makeRoundCowState(base, eval.block.BlockHeader, eval.prevHeader.TimeStamp, paysetHint)
471+
eval.state = makeRoundCowState(base, eval.block.BlockHeader, proto, eval.prevHeader.TimeStamp, paysetHint)
472472

473473
if validate {
474474
err := eval.block.BlockHeader.PreCheck(eval.prevHeader)
@@ -1119,7 +1119,14 @@ func (eval *BlockEvaluator) GenerateBlock() (*ValidatedBlock, error) {
11191119
delta: eval.state.deltas(),
11201120
}
11211121
eval.blockGenerated = true
1122-
eval.state = makeRoundCowState(eval.state, eval.block.BlockHeader, eval.prevHeader.TimeStamp, len(eval.block.Payset))
1122+
proto, ok := config.Consensus[eval.block.BlockHeader.CurrentProtocol]
1123+
if !ok {
1124+
return nil, fmt.Errorf(
1125+
"unknown consensus version: %s", eval.block.BlockHeader.CurrentProtocol)
1126+
}
1127+
eval.state = makeRoundCowState(
1128+
eval.state, eval.block.BlockHeader, proto, eval.prevHeader.TimeStamp,
1129+
len(eval.block.Payset))
11231130
return &vb, nil
11241131
}
11251132

ledger/eval_test.go

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func TestPrepareEvalParams(t *testing.T) {
347347
}
348348

349349
params := []config.ConsensusParams{
350-
config.ConsensusParams{Application: true, MaxAppProgramCost: 700},
350+
{Application: true, MaxAppProgramCost: 700},
351351
config.Consensus[protocol.ConsensusV29],
352352
config.Consensus[protocol.ConsensusFuture],
353353
}
@@ -856,7 +856,9 @@ func TestCowCompactCert(t *testing.T) {
856856
blocks := make(map[basics.Round]bookkeeping.BlockHeader)
857857
blockErr := make(map[basics.Round]error)
858858
ml := mockLedger{balanceMap: accts0, blocks: blocks, blockErr: blockErr}
859-
c0 := makeRoundCowState(&ml, bookkeeping.BlockHeader{}, 0, 0)
859+
c0 := makeRoundCowState(
860+
&ml, bookkeeping.BlockHeader{}, config.Consensus[protocol.ConsensusCurrentVersion],
861+
0, 0)
860862

861863
certType = protocol.CompactCertType(1234) // bad cert type
862864
err := c0.compactCert(certRnd, certType, cert, atRound, validate)
@@ -1402,3 +1404,100 @@ func TestModifiedAppLocalStates(t *testing.T) {
14021404
assert.False(t, created)
14031405
}
14041406
}
1407+
1408+
// Test that overriding the consensus parameters effects the generated apply data.
1409+
func TestCustomProtocolParams(t *testing.T) {
1410+
partitiontest.PartitionTest(t)
1411+
1412+
genesisBalances, addrs, _ := newTestGenesis()
1413+
1414+
var genHash crypto.Digest
1415+
crypto.RandBytes(genHash[:])
1416+
block, err := bookkeeping.MakeGenesisBlock(protocol.ConsensusV24,
1417+
genesisBalances, "test", genHash)
1418+
1419+
dbName := fmt.Sprintf("%s", t.Name())
1420+
cfg := config.GetDefaultLocal()
1421+
cfg.Archival = true
1422+
l, err := OpenLedger(logging.Base(), dbName, true, InitState{
1423+
Block: block,
1424+
Accounts: genesisBalances.Balances,
1425+
GenesisHash: genHash,
1426+
}, cfg)
1427+
require.NoError(t, err)
1428+
defer l.Close()
1429+
1430+
const assetid basics.AssetIndex = 1
1431+
proto := config.Consensus[protocol.ConsensusV24]
1432+
1433+
block = bookkeeping.MakeBlock(block.BlockHeader)
1434+
1435+
createTxn := txntest.Txn{
1436+
Type: "acfg",
1437+
Sender: addrs[0],
1438+
GenesisHash: block.GenesisHash(),
1439+
AssetParams: basics.AssetParams{
1440+
Total: 200,
1441+
Decimals: 0,
1442+
Manager: addrs[0],
1443+
Reserve: addrs[0],
1444+
Freeze: addrs[0],
1445+
Clawback: addrs[0],
1446+
},
1447+
}
1448+
createTxn.FillDefaults(proto)
1449+
createStib, err := block.BlockHeader.EncodeSignedTxn(
1450+
createTxn.SignedTxn(), transactions.ApplyData{})
1451+
require.NoError(t, err)
1452+
1453+
optInTxn := txntest.Txn{
1454+
Type: "axfer",
1455+
Sender: addrs[1],
1456+
GenesisHash: block.GenesisHash(),
1457+
XferAsset: assetid,
1458+
AssetAmount: 0,
1459+
AssetReceiver: addrs[1],
1460+
}
1461+
optInTxn.FillDefaults(proto)
1462+
optInStib, err := block.BlockHeader.EncodeSignedTxn(
1463+
optInTxn.SignedTxn(), transactions.ApplyData{})
1464+
require.NoError(t, err)
1465+
1466+
fundTxn := txntest.Txn{
1467+
Type: "axfer",
1468+
Sender: addrs[0],
1469+
GenesisHash: block.GenesisHash(),
1470+
XferAsset: assetid,
1471+
AssetAmount: 100,
1472+
AssetReceiver: addrs[1],
1473+
}
1474+
fundTxn.FillDefaults(proto)
1475+
fundStib, err := block.BlockHeader.EncodeSignedTxn(
1476+
fundTxn.SignedTxn(), transactions.ApplyData{})
1477+
require.NoError(t, err)
1478+
1479+
optOutTxn := txntest.Txn{
1480+
Type: "axfer",
1481+
Sender: addrs[1],
1482+
GenesisHash: block.GenesisHash(),
1483+
XferAsset: assetid,
1484+
AssetAmount: 30,
1485+
AssetReceiver: addrs[0],
1486+
AssetCloseTo: addrs[0],
1487+
}
1488+
optOutTxn.FillDefaults(proto)
1489+
optOutStib, err := block.BlockHeader.EncodeSignedTxn(
1490+
optOutTxn.SignedTxn(), transactions.ApplyData{})
1491+
require.NoError(t, err)
1492+
1493+
block.Payset = []transactions.SignedTxnInBlock{
1494+
createStib, optInStib, fundStib, optOutStib,
1495+
}
1496+
1497+
proto.EnableAssetCloseAmount = true
1498+
_, modifiedTxns, err := Eval(l, &block, proto)
1499+
require.NoError(t, err)
1500+
1501+
require.Equal(t, 4, len(modifiedTxns))
1502+
assert.Equal(t, uint64(70), modifiedTxns[3].AssetClosingAmount)
1503+
}

0 commit comments

Comments
 (0)