Пример #1
0
func DeriveSha(list DerivableList) common.Hash {
	db, _ := ethdb.NewMemDatabase()
	trie := trie.New(nil, db)
	for i := 0; i < list.Len(); i++ {
		key, _ := rlp.EncodeToBytes(uint(i))
		trie.Update(key, list.GetRlp(i))
	}

	return common.BytesToHash(trie.Root())
}
Пример #2
0
func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
	switch req := req.(type) {
	case *TrieRequest:
		t, _ := trie.New(req.root, odr.sdb)
		req.proof = t.Prove(req.key)
		trie.ClearGlobalCache()
	case *NodeDataRequest:
		req.data, _ = odr.sdb.Get(req.hash[:])
	}
	req.StoreResult(odr.ldb)
	return nil
}
Пример #3
0
func NewTrie() *trie.Trie {
	db, _ := NewMemDatabase()

	return trie.New(nil, db)
}
Пример #4
0
// step moves the iterator to the next entry of the state trie.
func (it *NodeIterator) step() error {
	// Abort if we reached the end of the iteration
	if it.state == nil {
		return nil
	}
	// Initialize the iterator if we've just started
	if it.stateIt == nil {
		it.stateIt = trie.NewNodeIterator(it.state.trie.Trie)
	}
	// If we had data nodes previously, we surely have at least state nodes
	if it.dataIt != nil {
		if cont := it.dataIt.Next(); !cont {
			if it.dataIt.Error != nil {
				return it.dataIt.Error
			}
			it.dataIt = nil
		}
		return nil
	}
	// If we had source code previously, discard that
	if it.code != nil {
		it.code = nil
		return nil
	}
	// Step to the next state trie node, terminating if we're out of nodes
	if cont := it.stateIt.Next(); !cont {
		if it.stateIt.Error != nil {
			return it.stateIt.Error
		}
		it.state, it.stateIt = nil, nil
		return nil
	}
	// If the state trie node is an internal entry, leave as is
	if !it.stateIt.Leaf {
		return nil
	}
	// Otherwise we've reached an account node, initiate data iteration
	var account struct {
		Nonce    uint64
		Balance  *big.Int
		Root     common.Hash
		CodeHash []byte
	}
	if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account); err != nil {
		return err
	}
	dataTrie, err := trie.New(account.Root, it.state.db)
	if err != nil {
		return err
	}
	it.dataIt = trie.NewNodeIterator(dataTrie)
	if !it.dataIt.Next() {
		it.dataIt = nil
	}
	if bytes.Compare(account.CodeHash, emptyCodeHash) != 0 {
		it.codeHash = common.BytesToHash(account.CodeHash)
		it.code, err = it.state.db.Get(account.CodeHash)
		if err != nil {
			return fmt.Errorf("code %x: %v", account.CodeHash, err)
		}
	}
	it.accountHash = it.stateIt.Parent
	return nil
}