-
Notifications
You must be signed in to change notification settings - Fork 524
ledger: draft of adding UpdateCommitment header from StateDelta #6497
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
Draft
cce
wants to merge
4
commits into
algorand:master
Choose a base branch
from
cce:updatetrie
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,91 @@ | ||
| // Copyright (C) 2019-2025 Algorand, Inc. | ||
| // This file is part of go-algorand | ||
| // | ||
| // go-algorand is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // go-algorand is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package statecommit | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "slices" | ||
|
|
||
| "github.com/algorand/go-algorand/crypto" | ||
| "github.com/algorand/go-algorand/crypto/merklearray" | ||
| "github.com/algorand/go-algorand/protocol" | ||
| ) | ||
|
|
||
| // stateUpdate represents a single insert/update or delete operation | ||
| type stateUpdate struct { | ||
| _struct struct{} `codec:",omitempty,omitemptyarray"` | ||
|
|
||
| Key []byte `codec:"k"` | ||
| Value []byte `codec:"v"` | ||
| Deleted bool `codec:"d"` | ||
| } | ||
|
|
||
| // ToBeHashed implements crypto.Hashable for stateUpdate | ||
| func (u *stateUpdate) ToBeHashed() (protocol.HashID, []byte) { | ||
| return protocol.StateUpdateLeaf, protocol.Encode(u) | ||
| } | ||
|
|
||
| // merkleArrayCommitter implements UpdateCommitter using a Merkle array | ||
| type merkleArrayCommitter struct{ updates []stateUpdate } | ||
|
|
||
| // newMerkleArrayCommitter creates a new Merkle array-based update committer | ||
| func newMerkleArrayCommitter() UpdateCommitter { return &merkleArrayCommitter{} } | ||
|
|
||
| // updateArray implements merklearray.Array for stateUpdates | ||
| type updateArray struct{ updates []stateUpdate } | ||
|
|
||
| func (a *updateArray) Length() uint64 { return uint64(len(a.updates)) } | ||
| func (a *updateArray) Marshal(pos uint64) (crypto.Hashable, error) { return &a.updates[pos], nil } | ||
|
|
||
| // Add adds a key-value update | ||
| func (m *merkleArrayCommitter) Add(key, val []byte) error { | ||
| m.updates = append(m.updates, stateUpdate{Key: key, Value: val, Deleted: false}) | ||
| return nil | ||
| } | ||
|
|
||
| // Delete adds a deletion update | ||
| func (m *merkleArrayCommitter) Delete(key []byte) error { | ||
| m.updates = append(m.updates, stateUpdate{Key: key, Value: nil, Deleted: true}) | ||
| return nil | ||
| } | ||
|
|
||
| // Root returns the Merkle root commitment of all updates | ||
| func (m *merkleArrayCommitter) Root() (crypto.Sha512Digest, error) { | ||
| if len(m.updates) == 0 { | ||
| return crypto.Sha512Digest{}, nil | ||
| } | ||
|
|
||
| // Sort updates by key to ensure deterministic commitment (KvMods is a map) | ||
| slices.SortFunc(m.updates, func(a, b stateUpdate) int { return bytes.Compare(a.Key, b.Key) }) | ||
|
|
||
| array := &updateArray{updates: m.updates} | ||
| // not calling merklearray.BuildVectorCommitmentTree (we don't want proof of position in array) | ||
| tree, err := merklearray.Build(array, crypto.HashFactory{HashType: crypto.Sha512}) | ||
| if err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
|
|
||
| rootSlice := tree.Root().ToSlice() | ||
| var root crypto.Sha512Digest | ||
| copy(root[:], rootSlice) | ||
| return root, nil | ||
| } | ||
|
|
||
| // Reset clears the committer state | ||
| func (m *merkleArrayCommitter) Reset() { | ||
| m.updates = nil | ||
| } | ||
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 @@ | ||
| // Copyright (C) 2019-2025 Algorand, Inc. | ||
| // This file is part of go-algorand | ||
| // | ||
| // go-algorand is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // go-algorand is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package statecommit | ||
|
|
||
| import ( | ||
| "github.com/algorand/go-algorand/crypto" | ||
| "github.com/algorand/go-algorand/data/basics" | ||
| "github.com/algorand/go-algorand/ledger/ledgercore" | ||
| "github.com/algorand/go-algorand/protocol" | ||
| ) | ||
|
|
||
| // stateChange represents any type of state change that can be encoded into a trie update. | ||
| // This interface abstracts over accounts, resources (assets/apps), and key-value pairs. | ||
| type stateChange interface { | ||
| isDeleted() bool | ||
| hasNewValue() bool | ||
| encodeKey() []byte | ||
| encodeValue() []byte | ||
| } | ||
|
|
||
| // Wrapper types to allow various delta types to implement the stateChange interface. | ||
| // Each wrapper provides methods for encoding one specific delta type. | ||
|
|
||
| //msgp:ignore accountUpdate | ||
| type accountUpdate ledgercore.BalanceRecord | ||
|
|
||
| func (u *accountUpdate) isDeleted() bool { return u.AccountData.IsZero() } | ||
| func (u *accountUpdate) hasNewValue() bool { return !u.AccountData.IsZero() } | ||
| func (u *accountUpdate) encodeKey() []byte { return EncodeAccountKey(u.Addr) } | ||
| func (u *accountUpdate) encodeValue() []byte { | ||
| // encode using codec tags on basics.AccountData | ||
| var ad basics.AccountData | ||
| ledgercore.AssignAccountData(&ad, u.AccountData) | ||
| return protocol.Encode(&ad) | ||
| } | ||
|
|
||
| //msgp:ignore kvUpdate | ||
| type kvUpdate struct { | ||
| key *string | ||
| delta *ledgercore.KvValueDelta | ||
| } | ||
|
|
||
| func (u *kvUpdate) isDeleted() bool { return u.delta.Data == nil } | ||
| func (u *kvUpdate) hasNewValue() bool { return u.delta.Data != nil } | ||
| func (u *kvUpdate) encodeKey() []byte { return EncodeKvPairKey(*u.key) } | ||
| func (u *kvUpdate) encodeValue() []byte { return u.delta.Data } // XXX need to distinguish between nil and empty? | ||
|
|
||
| //msgp:ignore assetHoldingUpdate | ||
| type assetHoldingUpdate ledgercore.AssetResourceRecord | ||
|
|
||
| func (u *assetHoldingUpdate) isDeleted() bool { return u.Holding.Deleted } | ||
| func (u *assetHoldingUpdate) hasNewValue() bool { return u.Holding.Holding != nil } | ||
| func (u *assetHoldingUpdate) encodeKey() []byte { return EncodeAssetHoldingKey(u.Addr, u.Aidx) } | ||
| func (u *assetHoldingUpdate) encodeValue() []byte { return protocol.Encode(u.Holding.Holding) } | ||
|
|
||
| //msgp:ignore assetParamsUpdate | ||
| type assetParamsUpdate ledgercore.AssetResourceRecord | ||
|
|
||
| func (u *assetParamsUpdate) isDeleted() bool { return u.Params.Deleted } | ||
| func (u *assetParamsUpdate) hasNewValue() bool { return u.Params.Params != nil } | ||
| func (u *assetParamsUpdate) encodeKey() []byte { return EncodeAssetParamsKey(u.Addr, u.Aidx) } | ||
| func (u *assetParamsUpdate) encodeValue() []byte { return protocol.Encode(u.Params.Params) } | ||
|
|
||
| //msgp:ignore appLocalStateUpdate | ||
| type appLocalStateUpdate ledgercore.AppResourceRecord | ||
|
|
||
| func (u *appLocalStateUpdate) isDeleted() bool { return u.State.Deleted } | ||
| func (u *appLocalStateUpdate) hasNewValue() bool { return u.State.LocalState != nil } | ||
| func (u *appLocalStateUpdate) encodeKey() []byte { return EncodeAppLocalStateKey(u.Addr, u.Aidx) } | ||
| func (u *appLocalStateUpdate) encodeValue() []byte { return protocol.Encode(u.State.LocalState) } | ||
|
|
||
| //msgp:ignore appParamsUpdate | ||
| type appParamsUpdate ledgercore.AppResourceRecord | ||
|
|
||
| func (u *appParamsUpdate) isDeleted() bool { return u.Params.Deleted } | ||
| func (u *appParamsUpdate) hasNewValue() bool { return u.Params.Params != nil } | ||
| func (u *appParamsUpdate) encodeKey() []byte { return EncodeAppParamsKey(u.Addr, u.Aidx) } | ||
| func (u *appParamsUpdate) encodeValue() []byte { return protocol.Encode(u.Params.Params) } | ||
|
|
||
| // maybeCommitUpdate checks if an update has changes and adds it to the committer | ||
| func maybeCommitUpdate[T stateChange](update T, committer UpdateCommitter) error { | ||
| if update.isDeleted() { | ||
| return committer.Delete(update.encodeKey()) | ||
| } | ||
| if update.hasNewValue() { | ||
| return committer.Add(update.encodeKey(), update.encodeValue()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // StateDeltaCommitment computes a cryptographic commitment to all state changes in a StateDelta. | ||
| // This is the primary function for converting ledger state changes into a state commitment. | ||
| func StateDeltaCommitment(sd *ledgercore.StateDelta) (crypto.Sha512Digest, error) { | ||
| return stateDeltaCommitmentWithCommitter(sd, newMerkleArrayCommitter()) | ||
| } | ||
|
|
||
| // stateDeltaCommitmentWithCommitter computes a cryptographic commitment using the provided UpdateCommitter. | ||
| // This allows flexibility in the commitment scheme used. | ||
| func stateDeltaCommitmentWithCommitter(sd *ledgercore.StateDelta, committer UpdateCommitter) (crypto.Sha512Digest, error) { | ||
| // Process base account data changes | ||
| for i := range sd.Accts.Accts { | ||
| if err := maybeCommitUpdate((*accountUpdate)(&sd.Accts.Accts[i]), committer); err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
| } | ||
|
|
||
| // Process asset resources (holdings and params separately) | ||
| for i := range sd.Accts.AssetResources { | ||
| rec := &sd.Accts.AssetResources[i] | ||
| if err := maybeCommitUpdate((*assetHoldingUpdate)(rec), committer); err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
| if err := maybeCommitUpdate((*assetParamsUpdate)(rec), committer); err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
| } | ||
|
|
||
| // Process app resources (local state and params separately) | ||
| for i := range sd.Accts.AppResources { | ||
| rec := &sd.Accts.AppResources[i] | ||
| if err := maybeCommitUpdate((*appLocalStateUpdate)(rec), committer); err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
| if err := maybeCommitUpdate((*appParamsUpdate)(rec), committer); err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
| } | ||
|
|
||
| // Process KV modifications | ||
| for key, kvDelta := range sd.KvMods { | ||
| if err := maybeCommitUpdate(&kvUpdate{&key, &kvDelta}, committer); err != nil { | ||
| return crypto.Sha512Digest{}, err | ||
| } | ||
| } | ||
|
|
||
| return committer.Root() | ||
| } |
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.
You don't want to use
nilValue as delete? I think we do that quite a bit elsewhere.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.
I wasn't sure about nil vs empty for boxes and wanted to double check but it would be fine for the other types