-
Notifications
You must be signed in to change notification settings - Fork 214
[Storehouse] 005 Payloadless View Committer #8576
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
Open
zhangchiqing
wants to merge
7
commits into
leo/payloadless-ledger
from
leo/payloadless-view-committer
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d57465a
remote ledger service
zhangchiqing 9872242
HashState to return (bool, error) for distinguishing exceptions
zhangchiqing be8a9ca
reject tailing bytes in decodePayloadlessTrieBatchProof
zhangchiqing 43258bb
add payloadless view committer
zhangchiqing c8a068b
refactor reconstructPayloadlessProof
zhangchiqing 05e7849
fix lint
zhangchiqing 2a29ca6
Apply go-fix rewrites for admin unit test job
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
engine/execution/computation/committer/payloadless_committer.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package committer | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/hashicorp/go-multierror" | ||
|
|
||
| "github.com/onflow/flow-go/engine/execution" | ||
| execState "github.com/onflow/flow-go/engine/execution/state" | ||
| "github.com/onflow/flow-go/fvm/storage/snapshot" | ||
| "github.com/onflow/flow-go/ledger" | ||
| "github.com/onflow/flow-go/ledger/complete/payloadless" | ||
| "github.com/onflow/flow-go/model/flow" | ||
| "github.com/onflow/flow-go/module" | ||
| ) | ||
|
|
||
| // PayloadlessLedgerViewCommitter commits an execution snapshot to a | ||
| // payloadless ledger and returns a proof whose leaves are reconstructed back | ||
| // to full ledger payloads using values read from the pre-execution storage | ||
| // snapshot. The returned bytes are wire-compatible with the full mtrie's | ||
| // proof format: downstream consumers decode them with | ||
| // ledger.DecodeTrieBatchProof, identical to full-mode behaviour. | ||
| // | ||
| // It is the payloadless-mode counterpart of LedgerViewCommitter and satisfies | ||
| // the same computer.ViewCommitter interface. Mode is selected by which | ||
| // constructor is called at startup; there is no runtime mode flag. | ||
| type PayloadlessLedgerViewCommitter struct { | ||
| ledger ledger.PayloadlessLedger | ||
| tracer module.Tracer | ||
| pathFinderVersion uint8 | ||
| } | ||
|
|
||
| // NewPayloadlessLedgerViewCommitter returns a committer that drives a | ||
| // payloadless ledger and emits reconstructed full-format proof bytes. | ||
| // | ||
| // pathFinderVersion must match the version used by the underlying ledger so | ||
| // the path → registerID mapping built during reconstruction agrees with the | ||
| // paths carried by the proof. In production this is | ||
| // complete.DefaultPathFinderVersion. | ||
| func NewPayloadlessLedgerViewCommitter( | ||
| ledger ledger.PayloadlessLedger, | ||
| tracer module.Tracer, | ||
| pathFinderVersion uint8, | ||
| ) *PayloadlessLedgerViewCommitter { | ||
| return &PayloadlessLedgerViewCommitter{ | ||
| ledger: ledger, | ||
| tracer: tracer, | ||
| pathFinderVersion: pathFinderVersion, | ||
| } | ||
| } | ||
|
|
||
| // CommitView commits an execution snapshot and collects a payloadless proof. | ||
| // Concurrency: state commitment and proof collection run in parallel, matching LedgerViewCommitter. | ||
| func (committer *PayloadlessLedgerViewCommitter) CommitView( | ||
| snapshot *snapshot.ExecutionSnapshot, | ||
| baseStorageSnapshot execution.ExtendableStorageSnapshot, | ||
| ) ( | ||
| newCommit flow.StateCommitment, | ||
| proof []byte, | ||
| trieUpdate *ledger.TrieUpdate, | ||
| newStorageSnapshot execution.ExtendableStorageSnapshot, | ||
| err error, | ||
| ) { | ||
| var err1, err2 error | ||
| var wg sync.WaitGroup | ||
| wg.Go(func() { | ||
| proof, err2 = committer.collectProofs(snapshot, baseStorageSnapshot) | ||
| }) | ||
|
|
||
| newCommit, trieUpdate, newStorageSnapshot, err1 = committer.commitDelta(snapshot, baseStorageSnapshot) | ||
| wg.Wait() | ||
|
|
||
| if err1 != nil { | ||
| err = multierror.Append(err, err1) | ||
| } | ||
| if err2 != nil { | ||
| err = multierror.Append(err, err2) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // commitDelta mirrors execState.CommitDelta but accepts a PayloadlessLedger | ||
| // directly so we don't have to widen the shared helper's ledger.Ledger | ||
| // dependency. The body is byte-for-byte equivalent to execState.CommitDelta | ||
| // up to the ledger.Set call. | ||
| func (committer *PayloadlessLedgerViewCommitter) commitDelta( | ||
| ruh execState.RegisterUpdatesHolder, | ||
| baseStorageSnapshot execution.ExtendableStorageSnapshot, | ||
| ) (flow.StateCommitment, *ledger.TrieUpdate, execution.ExtendableStorageSnapshot, error) { | ||
|
|
||
| updatedRegisters := ruh.UpdatedRegisters() | ||
| keys, values := execState.RegisterEntriesToKeysValues(updatedRegisters) | ||
| baseState := baseStorageSnapshot.Commitment() | ||
| update, err := ledger.NewUpdate(ledger.State(baseState), keys, values) | ||
| if err != nil { | ||
| return flow.DummyStateCommitment, nil, nil, fmt.Errorf("cannot create ledger update: %w", err) | ||
| } | ||
|
|
||
| newState, trieUpdate, err := committer.ledger.Set(update) | ||
| if err != nil { | ||
| return flow.DummyStateCommitment, nil, nil, fmt.Errorf("could not update ledger: %w", err) | ||
| } | ||
|
|
||
| newCommit := flow.StateCommitment(newState) | ||
| newStorageSnapshot := baseStorageSnapshot.Extend(newCommit, ruh.UpdatedRegisterSet()) | ||
|
|
||
| return newCommit, trieUpdate, newStorageSnapshot, nil | ||
| } | ||
|
|
||
| // collectProofs queries the payloadless ledger for a proof over all registers | ||
| // touched by the execution snapshot (read and written), then reconstructs the | ||
| // proof's leaf values from the pre-execution storage snapshot. The returned | ||
| // bytes encode a *ledger.TrieBatchProof — wire-compatible with the full | ||
| // committer's output. | ||
| func (committer *PayloadlessLedgerViewCommitter) collectProofs( | ||
| execSnapshot *snapshot.ExecutionSnapshot, | ||
| baseStorageSnapshot execution.ExtendableStorageSnapshot, | ||
| ) ( | ||
| proof []byte, | ||
| err error, | ||
| ) { | ||
| baseState := baseStorageSnapshot.Commitment() | ||
| // Reason for including AllRegisterIDs (read and written registers) instead of ReadRegisterIDs (only read registers): | ||
| // AllRegisterIDs returns deduplicated register IDs that were touched by both | ||
| // reads and writes during the block execution. | ||
| // Verification nodes only need the registers in the storage proof that were touched by reads | ||
| // in order to execute transactions in a chunk. However, without the registers touched | ||
| // by writes, especially the interim trie nodes for them, verification nodes won't be | ||
| // able to reconstruct the trie root hash of the execution state post execution. That's why | ||
| // the storage proof needs both read registers and write registers, which specifically is AllRegisterIDs | ||
| allIds := execSnapshot.AllRegisterIDs() | ||
|
|
||
| // The proof is generated from baseState (pre-execution), so we read | ||
| // pre-execution values to re-attach to leaves during reconstruction. | ||
| valueReader := func(id flow.RegisterID) (flow.RegisterValue, error) { | ||
| return baseStorageSnapshot.Get(id) | ||
| } | ||
|
|
||
| proof, err = payloadless.ProveAndReconstruct( | ||
| committer.ledger, | ||
| ledger.State(baseState), | ||
| allIds, | ||
| valueReader, | ||
| committer.pathFinderVersion, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not collect payloadless proof: %w", err) | ||
| } | ||
| return proof, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valueReader is the storehouse. Reconstructing the proof happens in the ProveAndReconstruct functions, it also verifies the payload value retrieved from valueReader matches the leafHash from the payloadless trie.