forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
MNT PART 1: core/types: adopt pyquarkchain account encoding #54
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
ping-ke
wants to merge
2
commits into
goshard/base
Choose a base branch
from
feature/py-mnt-core-types
base: goshard/base
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.
Open
Changes from all commits
Commits
Show all changes
2 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 was deleted.
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,97 @@ | ||
| // Copyright 2026-2027, QuarkChain. | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| qkccommon "github.com/ethereum/go-ethereum/qkc/common" | ||
| "github.com/ethereum/go-ethereum/rlp" | ||
| "github.com/holiman/uint256" | ||
| ) | ||
|
|
||
| // qkcAccountRLP matches pyquarkchain's six-field _Account encoding. | ||
| type qkcAccountRLP struct { | ||
| Nonce uint64 | ||
| TokenBal []byte | ||
| Root common.Hash | ||
| CodeHash []byte | ||
| FullShardKey qkccommon.Uint32 | ||
| Optional []byte | ||
| } | ||
|
|
||
| // GetMntBalance returns the balance of tokenID in the account's unified balance map. | ||
| func (acct *StateAccount) GetMntBalance(tokenID uint64) *uint256.Int { | ||
| if acct.MntBalances == nil { | ||
| return new(uint256.Int) | ||
| } | ||
| return acct.MntBalances.GetTokenBalance(tokenID) | ||
| } | ||
|
|
||
| // Balance returns the account's QKC balance. | ||
| func (acct *StateAccount) Balance() *uint256.Int { | ||
| return acct.GetMntBalance(qkccommon.DefaultTokenID) | ||
| } | ||
|
|
||
| // SetBalance sets the account's QKC balance. | ||
| func (acct *StateAccount) SetBalance(balance *uint256.Int) { | ||
| if acct.MntBalances == nil { | ||
| acct.MntBalances = qkccommon.NewEmptyTokenBalances() | ||
| } | ||
| acct.MntBalances.SetValue(balance, qkccommon.DefaultTokenID) | ||
| } | ||
|
|
||
| // NewQKCTokenBalances creates an account balance map with a QKC balance. | ||
| func NewQKCTokenBalances(balance *uint256.Int) *qkccommon.TokenBalances { | ||
| balances := qkccommon.NewEmptyTokenBalances() | ||
| if balance != nil { | ||
| balances.SetValue(balance, qkccommon.DefaultTokenID) | ||
| } | ||
| return balances | ||
| } | ||
|
|
||
| // EncodeRLP implements pyquarkchain's account encoding. | ||
| func (acct *StateAccount) EncodeRLP(w io.Writer) error { | ||
| balances := acct.MntBalances | ||
| if balances == nil { | ||
| balances = qkccommon.NewEmptyTokenBalances() | ||
| } | ||
| tokenBal, err := balances.SerializeToBytes() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return rlp.Encode(w, &qkcAccountRLP{ | ||
| Nonce: acct.Nonce, | ||
| TokenBal: tokenBal, | ||
| Root: acct.Root, | ||
| CodeHash: acct.CodeHash, | ||
| FullShardKey: qkccommon.Uint32(acct.FullShardKey), | ||
| }) | ||
| } | ||
|
|
||
| // DecodeRLP implements pyquarkchain's account decoding. | ||
| func (acct *StateAccount) DecodeRLP(s *rlp.Stream) error { | ||
| raw, err := s.Raw() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| var wire qkcAccountRLP | ||
| if err := rlp.DecodeBytes(raw, &wire); err != nil { | ||
| return err | ||
| } | ||
| if len(wire.Optional) != 0 { | ||
| return errors.New("unsupported non-empty QuarkChain account optional field") | ||
| } | ||
| acct.Nonce = wire.Nonce | ||
| acct.Root = wire.Root | ||
| acct.CodeHash = wire.CodeHash | ||
| acct.FullShardKey = uint32(wire.FullShardKey) | ||
| balances, err := qkccommon.NewTokenBalances(wire.TokenBal) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| acct.MntBalances = balances | ||
| return 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,180 @@ | ||
| // Copyright 2026-2027, QuarkChain. | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "testing" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| qkccommon "github.com/ethereum/go-ethereum/qkc/common" | ||
| "github.com/ethereum/go-ethereum/rlp" | ||
| "github.com/holiman/uint256" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| var ( | ||
| pyqkcAccountQKC = decodeStateAccountHex("f853018900c7c6828bb08203e8a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470840000000180") | ||
| pyqkcAccountMNT = decodeStateAccountHex("f858058e00ccc4648201f4c6828bb08207d0a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470840000000180") | ||
| pyqkcAccountZero = decodeStateAccountHex("f84a8080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470840000000180") | ||
| ) | ||
|
|
||
| func decodeStateAccountHex(input string) []byte { | ||
| data, err := hex.DecodeString(input) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| func TestStateAccountPyquarkchainEncoding(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| acct StateAccount | ||
| want []byte | ||
| }{ | ||
| { | ||
| name: "QKC balance", | ||
| acct: StateAccount{ | ||
| Nonce: 1, | ||
| MntBalances: qkccommon.NewTokenBalancesWithMap(map[uint64]*uint256.Int{ | ||
| qkccommon.DefaultTokenID: uint256.NewInt(1000), | ||
| }), | ||
| Root: EmptyRootHash, CodeHash: EmptyCodeHash[:], FullShardKey: 1, | ||
| }, | ||
| want: pyqkcAccountQKC, | ||
| }, | ||
| { | ||
| name: "QKC and MNT balances", | ||
| acct: StateAccount{ | ||
| Nonce: 5, | ||
| MntBalances: qkccommon.NewTokenBalancesWithMap(map[uint64]*uint256.Int{ | ||
| 100: uint256.NewInt(500), | ||
| qkccommon.DefaultTokenID: uint256.NewInt(2000), | ||
| }), | ||
| Root: EmptyRootHash, | ||
| CodeHash: EmptyCodeHash[:], | ||
| FullShardKey: 1, | ||
| }, | ||
| want: pyqkcAccountMNT, | ||
| }, | ||
| { | ||
| name: "zero account", | ||
| acct: StateAccount{MntBalances: qkccommon.NewEmptyTokenBalances(), Root: EmptyRootHash, CodeHash: EmptyCodeHash[:], FullShardKey: 1}, | ||
| want: pyqkcAccountZero, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| got, err := rlp.EncodeToBytes(&test.acct) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, test.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestStateAccountPyquarkchainDecoding(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []byte | ||
| nonce uint64 | ||
| balances map[uint64]*uint256.Int | ||
| }{ | ||
| {name: "QKC balance", input: pyqkcAccountQKC, nonce: 1, balances: map[uint64]*uint256.Int{qkccommon.DefaultTokenID: uint256.NewInt(1000)}}, | ||
| {name: "QKC and MNT balances", input: pyqkcAccountMNT, nonce: 5, balances: map[uint64]*uint256.Int{100: uint256.NewInt(500), qkccommon.DefaultTokenID: uint256.NewInt(2000)}}, | ||
| {name: "zero account", input: pyqkcAccountZero, balances: map[uint64]*uint256.Int{}}, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var acct StateAccount | ||
| require.NoError(t, rlp.DecodeBytes(test.input, &acct)) | ||
| assert.Equal(t, test.nonce, acct.Nonce) | ||
| assert.Equal(t, EmptyRootHash, acct.Root) | ||
| assert.Equal(t, EmptyCodeHash[:], acct.CodeHash) | ||
| assert.Equal(t, uint32(1), acct.FullShardKey) | ||
| if assert.NotNil(t, acct.MntBalances) { | ||
| assert.Equal(t, test.balances, acct.MntBalances.GetBalanceMap()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestStateAccountExplicitZeroBalanceEncoding(t *testing.T) { | ||
| acct := NewEmptyStateAccount() | ||
| encoded, err := rlp.EncodeToBytes(acct) | ||
| require.NoError(t, err) | ||
| var wire qkcAccountRLP | ||
| require.NoError(t, rlp.DecodeBytes(encoded, &wire)) | ||
| assert.Empty(t, wire.TokenBal) | ||
|
|
||
| previous := acct.MntBalances.GetTokenBalance(qkccommon.DefaultTokenID) | ||
| acct.MntBalances.SetValue(uint256.NewInt(1), qkccommon.DefaultTokenID) | ||
| acct.MntBalances.SetValue(previous, qkccommon.DefaultTokenID) | ||
| encoded, err = rlp.EncodeToBytes(acct) | ||
| require.NoError(t, err) | ||
| require.NoError(t, rlp.DecodeBytes(encoded, &wire)) | ||
| assert.Equal(t, []byte{0x00, 0xc0}, wire.TokenBal) | ||
| } | ||
|
|
||
| func TestStateAccountCopyMntBalances(t *testing.T) { | ||
| original := StateAccount{ | ||
| MntBalances: qkccommon.NewTokenBalancesWithMap(map[uint64]*uint256.Int{ | ||
| 100: uint256.NewInt(2), | ||
| qkccommon.DefaultTokenID: uint256.NewInt(1), | ||
| }), | ||
| FullShardKey: 3, | ||
| } | ||
| copied := original.Copy() | ||
| copied.MntBalances.SetValue(uint256.NewInt(4), 100) | ||
| copied.MntBalances.SetValue(uint256.NewInt(5), qkccommon.DefaultTokenID) | ||
|
|
||
| assert.Equal(t, uint256.NewInt(2), original.MntBalances.GetTokenBalance(100)) | ||
| assert.Equal(t, uint256.NewInt(4), copied.MntBalances.GetTokenBalance(100)) | ||
| assert.Equal(t, uint256.NewInt(1), original.MntBalances.GetTokenBalance(qkccommon.DefaultTokenID)) | ||
| assert.Equal(t, uint256.NewInt(5), copied.MntBalances.GetTokenBalance(qkccommon.DefaultTokenID)) | ||
| assert.Equal(t, original.FullShardKey, copied.FullShardKey) | ||
| } | ||
|
|
||
| func TestStateAccountBalance(t *testing.T) { | ||
| acct := StateAccount{MntBalances: NewQKCTokenBalances(uint256.NewInt(42))} | ||
| if got := acct.Balance(); got.Cmp(uint256.NewInt(42)) != 0 { | ||
| t.Fatalf("QKC balance mismatch: have %v, want 42", got) | ||
| } | ||
| acct.SetBalance(uint256.NewInt(43)) | ||
| if got := acct.Balance(); got.Cmp(uint256.NewInt(43)) != 0 { | ||
| t.Fatalf("updated QKC balance mismatch: have %v, want 43", got) | ||
| } | ||
| acct.SetBalance(nil) | ||
| if got := acct.Balance(); !got.IsZero() { | ||
| t.Fatalf("nil QKC balance should set zero, have %v", got) | ||
| } | ||
| newAcct := &StateAccount{} | ||
| newAcct.SetBalance(uint256.NewInt(7)) | ||
| if got := newAcct.Balance(); got.Cmp(uint256.NewInt(7)) != 0 { | ||
| t.Fatalf("QKC balance on nil map mismatch: have %v, want 7", got) | ||
| } | ||
| if got := (&StateAccount{}).Balance(); !got.IsZero() { | ||
| t.Fatalf("nil balance map should return zero, have %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestStateAccountRejectsUnsupportedQKCEncoding(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| wire qkcAccountRLP | ||
| want string | ||
| }{ | ||
| {name: "token trie", wire: qkcAccountRLP{TokenBal: append([]byte{1}, make([]byte, common.HashLength)...), Root: EmptyRootHash}, want: "trie"}, | ||
| {name: "unknown token encoding", wire: qkcAccountRLP{TokenBal: []byte{2}, Root: EmptyRootHash}, want: "enum byte"}, | ||
| {name: "optional field", wire: qkcAccountRLP{Root: EmptyRootHash, Optional: []byte{1}}, want: "optional field"}, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| encoded, err := rlp.EncodeToBytes(&test.wire) | ||
| require.NoError(t, err) | ||
| var acct StateAccount | ||
| assert.ErrorContains(t, rlp.DecodeBytes(encoded, &acct), test.want) | ||
| }) | ||
| } | ||
| } |
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.
This comment is the only thing standing between the node and a silently wrong state root - nothing in the tree actually disables snap sync or the flat reader, and geth enables the snapshot by default. Once core/state is wired up in PR 2, core/state/database_mpt.go:159 calls EncodeMPTState() -> SlimAccountRLP on every commit, so the snapshot layer stores accounts with MNT balances and FullShardKey dropped, and core/state/reader.go:110 rebuilds StateAccount from them. That fails silently rather than loudly.
The conversion is also lossy in a second way the comment does not mention: FullAccount builds its balances with NewQKCTokenBalances(slim.Balance), which inserts an explicit zero entry when the balance is zero. Re-encoding then yields token_balances = 0x00c0 instead of the empty string, so even a pure-QKC account with a zero balance round-trips to a different leaf hash - snapshot-driven trie regeneration (core/state/snapshot/conversion.go) would produce a different state root.
SlimAccountRLP itself is on the unconditional commit path, so it cannot panic. Please put a real guard on the consuming side instead - refuse to construct the snapshot / flat reader, or return an error from the snapshot read path in core/state/reader.go - so that "must not be enabled" is enforced rather than documented.