github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state/sync.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package state
    13  
    14  import (
    15  	"bytes"
    16  
    17  	"github.com/Sberex/go-sberex/common"
    18  	"github.com/Sberex/go-sberex/rlp"
    19  	"github.com/Sberex/go-sberex/trie"
    20  )
    21  
    22  // NewStateSync create a new state trie download scheduler.
    23  func NewStateSync(root common.Hash, database trie.DatabaseReader) *trie.TrieSync {
    24  	var syncer *trie.TrieSync
    25  	callback := func(leaf []byte, parent common.Hash) error {
    26  		var obj Account
    27  		if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
    28  			return err
    29  		}
    30  		syncer.AddSubTrie(obj.Root, 64, parent, nil)
    31  		syncer.AddRawEntry(common.BytesToHash(obj.CodeHash), 64, parent)
    32  		return nil
    33  	}
    34  	syncer = trie.NewTrieSync(root, database, callback)
    35  	return syncer
    36  }