Implemented POST

This commit is contained in:
obscuren 2014-08-25 11:29:42 +02:00
parent cdbc3ecc2a
commit 3f904bf3ac
6 changed files with 217 additions and 79 deletions

View File

@ -278,6 +278,15 @@ func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context
ret, _, err = callerClosure.Call(vm, self.tx.Data)
if err == nil {
// Execute POSTs
for e := vm.Queue().Front(); e != nil; e = e.Next() {
msg := e.Value.(*ethvm.Message)
msg.Exec(transactor)
}
}
return
}

View File

@ -245,6 +245,7 @@ func (self *StateObject) Copy() *StateObject {
stateObject.InitCode = ethutil.CopyBytes(self.InitCode)
stateObject.storage = self.storage.Copy()
stateObject.gasPool.Set(self.gasPool)
stateObject.remove = self.remove
return stateObject
}
@ -271,6 +272,11 @@ func (c *StateObject) Init() Code {
return c.InitCode
}
// To satisfy ClosureRef
func (self *StateObject) Object() *StateObject {
return self
}
// Debug stuff
func (self *StateObject) CreateOutputForDiff() {
fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.Balance.Bytes(), self.Nonce)

View File

@ -12,6 +12,7 @@ import (
type ClosureRef interface {
ReturnGas(*big.Int, *big.Int)
Address() []byte
Object() *ethstate.StateObject
GetStorage(*big.Int) *ethutil.Value
SetStorage(*big.Int, *ethutil.Value)
}

View File

@ -145,6 +145,7 @@ const (
CREATE = 0xf0
CALL = 0xf1
RETURN = 0xf2
POST = 0xf3
// 0x70 range - other
LOG = 0xfe // XXX Unofficial
@ -438,6 +439,7 @@ var OpCodes = map[string]byte{
"CREATE": 0xf0,
"CALL": 0xf1,
"RETURN": 0xf2,
"POST": 0xf3,
// 0x70 range - other
"LOG": 0xfe,

View File

@ -1,6 +1,7 @@
package ethvm
import (
"container/list"
"fmt"
"math"
"math/big"
@ -18,11 +19,6 @@ type Debugger interface {
}
type Vm struct {
// Stack for processing contracts
stack *Stack
// non-persistent key/value memory storage
mem map[string]*big.Int
env Environment
Verbose bool
@ -40,6 +36,8 @@ type Vm struct {
Fn string
Recoverable bool
queue *list.List
}
type Environment interface {
@ -66,7 +64,7 @@ func New(env Environment) *Vm {
lt = LogTyDiff
}
return &Vm{env: env, logTy: lt, Recoverable: true}
return &Vm{env: env, logTy: lt, Recoverable: true, queue: list.New()}
}
func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
@ -215,6 +213,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
newMemSize = stack.data[stack.Len()-2].Uint64() + stack.data[stack.Len()-3].Uint64()
}
// BUG This will break on overflows. https://github.com/ethereum/eth-go/issues/47
newMemSize = (newMemSize + 31) / 32 * 32
if newMemSize > uint64(mem.Len()) {
m := GasMemory.Uint64() * (newMemSize - uint64(mem.Len())) / 32
@ -711,6 +710,8 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
err error
value = stack.Pop()
size, offset = stack.Popn()
input = mem.Get(offset.Int64(), size.Int64())
gas = new(big.Int).Set(closure.Gas)
// Snapshot the current stack so we are able to
// revert back to it later.
@ -726,6 +727,23 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
self.Printf(" (*) %x", addr).Endl()
closure.UseGas(closure.Gas)
msg := NewMessage(self, addr, input, gas, closure.Price, value)
ret, err := msg.Exec(closure)
if err != nil {
stack.Push(ethutil.BigFalse)
// Revert the state as it was before.
self.env.State().Set(snapshot)
self.Printf("CREATE err %v", err)
} else {
msg.object.Code = ret
stack.Push(ethutil.BigD(addr))
}
/*
msg := self.env.State().Manifest().AddMessage(&ethstate.Message{
To: addr, From: closure.Address(),
Origin: self.env.Origin(),
@ -769,6 +787,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
msg.Output = contract.Code
}
*/
self.Endl()
// Debug hook
@ -791,6 +810,26 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
// Get the arguments from the memory
args := mem.Get(inOffset.Int64(), inSize.Int64())
snapshot := self.env.State().Copy()
msg := NewMessage(self, addr.Bytes(), args, gas, closure.Price, value)
ret, err := msg.Exec(closure)
if err != nil {
stack.Push(ethutil.BigFalse)
self.env.State().Set(snapshot)
} else {
stack.Push(ethutil.BigTrue)
mem.Set(retOffset.Int64(), retSize.Int64(), ret)
}
// Debug hook
if self.Dbg != nil {
self.Dbg.SetCode(closure.Code)
}
/*
msg := self.env.State().Manifest().AddMessage(&ethstate.Message{
To: addr.Bytes(), From: closure.Address(),
Input: args,
@ -836,6 +875,23 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
self.Dbg.SetCode(closure.Code)
}
}
*/
case POST:
require(6)
self.Endl()
gas := stack.Pop()
// Pop gas and value of the stack.
value, addr := stack.Popn()
// Pop input size and offset
inSize, inOffset := stack.Popn()
// Get the arguments from the memory
args := mem.Get(inOffset.Int64(), inSize.Int64())
msg := NewMessage(self, addr.Bytes(), args, gas, closure.Price, value)
msg.Postpone()
case RETURN:
require(2)
size, offset := stack.Popn()
@ -887,6 +943,10 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
}
}
func (self *Vm) Queue() *list.List {
return self.queue
}
func (self *Vm) Printf(format string, v ...interface{}) *Vm {
if self.Verbose && self.logTy == LogTyPretty {
self.logStr += fmt.Sprintf(format, v...)
@ -918,3 +978,64 @@ func ensure256(x *big.Int) {
x.SetInt64(0)
}
}
type Message struct {
vm *Vm
closure *Closure
address, input []byte
gas, price, value *big.Int
object *ethstate.StateObject
}
func NewMessage(vm *Vm, address, input []byte, gas, gasPrice, value *big.Int) *Message {
return &Message{vm: vm, address: address, input: input, gas: gas, price: gasPrice, value: value}
}
func (self *Message) Postpone() {
self.vm.queue.PushBack(self)
}
func (self *Message) Exec(caller ClosureRef) (ret []byte, err error) {
queue := self.vm.queue
self.vm.queue = list.New()
defer func() {
if err == nil {
queue.PushBackList(self.vm.queue)
}
self.vm.queue = queue
}()
msg := self.vm.env.State().Manifest().AddMessage(&ethstate.Message{
To: self.address, From: caller.Address(),
Input: self.input,
Origin: self.vm.env.Origin(),
Block: self.vm.env.BlockHash(), Timestamp: self.vm.env.Time(), Coinbase: self.vm.env.Coinbase(), Number: self.vm.env.BlockNumber(),
Value: self.value,
})
object := caller.Object()
if object.Balance.Cmp(self.value) < 0 {
caller.ReturnGas(self.gas, self.price)
err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, object.Balance)
} else {
stateObject := self.vm.env.State().GetOrNewStateObject(self.address)
self.object = stateObject
caller.Object().SubAmount(self.value)
stateObject.AddAmount(self.value)
// Create a new callable closure
c := NewClosure(msg, caller, object, object.Code, self.gas, self.price)
// Executer the closure and get the return value (if any)
ret, _, err = c.Call(self.vm, self.input)
msg.Output = ret
return ret, err
}
return
}

View File

@ -1,7 +1,6 @@
package eth
import (
//natpmp "code.google.com/p/go-nat-pmp"
"fmt"
"net"