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

feat: dual code hash #583

Merged
merged 32 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
07d6c66
add crypto/codehash/codehash.go
0xmountaintop Nov 27, 2023
d081a69
init EmptyCodeHash
0xmountaintop Nov 27, 2023
3a52b19
fix
0xmountaintop Nov 27, 2023
e8498e6
WIP
0xmountaintop Nov 27, 2023
221a197
fix
0xmountaintop Nov 27, 2023
4751b5d
???
0xmountaintop Nov 27, 2023
e2c0331
minor
0xmountaintop Nov 27, 2023
d15325b
update light/trie.go
0xmountaintop Nov 27, 2023
c4844af
update cmd/geth/snapshot.go
0xmountaintop Nov 27, 2023
8ef072c
update core/state/dump.go
0xmountaintop Nov 27, 2023
301d42a
update core/state/snapshot/conversion.go
0xmountaintop Nov 28, 2023
bb5126e
update core/state/snapshot/generate.go
0xmountaintop Nov 28, 2023
8e63d7b
update core/state/pruner/pruner.go
0xmountaintop Nov 29, 2023
c3f7a3a
update core/state/iterator.go
0xmountaintop Nov 29, 2023
0ea2b24
update core/state/sync.go
0xmountaintop Nov 29, 2023
ad1fa1c
update core/state/state_object.go
0xmountaintop Nov 29, 2023
b445d54
update core/state/statedb.go
0xmountaintop Nov 29, 2023
55044bd
update core/vm/interface.go
0xmountaintop Nov 29, 2023
b8cdbd9
update core/vm/evm.go
0xmountaintop Nov 29, 2023
7f8bdc1
update core/vm/instructions.go
0xmountaintop Nov 29, 2023
f1c7e31
update core/vm/contract.go
0xmountaintop Nov 29, 2023
883e912
update core/state_transition.go
0xmountaintop Nov 29, 2023
e2883e1
update les/server_requests.go
0xmountaintop Nov 29, 2023
c3736c0
update eth/protocols/snap/sync.go
0xmountaintop Nov 29, 2023
5487eb1
update internal/ethapi/api.go
0xmountaintop Nov 29, 2023
ac1c4de
update cmd/geth/snapshot.go
0xmountaintop Nov 29, 2023
1e66a46
update ethclient/gethclient/gethclient.go
0xmountaintop Nov 29, 2023
eae3919
gofmt
0xmountaintop Nov 29, 2023
74da83b
fix `(s *stateObject) SetCode`
0xmountaintop Nov 30, 2023
99cd37a
fix tests
0xmountaintop Nov 30, 2023
1460b2d
minor
0xmountaintop Nov 30, 2023
df0d505
minor
0xmountaintop Dec 1, 2023
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
6 changes: 3 additions & 3 deletions cmd/devp2p/internal/ethtest/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ func (s *Suite) TestSnapGetByteCodes(t *utesting.T) {
expHashes: 0,
},
{
nBytes: 10000, hashes: []common.Hash{types.EmptyCodeHash},
nBytes: 10000, hashes: []common.Hash{types.EmptyKeccakCodeHash},
expHashes: 1,
},
{
nBytes: 10000, hashes: []common.Hash{types.EmptyCodeHash, types.EmptyCodeHash, types.EmptyCodeHash},
nBytes: 10000, hashes: []common.Hash{types.EmptyKeccakCodeHash, types.EmptyKeccakCodeHash, types.EmptyKeccakCodeHash},
expHashes: 3,
},
// The existing bytecodes
Expand Down Expand Up @@ -357,7 +357,7 @@ func (s *Suite) TestSnapTrieNodes(t *utesting.T) {
for i := 1; i <= 65; i++ {
accPaths = append(accPaths, pathTo(i))
}
empty := types.EmptyCodeHash
empty := types.EmptyKeccakCodeHash
for i, tc := range []trieNodesTest{
{
root: s.chain.RootAt(999),
Expand Down
25 changes: 13 additions & 12 deletions cmd/geth/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ func traverseState(ctx *cli.Context) error {
return storageIter.Err
}
}
if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash.Bytes()) {
if !rawdb.HasCode(chaindb, common.BytesToHash(acc.CodeHash)) {
log.Error("Code is missing", "hash", common.BytesToHash(acc.CodeHash))
if !bytes.Equal(acc.KeccakCodeHash, types.EmptyKeccakCodeHash.Bytes()) {
if !rawdb.HasCode(chaindb, common.BytesToHash(acc.KeccakCodeHash)) {
log.Error("Code is missing", "hash", common.BytesToHash(acc.KeccakCodeHash))
return errors.New("missing code")
}
codes += 1
Expand Down Expand Up @@ -496,8 +496,8 @@ func traverseRawState(ctx *cli.Context) error {
return storageIter.Error()
}
}
if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash.Bytes()) {
if !rawdb.HasCode(chaindb, common.BytesToHash(acc.CodeHash)) {
if !bytes.Equal(acc.KeccakCodeHash, types.EmptyKeccakCodeHash.Bytes()) {
if !rawdb.HasCode(chaindb, common.BytesToHash(acc.KeccakCodeHash)) {
log.Error("Code is missing", "account", common.BytesToHash(accIter.LeafKey()))
return errors.New("missing code")
}
Expand Down Expand Up @@ -568,14 +568,15 @@ func dumpState(ctx *cli.Context) error {
return err
}
da := &state.DumpAccount{
Balance: account.Balance.String(),
Nonce: account.Nonce,
Root: account.Root.Bytes(),
CodeHash: account.CodeHash,
SecureKey: accIt.Hash().Bytes(),
Balance: account.Balance.String(),
Nonce: account.Nonce,
Root: account.Root.Bytes(),
KeccakCodeHash: account.KeccakCodeHash,
PoseidonCodeHash: account.PoseidonCodeHash,
SecureKey: accIt.Hash().Bytes(),
}
if !conf.SkipCode && !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
da.Code = rawdb.ReadCode(db, common.BytesToHash(account.CodeHash))
if !conf.SkipCode && !bytes.Equal(account.KeccakCodeHash, types.EmptyKeccakCodeHash.Bytes()) {
da.Code = rawdb.ReadCode(db, common.BytesToHash(account.KeccakCodeHash))
}
if !conf.SkipStorage {
da.Storage = make(map[common.Hash]string)
Expand Down
45 changes: 24 additions & 21 deletions core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ type DumpCollector interface {

// DumpAccount represents an account in the state.
type DumpAccount struct {
Balance string `json:"balance"`
Nonce uint64 `json:"nonce"`
Root hexutil.Bytes `json:"root"`
CodeHash hexutil.Bytes `json:"codeHash"`
Code hexutil.Bytes `json:"code,omitempty"`
Storage map[common.Hash]string `json:"storage,omitempty"`
Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
Balance string `json:"balance"`
Nonce uint64 `json:"nonce"`
Root hexutil.Bytes `json:"root"`
KeccakCodeHash hexutil.Bytes `json:"keccakCodeHash"`
PoseidonCodeHash hexutil.Bytes `json:"poseidonCodeHash"`
Code hexutil.Bytes `json:"code,omitempty"`
Storage map[common.Hash]string `json:"storage,omitempty"`
Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key

0xmountaintop marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -105,14 +106,15 @@ type iterativeDump struct {
// OnAccount implements DumpCollector interface
func (d iterativeDump) OnAccount(addr *common.Address, account DumpAccount) {
dumpAccount := &DumpAccount{
Balance: account.Balance,
Nonce: account.Nonce,
Root: account.Root,
CodeHash: account.CodeHash,
Code: account.Code,
Storage: account.Storage,
SecureKey: account.SecureKey,
Address: addr,
Balance: account.Balance,
Nonce: account.Nonce,
Root: account.Root,
KeccakCodeHash: account.KeccakCodeHash,
PoseidonCodeHash: account.PoseidonCodeHash,
Code: account.Code,
Storage: account.Storage,
SecureKey: account.SecureKey,
Address: addr,
}
d.Encode(dumpAccount)
}
Expand Down Expand Up @@ -151,11 +153,12 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
panic(err)
}
account := DumpAccount{
Balance: data.Balance.String(),
Nonce: data.Nonce,
Root: data.Root[:],
CodeHash: data.CodeHash,
SecureKey: it.Key,
Balance: data.Balance.String(),
Nonce: data.Nonce,
Root: data.Root[:],
KeccakCodeHash: data.KeccakCodeHash,
PoseidonCodeHash: data.PoseidonCodeHash,
SecureKey: it.Key,
}
var (
addrBytes = s.trie.GetKey(it.Key)
Expand Down
8 changes: 4 additions & 4 deletions core/state/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ func (it *nodeIterator) step() error {
if !it.dataIt.Next(true) {
it.dataIt = nil
}
if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
it.codeHash = common.BytesToHash(account.CodeHash)
it.code, err = it.state.db.ContractCode(address, common.BytesToHash(account.CodeHash))
if !bytes.Equal(account.KeccakCodeHash, types.EmptyKeccakCodeHash.Bytes()) {
it.codeHash = common.BytesToHash(account.KeccakCodeHash)
it.code, err = it.state.db.ContractCode(address, common.BytesToHash(account.KeccakCodeHash))
if err != nil {
return fmt.Errorf("code %x: %v", account.CodeHash, err)
return fmt.Errorf("code %x: %v", account.KeccakCodeHash, err)
}
}
it.accountHash = it.stateIt.Parent()
Expand Down
2 changes: 1 addition & 1 deletion core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (ch nonceChange) dirtied() *common.Address {
}

func (ch codeChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
s.getStateObject(*ch.account).setCode(ch.prevcode)
}

func (ch codeChange) dirtied() *common.Address {
Expand Down
4 changes: 2 additions & 2 deletions core/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error {
return storageIter.Error()
}
}
if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash.Bytes()) {
stateBloom.Put(acc.CodeHash, nil)
if !bytes.Equal(acc.KeccakCodeHash, types.EmptyKeccakCodeHash.Bytes()) {
stateBloom.Put(acc.KeccakCodeHash, nil)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/state/snapshot/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GenerateTrie(snaptree *Tree, root common.Hash, src ethdb.Database, dst ethd
scheme := snaptree.triedb.Scheme()
got, err := generateTrieRoot(dst, scheme, acctIt, common.Hash{}, stackTrieGenerate, func(dst ethdb.KeyValueWriter, accountHash, codeHash common.Hash, stat *generateStats) (common.Hash, error) {
// Migrate the code first, commit the contract code into the tmp db.
if codeHash != types.EmptyCodeHash {
if codeHash != types.EmptyKeccakCodeHash {
code := rawdb.ReadCode(src, codeHash)
if len(code) == 0 {
return common.Hash{}, errors.New("failed to read contract code")
Expand Down Expand Up @@ -317,7 +317,7 @@ func generateTrieRoot(db ethdb.KeyValueWriter, scheme string, it Iterator, accou
return stop(err)
}
go func(hash common.Hash) {
subroot, err := leafCallback(db, hash, common.BytesToHash(account.CodeHash), stats)
subroot, err := leafCallback(db, hash, common.BytesToHash(account.KeccakCodeHash), stats)
if err != nil {
results <- err
return
Expand Down
7 changes: 5 additions & 2 deletions core/state/snapshot/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,11 @@ func generateAccounts(ctx *generatorContext, dl *diskLayer, accMarker []byte) er
if accMarker == nil || !bytes.Equal(account[:], accMarker) {
dataLen := len(val) // Approximate size, saves us a round of RLP-encoding
if !write {
if bytes.Equal(acc.CodeHash, types.EmptyCodeHash[:]) {
dataLen -= 32
if bytes.Equal(acc.KeccakCodeHash, types.EmptyKeccakCodeHash[:]) {
// TODO: codesize
// dataLen = dataLen - 32 - 32 - 8
// account for keccakCodeHash, poseidonCodeHash
dataLen = dataLen - 32 - 32
}
if acc.Root == types.EmptyRootHash {
dataLen -= 32
Expand Down
Loading
Loading