aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-05-17 20:07:52 +0800
committerobscuren <geffobscura@gmail.com>2014-05-17 20:07:52 +0800
commit8730dfdcc2e2b40410a57385e4864d15f2f0336b (patch)
treeeabf6400bc78b1796317fbb7cef642a7f2da2306 /ethchain
parent88686cbed27ad2e7d5c111051e06c270b1b352a3 (diff)
downloadgo-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.tar
go-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.tar.gz
go-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.tar.bz2
go-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.tar.lz
go-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.tar.xz
go-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.tar.zst
go-tangerine-8730dfdcc2e2b40410a57385e4864d15f2f0336b.zip
Changed how changes are being applied to states
Diffstat (limited to 'ethchain')
-rw-r--r--ethchain/block_chain.go4
-rw-r--r--ethchain/block_chain_test.go17
-rw-r--r--ethchain/state_manager.go109
-rw-r--r--ethchain/transaction_pool.go3
-rw-r--r--ethchain/vm_test.go2
5 files changed, 69 insertions, 66 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go
index 1848b9ddb..6c3b15a6a 100644
--- a/ethchain/block_chain.go
+++ b/ethchain/block_chain.go
@@ -280,7 +280,7 @@ func (bc *BlockChain) setLastBlock() {
bc.LastBlockHash = block.Hash()
bc.LastBlockNumber = info.Number
- log.Printf("[CHAIN] Last known block height #%d\n", bc.LastBlockNumber)
+ ethutil.Config.Log.Infof("[CHAIN] Last known block height #%d\n", bc.LastBlockNumber)
} else {
AddTestNetFunds(bc.genesisBlock)
@@ -295,7 +295,7 @@ func (bc *BlockChain) setLastBlock() {
// Set the last know difficulty (might be 0x0 as initial value, Genesis)
bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD())
- log.Printf("Last block: %x\n", bc.CurrentBlock.Hash())
+ ethutil.Config.Log.Infof("Last block: %x\n", bc.CurrentBlock.Hash())
}
func (bc *BlockChain) SetTotalDifficulty(td *big.Int) {
diff --git a/ethchain/block_chain_test.go b/ethchain/block_chain_test.go
index 30eb62266..4e4bb9dd4 100644
--- a/ethchain/block_chain_test.go
+++ b/ethchain/block_chain_test.go
@@ -18,6 +18,18 @@ type TestManager struct {
Blocks []*Block
}
+func (s *TestManager) IsListening() bool {
+ return false
+}
+
+func (s *TestManager) IsMining() bool {
+ return false
+}
+
+func (s *TestManager) PeerCount() int {
+ return 0
+}
+
func (s *TestManager) BlockChain() *BlockChain {
return s.blockChain
}
@@ -38,7 +50,7 @@ func (tm *TestManager) Broadcast(msgType ethwire.MsgType, data []interface{}) {
}
func NewTestManager() *TestManager {
- ethutil.ReadConfig(".ethtest")
+ ethutil.ReadConfig(".ethtest", ethutil.LogStd)
db, err := ethdb.NewMemDatabase()
if err != nil {
@@ -62,8 +74,7 @@ func NewTestManager() *TestManager {
func (tm *TestManager) AddFakeBlock(blk []byte) error {
block := NewBlockFromBytes(blk)
tm.Blocks = append(tm.Blocks, block)
- tm.StateManager().PrepareDefault(block)
- err := tm.StateManager().ProcessBlock(block, false)
+ err := tm.StateManager().ProcessBlock(tm.StateManager().CurrentState(), block, false)
return err
}
func (tm *TestManager) CreateChain1() error {
diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go
index 143d9d647..28570775b 100644
--- a/ethchain/state_manager.go
+++ b/ethchain/state_manager.go
@@ -39,20 +39,13 @@ type StateManager struct {
// The ethereum manager interface
Ethereum EthManager
// The managed states
- // Processor state. Anything processed will be applied to this
- // state
- procState *State
- // Comparative state it used for comparing and validating end
- // results
- compState *State
// Transiently state. The trans state isn't ever saved, validated and
// it could be used for setting account nonces without effecting
// the main states.
transState *State
- // Manifest for keeping changes regarding state objects. See `notify`
- // XXX Should we move the manifest to the State object. Benefit:
- // * All states can keep their own local changes
- //manifest *Manifest
+ // Mining state. The mining state is used purely and solely by the mining
+ // operation.
+ miningState *State
}
func NewStateManager(ethereum EthManager) *StateManager {
@@ -62,30 +55,39 @@ func NewStateManager(ethereum EthManager) *StateManager {
Pow: &EasyPow{},
Ethereum: ethereum,
bc: ethereum.BlockChain(),
- //manifest: NewManifest(),
}
- sm.procState = ethereum.BlockChain().CurrentBlock.State()
- sm.transState = sm.procState.Copy()
+ sm.transState = ethereum.BlockChain().CurrentBlock.State().Copy()
+ sm.miningState = ethereum.BlockChain().CurrentBlock.State().Copy()
return sm
}
-func (sm *StateManager) ProcState() *State {
- return sm.procState
+func (sm *StateManager) CurrentState() *State {
+ return sm.Ethereum.BlockChain().CurrentBlock.State()
}
func (sm *StateManager) TransState() *State {
return sm.transState
}
+func (sm *StateManager) MiningState() *State {
+ return sm.miningState
+}
+
+func (sm *StateManager) NewMiningState() *State {
+ sm.miningState = sm.Ethereum.BlockChain().CurrentBlock.State().Copy()
+
+ return sm.miningState
+}
+
func (sm *StateManager) BlockChain() *BlockChain {
return sm.bc
}
-func (sm *StateManager) MakeContract(tx *Transaction) *StateObject {
- contract := MakeContract(tx, sm.procState)
+func (sm *StateManager) MakeContract(state *State, tx *Transaction) *StateObject {
+ contract := MakeContract(tx, state)
if contract != nil {
- sm.procState.states[string(tx.Hash()[12:])] = contract.state
+ state.states[string(tx.Hash()[12:])] = contract.state
return contract
}
@@ -95,7 +97,7 @@ func (sm *StateManager) MakeContract(tx *Transaction) *StateObject {
// Apply transactions uses the transaction passed to it and applies them onto
// the current processing state.
-func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
+func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Transaction) {
// Process each transaction/contract
for _, tx := range txs {
// If there's no recipient, it's a contract
@@ -104,9 +106,9 @@ func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
if tx.IsContract() {
err := sm.Ethereum.TxPool().ProcessTransaction(tx, block, false)
if err == nil {
- contract := sm.MakeContract(tx)
+ contract := sm.MakeContract(state, tx)
if contract != nil {
- sm.EvalScript(contract.Init(), contract, tx, block)
+ sm.EvalScript(state, contract.Init(), contract, tx, block)
} else {
ethutil.Config.Log.Infoln("[STATE] Unable to create contract")
}
@@ -115,9 +117,9 @@ func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
}
} else {
err := sm.Ethereum.TxPool().ProcessTransaction(tx, block, false)
- contract := sm.procState.GetContract(tx.Recipient)
+ contract := state.GetContract(tx.Recipient)
if err == nil && len(contract.Script()) > 0 {
- sm.EvalScript(contract.Script(), contract, tx, block)
+ sm.EvalScript(state, contract.Script(), contract, tx, block)
} else if err != nil {
ethutil.Config.Log.Infoln("[STATE] process:", err)
}
@@ -125,20 +127,8 @@ func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
}
}
-// The prepare function, prepares the state manager for the next
-// "ProcessBlock" action.
-func (sm *StateManager) Prepare(processor *State, comparative *State) {
- sm.compState = comparative
- sm.procState = processor
-}
-
-// Default prepare function
-func (sm *StateManager) PrepareDefault(block *Block) {
- sm.Prepare(sm.BlockChain().CurrentBlock.State(), block.State())
-}
-
// Block processing and validating with a given (temporarily) state
-func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
+func (sm *StateManager) ProcessBlock(state *State, block *Block, dontReact bool) error {
// Processing a blocks may never happen simultaneously
sm.mutex.Lock()
defer sm.mutex.Unlock()
@@ -153,7 +143,7 @@ func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
// we don't want to undo but since undo only happens on dirty
// nodes this won't happen because Commit would have been called
// before that.
- defer sm.bc.CurrentBlock.Undo()
+ defer state.Reset()
// Check if we have the parent hash, if it isn't known we discard it
// Reasons might be catching up or simply an invalid block
@@ -162,7 +152,7 @@ func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
}
// Process the transactions on to current block
- sm.ApplyTransactions(sm.bc.CurrentBlock, block.Transactions())
+ sm.ApplyTransactions(state, sm.bc.CurrentBlock, block.Transactions())
// Block validation
if err := sm.ValidateBlock(block); err != nil {
@@ -172,19 +162,20 @@ func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
// I'm not sure, but I don't know if there should be thrown
// any errors at this time.
- if err := sm.AccumelateRewards(block); err != nil {
+ if err := sm.AccumelateRewards(state, block); err != nil {
fmt.Println("[SM] Error accumulating reward", err)
return err
}
- if !sm.compState.Cmp(sm.procState) {
- return fmt.Errorf("Invalid merkle root. Expected %x, got %x", sm.compState.trie.Root, sm.procState.trie.Root)
+ //if !sm.compState.Cmp(state) {
+ if !block.State().Cmp(state) {
+ return fmt.Errorf("Invalid merkle root. Expected %x, got %x", block.State().trie.Root, state.trie.Root)
}
// Calculate the new total difficulty and sync back to the db
if sm.CalculateTD(block) {
// Sync the current block's state to the database and cancelling out the deferred Undo
- sm.procState.Sync()
+ state.Sync()
// Add the block to the chain
sm.bc.Add(block)
@@ -193,14 +184,14 @@ func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
if dontReact == false {
sm.Ethereum.Reactor().Post("newBlock", block)
- sm.procState.manifest.Reset()
+ state.manifest.Reset()
}
- sm.notifyChanges()
+ sm.notifyChanges(state)
sm.Ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
- sm.Ethereum.TxPool().RemoveInvalid(sm.procState)
+ sm.Ethereum.TxPool().RemoveInvalid(state)
} else {
fmt.Println("total diff failed")
}
@@ -276,21 +267,21 @@ func CalculateUncleReward(block *Block) *big.Int {
return UncleReward
}
-func (sm *StateManager) AccumelateRewards(block *Block) error {
+func (sm *StateManager) AccumelateRewards(state *State, block *Block) error {
// Get the account associated with the coinbase
- account := sm.procState.GetAccount(block.Coinbase)
+ account := state.GetAccount(block.Coinbase)
// Reward amount of ether to the coinbase address
account.AddAmount(CalculateBlockReward(block, len(block.Uncles)))
addr := make([]byte, len(block.Coinbase))
copy(addr, block.Coinbase)
- sm.procState.UpdateStateObject(account)
+ state.UpdateStateObject(account)
for _, uncle := range block.Uncles {
- uncleAccount := sm.procState.GetAccount(uncle.Coinbase)
+ uncleAccount := state.GetAccount(uncle.Coinbase)
uncleAccount.AddAmount(CalculateUncleReward(uncle))
- sm.procState.UpdateStateObject(uncleAccount)
+ state.UpdateStateObject(uncleAccount)
}
return nil
@@ -300,8 +291,8 @@ func (sm *StateManager) Stop() {
sm.bc.Stop()
}
-func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Transaction, block *Block) {
- account := sm.procState.GetAccount(tx.Sender())
+func (sm *StateManager) EvalScript(state *State, script []byte, object *StateObject, tx *Transaction, block *Block) {
+ account := state.GetAccount(tx.Sender())
err := account.ConvertGas(tx.Gas, tx.GasPrice)
if err != nil {
@@ -309,8 +300,8 @@ func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Trans
return
}
- closure := NewClosure(account, object, script, sm.procState, tx.Gas, tx.GasPrice)
- vm := NewVm(sm.procState, sm, RuntimeVars{
+ closure := NewClosure(account, object, script, state, tx.Gas, tx.GasPrice)
+ vm := NewVm(state, sm, RuntimeVars{
Origin: account.Address(),
BlockNumber: block.BlockInfo().Number,
PrevHash: block.PrevHash,
@@ -323,16 +314,16 @@ func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Trans
closure.Call(vm, tx.Data, nil)
// Update the account (refunds)
- sm.procState.UpdateStateObject(account)
- sm.procState.UpdateStateObject(object)
+ state.UpdateStateObject(account)
+ state.UpdateStateObject(object)
}
-func (sm *StateManager) notifyChanges() {
- for addr, stateObject := range sm.procState.manifest.objectChanges {
+func (sm *StateManager) notifyChanges(state *State) {
+ for addr, stateObject := range state.manifest.objectChanges {
sm.Ethereum.Reactor().Post("object:"+addr, stateObject)
}
- for stateObjectAddr, mappedObjects := range sm.procState.manifest.storageChanges {
+ for stateObjectAddr, mappedObjects := range state.manifest.storageChanges {
for addr, value := range mappedObjects {
sm.Ethereum.Reactor().Post("storage:"+stateObjectAddr+":"+addr, &StorageState{[]byte(stateObjectAddr), []byte(addr), value})
}
diff --git a/ethchain/transaction_pool.go b/ethchain/transaction_pool.go
index 21ce8cdc1..da77f32b3 100644
--- a/ethchain/transaction_pool.go
+++ b/ethchain/transaction_pool.go
@@ -149,7 +149,8 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
}
// Get the sender
- sender := pool.Ethereum.StateManager().procState.GetAccount(tx.Sender())
+ //sender := pool.Ethereum.StateManager().procState.GetAccount(tx.Sender())
+ sender := pool.Ethereum.StateManager().CurrentState().GetAccount(tx.Sender())
totAmount := new(big.Int).Add(tx.Value, new(big.Int).Mul(TxFee, TxFeeRat))
// Make sure there's enough in the sender's account. Having insufficient
diff --git a/ethchain/vm_test.go b/ethchain/vm_test.go
index b919b496f..5d03ccf0c 100644
--- a/ethchain/vm_test.go
+++ b/ethchain/vm_test.go
@@ -12,7 +12,7 @@ import (
)
func TestRun4(t *testing.T) {
- ethutil.ReadConfig("")
+ ethutil.ReadConfig("", ethutil.LogStd)
db, _ := ethdb.NewMemDatabase()
state := NewState(ethutil.NewTrie(db, ""))