Commit ff183c82 authored by George Hotz's avatar George Hotz

trie progress

parent be29bfd9
......@@ -5,18 +5,26 @@ import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/oracle"
"github.com/ethereum/go-ethereum/trie"
)
// TODO: add oracle calls here
// wrapper for the oracle
type Database struct {
db *trie.Database
BlockNumber *big.Int
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)
func unhash(addrHash common.Hash) common.Address {
......@@ -42,13 +50,17 @@ func (db *Database) ContractCodeSize(addrHash common.Hash, codeHash common.Hash)
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.
func (db *Database) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
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 {
// 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
......@@ -71,7 +83,7 @@ type Trie interface {
// Commit writes all nodes to the trie's memory database, tracking the internal
// and external (for account tries) references.
Commit(onleaf LeafCallback) (common.Hash, error)
Commit(onleaf trie.LeafCallback) (common.Hash, error)
}
type SimpleTrie struct {
......@@ -81,7 +93,7 @@ type SimpleTrie struct {
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")
return trie.Root, nil
}
......
......@@ -117,23 +117,35 @@ type StateDB struct {
SnapshotAccountReads time.Duration
SnapshotStorageReads time.Duration
SnapshotCommits time.Duration
blockNumber *big.Int
}
func NewStateDB(header types.Header) *StateDB {
return &StateDB{
blockNumber: header.Number,
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
tr, err := db.OpenTrie(root)
if err != nil {
return nil, err
}
sdb := &StateDB{
db: db,
trie: tr,
originalRoot: root,
snaps: snaps,
stateObjects: make(map[common.Address]*stateObject),
stateObjectsPending: make(map[common.Address]struct{}),
stateObjectsDirty: make(map[common.Address]struct{}),
originalRoot: header.Root,
db: Database{BlockNumber: header.Number, StateRoot: header.Root},
trie: SimpleTrie{BlockNumber: header.Number, Root: header.Root, Storage: false},
logs: make(map[common.Hash][]*types.Log),
preimages: make(map[common.Hash][]byte),
journal: newJournal(),
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.
......
......@@ -24,7 +24,8 @@ func main() {
}
bc := core.NewBlockChain()
statedb := state.NewStateDB(header)
database := state.NewDatabase(header)
statedb, _ := state.New(header.Root, database, nil)
vmconfig := vm.Config{}
processor := core.NewStateProcessor(params.MainnetChainConfig, bc, bc.Engine())
fmt.Println("made state processor")
......
package trie
import (
"fmt"
"io"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/common"
......@@ -21,18 +23,22 @@ func (n rawNode) EncodeRLP(w io.Writer) error {
}
type Database struct {
BlockNumber *big.Int
lock sync.RWMutex
}
// Node retrieves an encoded cached trie node from memory. If it cannot be found
// cached, the method queries the persistent database for the content.
func (db *Database) Node(hash common.Hash) ([]byte, error) {
fmt.Println("trie Node", hash)
return []byte{}, nil
}
// node retrieves a cached trie node from memory, or returns nil if none can be
// found in the memory cache.
func (db *Database) node(hash common.Hash) node {
fmt.Println("trie node", hash)
//return hashNode(hash.Bytes())
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