aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/closure.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-03-21 00:26:07 +0800
committerobscuren <geffobscura@gmail.com>2014-03-21 00:26:07 +0800
commit38ea6a6d5dffb7573b29541c2a6687145bdcc495 (patch)
treef3365ae19bd2c02b0e09c7bbd2201ec370d28bf6 /ethchain/closure.go
parent82d0f65dab253e215349ea685382bca9672378d8 (diff)
downloadgo-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.tar
go-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.tar.gz
go-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.tar.bz2
go-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.tar.lz
go-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.tar.xz
go-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.tar.zst
go-tangerine-38ea6a6d5dffb7573b29541c2a6687145bdcc495.zip
Closures and vm based on closures
Status: Work in progress
Diffstat (limited to 'ethchain/closure.go')
-rw-r--r--ethchain/closure.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/ethchain/closure.go b/ethchain/closure.go
new file mode 100644
index 000000000..4ef43a2da
--- /dev/null
+++ b/ethchain/closure.go
@@ -0,0 +1,68 @@
+package ethchain
+
+// TODO Re write VM to use values instead of big integers?
+
+import (
+ "github.com/ethereum/eth-go/ethutil"
+ "math/big"
+)
+
+type Callee interface {
+ ReturnGas(*big.Int, *State)
+}
+
+type ClosureBody interface {
+ Callee
+ ethutil.RlpEncodable
+ GetMem(int64) *ethutil.Value
+}
+
+// Basic inline closure object which implement the 'closure' interface
+type Closure struct {
+ callee Callee
+ object ClosureBody
+ state *State
+
+ gas *big.Int
+ val *big.Int
+}
+
+// Create a new closure for the given data items
+func NewClosure(callee Callee, object ClosureBody, state *State, gas, val *big.Int) *Closure {
+ return &Closure{callee, object, state, gas, val}
+}
+
+// Retuns the x element in data slice
+func (c *Closure) GetMem(x int64) *ethutil.Value {
+ m := c.object.GetMem(x)
+ if m == nil {
+ return ethutil.EmptyValue()
+ }
+
+ return m
+}
+
+func (c *Closure) Return(ret []byte) []byte {
+ // Return the remaining gas to the callee
+ // If no callee is present return it to
+ // the origin (i.e. contract or tx)
+ if c.callee != nil {
+ c.callee.ReturnGas(c.gas, c.state)
+ } else {
+ c.object.ReturnGas(c.gas, c.state)
+ // TODO incase it's a POST contract we gotta serialise the contract again.
+ // But it's not yet defined
+ }
+
+ return ret
+}
+
+// Implement the Callee interface
+func (c *Closure) ReturnGas(gas *big.Int, state *State) {
+ // Return the gas to the closure
+ c.gas.Add(c.gas, gas)
+}
+
+func (c *Closure) GetGas() *big.Int {
+ return c.gas
+}