Closure return, arguments fixed. Added proper tests

This commit is contained in:
obscuren 2014-03-21 14:47:55 +01:00
parent fa1db8d2dc
commit 2ea4c632d1
7 changed files with 92 additions and 60 deletions

View File

@ -6,7 +6,7 @@ import (
) )
type Account struct { type Account struct {
Address []byte address []byte
Amount *big.Int Amount *big.Int
Nonce uint64 Nonce uint64
} }
@ -16,7 +16,7 @@ func NewAccount(address []byte, amount *big.Int) *Account {
} }
func NewAccountFromData(address, data []byte) *Account { func NewAccountFromData(address, data []byte) *Account {
account := &Account{Address: address} account := &Account{address: address}
account.RlpDecode(data) account.RlpDecode(data)
return account return account
@ -30,11 +30,15 @@ func (a *Account) AddFunds(funds *big.Int) {
a.Amount.Add(a.Amount, funds) a.Amount.Add(a.Amount, funds)
} }
func (a *Account) Address() []byte {
return a.address
}
// Implements Callee // Implements Callee
func (a *Account) ReturnGas(value *big.Int, state *State) { func (a *Account) ReturnGas(value *big.Int, state *State) {
// Return the value back to the sender // Return the value back to the sender
a.AddFunds(value) a.AddFunds(value)
state.UpdateAccount(a.Address, a) state.UpdateAccount(a.address, a)
} }
func (a *Account) RlpEncode() []byte { func (a *Account) RlpEncode() []byte {

View File

@ -9,13 +9,13 @@ import (
type Callee interface { type Callee interface {
ReturnGas(*big.Int, *State) ReturnGas(*big.Int, *State)
Address() []byte
} }
type ClosureBody interface { type ClosureBody interface {
Callee Callee
ethutil.RlpEncodable ethutil.RlpEncodable
GetMem(int64) *ethutil.Value GetMem(int64) *ethutil.Value
Address() []byte
} }
// Basic inline closure object which implement the 'closure' interface // Basic inline closure object which implement the 'closure' interface
@ -24,8 +24,8 @@ type Closure struct {
object ClosureBody object ClosureBody
State *State State *State
gas *big.Int Gas *big.Int
val *big.Int Value *big.Int
Args []byte Args []byte
} }
@ -45,6 +45,10 @@ func (c *Closure) GetMem(x int64) *ethutil.Value {
return m return m
} }
func (c *Closure) Address() []byte {
return c.object.Address()
}
func (c *Closure) Call(vm *Vm, args []byte) []byte { func (c *Closure) Call(vm *Vm, args []byte) []byte {
c.Args = args c.Args = args
@ -56,9 +60,9 @@ func (c *Closure) Return(ret []byte) []byte {
// If no callee is present return it to // If no callee is present return it to
// the origin (i.e. contract or tx) // the origin (i.e. contract or tx)
if c.callee != nil { if c.callee != nil {
c.callee.ReturnGas(c.gas, c.State) c.callee.ReturnGas(c.Gas, c.State)
} else { } else {
c.object.ReturnGas(c.gas, c.State) c.object.ReturnGas(c.Gas, c.State)
// TODO incase it's a POST contract we gotta serialise the contract again. // TODO incase it's a POST contract we gotta serialise the contract again.
// But it's not yet defined // But it's not yet defined
} }
@ -69,9 +73,13 @@ func (c *Closure) Return(ret []byte) []byte {
// Implement the Callee interface // Implement the Callee interface
func (c *Closure) ReturnGas(gas *big.Int, state *State) { func (c *Closure) ReturnGas(gas *big.Int, state *State) {
// Return the gas to the closure // Return the gas to the closure
c.gas.Add(c.gas, gas) c.Gas.Add(c.Gas, gas)
} }
func (c *Closure) GetGas() *big.Int { func (c *Closure) Object() ClosureBody {
return c.gas return c.object
}
func (c *Closure) Callee() Callee {
return c.callee
} }

View File

@ -36,24 +36,22 @@ const (
oSHA3 = 0x20 oSHA3 = 0x20
// 0x30 range - closure state // 0x30 range - closure state
oADDRESS = 0x30 oADDRESS = 0x30
oBALANCE = 0x31 oBALANCE = 0x31
oORIGIN = 0x32 oORIGIN = 0x32
oCALLER = 0x33 oCALLER = 0x33
oCALLVALUE = 0x34 oCALLVALUE = 0x34
oCALLDATA = 0x35 oCALLDATA = 0x35
oCALLDATASIZE = 0x36 oCALLDATASIZE = 0x36
oRETURNDATASIZE = 0x37 oGASPRICE = 0x37
oTXGASPRICE = 0x38
// 0x40 range - block operations // 0x40 range - block operations
oPREVHASH = 0x40 oPREVHASH = 0x40
oPREVNONCE = 0x41 oCOINBASE = 0x41
oCOINBASE = 0x42 oTIMESTAMP = 0x42
oTIMESTAMP = 0x43 oNUMBER = 0x43
oNUMBER = 0x44 oDIFFICULTY = 0x44
oDIFFICULTY = 0x45 oGASLIMIT = 0x45
oGASLIMIT = 0x46
// 0x50 range - 'storage' and execution // 0x50 range - 'storage' and execution
oPUSH = 0x50 oPUSH = 0x50
@ -108,19 +106,17 @@ var opCodeToString = map[OpCode]string{
oSHA3: "SHA3", oSHA3: "SHA3",
// 0x30 range - closure state // 0x30 range - closure state
oADDRESS: "ADDRESS", oADDRESS: "ADDRESS",
oBALANCE: "BALANCE", oBALANCE: "BALANCE",
oORIGIN: "ORIGIN", oORIGIN: "ORIGIN",
oCALLER: "CALLER", oCALLER: "CALLER",
oCALLVALUE: "CALLVALUE", oCALLVALUE: "CALLVALUE",
oCALLDATA: "CALLDATA", oCALLDATA: "CALLDATA",
oCALLDATASIZE: "CALLDATASIZE", oCALLDATASIZE: "CALLDATASIZE",
oRETURNDATASIZE: "RETURNDATASIZE", oGASPRICE: "TXGASPRICE",
oTXGASPRICE: "TXGASPRICE",
// 0x40 range - block operations // 0x40 range - block operations
oPREVHASH: "PREVHASH", oPREVHASH: "PREVHASH",
oPREVNONCE: "PREVNONCE",
oCOINBASE: "COINBASE", oCOINBASE: "COINBASE",
oTIMESTAMP: "TIMESTAMP", oTIMESTAMP: "TIMESTAMP",
oNUMBER: "NUMBER", oNUMBER: "NUMBER",
@ -244,7 +240,11 @@ func (m *Memory) Get(offset, size int64) []byte {
func (m *Memory) Print() { func (m *Memory) Print() {
fmt.Println("### MEM ###") fmt.Println("### MEM ###")
if len(m.store) > 0 { if len(m.store) > 0 {
fmt.Println(m.store) addr := 0
for i := 0; i+32 < len(m.store); i += 32 {
fmt.Printf("%03d %v\n", addr, m.store[i:i+32])
addr++
}
} else { } else {
fmt.Println("-- empty --") fmt.Println("-- empty --")
} }

View File

@ -308,7 +308,7 @@ func (sm *StateManager) ProcessContract(contract *Contract, tx *Transaction, blo
caller := sm.procState.GetAccount(tx.Sender()) caller := sm.procState.GetAccount(tx.Sender())
closure := NewClosure(caller, contract, sm.procState, tx.Gas, tx.Value) closure := NewClosure(caller, contract, sm.procState, tx.Gas, tx.Value)
vm := NewVm(sm.procState, RuntimeVars{ vm := NewVm(sm.procState, RuntimeVars{
origin: caller.Address, origin: caller.Address(),
blockNumber: block.BlockInfo().Number, blockNumber: block.BlockInfo().Number,
prevHash: block.PrevHash, prevHash: block.PrevHash,
coinbase: block.Coinbase, coinbase: block.Coinbase,

View File

@ -40,7 +40,7 @@ var Pow256 = ethutil.BigPow(2, 256)
func (vm *Vm) RunClosure(closure *Closure) []byte { func (vm *Vm) RunClosure(closure *Closure) []byte {
// If the amount of gas supplied is less equal to 0 // If the amount of gas supplied is less equal to 0
if closure.GetGas().Cmp(big.NewInt(0)) <= 0 { if closure.Gas.Cmp(big.NewInt(0)) <= 0 {
// TODO Do something // TODO Do something
} }
@ -73,7 +73,7 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
fee := new(big.Int) fee := new(big.Int)
fee.Add(fee, big.NewInt(1000)) fee.Add(fee, big.NewInt(1000))
if closure.GetGas().Cmp(fee) < 0 { if closure.Gas.Cmp(fee) < 0 {
return closure.Return(nil) return closure.Return(nil)
} }
@ -192,25 +192,37 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
// 0x30 range // 0x30 range
case oADDRESS: case oADDRESS:
stack.Push(ethutil.BigD(closure.Object().Address()))
case oBALANCE: case oBALANCE:
stack.Push(closure.Value)
case oORIGIN: case oORIGIN:
stack.Push(ethutil.BigD(vm.vars.origin))
case oCALLER: case oCALLER:
stack.Push(ethutil.BigD(closure.Callee().Address()))
case oCALLVALUE: case oCALLVALUE:
// FIXME: Original value of the call, not the current value
stack.Push(closure.Value)
case oCALLDATA: case oCALLDATA:
offset := stack.Pop() offset := stack.Pop()
mem.Set(offset.Int64(), int64(len(closure.Args)), closure.Args) mem.Set(offset.Int64(), int64(len(closure.Args)), closure.Args)
case oCALLDATASIZE: case oCALLDATASIZE:
case oRETURNDATASIZE: stack.Push(big.NewInt(int64(len(closure.Args))))
case oTXGASPRICE: case oGASPRICE:
// TODO
// 0x40 range // 0x40 range
case oPREVHASH: case oPREVHASH:
case oPREVNONCE: stack.Push(ethutil.BigD(vm.vars.prevHash))
case oCOINBASE: case oCOINBASE:
stack.Push(ethutil.BigD(vm.vars.coinbase))
case oTIMESTAMP: case oTIMESTAMP:
stack.Push(big.NewInt(vm.vars.time))
case oNUMBER: case oNUMBER:
stack.Push(big.NewInt(int64(vm.vars.blockNumber)))
case oDIFFICULTY: case oDIFFICULTY:
stack.Push(vm.vars.diff)
case oGASLIMIT: case oGASLIMIT:
// TODO
// 0x50 range // 0x50 range
case oPUSH: // Push PC+1 on to the stack case oPUSH: // Push PC+1 on to the stack
@ -218,8 +230,13 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
val := closure.GetMem(pc).BigInt() val := closure.GetMem(pc).BigInt()
stack.Push(val) stack.Push(val)
case oPOP: case oPOP:
stack.Pop()
case oDUP: case oDUP:
stack.Push(stack.Peek())
case oSWAP: case oSWAP:
x, y := stack.Popn()
stack.Push(y)
stack.Push(x)
case oMLOAD: case oMLOAD:
offset := stack.Pop() offset := stack.Pop()
stack.Push(ethutil.BigD(mem.Get(offset.Int64(), 32))) stack.Push(ethutil.BigD(mem.Get(offset.Int64(), 32)))
@ -228,7 +245,13 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
val, mStart := stack.Popn() val, mStart := stack.Popn()
mem.Set(mStart.Int64(), 32, ethutil.BigToBytes(val, 256)) mem.Set(mStart.Int64(), 32, ethutil.BigToBytes(val, 256))
case oMSTORE8: case oMSTORE8:
val, mStart := stack.Popn()
base.And(val, new(big.Int).SetInt64(0xff))
mem.Set(mStart.Int64(), 32, ethutil.BigToBytes(base, 256))
case oSLOAD: case oSLOAD:
loc := stack.Pop()
val := closure.GetMem(loc.Int64())
stack.Push(val.BigInt())
case oSSTORE: case oSSTORE:
case oJUMP: case oJUMP:
case oJUMPI: case oJUMPI:

View File

@ -126,7 +126,6 @@ func TestRun3(t *testing.T) {
"PUSH", "64", "PUSH", "64",
"PUSH", "0", "PUSH", "0",
"LOG",
"RETURN", "RETURN",
}) })
tx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), script) tx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), script)
@ -159,7 +158,7 @@ func TestRun3(t *testing.T) {
callerClosure := NewClosure(account, MakeContract(callerTx, state), state, big.NewInt(1000000000), new(big.Int)) callerClosure := NewClosure(account, MakeContract(callerTx, state), state, big.NewInt(1000000000), new(big.Int))
vm := NewVm(state, RuntimeVars{ vm := NewVm(state, RuntimeVars{
origin: account.Address, origin: account.Address(),
blockNumber: 1, blockNumber: 1,
prevHash: ethutil.FromHex("5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"), prevHash: ethutil.FromHex("5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"),
coinbase: ethutil.FromHex("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"), coinbase: ethutil.FromHex("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"),

View File

@ -33,24 +33,22 @@ var OpCodes = map[string]byte{
"SHA3": 0x20, "SHA3": 0x20,
// 0x30 range - closure state // 0x30 range - closure state
"ADDRESS": 0x30, "ADDRESS": 0x30,
"BALANCE": 0x31, "BALANCE": 0x31,
"ORIGIN": 0x32, "ORIGIN": 0x32,
"CALLER": 0x33, "CALLER": 0x33,
"CALLVALUE": 0x34, "CALLVALUE": 0x34,
"CALLDATA": 0x35, "CALLDATA": 0x35,
"CALLDATASIZE": 0x36, "CALLDATASIZE": 0x36,
"RETURNDATASIZE": 0x37, "GASPRICE": 0x38,
"TXGASPRICE": 0x38,
// 0x40 range - block operations // 0x40 range - block operations
"PREVHASH": 0x40, "PREVHASH": 0x40,
"PREVNONCE": 0x41, "COINBASE": 0x41,
"COINBASE": 0x42, "TIMESTAMP": 0x42,
"TIMESTAMP": 0x43, "NUMBER": 0x43,
"NUMBER": 0x44, "DIFFICULTY": 0x44,
"DIFFICULTY": 0x45, "GASLIMIT": 0x45,
"GASLIMIT": 0x46,
// 0x50 range - 'storage' and execution // 0x50 range - 'storage' and execution
"PUSH": 0x50, "PUSH": 0x50,