forked from ava-labs/subnet-evm
/
sync_helpers.go
39 lines (35 loc) · 1.42 KB
/
sync_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// (c) 2021-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package statesync
import (
"github.com/MetalBlockchain/subnet-evm/core/rawdb"
"github.com/MetalBlockchain/subnet-evm/core/state/snapshot"
"github.com/MetalBlockchain/subnet-evm/core/types"
"github.com/MetalBlockchain/subnet-evm/ethdb"
"github.com/MetalBlockchain/subnet-evm/trie"
"github.com/ethereum/go-ethereum/common"
)
// writeAccountSnapshot stores the account represented by [acc] to the snapshot at [accHash], using
// SlimAccountRLP format (omitting empty code/storage).
func writeAccountSnapshot(db ethdb.KeyValueWriter, accHash common.Hash, acc types.StateAccount) {
slimAccount := snapshot.SlimAccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash)
rawdb.WriteAccountSnapshot(db, accHash, slimAccount)
}
// writeAccountStorageSnapshotFromTrie iterates the trie at [storageTrie] and copies all entries
// to the storage snapshot for [accountHash].
func writeAccountStorageSnapshotFromTrie(batch ethdb.Batch, batchSize int, accountHash common.Hash, storageTrie *trie.Trie) error {
it := trie.NewIterator(storageTrie.NodeIterator(nil))
for it.Next() {
rawdb.WriteStorageSnapshot(batch, accountHash, common.BytesToHash(it.Key), it.Value)
if batch.ValueSize() > batchSize {
if err := batch.Write(); err != nil {
return err
}
batch.Reset()
}
}
if it.Err != nil {
return it.Err
}
return batch.Write()
}