Skip to content

Commit 5eb09af

Browse files
ledger: do final validation in endOfBlock() (#3132)
## Summary 1. It doesn't make sense to calculate new totals in `finalValidation()` 2. `endOfBlock()` already does some validation Moving the content of `finalValidation()` to `endOfBlock()` makes things more reasonable.
1 parent b8ac4ba commit 5eb09af

File tree

2 files changed

+45
-62
lines changed

2 files changed

+45
-62
lines changed

ledger/internal/eval.go

+45-56
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,51 @@ func (eval *BlockEvaluator) endOfBlock() error {
10941094
return err
10951095
}
10961096

1097+
eval.state.mods.OptimizeAllocatedMemory(eval.proto)
1098+
1099+
if eval.validate {
1100+
// check commitments
1101+
txnRoot, err := eval.block.PaysetCommit()
1102+
if err != nil {
1103+
return err
1104+
}
1105+
if txnRoot != eval.block.TxnRoot {
1106+
return fmt.Errorf("txn root wrong: %v != %v", txnRoot, eval.block.TxnRoot)
1107+
}
1108+
1109+
var expectedTxnCount uint64
1110+
if eval.proto.TxnCounter {
1111+
expectedTxnCount = eval.state.txnCounter()
1112+
}
1113+
if eval.block.TxnCounter != expectedTxnCount {
1114+
return fmt.Errorf("txn count wrong: %d != %d", eval.block.TxnCounter, expectedTxnCount)
1115+
}
1116+
1117+
expectedVoters, expectedVotersWeight, err := eval.compactCertVotersAndTotal()
1118+
if err != nil {
1119+
return err
1120+
}
1121+
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters != expectedVoters {
1122+
return fmt.Errorf("CompactCertVoters wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters, expectedVoters)
1123+
}
1124+
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal != expectedVotersWeight {
1125+
return fmt.Errorf("CompactCertVotersTotal wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal, expectedVotersWeight)
1126+
}
1127+
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound != eval.state.compactCertNext() {
1128+
return fmt.Errorf("CompactCertNextRound wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound, eval.state.compactCertNext())
1129+
}
1130+
for ccType := range eval.block.CompactCert {
1131+
if ccType != protocol.CompactCertBasic {
1132+
return fmt.Errorf("CompactCertType %d unexpected", ccType)
1133+
}
1134+
}
1135+
}
1136+
1137+
err = eval.state.CalculateTotals()
1138+
if err != nil {
1139+
return err
1140+
}
1141+
10971142
return nil
10981143
}
10991144

@@ -1189,7 +1234,6 @@ func (eval *BlockEvaluator) validateExpiredOnlineAccounts() error {
11891234

11901235
// resetExpiredOnlineAccountsParticipationKeys after all transactions and rewards are processed, modify the accounts so that their status is offline
11911236
func (eval *BlockEvaluator) resetExpiredOnlineAccountsParticipationKeys() error {
1192-
11931237
expectedMaxNumberOfExpiredAccounts := eval.proto.MaxProposedExpiredOnlineAccounts
11941238
lengthOfExpiredParticipationAccounts := len(eval.block.ParticipationUpdates.ExpiredParticipationAccounts)
11951239

@@ -1218,51 +1262,6 @@ func (eval *BlockEvaluator) resetExpiredOnlineAccountsParticipationKeys() error
12181262
return nil
12191263
}
12201264

1221-
// FinalValidation does the validation that must happen after the block is built and all state updates are computed
1222-
func (eval *BlockEvaluator) finalValidation() error {
1223-
eval.state.mods.OptimizeAllocatedMemory(eval.proto)
1224-
if eval.validate {
1225-
// check commitments
1226-
txnRoot, err := eval.block.PaysetCommit()
1227-
if err != nil {
1228-
return err
1229-
}
1230-
if txnRoot != eval.block.TxnRoot {
1231-
return fmt.Errorf("txn root wrong: %v != %v", txnRoot, eval.block.TxnRoot)
1232-
}
1233-
1234-
var expectedTxnCount uint64
1235-
if eval.proto.TxnCounter {
1236-
expectedTxnCount = eval.state.txnCounter()
1237-
}
1238-
if eval.block.TxnCounter != expectedTxnCount {
1239-
return fmt.Errorf("txn count wrong: %d != %d", eval.block.TxnCounter, expectedTxnCount)
1240-
}
1241-
1242-
expectedVoters, expectedVotersWeight, err := eval.compactCertVotersAndTotal()
1243-
if err != nil {
1244-
return err
1245-
}
1246-
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters != expectedVoters {
1247-
return fmt.Errorf("CompactCertVoters wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters, expectedVoters)
1248-
}
1249-
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal != expectedVotersWeight {
1250-
return fmt.Errorf("CompactCertVotersTotal wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal, expectedVotersWeight)
1251-
}
1252-
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound != eval.state.compactCertNext() {
1253-
return fmt.Errorf("CompactCertNextRound wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound, eval.state.compactCertNext())
1254-
}
1255-
for ccType := range eval.block.CompactCert {
1256-
if ccType != protocol.CompactCertBasic {
1257-
return fmt.Errorf("CompactCertType %d unexpected", ccType)
1258-
}
1259-
}
1260-
1261-
}
1262-
1263-
return eval.state.CalculateTotals()
1264-
}
1265-
12661265
// GenerateBlock produces a complete block from the BlockEvaluator. This is
12671266
// used during proposal to get an actual block that will be proposed, after
12681267
// feeding in tentative transactions into this block evaluator.
@@ -1284,11 +1283,6 @@ func (eval *BlockEvaluator) GenerateBlock() (*ledgercore.ValidatedBlock, error)
12841283
return nil, err
12851284
}
12861285

1287-
err = eval.finalValidation()
1288-
if err != nil {
1289-
return nil, err
1290-
}
1291-
12921286
vb := ledgercore.MakeValidatedBlock(eval.block, eval.state.deltas())
12931287
eval.blockGenerated = true
12941288
proto, ok := config.Consensus[eval.block.BlockHeader.CurrentProtocol]
@@ -1450,11 +1444,6 @@ transactionGroupLoop:
14501444
}
14511445
}
14521446

1453-
err = eval.finalValidation()
1454-
if err != nil {
1455-
return ledgercore.StateDelta{}, err
1456-
}
1457-
14581447
return eval.state.deltas(), nil
14591448
}
14601449

ledger/internal/evalindexer.go

-6
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,5 @@ func (eval *BlockEvaluator) ProcessBlockForIndexer(block *bookkeeping.Block) (le
4747
fmt.Errorf("ProcessBlockForIndexer() err: %w", err)
4848
}
4949

50-
err = eval.finalValidation()
51-
if err != nil {
52-
return ledgercore.StateDelta{}, []transactions.SignedTxnInBlock{},
53-
fmt.Errorf("ProcessBlockForIndexer() err: %w", err)
54-
}
55-
5650
return eval.state.deltas(), eval.block.Payset, nil
5751
}

0 commit comments

Comments
 (0)