Fixed coinbase copy in state

This commit is contained in:
obscuren 2014-07-17 14:53:27 +02:00
parent ed3424ff75
commit 0415e4a637
5 changed files with 29 additions and 19 deletions

View File

@ -61,6 +61,7 @@ func (self *State) UpdateStateObject(stateObject *StateObject) {
addr := stateObject.Address()
ethutil.Config.Db.Put(ethcrypto.Sha3Bin(stateObject.Script()), stateObject.Script())
fmt.Printf("balance %v %p\n", stateObject.Amount, stateObject)
self.trie.Update(string(addr), string(stateObject.RlpEncode()))
@ -174,7 +175,7 @@ func (s *State) Reset() {
func (s *State) Sync() {
// Sync all nested states
for _, stateObject := range s.stateObjects {
s.UpdateStateObject(stateObject)
//s.UpdateStateObject(stateObject)
if stateObject.state == nil {
continue

View File

@ -120,7 +120,9 @@ func (self *StateManager) ProcessTransactions(coinbase *StateObject, state *Stat
done:
for i, tx := range txs {
txGas := new(big.Int).Set(tx.Gas)
st := NewStateTransition(coinbase, tx, state, block)
cb := state.GetStateObject(coinbase.Address())
st := NewStateTransition(cb, tx, state, block)
//fmt.Printf("#%d\n", i+1)
err = st.TransitionState()
if err != nil {

View File

@ -80,6 +80,7 @@ func NewStateObject(addr []byte) *StateObject {
object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)}
object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, ""))
object.storage = make(Storage)
object.gasPool = new(big.Int)
return object
}
@ -183,7 +184,7 @@ func (self *StateObject) Sync() {
fmt.Printf("%x %x %x\n", self.Address(), []byte(key), value.Bytes())
})
*/
//fmt.Printf("%x @:%x\n", self.Address(), self.state.Root())
fmt.Printf("%x @:%x\n", self.Address(), self.state.Root())
}
func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
@ -197,13 +198,13 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
func (c *StateObject) AddAmount(amount *big.Int) {
c.SetAmount(new(big.Int).Add(c.Amount, amount))
statelogger.DebugDetailf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount)
statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount)
}
func (c *StateObject) SubAmount(amount *big.Int) {
c.SetAmount(new(big.Int).Sub(c.Amount, amount))
statelogger.DebugDetailf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount)
statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount)
}
func (c *StateObject) SetAmount(amount *big.Int) {
@ -266,6 +267,7 @@ func (self *StateObject) Copy() *StateObject {
stateObject.script = ethutil.CopyBytes(self.script)
stateObject.initScript = ethutil.CopyBytes(self.initScript)
stateObject.storage = self.storage.Copy()
stateObject.gasPool.Set(self.gasPool)
return stateObject
}
@ -324,6 +326,7 @@ func (c *StateObject) RlpDecode(data []byte) {
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.gasPool = new(big.Int)
c.ScriptHash = decoder.Get(3).Bytes()

View File

@ -42,7 +42,7 @@ func (self *StateTransition) Coinbase() *StateObject {
return self.cb
}
self.cb = self.state.GetAccount(self.coinbase)
self.cb = self.state.GetOrNewStateObject(self.coinbase)
return self.cb
}
func (self *StateTransition) Sender() *StateObject {
@ -50,7 +50,7 @@ func (self *StateTransition) Sender() *StateObject {
return self.sen
}
self.sen = self.state.GetAccount(self.tx.Sender())
self.sen = self.state.GetOrNewStateObject(self.tx.Sender())
return self.sen
}
@ -63,7 +63,7 @@ func (self *StateTransition) Receiver() *StateObject {
return self.rec
}
self.rec = self.state.GetAccount(self.tx.Recipient)
self.rec = self.state.GetOrNewStateObject(self.tx.Recipient)
return self.rec
}
@ -174,13 +174,16 @@ func (self *StateTransition) TransitionState() (err error) {
return
}
/* FIXME
* If tx goes TO "0", goes OOG during init, reverse changes, but initial endowment should happen. The ether is lost forever
*/
var snapshot *State
if sender.Amount.Cmp(self.value) < 0 {
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Amount)
}
var snapshot *State
// If the receiver is nil it's a contract (\0*32).
if tx.CreatesContract() {
// Subtract the (irreversible) amount from the senders account
sender.SubAmount(self.value)
snapshot = self.state.Copy()
// Create a new state object for the contract
@ -189,16 +192,17 @@ func (self *StateTransition) TransitionState() (err error) {
if receiver == nil {
return fmt.Errorf("Unable to create contract")
}
// Add the amount to receivers account which should conclude this transaction
receiver.AddAmount(self.value)
} else {
receiver = self.Receiver()
}
// Transfer value from sender to receiver
if err = self.transferValue(sender, receiver); err != nil {
return
}
// Subtract the amount from the senders account
sender.SubAmount(self.value)
// Add the amount to receivers account which should conclude this transaction
receiver.AddAmount(self.value)
if snapshot == nil {
snapshot = self.state.Copy()
}

View File

@ -456,7 +456,7 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
case BYTE:
require(2)
val, th := stack.Popn()
if th.Cmp(big.NewInt(32)) < 0 {
if th.Cmp(big.NewInt(32)) < 0 && th.Cmp(big.NewInt(int64(len(val.Bytes())))) < 0 {
byt := big.NewInt(int64(val.Bytes()[th.Int64()]))
stack.Push(byt)