diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-04 00:18:43 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-04 00:18:43 +0800 |
commit | ca1b2a1a91401255ab4e26cec7eb575b99ecb8da (patch) | |
tree | 87b3c8fed57f688c138288ed5d90a0a3a1e3274b /core | |
parent | 16f417f5af16de8f1c2c140f8b249bd989200bd3 (diff) | |
download | dexon-ca1b2a1a91401255ab4e26cec7eb575b99ecb8da.tar.gz dexon-ca1b2a1a91401255ab4e26cec7eb575b99ecb8da.tar.zst dexon-ca1b2a1a91401255ab4e26cec7eb575b99ecb8da.zip |
Changed prev_hash to block_hash, state transition now uses vm env
* PREVHASH => BLOCKHASH( N )
* State transition object uses VMEnv as it's query interface
* Updated vm.Enviroment has GetHash( n ) for BLOCKHASH instruction
* Added GetHash to xeth, core, utils & test environments
Diffstat (limited to 'core')
-rw-r--r-- | core/block_manager.go | 12 | ||||
-rw-r--r-- | core/chain_manager.go | 16 | ||||
-rw-r--r-- | core/state_transition.go | 41 | ||||
-rw-r--r-- | core/vm_env.go | 13 |
4 files changed, 46 insertions, 36 deletions
diff --git a/core/block_manager.go b/core/block_manager.go index 8a5455306..09f569d96 100644 --- a/core/block_manager.go +++ b/core/block_manager.go @@ -113,7 +113,7 @@ done: txGas := new(big.Int).Set(tx.Gas()) cb := state.GetStateObject(coinbase.Address()) - st := NewStateTransition(cb, tx, state, block) + st := NewStateTransition(NewEnv(state, self.bc, tx, block), tx, cb) _, err = st.TransitionState() if err != nil { switch { @@ -232,6 +232,8 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I // Sync the current block's state to the database and cancelling out the deferred Undo state.Sync() + state.Manifest().SetHash(block.Hash()) + messages := state.Manifest().Messages state.Manifest().Reset() @@ -339,10 +341,10 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent account.AddAmount(reward) statedb.Manifest().AddMessage(&state.Message{ - To: block.Header().Coinbase, - Input: nil, - Origin: nil, - Block: block.Hash(), Timestamp: int64(block.Header().Time), Coinbase: block.Header().Coinbase, Number: block.Header().Number, + To: block.Header().Coinbase, + Input: nil, + Origin: nil, + Timestamp: int64(block.Header().Time), Coinbase: block.Header().Coinbase, Number: block.Header().Number, Value: new(big.Int).Add(reward, block.Reward), }) diff --git a/core/chain_manager.go b/core/chain_manager.go index ece98d783..82b17cd93 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -271,15 +271,15 @@ func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block { self.mu.RLock() defer self.mu.RUnlock() - block := self.currentBlock - for ; block != nil; block = self.GetBlock(block.Header().ParentHash) { - if block.Header().Number.Uint64() == num { - break - } - } + var block *types.Block - if block != nil && block.Header().Number.Uint64() == 0 && num != 0 { - return nil + if num <= self.currentBlock.Number().Uint64() { + block = self.currentBlock + for ; block != nil; block = self.GetBlock(block.Header().ParentHash) { + if block.Header().Number.Uint64() == num { + break + } + } } return block diff --git a/core/state_transition.go b/core/state_transition.go index 91cfd5fe3..b22c5bf21 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -4,7 +4,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" @@ -28,18 +27,17 @@ import ( * 6) Derive new state root */ type StateTransition struct { - coinbase, receiver []byte - msg Message - gas, gasPrice *big.Int - initialGas *big.Int - value *big.Int - data []byte - state *state.StateDB - block *types.Block + coinbase []byte + msg Message + gas, gasPrice *big.Int + initialGas *big.Int + value *big.Int + data []byte + state *state.StateDB cb, rec, sen *state.StateObject - Env vm.Environment + env vm.Environment } type Message interface { @@ -69,16 +67,19 @@ func MessageGasValue(msg Message) *big.Int { return new(big.Int).Mul(msg.Gas(), msg.GasPrice()) } -func NewStateTransition(coinbase *state.StateObject, msg Message, state *state.StateDB, block *types.Block) *StateTransition { - return &StateTransition{coinbase.Address(), msg.To(), msg, new(big.Int), new(big.Int).Set(msg.GasPrice()), new(big.Int), msg.Value(), msg.Data(), state, block, coinbase, nil, nil, nil} -} - -func (self *StateTransition) VmEnv() vm.Environment { - if self.Env == nil { - self.Env = NewEnv(self.state, self.msg, self.block) +func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateObject) *StateTransition { + return &StateTransition{ + coinbase: coinbase.Address(), + env: env, + msg: msg, + gas: new(big.Int), + gasPrice: new(big.Int).Set(msg.GasPrice()), + initialGas: new(big.Int), + value: msg.Value(), + data: msg.Data(), + state: env.State(), + cb: coinbase, } - - return self.Env } func (self *StateTransition) Coinbase() *state.StateObject { @@ -183,7 +184,7 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) { return } - vmenv := self.VmEnv() + vmenv := self.env var ref vm.ContextRef if MessageCreatesContract(msg) { contract := MakeContract(msg, self.state) diff --git a/core/vm_env.go b/core/vm_env.go index 4e0315ff3..624a63333 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -13,10 +13,12 @@ type VMEnv struct { block *types.Block msg Message depth int + chain *ChainManager } -func NewEnv(state *state.StateDB, msg Message, block *types.Block) *VMEnv { +func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types.Block) *VMEnv { return &VMEnv{ + chain: chain, state: state, block: block, msg: msg, @@ -25,16 +27,21 @@ func NewEnv(state *state.StateDB, msg Message, block *types.Block) *VMEnv { func (self *VMEnv) Origin() []byte { return self.msg.From() } func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() } -func (self *VMEnv) PrevHash() []byte { return self.block.ParentHash() } func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase() } func (self *VMEnv) Time() int64 { return self.block.Time() } func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() } -func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() } func (self *VMEnv) Value() *big.Int { return self.msg.Value() } func (self *VMEnv) State() *state.StateDB { return self.state } func (self *VMEnv) Depth() int { return self.depth } func (self *VMEnv) SetDepth(i int) { self.depth = i } +func (self *VMEnv) GetHash(n uint64) []byte { + if block := self.chain.GetBlockByNumber(n); block != nil { + return block.Hash() + } + + return nil +} func (self *VMEnv) AddLog(log state.Log) { self.state.AddLog(log) } |