From e504088b79f55dd08749f08f434cb85a0033898f Mon Sep 17 00:00:00 2001
From: obscuren <geffobscura@gmail.com>
Date: Thu, 10 Jul 2014 15:05:06 +0200
Subject: Consensus and bug fixes

* Ensure that each state object has an address that is 20 bytes
* Byte logging for vm
* changed diff output
---
 ethchain/state.go         |  9 ++++++--
 ethchain/state_manager.go |  7 +++++++
 ethchain/state_object.go  | 53 +++++++++++++++++++++++++++++++++++++----------
 ethchain/vm.go            |  2 ++
 4 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/ethchain/state.go b/ethchain/state.go
index f5c038226..155366376 100644
--- a/ethchain/state.go
+++ b/ethchain/state.go
@@ -1,6 +1,7 @@
 package ethchain
 
 import (
+	"fmt"
 	"github.com/ethereum/eth-go/ethcrypto"
 	"github.com/ethereum/eth-go/ethtrie"
 	"github.com/ethereum/eth-go/ethutil"
@@ -36,7 +37,8 @@ func (s *State) Reset() {
 			continue
 		}
 
-		stateObject.state.Reset()
+		//stateObject.state.Reset()
+		stateObject.Reset()
 	}
 
 	s.Empty()
@@ -69,6 +71,10 @@ func (self *State) Update() {
 		if stateObject.remove {
 			self.DeleteStateObject(stateObject)
 		} else {
+			stateObject.Sync()
+
+			fmt.Printf("%x %x\n", stateObject.Address(), stateObject.state.Root())
+
 			self.UpdateStateObject(stateObject)
 		}
 	}
@@ -78,7 +84,6 @@ func (self *State) Update() {
 	if !valid {
 		self.trie = t2
 	}
-
 }
 
 // Purges the current trie.
diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go
index f3fd92913..a5afa5096 100644
--- a/ethchain/state_manager.go
+++ b/ethchain/state_manager.go
@@ -66,6 +66,11 @@ type StateManager struct {
 	// Mining state. The mining state is used purely and solely by the mining
 	// operation.
 	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 {
@@ -165,6 +170,8 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) {
 		return ParentError(block.PrevHash)
 	}
 
+	sm.lastAttemptedBlock = block
+
 	var (
 		parent = sm.bc.GetBlock(block.PrevHash)
 		state  = parent.State()
diff --git a/ethchain/state_object.go b/ethchain/state_object.go
index 2c7f36e65..ebc050863 100644
--- a/ethchain/state_object.go
+++ b/ethchain/state_object.go
@@ -27,6 +27,8 @@ type StateObject struct {
 	script     Code
 	initScript Code
 
+	storage map[string]*ethutil.Value
+
 	// Total gas pool is the total amount of gas currently
 	// left if this object is the coinbase. Gas is directly
 	// purchased of the coinbase.
@@ -38,6 +40,10 @@ type StateObject struct {
 	remove bool
 }
 
+func (self *StateObject) Reset() {
+	self.storage = make(map[string]*ethutil.Value)
+}
+
 // Converts an transaction in to a state object
 func MakeContract(tx *Transaction, state *State) *StateObject {
 	// Create contract if there's no recipient
@@ -55,14 +61,19 @@ func MakeContract(tx *Transaction, state *State) *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.storage = make(map[string]*ethutil.Value)
 
 	return object
 }
 
 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)))
 
 	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()))
 }
 
-func (c *StateObject) SetStorage(num *big.Int, val *ethutil.Value) {
-	addr := ethutil.BigToBytes(num, 256)
+func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value {
+	return self.getStorage(key.Bytes())
+}
+func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) {
+	self.setStorage(key.Bytes(), value)
+}
 
-	if val.BigInt().Cmp(ethutil.Big0) == 0 {
-		c.state.trie.Delete(string(addr))
+func (self *StateObject) getStorage(key []byte) *ethutil.Value {
+	k := ethutil.LeftPadBytes(key, 32)
 
-		return
+	value := self.storage[string(k)]
+	if value == nil {
+		value = self.GetAddr(k)
+
+		self.storage[string(k)] = value
 	}
 
-	c.SetAddr(addr, val)
+	return value
 }
 
-func (c *StateObject) GetStorage(num *big.Int) *ethutil.Value {
-	nb := ethutil.BigToBytes(num, 256)
+func (self *StateObject) setStorage(key []byte, value *ethutil.Value) {
+	k := ethutil.LeftPadBytes(key, 32)
 
-	return c.GetAddr(nb)
+	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 {
 	if int64(len(c.script)-1) < pc.Int64() {
 		return ethutil.NewValue(0)
@@ -249,6 +279,7 @@ func (c *StateObject) RlpDecode(data []byte) {
 	c.Nonce = decoder.Get(0).Uint()
 	c.Amount = decoder.Get(1).BigInt()
 	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()
 
diff --git a/ethchain/vm.go b/ethchain/vm.go
index 3b58d6106..56456fdab 100644
--- a/ethchain/vm.go
+++ b/ethchain/vm.go
@@ -182,7 +182,9 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
 			require(2)
 			newMemSize = stack.Peek().Uint64() + 32
 		case MLOAD:
+			require(1)
 
+			newMemSize = stack.Peek().Uint64() + 32
 		case MSTORE8:
 			require(2)
 			newMemSize = stack.Peek().Uint64() + 1
-- 
cgit v1.2.3