aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-01-07 20:17:48 +0800
committerobscuren <geffobscura@gmail.com>2015-01-07 20:17:48 +0800
commitfed3e6a808921fb8274b50043c5c39a24a1bbccf (patch)
treed632e95cfce78bd9a99a52bf7f30ee3ff980dfd3 /core
parent032ab665299d75bffc25260e8fa477ace19db06a (diff)
downloaddexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.tar
dexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.tar.gz
dexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.tar.bz2
dexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.tar.lz
dexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.tar.xz
dexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.tar.zst
dexon-fed3e6a808921fb8274b50043c5c39a24a1bbccf.zip
Refactored ethutil.Config.Db out
Diffstat (limited to 'core')
-rw-r--r--core/block_processor.go10
-rw-r--r--core/chain_manager.go29
-rw-r--r--core/chain_manager_test.go20
-rw-r--r--core/genesis.go5
-rw-r--r--core/helper_test.go1
-rw-r--r--core/transaction_pool_test.go14
-rw-r--r--core/types/block.go22
-rw-r--r--core/types/derive_sha.go4
8 files changed, 53 insertions, 52 deletions
diff --git a/core/block_processor.go b/core/block_processor.go
index 233e5e4db..87e823362 100644
--- a/core/block_processor.go
+++ b/core/block_processor.go
@@ -36,6 +36,7 @@ type EthManager interface {
}
type BlockProcessor struct {
+ db ethutil.Database
// Mutex for locking the block processor. Blocks can only be handled one at a time
mutex sync.Mutex
// Canonical block chain
@@ -57,8 +58,9 @@ type BlockProcessor struct {
eventMux *event.TypeMux
}
-func NewBlockProcessor(txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
+func NewBlockProcessor(db ethutil.Database, txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
sm := &BlockProcessor{
+ db: db,
mem: make(map[string]*big.Int),
Pow: ezp.New(),
bc: chainManager,
@@ -170,7 +172,8 @@ func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, msgs state.M
func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big.Int, messages state.Messages, err error) {
sm.lastAttemptedBlock = block
- state := state.New(parent.Trie().Copy())
+ state := state.New(parent.Root(), sm.db)
+ //state := state.New(parent.Trie().Copy())
// Block validation
if err = sm.ValidateBlock(block, parent); err != nil {
@@ -352,7 +355,8 @@ func (sm *BlockProcessor) GetMessages(block *types.Block) (messages []*state.Mes
var (
parent = sm.bc.GetBlock(block.Header().ParentHash)
- state = state.New(parent.Trie().Copy())
+ //state = state.New(parent.Trie().Copy())
+ state = state.New(parent.Root(), sm.db)
)
defer state.Reset()
diff --git a/core/chain_manager.go b/core/chain_manager.go
index e73ea6378..2d4001f0f 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -55,6 +55,7 @@ func CalcGasLimit(parent, block *types.Block) *big.Int {
type ChainManager struct {
//eth EthManager
+ db ethutil.Database
processor types.BlockProcessor
eventMux *event.TypeMux
genesisBlock *types.Block
@@ -96,13 +97,9 @@ func (self *ChainManager) CurrentBlock() *types.Block {
return self.currentBlock
}
-func NewChainManager(mux *event.TypeMux) *ChainManager {
- bc := &ChainManager{}
- bc.genesisBlock = GenesisBlock()
- bc.eventMux = mux
-
+func NewChainManager(db ethutil.Database, mux *event.TypeMux) *ChainManager {
+ bc := &ChainManager{db: db, genesisBlock: GenesisBlock(db), eventMux: mux}
bc.setLastBlock()
-
bc.transState = bc.State().Copy()
return bc
@@ -120,7 +117,7 @@ func (self *ChainManager) SetProcessor(proc types.BlockProcessor) {
}
func (self *ChainManager) State() *state.StateDB {
- return state.New(self.CurrentBlock().Trie())
+ return state.New(self.CurrentBlock().Root(), self.db)
}
func (self *ChainManager) TransState() *state.StateDB {
@@ -128,7 +125,7 @@ func (self *ChainManager) TransState() *state.StateDB {
}
func (bc *ChainManager) setLastBlock() {
- data, _ := ethutil.Config.Db.Get([]byte("LastBlock"))
+ data, _ := bc.db.Get([]byte("LastBlock"))
if len(data) != 0 {
var block types.Block
rlp.Decode(bytes.NewReader(data), &block)
@@ -137,7 +134,7 @@ func (bc *ChainManager) setLastBlock() {
bc.lastBlockNumber = block.Header().Number.Uint64()
// Set the last know difficulty (might be 0x0 as initial value, Genesis)
- bc.td = ethutil.BigD(ethutil.Config.Db.LastKnownTD())
+ bc.td = ethutil.BigD(bc.db.LastKnownTD())
} else {
bc.Reset()
}
@@ -183,7 +180,7 @@ func (bc *ChainManager) Reset() {
defer bc.mu.Unlock()
for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.Header().ParentHash) {
- ethutil.Config.Db.Delete(block.Hash())
+ bc.db.Delete(block.Hash())
}
// Prepare the genesis block
@@ -210,7 +207,7 @@ func (self *ChainManager) Export() []byte {
func (bc *ChainManager) insert(block *types.Block) {
encodedBlock := ethutil.Encode(block)
- ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock)
+ bc.db.Put([]byte("LastBlock"), encodedBlock)
bc.currentBlock = block
bc.lastBlockHash = block.Hash()
}
@@ -219,7 +216,7 @@ func (bc *ChainManager) write(block *types.Block) {
bc.writeBlockInfo(block)
encodedBlock := ethutil.Encode(block)
- ethutil.Config.Db.Put(block.Hash(), encodedBlock)
+ bc.db.Put(block.Hash(), encodedBlock)
}
// Accessors
@@ -229,7 +226,7 @@ func (bc *ChainManager) Genesis() *types.Block {
// Block fetching methods
func (bc *ChainManager) HasBlock(hash []byte) bool {
- data, _ := ethutil.Config.Db.Get(hash)
+ data, _ := bc.db.Get(hash)
return len(data) != 0
}
@@ -254,7 +251,7 @@ func (self *ChainManager) GetBlockHashesFromHash(hash []byte, max uint64) (chain
}
func (self *ChainManager) GetBlock(hash []byte) *types.Block {
- data, _ := ethutil.Config.Db.Get(hash)
+ data, _ := self.db.Get(hash)
if len(data) == 0 {
return nil
}
@@ -286,7 +283,7 @@ func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block {
}
func (bc *ChainManager) setTotalDifficulty(td *big.Int) {
- ethutil.Config.Db.Put([]byte("LTD"), td.Bytes())
+ bc.db.Put([]byte("LTD"), td.Bytes())
bc.td = td
}
@@ -348,7 +345,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
self.setTotalDifficulty(td)
self.insert(block)
- self.transState = state.New(cblock.Trie().Copy())
+ self.transState = state.New(cblock.Root(), self.db) //state.New(cblock.Trie().Copy())
}
}
diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go
index 6c66961b0..b384c4926 100644
--- a/core/chain_manager_test.go
+++ b/core/chain_manager_test.go
@@ -21,14 +21,6 @@ func init() {
ethutil.ReadConfig("/tmp/ethtest", "/tmp/ethtest", "ETH")
}
-func reset() {
- db, err := ethdb.NewMemDatabase()
- if err != nil {
- panic("Could not create mem-db, failing")
- }
- ethutil.Config.Db = db
-}
-
func loadChain(fn string, t *testing.T) (types.Blocks, error) {
fh, err := os.OpenFile(path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "_data", fn), os.O_RDONLY, os.ModePerm)
if err != nil {
@@ -54,7 +46,7 @@ func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *
}
func TestChainInsertions(t *testing.T) {
- reset()
+ db, _ := ethdb.NewMemDatabase()
chain1, err := loadChain("valid1", t)
if err != nil {
@@ -69,9 +61,9 @@ func TestChainInsertions(t *testing.T) {
}
var eventMux event.TypeMux
- chainMan := NewChainManager(&eventMux)
+ chainMan := NewChainManager(db, &eventMux)
txPool := NewTxPool(&eventMux)
- blockMan := NewBlockProcessor(txPool, chainMan, &eventMux)
+ blockMan := NewBlockProcessor(db, txPool, chainMan, &eventMux)
chainMan.SetProcessor(blockMan)
const max = 2
@@ -94,7 +86,7 @@ func TestChainInsertions(t *testing.T) {
}
func TestChainMultipleInsertions(t *testing.T) {
- reset()
+ db, _ := ethdb.NewMemDatabase()
const max = 4
chains := make([]types.Blocks, max)
@@ -113,9 +105,9 @@ func TestChainMultipleInsertions(t *testing.T) {
}
}
var eventMux event.TypeMux
- chainMan := NewChainManager(&eventMux)
+ chainMan := NewChainManager(db, &eventMux)
txPool := NewTxPool(&eventMux)
- blockMan := NewBlockProcessor(txPool, chainMan, &eventMux)
+ blockMan := NewBlockProcessor(db, txPool, chainMan, &eventMux)
chainMan.SetProcessor(blockMan)
done := make(chan bool, max)
for i, chain := range chains {
diff --git a/core/genesis.go b/core/genesis.go
index 10b40516f..1590818a8 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -19,7 +19,7 @@ var ZeroHash512 = make([]byte, 64)
var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{}))
var EmptyListRoot = crypto.Sha3(ethutil.Encode(""))
-func GenesisBlock() *types.Block {
+func GenesisBlock(db ethutil.Database) *types.Block {
genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(131072), crypto.Sha3(big.NewInt(42).Bytes()), "")
genesis.Header().Number = ethutil.Big0
genesis.Header().GasLimit = big.NewInt(1000000)
@@ -30,7 +30,8 @@ func GenesisBlock() *types.Block {
genesis.SetTransactions(types.Transactions{})
genesis.SetReceipts(types.Receipts{})
- statedb := state.New(genesis.Trie())
+ statedb := state.New(genesis.Root(), db)
+ //statedb := state.New(genesis.Trie())
for _, addr := range []string{
"51ba59315b3a95761d0863b05ccc7a7f54703d99",
"e4157b34ea9615cfbde6b4fda419828124b70c78",
diff --git a/core/helper_test.go b/core/helper_test.go
index b8bf254d7..7b41b86f1 100644
--- a/core/helper_test.go
+++ b/core/helper_test.go
@@ -77,7 +77,6 @@ func NewTestManager() *TestManager {
fmt.Println("Could not create mem-db, failing")
return nil
}
- ethutil.Config.Db = db
testManager := &TestManager{}
testManager.eventMux = new(event.TypeMux)
diff --git a/core/transaction_pool_test.go b/core/transaction_pool_test.go
index 2e1debfbe..7f192fc4d 100644
--- a/core/transaction_pool_test.go
+++ b/core/transaction_pool_test.go
@@ -6,16 +6,22 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/state"
)
// State query interface
-type stateQuery struct{}
+type stateQuery struct{ db ethutil.Database }
+
+func SQ() stateQuery {
+ db, _ := ethdb.NewMemDatabase()
+ return stateQuery{db: db}
+}
func (self stateQuery) GetAccount(addr []byte) *state.StateObject {
- return state.NewStateObject(addr)
+ return state.NewStateObject(addr, self.db)
}
func transaction() *types.Transaction {
@@ -66,7 +72,7 @@ func TestRemoveInvalid(t *testing.T) {
pool, key := setup()
tx1 := transaction()
pool.addTx(tx1)
- pool.RemoveInvalid(stateQuery{})
+ pool.RemoveInvalid(SQ())
if pool.Size() > 0 {
t.Error("expected pool size to be 0")
}
@@ -74,7 +80,7 @@ func TestRemoveInvalid(t *testing.T) {
tx1.SetNonce(1)
tx1.SignECDSA(key)
pool.addTx(tx1)
- pool.RemoveInvalid(stateQuery{})
+ pool.RemoveInvalid(SQ())
if pool.Size() != 1 {
t.Error("expected pool size to be 1, is", pool.Size())
}
diff --git a/core/types/block.go b/core/types/block.go
index 7e19d003f..be57e86a6 100644
--- a/core/types/block.go
+++ b/core/types/block.go
@@ -9,9 +9,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
- "github.com/ethereum/go-ethereum/ptrie"
"github.com/ethereum/go-ethereum/rlp"
- "github.com/ethereum/go-ethereum/state"
)
type Header struct {
@@ -168,16 +166,18 @@ func (self *Block) RlpDataForStorage() interface{} {
}
// Header accessors (add as you need them)
-func (self *Block) Number() *big.Int { return self.header.Number }
-func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
-func (self *Block) Bloom() []byte { return self.header.Bloom }
-func (self *Block) Coinbase() []byte { return self.header.Coinbase }
-func (self *Block) Time() int64 { return int64(self.header.Time) }
-func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
-func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
-func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) }
+func (self *Block) Number() *big.Int { return self.header.Number }
+func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
+func (self *Block) Bloom() []byte { return self.header.Bloom }
+func (self *Block) Coinbase() []byte { return self.header.Coinbase }
+func (self *Block) Time() int64 { return int64(self.header.Time) }
+func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
+func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
+
+//func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) }
+//func (self *Block) State() *state.StateDB { return state.New(self.Trie()) }
+func (self *Block) Root() []byte { return self.header.Root }
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
-func (self *Block) State() *state.StateDB { return state.New(self.Trie()) }
func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) }
// Implement pow.Block
diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go
index 0beb19670..0e286bd8b 100644
--- a/core/types/derive_sha.go
+++ b/core/types/derive_sha.go
@@ -1,6 +1,7 @@
package types
import (
+ "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ptrie"
)
@@ -11,7 +12,8 @@ type DerivableList interface {
}
func DeriveSha(list DerivableList) []byte {
- trie := ptrie.New(nil, ethutil.Config.Db)
+ db, _ := ethdb.NewMemDatabase()
+ trie := ptrie.New(nil, db)
for i := 0; i < list.Len(); i++ {
trie.Update(ethutil.Encode(i), list.GetRlp(i))
}