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

ledger: do final validation in endOfBlock() #3132

Merged
merged 2 commits into from
Oct 29, 2021
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
101 changes: 45 additions & 56 deletions ledger/internal/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,51 @@ func (eval *BlockEvaluator) endOfBlock() error {
return err
}

eval.state.mods.OptimizeAllocatedMemory(eval.proto)

if eval.validate {
// check commitments
txnRoot, err := eval.block.PaysetCommit()
if err != nil {
return err
}
if txnRoot != eval.block.TxnRoot {
return fmt.Errorf("txn root wrong: %v != %v", txnRoot, eval.block.TxnRoot)
}

var expectedTxnCount uint64
if eval.proto.TxnCounter {
expectedTxnCount = eval.state.txnCounter()
}
if eval.block.TxnCounter != expectedTxnCount {
return fmt.Errorf("txn count wrong: %d != %d", eval.block.TxnCounter, expectedTxnCount)
}

expectedVoters, expectedVotersWeight, err := eval.compactCertVotersAndTotal()
if err != nil {
return err
}
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters != expectedVoters {
return fmt.Errorf("CompactCertVoters wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters, expectedVoters)
}
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal != expectedVotersWeight {
return fmt.Errorf("CompactCertVotersTotal wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal, expectedVotersWeight)
}
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound != eval.state.compactCertNext() {
return fmt.Errorf("CompactCertNextRound wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound, eval.state.compactCertNext())
}
for ccType := range eval.block.CompactCert {
if ccType != protocol.CompactCertBasic {
return fmt.Errorf("CompactCertType %d unexpected", ccType)
}
}
}

err = eval.state.CalculateTotals()
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -1187,7 +1232,6 @@ func (eval *BlockEvaluator) validateExpiredOnlineAccounts() error {

// resetExpiredOnlineAccountsParticipationKeys after all transactions and rewards are processed, modify the accounts so that their status is offline
func (eval *BlockEvaluator) resetExpiredOnlineAccountsParticipationKeys() error {

expectedMaxNumberOfExpiredAccounts := eval.proto.MaxProposedExpiredOnlineAccounts
lengthOfExpiredParticipationAccounts := len(eval.block.ParticipationUpdates.ExpiredParticipationAccounts)

Expand Down Expand Up @@ -1216,51 +1260,6 @@ func (eval *BlockEvaluator) resetExpiredOnlineAccountsParticipationKeys() error
return nil
}

// FinalValidation does the validation that must happen after the block is built and all state updates are computed
func (eval *BlockEvaluator) finalValidation() error {
eval.state.mods.OptimizeAllocatedMemory(eval.proto)
if eval.validate {
// check commitments
txnRoot, err := eval.block.PaysetCommit()
if err != nil {
return err
}
if txnRoot != eval.block.TxnRoot {
return fmt.Errorf("txn root wrong: %v != %v", txnRoot, eval.block.TxnRoot)
}

var expectedTxnCount uint64
if eval.proto.TxnCounter {
expectedTxnCount = eval.state.txnCounter()
}
if eval.block.TxnCounter != expectedTxnCount {
return fmt.Errorf("txn count wrong: %d != %d", eval.block.TxnCounter, expectedTxnCount)
}

expectedVoters, expectedVotersWeight, err := eval.compactCertVotersAndTotal()
if err != nil {
return err
}
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters != expectedVoters {
return fmt.Errorf("CompactCertVoters wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVoters, expectedVoters)
}
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal != expectedVotersWeight {
return fmt.Errorf("CompactCertVotersTotal wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertVotersTotal, expectedVotersWeight)
}
if eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound != eval.state.compactCertNext() {
return fmt.Errorf("CompactCertNextRound wrong: %v != %v", eval.block.CompactCert[protocol.CompactCertBasic].CompactCertNextRound, eval.state.compactCertNext())
}
for ccType := range eval.block.CompactCert {
if ccType != protocol.CompactCertBasic {
return fmt.Errorf("CompactCertType %d unexpected", ccType)
}
}

}

return eval.state.CalculateTotals()
}

// GenerateBlock produces a complete block from the BlockEvaluator. This is
// used during proposal to get an actual block that will be proposed, after
// feeding in tentative transactions into this block evaluator.
Expand All @@ -1282,11 +1281,6 @@ func (eval *BlockEvaluator) GenerateBlock() (*ledgercore.ValidatedBlock, error)
return nil, err
}

err = eval.finalValidation()
if err != nil {
return nil, err
}

vb := ledgercore.MakeValidatedBlock(eval.block, eval.state.deltas())
eval.blockGenerated = true
proto, ok := config.Consensus[eval.block.BlockHeader.CurrentProtocol]
Expand Down Expand Up @@ -1448,11 +1442,6 @@ transactionGroupLoop:
}
}

err = eval.finalValidation()
if err != nil {
return ledgercore.StateDelta{}, err
}

return eval.state.deltas(), nil
}

Expand Down
6 changes: 0 additions & 6 deletions ledger/internal/evalindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,5 @@ func (eval *BlockEvaluator) ProcessBlockForIndexer(block *bookkeeping.Block) (le
fmt.Errorf("ProcessBlockForIndexer() err: %w", err)
}

err = eval.finalValidation()
if err != nil {
return ledgercore.StateDelta{}, []transactions.SignedTxnInBlock{},
fmt.Errorf("ProcessBlockForIndexer() err: %w", err)
}

return eval.state.deltas(), eval.block.Payset, nil
}