From 7705b23f248156878d00c301fbbadafedaf7e3d2 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 20 Mar 2014 23:17:53 +0100 Subject: [PATCH] Removed caller from tx and added "callership" to account. Transactions can no longer serve as callers. Accounts are now the initial callee of closures. Transactions now serve as transport to call closures. --- ethchain/address.go | 24 ++++++++++++++++-------- ethchain/block.go | 2 +- ethchain/closure.go | 4 ++-- ethchain/state.go | 4 ++-- ethchain/transaction.go | 9 --------- ethchain/vm.go | 4 ++++ ethchain/vm_test.go | 13 ++++++------- 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/ethchain/address.go b/ethchain/address.go index f1f27a1a5..9c6acbe08 100644 --- a/ethchain/address.go +++ b/ethchain/address.go @@ -6,19 +6,20 @@ import ( ) type Account struct { - Amount *big.Int - Nonce uint64 + Address []byte + Amount *big.Int + Nonce uint64 } -func NewAccount(amount *big.Int) *Account { - return &Account{Amount: amount, Nonce: 0} +func NewAccount(address []byte, amount *big.Int) *Account { + return &Account{address, amount, 0} } -func NewAccountFromData(data []byte) *Account { - address := &Account{} - address.RlpDecode(data) +func NewAccountFromData(address, data []byte) *Account { + account := &Account{Address: address} + account.RlpDecode(data) - return address + return account } func (a *Account) AddFee(fee *big.Int) { @@ -29,6 +30,13 @@ func (a *Account) AddFunds(funds *big.Int) { a.Amount.Add(a.Amount, funds) } +// Implements Callee +func (a *Account) ReturnGas(value *big.Int, state *State) { + // Return the value back to the sender + a.AddFunds(value) + state.UpdateAccount(a.Address, a) +} + func (a *Account) RlpEncode() []byte { return ethutil.Encode([]interface{}{a.Amount, a.Nonce}) } diff --git a/ethchain/block.go b/ethchain/block.go index 20af73ba2..1f63c2c9e 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -142,7 +142,7 @@ func (block *Block) PayFee(addr []byte, fee *big.Int) bool { data := block.state.trie.Get(string(block.Coinbase)) // Get the ether (Coinbase) and add the fee (gief fee to miner) - ether := NewAccountFromData([]byte(data)) + ether := NewAccountFromData(block.Coinbase, []byte(data)) base = new(big.Int) ether.Amount = base.Add(ether.Amount, fee) diff --git a/ethchain/closure.go b/ethchain/closure.go index 204fbce06..9453ce22c 100644 --- a/ethchain/closure.go +++ b/ethchain/closure.go @@ -26,7 +26,7 @@ type Closure struct { gas *big.Int val *big.Int - args []byte + Args []byte } // Create a new closure for the given data items @@ -45,7 +45,7 @@ func (c *Closure) GetMem(x int64) *ethutil.Value { } func (c *Closure) Call(vm *Vm, args []byte) []byte { - c.args = args + c.Args = args return vm.RunClosure(c) } diff --git a/ethchain/state.go b/ethchain/state.go index b84d60c6c..b6750d62d 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -86,9 +86,9 @@ func (s *State) UpdateContract(addr []byte, contract *Contract) { func (s *State) GetAccount(addr []byte) (account *Account) { data := s.trie.Get(string(addr)) if data == "" { - account = NewAccount(big.NewInt(0)) + account = NewAccount(addr, big.NewInt(0)) } else { - account = NewAccountFromData([]byte(data)) + account = NewAccountFromData(addr, []byte(data)) } return diff --git a/ethchain/transaction.go b/ethchain/transaction.go index 07e7ea970..57df9cdc4 100644 --- a/ethchain/transaction.go +++ b/ethchain/transaction.go @@ -29,15 +29,6 @@ func NewTransaction(to []byte, value *big.Int, data []string) *Transaction { return &tx } -// Implements Callee -func (tx *Transaction) ReturnGas(value *big.Int, state *State) { - // Return the value back to the sender - sender := tx.Sender() - account := state.GetAccount(sender) - account.AddFunds(value) - state.UpdateAccount(sender, account) -} - // XXX Deprecated func NewTransactionFromData(data []byte) *Transaction { return NewTransactionFromBytes(data) diff --git a/ethchain/vm.go b/ethchain/vm.go index 6479409f8..3d85e2c09 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -87,6 +87,10 @@ func (vm *Vm) RunClosure(closure *Closure) []byte { // Pop value of the stack val, mStart := stack.Popn() mem.Set(mStart.Int64(), 32, ethutil.BigToBytes(val, 256)) + + case oCALLDATA: + offset := stack.Pop() + mem.Set(offset.Int64(), int64(len(closure.Args)), closure.Args) case oCALL: // Pop return size and offset retSize, retOffset := stack.Popn() diff --git a/ethchain/vm_test.go b/ethchain/vm_test.go index 654ddb566..30c8a110e 100644 --- a/ethchain/vm_test.go +++ b/ethchain/vm_test.go @@ -133,10 +133,10 @@ func TestRun3(t *testing.T) { state.UpdateContract(addr, contract) callerScript := Compile([]string{ - "PUSH", "62", // REND - "PUSH", "0", // RSTART - "PUSH", "22", // MEND - "PUSH", "15", // MSTART + "PUSH", "62", // ret size + "PUSH", "0", // ret offset + "PUSH", "32", // arg size + "PUSH", "63", // arg offset "PUSH", "1000", /// Gas "PUSH", "0", /// value "PUSH", string(addr), // Sender @@ -144,10 +144,9 @@ func TestRun3(t *testing.T) { }) callerTx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), callerScript) callerAddr := callerTx.Hash()[12:] - executer := NewTransaction(ContractAddr, ethutil.Big("10000"), nil) - executer.Sign([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) - callerClosure := NewClosure(executer, MakeContract(callerTx, state), state, big.NewInt(1000000000), new(big.Int)) + account := NewAccount(ContractAddr, big.NewInt(10000000)) + callerClosure := NewClosure(account, MakeContract(callerTx, state), state, big.NewInt(1000000000), new(big.Int)) vm := NewVm(state, RuntimeVars{ address: callerAddr,