Consensus and bug fixes

* Ensure that each state object has an address that is 20 bytes
* Byte logging for vm
* changed diff output
This commit is contained in:
obscuren 2014-07-10 15:05:06 +02:00
parent d52e5f7130
commit e504088b79
4 changed files with 58 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package ethchain package ethchain
import ( import (
"fmt"
"github.com/ethereum/eth-go/ethcrypto" "github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethtrie" "github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
@ -36,7 +37,8 @@ func (s *State) Reset() {
continue continue
} }
stateObject.state.Reset() //stateObject.state.Reset()
stateObject.Reset()
} }
s.Empty() s.Empty()
@ -69,6 +71,10 @@ func (self *State) Update() {
if stateObject.remove { if stateObject.remove {
self.DeleteStateObject(stateObject) self.DeleteStateObject(stateObject)
} else { } else {
stateObject.Sync()
fmt.Printf("%x %x\n", stateObject.Address(), stateObject.state.Root())
self.UpdateStateObject(stateObject) self.UpdateStateObject(stateObject)
} }
} }
@ -78,7 +84,6 @@ func (self *State) Update() {
if !valid { if !valid {
self.trie = t2 self.trie = t2
} }
} }
// Purges the current trie. // Purges the current trie.

View File

@ -66,6 +66,11 @@ type StateManager struct {
// Mining state. The mining state is used purely and solely by the mining // Mining state. The mining state is used purely and solely by the mining
// operation. // operation.
miningState *State miningState *State
// The last attempted block is mainly used for debugging purposes
// This does not have to be a valid block and will be set during
// 'Process' & canonical validation.
lastAttemptedBlock *Block
} }
func NewStateManager(ethereum EthManager) *StateManager { func NewStateManager(ethereum EthManager) *StateManager {
@ -165,6 +170,8 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) {
return ParentError(block.PrevHash) return ParentError(block.PrevHash)
} }
sm.lastAttemptedBlock = block
var ( var (
parent = sm.bc.GetBlock(block.PrevHash) parent = sm.bc.GetBlock(block.PrevHash)
state = parent.State() state = parent.State()

View File

@ -27,6 +27,8 @@ type StateObject struct {
script Code script Code
initScript Code initScript Code
storage map[string]*ethutil.Value
// Total gas pool is the total amount of gas currently // Total gas pool is the total amount of gas currently
// left if this object is the coinbase. Gas is directly // left if this object is the coinbase. Gas is directly
// purchased of the coinbase. // purchased of the coinbase.
@ -38,6 +40,10 @@ type StateObject struct {
remove bool remove bool
} }
func (self *StateObject) Reset() {
self.storage = make(map[string]*ethutil.Value)
}
// Converts an transaction in to a state object // Converts an transaction in to a state object
func MakeContract(tx *Transaction, state *State) *StateObject { func MakeContract(tx *Transaction, state *State) *StateObject {
// Create contract if there's no recipient // Create contract if there's no recipient
@ -55,14 +61,19 @@ func MakeContract(tx *Transaction, state *State) *StateObject {
} }
func NewStateObject(addr []byte) *StateObject { func NewStateObject(addr []byte) *StateObject {
object := &StateObject{address: addr, Amount: new(big.Int), gasPool: new(big.Int)} // This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
address := ethutil.LeftPadBytes(addr, 20)
object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)}
object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, ""))
object.storage = make(map[string]*ethutil.Value)
return object return object
} }
func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject {
contract := &StateObject{address: address, Amount: Amount, Nonce: 0} contract := NewStateObject(address)
contract.Amount = Amount
contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root)))
return contract return contract
@ -95,24 +106,43 @@ func (c *StateObject) SetAddr(addr []byte, value interface{}) {
c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode()))
} }
func (c *StateObject) SetStorage(num *big.Int, val *ethutil.Value) { func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value {
addr := ethutil.BigToBytes(num, 256) return self.getStorage(key.Bytes())
}
if val.BigInt().Cmp(ethutil.Big0) == 0 { func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) {
c.state.trie.Delete(string(addr)) self.setStorage(key.Bytes(), value)
return
} }
c.SetAddr(addr, val) func (self *StateObject) getStorage(key []byte) *ethutil.Value {
k := ethutil.LeftPadBytes(key, 32)
value := self.storage[string(k)]
if value == nil {
value = self.GetAddr(k)
self.storage[string(k)] = value
} }
func (c *StateObject) GetStorage(num *big.Int) *ethutil.Value { return value
nb := ethutil.BigToBytes(num, 256)
return c.GetAddr(nb)
} }
func (self *StateObject) setStorage(key []byte, value *ethutil.Value) {
k := ethutil.LeftPadBytes(key, 32)
self.storage[string(k)] = value
}
func (self *StateObject) Sync() {
for key, value := range self.storage {
if value.BigInt().Cmp(ethutil.Big0) == 0 {
self.state.trie.Delete(string(key))
continue
}
self.SetAddr([]byte(key), value)
}
}
func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
if int64(len(c.script)-1) < pc.Int64() { if int64(len(c.script)-1) < pc.Int64() {
return ethutil.NewValue(0) return ethutil.NewValue(0)
@ -249,6 +279,7 @@ func (c *StateObject) RlpDecode(data []byte) {
c.Nonce = decoder.Get(0).Uint() c.Nonce = decoder.Get(0).Uint()
c.Amount = decoder.Get(1).BigInt() c.Amount = decoder.Get(1).BigInt()
c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))
c.storage = make(map[string]*ethutil.Value)
c.ScriptHash = decoder.Get(3).Bytes() c.ScriptHash = decoder.Get(3).Bytes()

View File

@ -182,7 +182,9 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
require(2) require(2)
newMemSize = stack.Peek().Uint64() + 32 newMemSize = stack.Peek().Uint64() + 32
case MLOAD: case MLOAD:
require(1)
newMemSize = stack.Peek().Uint64() + 32
case MSTORE8: case MSTORE8:
require(2) require(2)
newMemSize = stack.Peek().Uint64() + 1 newMemSize = stack.Peek().Uint64() + 1