Changed how changes are being applied to states

poc8
obscuren 11 years ago
parent 88686cbed2
commit 8730dfdcc2
  1. 4
      ethchain/block_chain.go
  2. 17
      ethchain/block_chain_test.go
  3. 109
      ethchain/state_manager.go
  4. 3
      ethchain/transaction_pool.go
  5. 2
      ethchain/vm_test.go
  6. 2
      ethereum.go
  7. 24
      ethminer/miner.go
  8. 2
      ethpub/pub.go
  9. 12
      ethutil/config.go
  10. 5
      peer.go

@ -280,7 +280,7 @@ func (bc *BlockChain) setLastBlock() {
bc.LastBlockHash = block.Hash() bc.LastBlockHash = block.Hash()
bc.LastBlockNumber = info.Number 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 { } else {
AddTestNetFunds(bc.genesisBlock) AddTestNetFunds(bc.genesisBlock)
@ -295,7 +295,7 @@ func (bc *BlockChain) setLastBlock() {
// Set the last know difficulty (might be 0x0 as initial value, Genesis) // Set the last know difficulty (might be 0x0 as initial value, Genesis)
bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) 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) { func (bc *BlockChain) SetTotalDifficulty(td *big.Int) {

@ -18,6 +18,18 @@ type TestManager struct {
Blocks []*Block 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 { func (s *TestManager) BlockChain() *BlockChain {
return s.blockChain return s.blockChain
} }
@ -38,7 +50,7 @@ func (tm *TestManager) Broadcast(msgType ethwire.MsgType, data []interface{}) {
} }
func NewTestManager() *TestManager { func NewTestManager() *TestManager {
ethutil.ReadConfig(".ethtest") ethutil.ReadConfig(".ethtest", ethutil.LogStd)
db, err := ethdb.NewMemDatabase() db, err := ethdb.NewMemDatabase()
if err != nil { if err != nil {
@ -62,8 +74,7 @@ func NewTestManager() *TestManager {
func (tm *TestManager) AddFakeBlock(blk []byte) error { func (tm *TestManager) AddFakeBlock(blk []byte) error {
block := NewBlockFromBytes(blk) block := NewBlockFromBytes(blk)
tm.Blocks = append(tm.Blocks, block) tm.Blocks = append(tm.Blocks, block)
tm.StateManager().PrepareDefault(block) err := tm.StateManager().ProcessBlock(tm.StateManager().CurrentState(), block, false)
err := tm.StateManager().ProcessBlock(block, false)
return err return err
} }
func (tm *TestManager) CreateChain1() error { func (tm *TestManager) CreateChain1() error {

@ -39,20 +39,13 @@ type StateManager struct {
// The ethereum manager interface // The ethereum manager interface
Ethereum EthManager Ethereum EthManager
// The managed states // 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 // Transiently state. The trans state isn't ever saved, validated and
// it could be used for setting account nonces without effecting // it could be used for setting account nonces without effecting
// the main states. // the main states.
transState *State transState *State
// Manifest for keeping changes regarding state objects. See `notify` // Mining state. The mining state is used purely and solely by the mining
// XXX Should we move the manifest to the State object. Benefit: // operation.
// * All states can keep their own local changes miningState *State
//manifest *Manifest
} }
func NewStateManager(ethereum EthManager) *StateManager { func NewStateManager(ethereum EthManager) *StateManager {
@ -62,30 +55,39 @@ func NewStateManager(ethereum EthManager) *StateManager {
Pow: &EasyPow{}, Pow: &EasyPow{},
Ethereum: ethereum, Ethereum: ethereum,
bc: ethereum.BlockChain(), bc: ethereum.BlockChain(),
//manifest: NewManifest(),
} }
sm.procState = ethereum.BlockChain().CurrentBlock.State() sm.transState = ethereum.BlockChain().CurrentBlock.State().Copy()
sm.transState = sm.procState.Copy() sm.miningState = ethereum.BlockChain().CurrentBlock.State().Copy()
return sm return sm
} }
func (sm *StateManager) ProcState() *State { func (sm *StateManager) CurrentState() *State {
return sm.procState return sm.Ethereum.BlockChain().CurrentBlock.State()
} }
func (sm *StateManager) TransState() *State { func (sm *StateManager) TransState() *State {
return sm.transState 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 { func (sm *StateManager) BlockChain() *BlockChain {
return sm.bc return sm.bc
} }
func (sm *StateManager) MakeContract(tx *Transaction) *StateObject { func (sm *StateManager) MakeContract(state *State, tx *Transaction) *StateObject {
contract := MakeContract(tx, sm.procState) contract := MakeContract(tx, state)
if contract != nil { if contract != nil {
sm.procState.states[string(tx.Hash()[12:])] = contract.state state.states[string(tx.Hash()[12:])] = contract.state
return contract 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 // Apply transactions uses the transaction passed to it and applies them onto
// the current processing state. // 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 // Process each transaction/contract
for _, tx := range txs { for _, tx := range txs {
// If there's no recipient, it's a contract // If there's no recipient, it's a contract
@ -104,9 +106,9 @@ func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
if tx.IsContract() { if tx.IsContract() {
err := sm.Ethereum.TxPool().ProcessTransaction(tx, block, false) err := sm.Ethereum.TxPool().ProcessTransaction(tx, block, false)
if err == nil { if err == nil {
contract := sm.MakeContract(tx) contract := sm.MakeContract(state, tx)
if contract != nil { if contract != nil {
sm.EvalScript(contract.Init(), contract, tx, block) sm.EvalScript(state, contract.Init(), contract, tx, block)
} else { } else {
ethutil.Config.Log.Infoln("[STATE] Unable to create contract") ethutil.Config.Log.Infoln("[STATE] Unable to create contract")
} }
@ -115,9 +117,9 @@ func (sm *StateManager) ApplyTransactions(block *Block, txs []*Transaction) {
} }
} else { } else {
err := sm.Ethereum.TxPool().ProcessTransaction(tx, block, false) 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 { 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 { } else if err != nil {
ethutil.Config.Log.Infoln("[STATE] process:", err) 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 // 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 // Processing a blocks may never happen simultaneously
sm.mutex.Lock() sm.mutex.Lock()
defer sm.mutex.Unlock() 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 // we don't want to undo but since undo only happens on dirty
// nodes this won't happen because Commit would have been called // nodes this won't happen because Commit would have been called
// before that. // 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 // 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 // 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 // Process the transactions on to current block
sm.ApplyTransactions(sm.bc.CurrentBlock, block.Transactions()) sm.ApplyTransactions(state, sm.bc.CurrentBlock, block.Transactions())
// Block validation // Block validation
if err := sm.ValidateBlock(block); err != nil { 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 // I'm not sure, but I don't know if there should be thrown
// any errors at this time. // 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) fmt.Println("[SM] Error accumulating reward", err)
return err return err
} }
if !sm.compState.Cmp(sm.procState) { //if !sm.compState.Cmp(state) {
return fmt.Errorf("Invalid merkle root. Expected %x, got %x", sm.compState.trie.Root, sm.procState.trie.Root) 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 // Calculate the new total difficulty and sync back to the db
if sm.CalculateTD(block) { if sm.CalculateTD(block) {
// Sync the current block's state to the database and cancelling out the deferred Undo // 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 // Add the block to the chain
sm.bc.Add(block) sm.bc.Add(block)
@ -193,14 +184,14 @@ func (sm *StateManager) ProcessBlock(block *Block, dontReact bool) error {
if dontReact == false { if dontReact == false {
sm.Ethereum.Reactor().Post("newBlock", block) 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.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
sm.Ethereum.TxPool().RemoveInvalid(sm.procState) sm.Ethereum.TxPool().RemoveInvalid(state)
} else { } else {
fmt.Println("total diff failed") fmt.Println("total diff failed")
} }
@ -276,21 +267,21 @@ func CalculateUncleReward(block *Block) *big.Int {
return UncleReward 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 // 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 // Reward amount of ether to the coinbase address
account.AddAmount(CalculateBlockReward(block, len(block.Uncles))) account.AddAmount(CalculateBlockReward(block, len(block.Uncles)))
addr := make([]byte, len(block.Coinbase)) addr := make([]byte, len(block.Coinbase))
copy(addr, block.Coinbase) copy(addr, block.Coinbase)
sm.procState.UpdateStateObject(account) state.UpdateStateObject(account)
for _, uncle := range block.Uncles { for _, uncle := range block.Uncles {
uncleAccount := sm.procState.GetAccount(uncle.Coinbase) uncleAccount := state.GetAccount(uncle.Coinbase)
uncleAccount.AddAmount(CalculateUncleReward(uncle)) uncleAccount.AddAmount(CalculateUncleReward(uncle))
sm.procState.UpdateStateObject(uncleAccount) state.UpdateStateObject(uncleAccount)
} }
return nil return nil
@ -300,8 +291,8 @@ func (sm *StateManager) Stop() {
sm.bc.Stop() sm.bc.Stop()
} }
func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Transaction, block *Block) { func (sm *StateManager) EvalScript(state *State, script []byte, object *StateObject, tx *Transaction, block *Block) {
account := sm.procState.GetAccount(tx.Sender()) account := state.GetAccount(tx.Sender())
err := account.ConvertGas(tx.Gas, tx.GasPrice) err := account.ConvertGas(tx.Gas, tx.GasPrice)
if err != nil { if err != nil {
@ -309,8 +300,8 @@ func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Trans
return return
} }
closure := NewClosure(account, object, script, sm.procState, tx.Gas, tx.GasPrice) closure := NewClosure(account, object, script, state, tx.Gas, tx.GasPrice)
vm := NewVm(sm.procState, sm, RuntimeVars{ vm := NewVm(state, sm, RuntimeVars{
Origin: account.Address(), Origin: account.Address(),
BlockNumber: block.BlockInfo().Number, BlockNumber: block.BlockInfo().Number,
PrevHash: block.PrevHash, PrevHash: block.PrevHash,
@ -323,16 +314,16 @@ func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Trans
closure.Call(vm, tx.Data, nil) closure.Call(vm, tx.Data, nil)
// Update the account (refunds) // Update the account (refunds)
sm.procState.UpdateStateObject(account) state.UpdateStateObject(account)
sm.procState.UpdateStateObject(object) state.UpdateStateObject(object)
} }
func (sm *StateManager) notifyChanges() { func (sm *StateManager) notifyChanges(state *State) {
for addr, stateObject := range sm.procState.manifest.objectChanges { for addr, stateObject := range state.manifest.objectChanges {
sm.Ethereum.Reactor().Post("object:"+addr, stateObject) 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 { for addr, value := range mappedObjects {
sm.Ethereum.Reactor().Post("storage:"+stateObjectAddr+":"+addr, &StorageState{[]byte(stateObjectAddr), []byte(addr), value}) sm.Ethereum.Reactor().Post("storage:"+stateObjectAddr+":"+addr, &StorageState{[]byte(stateObjectAddr), []byte(addr), value})
} }

@ -149,7 +149,8 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
} }
// Get the sender // 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)) 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 // Make sure there's enough in the sender's account. Having insufficient

@ -12,7 +12,7 @@ import (
) )
func TestRun4(t *testing.T) { func TestRun4(t *testing.T) {
ethutil.ReadConfig("") ethutil.ReadConfig("", ethutil.LogStd)
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
state := NewState(ethutil.NewTrie(db, "")) state := NewState(ethutil.NewTrie(db, ""))

@ -235,7 +235,7 @@ func (s *Ethereum) ConnectToPeer(addr string) error {
s.peers.PushBack(peer) s.peers.PushBack(peer)
log.Printf("[SERV] Adding peer %d / %d\n", s.peers.Len(), s.MaxPeers) ethutil.Config.Log.Infof("[SERV] Adding peer %d / %d\n", s.peers.Len(), s.MaxPeers)
} }
return nil return nil

@ -53,8 +53,8 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
} }
func (miner *Miner) Start() { func (miner *Miner) Start() {
// Prepare inital block // Prepare inital block
miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
go func() { miner.listener() }() go miner.listener()
} }
func (miner *Miner) listener() { func (miner *Miner) listener() {
for { for {
@ -88,7 +88,7 @@ func (miner *Miner) listener() {
if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
log.Println("[MINER] Adding uncle block") log.Println("[MINER] Adding uncle block")
miner.uncles = append(miner.uncles, block) miner.uncles = append(miner.uncles, block)
miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
} }
} }
} }
@ -119,31 +119,19 @@ func (miner *Miner) listener() {
miner.block.SetUncles(miner.uncles) miner.block.SetUncles(miner.uncles)
} }
// FIXME @ maranh, first block doesn't need this. Everything after the first block does.
// Please check and fix
miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
// Apply all transactions to the block // Apply all transactions to the block
miner.ethereum.StateManager().ApplyTransactions(miner.block, miner.block.Transactions()) miner.ethereum.StateManager().ApplyTransactions(miner.block.State(), miner.block, miner.block.Transactions())
miner.ethereum.StateManager().AccumelateRewards(miner.block) miner.ethereum.StateManager().AccumelateRewards(miner.block.State(), miner.block)
// Search the nonce // Search the nonce
//log.Println("[MINER] Initialision complete, starting mining")
miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan)
if miner.block.Nonce != nil { if miner.block.Nonce != nil {
miner.ethereum.StateManager().PrepareDefault(miner.block) err := miner.ethereum.StateManager().ProcessBlock(miner.ethereum.StateManager().CurrentState(), miner.block, true)
err := miner.ethereum.StateManager().ProcessBlock(miner.block, true)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.txs = []*ethchain.Transaction{} // Move this somewhere neat
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
} else { } else {
/*
// XXX @maranh This is already done in the state manager, why a 2nd time?
if !miner.ethereum.StateManager().Pow.Verify(miner.block.HashNoNonce(), miner.block.Difficulty, miner.block.Nonce) {
log.Printf("Second stage verification error: Block's nonce is invalid (= %v)\n", ethutil.Hex(miner.block.Nonce))
}
*/
miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val})
log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash())

@ -45,7 +45,7 @@ func (lib *PEthereum) GetKey() *PKey {
} }
func (lib *PEthereum) GetStateObject(address string) *PStateObject { func (lib *PEthereum) GetStateObject(address string) *PStateObject {
stateObject := lib.stateManager.ProcState().GetContract(ethutil.FromHex(address)) stateObject := lib.stateManager.CurrentState().GetContract(ethutil.FromHex(address))
if stateObject != nil { if stateObject != nil {
return NewPStateObject(stateObject) return NewPStateObject(stateObject)
} }

@ -9,14 +9,6 @@ import (
"runtime" "runtime"
) )
// Log types available
type LogType byte
const (
LogTypeStdIn = 1
LogTypeFile = 2
)
// Config struct // Config struct
type config struct { type config struct {
Db Database Db Database
@ -34,7 +26,7 @@ var Config *config
// Read config // Read config
// //
// Initialize the global Config variable with default settings // Initialize the global Config variable with default settings
func ReadConfig(base string) *config { func ReadConfig(base string, logTypes LoggerType) *config {
if Config == nil { if Config == nil {
usr, _ := user.Current() usr, _ := user.Current()
path := path.Join(usr.HomeDir, base) path := path.Join(usr.HomeDir, base)
@ -51,7 +43,7 @@ func ReadConfig(base string) *config {
} }
Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC6"} Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC6"}
Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug) Config.Log = NewLogger(logTypes, LogLevelDebug)
Config.SetClientString("/Ethereum(G)") Config.SetClientString("/Ethereum(G)")
} }

@ -331,8 +331,9 @@ func (p *Peer) HandleInbound() {
for i := msg.Data.Len() - 1; i >= 0; i-- { for i := msg.Data.Len() - 1; i >= 0; i-- {
block = ethchain.NewBlockFromRlpValue(msg.Data.Get(i)) block = ethchain.NewBlockFromRlpValue(msg.Data.Get(i))
p.ethereum.StateManager().PrepareDefault(block) //p.ethereum.StateManager().PrepareDefault(block)
err = p.ethereum.StateManager().ProcessBlock(block, false) state := p.ethereum.StateManager().CurrentState()
err = p.ethereum.StateManager().ProcessBlock(state, block, false)
if err != nil { if err != nil {
if ethutil.Config.Debug { if ethutil.Config.Debug {

Loading…
Cancel
Save