github.com/klaytn/klaytn@v1.12.1/blockchain/state/sync.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/state/sync.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package state
    22  
    23  import (
    24  	"bytes"
    25  
    26  	lru "github.com/hashicorp/golang-lru"
    27  	"github.com/klaytn/klaytn/blockchain/types/account"
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/rlp"
    30  	"github.com/klaytn/klaytn/storage/statedb"
    31  )
    32  
    33  // NewStateSync create a new state trie download scheduler.
    34  // LRU cache is mendatory when state syncing and block processing are executed simultaneously
    35  func NewStateSync(root common.Hash, database statedb.StateTrieReadDB, bloom *statedb.SyncBloom, lruCache *lru.Cache, onLeaf func(paths [][]byte, leaf []byte) error) *statedb.TrieSync {
    36  	// Register the storage slot callback if the external callback is specified.
    37  	var onSlot statedb.LeafCallback
    38  	if onLeaf != nil {
    39  		onSlot = func(paths [][]byte, _ []byte, leaf []byte, _ common.ExtHash, _ int) error {
    40  			return onLeaf(paths, leaf)
    41  		}
    42  	}
    43  	// Register the account callback to connect the state trie and the storage
    44  	// trie belongs to the contract.
    45  	var syncer *statedb.TrieSync
    46  	onAccount := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.ExtHash, parentDepth int) error {
    47  		if onLeaf != nil {
    48  			if err := onLeaf(paths, leaf); err != nil {
    49  				return err
    50  			}
    51  		}
    52  		serializer := account.NewAccountSerializer()
    53  		if err := rlp.Decode(bytes.NewReader(leaf), serializer); err != nil {
    54  			return err
    55  		}
    56  		obj := serializer.GetAccount()
    57  		if pa := account.GetProgramAccount(obj); pa != nil {
    58  			syncer.AddSubTrie(pa.GetStorageRoot().Unextend(), hexpath, parentDepth+1, parent.Unextend(), onSlot)
    59  			syncer.AddCodeEntry(common.BytesToHash(pa.GetCodeHash()), hexpath, parentDepth+1, parent.Unextend())
    60  		}
    61  		return nil
    62  	}
    63  	syncer = statedb.NewTrieSync(root, database, onAccount, bloom, lruCache)
    64  	return syncer
    65  }