Commit ff183c82 authored by George Hotz's avatar George Hotz

trie progress

parent be29bfd9
...@@ -5,18 +5,26 @@ import ( ...@@ -5,18 +5,26 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/oracle" "github.com/ethereum/go-ethereum/oracle"
"github.com/ethereum/go-ethereum/trie"
) )
// TODO: add oracle calls here // TODO: add oracle calls here
// wrapper for the oracle // wrapper for the oracle
type Database struct { type Database struct {
db *trie.Database
BlockNumber *big.Int BlockNumber *big.Int
StateRoot common.Hash StateRoot common.Hash
} }
func NewDatabase(header types.Header) Database {
triedb := trie.Database{BlockNumber: header.Number}
return Database{db: &triedb, BlockNumber: header.Number, StateRoot: header.Root}
}
var unhashMap = make(map[common.Hash]common.Address) var unhashMap = make(map[common.Hash]common.Address)
func unhash(addrHash common.Hash) common.Address { func unhash(addrHash common.Hash) common.Address {
...@@ -42,13 +50,17 @@ func (db *Database) ContractCodeSize(addrHash common.Hash, codeHash common.Hash) ...@@ -42,13 +50,17 @@ func (db *Database) ContractCodeSize(addrHash common.Hash, codeHash common.Hash)
return len(code), nil return len(code), nil
} }
// OpenTrie opens the main account trie at a specific root hash.
func (db *Database) OpenTrie(root common.Hash) (Trie, error) {
//tr, err := trie.NewSecure(root, db.db)
return trie.New(root, db.db)
}
// OpenStorageTrie opens the storage trie of an account. // OpenStorageTrie opens the storage trie of an account.
func (db *Database) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) { func (db *Database) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
return SimpleTrie{db.BlockNumber, root, true, unhash(addrHash)}, nil return SimpleTrie{db.BlockNumber, root, true, unhash(addrHash)}, nil
} }
type LeafCallback func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error
type Trie interface { type Trie interface {
// TryGet returns the value for key stored in the trie. The value bytes must // TryGet returns the value for key stored in the trie. The value bytes must
// not be modified by the caller. If a node was not found in the database, a // not be modified by the caller. If a node was not found in the database, a
...@@ -71,7 +83,7 @@ type Trie interface { ...@@ -71,7 +83,7 @@ type Trie interface {
// Commit writes all nodes to the trie's memory database, tracking the internal // Commit writes all nodes to the trie's memory database, tracking the internal
// and external (for account tries) references. // and external (for account tries) references.
Commit(onleaf LeafCallback) (common.Hash, error) Commit(onleaf trie.LeafCallback) (common.Hash, error)
} }
type SimpleTrie struct { type SimpleTrie struct {
...@@ -81,7 +93,7 @@ type SimpleTrie struct { ...@@ -81,7 +93,7 @@ type SimpleTrie struct {
Address common.Address Address common.Address
} }
func (trie SimpleTrie) Commit(onleaf LeafCallback) (common.Hash, error) { func (trie SimpleTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
fmt.Println("trie.Commit") fmt.Println("trie.Commit")
return trie.Root, nil return trie.Root, nil
} }
......
...@@ -117,23 +117,35 @@ type StateDB struct { ...@@ -117,23 +117,35 @@ type StateDB struct {
SnapshotAccountReads time.Duration SnapshotAccountReads time.Duration
SnapshotStorageReads time.Duration SnapshotStorageReads time.Duration
SnapshotCommits time.Duration SnapshotCommits time.Duration
blockNumber *big.Int
} }
func NewStateDB(header types.Header) *StateDB { func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
return &StateDB{ tr, err := db.OpenTrie(root)
blockNumber: header.Number, if err != nil {
return nil, err
}
sdb := &StateDB{
db: db,
trie: tr,
originalRoot: root,
snaps: snaps,
stateObjects: make(map[common.Address]*stateObject), stateObjects: make(map[common.Address]*stateObject),
stateObjectsPending: make(map[common.Address]struct{}), stateObjectsPending: make(map[common.Address]struct{}),
stateObjectsDirty: make(map[common.Address]struct{}), stateObjectsDirty: make(map[common.Address]struct{}),
originalRoot: header.Root, logs: make(map[common.Hash][]*types.Log),
db: Database{BlockNumber: header.Number, StateRoot: header.Root}, preimages: make(map[common.Hash][]byte),
trie: SimpleTrie{BlockNumber: header.Number, Root: header.Root, Storage: false},
journal: newJournal(), journal: newJournal(),
accessList: newAccessList(), accessList: newAccessList(),
logs: make(map[common.Hash][]*types.Log), hasher: crypto.NewKeccakState(),
} }
/*if sdb.snaps != nil {
if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
sdb.snapDestructs = make(map[common.Hash]struct{})
sdb.snapAccounts = make(map[common.Hash][]byte)
sdb.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
}
}*/
return sdb, nil
} }
// setError remembers the first non-nil error it is called with. // setError remembers the first non-nil error it is called with.
......
...@@ -24,7 +24,8 @@ func main() { ...@@ -24,7 +24,8 @@ func main() {
} }
bc := core.NewBlockChain() bc := core.NewBlockChain()
statedb := state.NewStateDB(header) database := state.NewDatabase(header)
statedb, _ := state.New(header.Root, database, nil)
vmconfig := vm.Config{} vmconfig := vm.Config{}
processor := core.NewStateProcessor(params.MainnetChainConfig, bc, bc.Engine()) processor := core.NewStateProcessor(params.MainnetChainConfig, bc, bc.Engine())
fmt.Println("made state processor") fmt.Println("made state processor")
......
package trie package trie
import ( import (
"fmt"
"io" "io"
"math/big"
"sync" "sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
...@@ -21,18 +23,22 @@ func (n rawNode) EncodeRLP(w io.Writer) error { ...@@ -21,18 +23,22 @@ func (n rawNode) EncodeRLP(w io.Writer) error {
} }
type Database struct { type Database struct {
lock sync.RWMutex BlockNumber *big.Int
lock sync.RWMutex
} }
// Node retrieves an encoded cached trie node from memory. If it cannot be found // Node retrieves an encoded cached trie node from memory. If it cannot be found
// cached, the method queries the persistent database for the content. // cached, the method queries the persistent database for the content.
func (db *Database) Node(hash common.Hash) ([]byte, error) { func (db *Database) Node(hash common.Hash) ([]byte, error) {
fmt.Println("trie Node", hash)
return []byte{}, nil return []byte{}, nil
} }
// node retrieves a cached trie node from memory, or returns nil if none can be // node retrieves a cached trie node from memory, or returns nil if none can be
// found in the memory cache. // found in the memory cache.
func (db *Database) node(hash common.Hash) node { func (db *Database) node(hash common.Hash) node {
fmt.Println("trie node", hash)
//return hashNode(hash.Bytes())
return nilValueNode return nilValueNode
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment