aboutsummaryrefslogtreecommitdiffstats
path: root/xeth/pipe.go
blob: c96c6efc0ad7a22a6510fae047b510996de2b749 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package xeth

/*
 * eXtended ETHereum
 */

import (
    "fmt"

    "github.com/ethereum/go-ethereum/chain"
    "github.com/ethereum/go-ethereum/chain/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethutil"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/state"
    "github.com/ethereum/go-ethereum/vm"
)

var pipelogger = logger.NewLogger("XETH")

type VmVars struct {
    State *state.State
}

type XEth struct {
    obj          chain.EthManager
    blockManager *chain.BlockManager
    blockChain   *chain.ChainManager
    world        *World

    Vm VmVars
}

func New(obj chain.EthManager) *XEth {
    pipe := &XEth{
        obj:          obj,
        blockManager: obj.BlockManager(),
        blockChain:   obj.ChainManager(),
    }
    pipe.world = NewWorld(pipe)

    return pipe
}

func (self *XEth) Balance(addr []byte) *ethutil.Value {
    return ethutil.NewValue(self.World().safeGet(addr).Balance)
}

func (self *XEth) Nonce(addr []byte) uint64 {
    return self.World().safeGet(addr).Nonce
}

func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
    return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price)
}

func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
    var (
        initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address())
        block     = self.blockChain.CurrentBlock
    )

    self.Vm.State = self.World().State().Copy()

    evm := vm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()), vm.Type(ethutil.Config.VmType))

    msg := vm.NewExecution(evm, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt())
    ret, err := msg.Exec(object.Address(), initiator)

    fmt.Println("returned from call", ret, err)

    return ret, err
}

func (self *XEth) Block(hash []byte) *types.Block {
    return self.blockChain.GetBlock(hash)
}

func (self *XEth) Storage(addr, storageAddr []byte) *ethutil.Value {
    return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr))
}

func (self *XEth) ToAddress(priv []byte) []byte {
    pair, err := crypto.NewKeyPairFromSec(priv)
    if err != nil {
        return nil
    }

    return pair.Address()
}

func (self *XEth) Exists(addr []byte) bool {
    return self.World().Get(addr) != nil
}

func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) {
    // Check if an address is stored by this address
    var hash []byte
    addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes()
    if len(addr) > 0 {
        hash = addr
    } else if ethutil.IsHex(rec) {
        hash = ethutil.Hex2Bytes(rec[2:])
    } else {
        hash = ethutil.Hex2Bytes(rec)
    }

    return self.Transact(key, hash, value, gas, price, data)
}

func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) {
    var hash []byte
    var contractCreation bool
    if types.IsContractAddr(to) {
        contractCreation = true
    } else {
        // Check if an address is stored by this address
        addr := self.World().Config().Get("NameReg").Storage(to).Bytes()
        if len(addr) > 0 {
            hash = addr
        } else {
            hash = to
        }
    }

    var tx *types.Transaction
    if contractCreation {
        tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data)
    } else {
        tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data)
    }

    state := self.blockManager.TransState()
    nonce := state.GetNonce(key.Address())

    tx.Nonce = nonce
    tx.Sign(key.PrivateKey)
    err := self.obj.TxPool().Add(tx)
    if err != nil {
        return nil, err
    }

    state.SetNonce(key.Address(), nonce+1)

    if contractCreation {
        addr := tx.CreationAddress(self.World().State())
        pipelogger.Infof("Contract addr %x\n", addr)
    }

    return tx, nil

    //acc := self.blockManager.TransState().GetOrNewStateObject(key.Address())
    //self.obj.TxPool().QueueTransaction(tx)

    //acc.Nonce += 1
    //self.blockManager.TransState().UpdateStateObject(acc)

}

func (self *XEth) PushTx(tx *types.Transaction) ([]byte, error) {
    err := self.obj.TxPool().Add(tx)
    if err != nil {
        return nil, err
    }

    if tx.Recipient == nil {
        addr := tx.CreationAddress(self.World().State())
        pipelogger.Infof("Contract addr %x\n", addr)
        return addr, nil
    }
    return tx.Hash(), nil
}

func (self *XEth) CompileMutan(code string) ([]byte, error) {
    data, err := ethutil.Compile(code, false)
    if err != nil {
        return nil, err
    }

    return data, nil
}