From e235b57234a68a8a39cfe7691a1825d8c6bb3443 Mon Sep 17 00:00:00 2001
From: obscuren <geffobscura@gmail.com>
Date: Thu, 26 Feb 2015 18:39:05 +0100
Subject: [PATCH] Fixed consensus issue for refunding

* Refund should _always_ go to the origin
---
 core/block_processor.go  |  8 ++++----
 core/state_transition.go |  9 ++++++---
 miner/worker.go          |  4 ++--
 state/dump.go            |  2 +-
 state/state_object.go    | 21 +++++++++++++++------
 vm/vm.go                 |  5 +++--
 6 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/core/block_processor.go b/core/block_processor.go
index f66d158b2..7eaeb5be0 100644
--- a/core/block_processor.go
+++ b/core/block_processor.go
@@ -48,9 +48,8 @@ type BlockProcessor struct {
 
 func NewBlockProcessor(db ethutil.Database, txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
 	sm := &BlockProcessor{
-		db:  db,
-		mem: make(map[string]*big.Int),
-		//Pow:      &ethash.Ethash{},
+		db:       db,
+		mem:      make(map[string]*big.Int),
 		Pow:      ezp.New(),
 		bc:       chainManager,
 		eventMux: eventMux,
@@ -100,7 +99,8 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
 	// Notify all subscribers
 	if !transientProcess {
 		go self.eventMux.Post(TxPostEvent{tx})
-		go self.eventMux.Post(statedb.Logs())
+		logs := statedb.Logs()
+		go self.eventMux.Post(logs)
 	}
 
 	return receipt, txGas, err
diff --git a/core/state_transition.go b/core/state_transition.go
index 36ffa23d9..a065c4f6b 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -126,7 +126,7 @@ func (self *StateTransition) BuyGas() error {
 
 	self.AddGas(self.msg.Gas())
 	self.initialGas.Set(self.msg.Gas())
-	sender.SubAmount(MessageGasValue(self.msg))
+	sender.SubBalance(MessageGasValue(self.msg))
 
 	return nil
 }
@@ -251,13 +251,16 @@ func (self *StateTransition) RefundGas() {
 	coinbase, sender := self.Coinbase(), self.From()
 	// Return remaining gas
 	remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice())
-	sender.AddAmount(remaining)
+	fmt.Println("REFUND:", remaining)
+	sender.AddBalance(remaining)
 
 	uhalf := new(big.Int).Div(self.GasUsed(), ethutil.Big2)
 	for addr, ref := range self.state.Refunds() {
 		refund := ethutil.BigMin(uhalf, ref)
 		self.gas.Add(self.gas, refund)
-		self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice()))
+		addToIt := refund.Mul(refund, self.msg.GasPrice())
+		fmt.Println("ADD TO IT", addToIt)
+		self.state.AddBalance([]byte(addr), addToIt)
 	}
 
 	coinbase.RefundGas(self.gas, self.msg.GasPrice())
diff --git a/miner/worker.go b/miner/worker.go
index 1f3a52ab5..4f0909302 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -197,7 +197,7 @@ gasLimit:
 	}
 	self.eth.TxPool().RemoveSet(remove)
 
-	self.current.coinbase.AddAmount(core.BlockReward)
+	self.current.coinbase.AddBalance(core.BlockReward)
 
 	self.current.state.Update(ethutil.Big0)
 	self.push()
@@ -225,7 +225,7 @@ func (self *worker) commitUncle(uncle *types.Header) error {
 	}
 
 	uncleAccount := self.current.state.GetAccount(uncle.Coinbase)
-	uncleAccount.AddAmount(uncleReward)
+	uncleAccount.AddBalance(uncleReward)
 
 	self.current.coinbase.AddBalance(uncleReward)
 
diff --git a/state/dump.go b/state/dump.go
index 81895f1a3..073f89414 100644
--- a/state/dump.go
+++ b/state/dump.go
@@ -35,7 +35,7 @@ func (self *StateDB) Dump() []byte {
 
 		storageIt := stateObject.State.trie.Iterator()
 		for storageIt.Next() {
-			account.Storage[ethutil.Bytes2Hex(it.Key)] = ethutil.Bytes2Hex(it.Value)
+			account.Storage[ethutil.Bytes2Hex(storageIt.Key)] = ethutil.Bytes2Hex(storageIt.Value)
 		}
 		world.Accounts[ethutil.Bytes2Hex(it.Key)] = account
 	}
diff --git a/state/state_object.go b/state/state_object.go
index 477b912a1..487952a02 100644
--- a/state/state_object.go
+++ b/state/state_object.go
@@ -19,6 +19,14 @@ func (self Code) String() string {
 
 type Storage map[string]*ethutil.Value
 
+func (self Storage) String() (str string) {
+	for key, value := range self {
+		str += fmt.Sprintf("%X : %X\n", key, value.Bytes())
+	}
+
+	return
+}
+
 func (self Storage) Copy() Storage {
 	cpy := make(Storage)
 	for key, value := range self {
@@ -119,10 +127,9 @@ func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value {
 }
 func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) {
 	self.SetState(key.Bytes(), value)
-	self.dirty = true
 }
 
-func (self *StateObject) Storage() map[string]*ethutil.Value {
+func (self *StateObject) Storage() Storage {
 	return self.storage
 }
 
@@ -172,20 +179,22 @@ func (c *StateObject) AddBalance(amount *big.Int) {
 
 	statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.nonce, c.balance, amount)
 }
-func (c *StateObject) AddAmount(amount *big.Int) { c.AddBalance(amount) }
 
 func (c *StateObject) SubBalance(amount *big.Int) {
 	c.SetBalance(new(big.Int).Sub(c.balance, amount))
 
 	statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.nonce, c.balance, amount)
 }
-func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) }
 
 func (c *StateObject) SetBalance(amount *big.Int) {
 	c.balance = amount
 	c.dirty = true
 }
 
+func (c *StateObject) St() Storage {
+	return c.storage
+}
+
 //
 // Gas setters and getters
 //
@@ -198,7 +207,7 @@ func (c *StateObject) ConvertGas(gas, price *big.Int) error {
 		return fmt.Errorf("insufficient amount: %v, %v", c.balance, total)
 	}
 
-	c.SubAmount(total)
+	c.SubBalance(total)
 
 	c.dirty = true
 
@@ -221,7 +230,7 @@ func (self *StateObject) BuyGas(gas, price *big.Int) error {
 	rGas := new(big.Int).Set(gas)
 	rGas.Mul(rGas, price)
 
-	self.AddAmount(rGas)
+	self.AddBalance(rGas)
 
 	self.dirty = true
 
diff --git a/vm/vm.go b/vm/vm.go
index f9efeed96..7aeeea661 100644
--- a/vm/vm.go
+++ b/vm/vm.go
@@ -664,6 +664,7 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
 				}
 				addr = ref.Address()
 
+				fmt.Printf("CREATE %X\n", addr)
 				stack.Push(ethutil.BigD(addr))
 
 			}
@@ -727,7 +728,7 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
 
 			self.Printf(" => (%x) %v", receiver.Address()[:4], balance)
 
-			receiver.AddAmount(balance)
+			receiver.AddBalance(balance)
 			statedb.Delete(context.Address())
 
 			fallthrough
@@ -828,7 +829,7 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
 			// 0 => non 0
 			mult = ethutil.Big3
 		} else if len(val) > 0 && len(y.Bytes()) == 0 {
-			statedb.Refund(caller.Address(), GasSStoreRefund)
+			statedb.Refund(self.env.Origin(), GasSStoreRefund)
 
 			mult = ethutil.Big0
 		} else {
-- 
GitLab