2014-08-04 07:25:53 -07:00
|
|
|
package ethpipe
|
|
|
|
|
|
|
|
import (
|
2014-09-22 05:51:21 -07:00
|
|
|
"fmt"
|
2014-08-04 07:25:53 -07:00
|
|
|
"strings"
|
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
"github.com/ethereum/go-ethereum/chain"
|
2014-10-31 04:37:43 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-10-23 06:01:27 -07:00
|
|
|
"github.com/ethereum/go-ethereum/ethlog"
|
|
|
|
"github.com/ethereum/go-ethereum/ethstate"
|
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
|
|
|
"github.com/ethereum/go-ethereum/vm"
|
2014-08-04 07:25:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger = ethlog.NewLogger("PIPE")
|
|
|
|
|
2014-08-05 02:10:24 -07:00
|
|
|
type VmVars struct {
|
|
|
|
State *ethstate.State
|
|
|
|
}
|
|
|
|
|
2014-08-04 07:25:53 -07:00
|
|
|
type Pipe struct {
|
2014-10-31 02:59:17 -07:00
|
|
|
obj chain.EthManager
|
|
|
|
stateManager *chain.StateManager
|
|
|
|
blockChain *chain.ChainManager
|
2014-08-05 02:30:12 -07:00
|
|
|
world *World
|
2014-08-05 02:10:24 -07:00
|
|
|
|
|
|
|
Vm VmVars
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
func New(obj chain.EthManager) *Pipe {
|
2014-08-04 07:25:53 -07:00
|
|
|
pipe := &Pipe{
|
|
|
|
obj: obj,
|
|
|
|
stateManager: obj.StateManager(),
|
2014-10-20 02:53:11 -07:00
|
|
|
blockChain: obj.ChainManager(),
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
|
|
|
pipe.world = NewWorld(pipe)
|
|
|
|
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Pipe) Balance(addr []byte) *ethutil.Value {
|
|
|
|
return ethutil.NewValue(self.World().safeGet(addr).Balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Pipe) Nonce(addr []byte) uint64 {
|
|
|
|
return self.World().safeGet(addr).Nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Pipe) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
|
2014-08-05 02:26:12 -07:00
|
|
|
return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price)
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:26:12 -07:00
|
|
|
func (self *Pipe) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
|
2014-08-04 07:25:53 -07:00
|
|
|
var (
|
2014-09-22 05:51:21 -07:00
|
|
|
initiator = ethstate.NewStateObject(self.obj.KeyManager().KeyPair().Address())
|
|
|
|
block = self.blockChain.CurrentBlock
|
2014-08-04 07:25:53 -07:00
|
|
|
)
|
2014-09-22 05:51:21 -07:00
|
|
|
|
|
|
|
self.Vm.State = self.World().State().Copy()
|
2014-08-04 07:25:53 -07:00
|
|
|
|
2014-10-18 04:31:20 -07:00
|
|
|
evm := vm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()), vm.Type(ethutil.Config.VmType))
|
2014-08-04 07:25:53 -07:00
|
|
|
|
2014-10-18 04:31:20 -07:00
|
|
|
msg := vm.NewExecution(evm, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt())
|
2014-09-22 05:51:21 -07:00
|
|
|
ret, err := msg.Exec(object.Address(), initiator)
|
|
|
|
|
|
|
|
fmt.Println("returned from call", ret, err)
|
2014-08-04 07:25:53 -07:00
|
|
|
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
func (self *Pipe) Block(hash []byte) *chain.Block {
|
2014-08-04 07:25:53 -07:00
|
|
|
return self.blockChain.GetBlock(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Pipe) Storage(addr, storageAddr []byte) *ethutil.Value {
|
|
|
|
return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Pipe) ToAddress(priv []byte) []byte {
|
2014-10-31 04:37:43 -07:00
|
|
|
pair, err := crypto.NewKeyPairFromSec(priv)
|
2014-08-04 07:25:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return pair.Address()
|
|
|
|
}
|
|
|
|
|
2014-08-04 07:34:55 -07:00
|
|
|
func (self *Pipe) Exists(addr []byte) bool {
|
|
|
|
return self.World().Get(addr) != nil
|
|
|
|
}
|
|
|
|
|
2014-10-31 04:37:43 -07:00
|
|
|
func (self *Pipe) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) ([]byte, error) {
|
2014-08-04 07:25:53 -07:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2014-10-31 04:37:43 -07:00
|
|
|
func (self *Pipe) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) ([]byte, error) {
|
2014-08-04 07:25:53 -07:00
|
|
|
var hash []byte
|
|
|
|
var contractCreation bool
|
|
|
|
if rec == nil {
|
|
|
|
contractCreation = true
|
|
|
|
}
|
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
var tx *chain.Transaction
|
2014-08-04 07:25:53 -07:00
|
|
|
// Compile and assemble the given data
|
|
|
|
if contractCreation {
|
|
|
|
script, err := ethutil.Compile(string(data), false)
|
|
|
|
if err != nil {
|
2014-08-05 02:10:24 -07:00
|
|
|
return nil, err
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script)
|
2014-08-04 07:25:53 -07:00
|
|
|
} else {
|
|
|
|
data := ethutil.StringToByteFunc(string(data), func(s string) (ret []byte) {
|
|
|
|
slice := strings.Split(s, "\n")
|
|
|
|
for _, dataItem := range slice {
|
|
|
|
d := ethutil.FormatData(dataItem)
|
|
|
|
ret = append(ret, d...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data)
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
acc := self.stateManager.TransState().GetOrNewStateObject(key.Address())
|
|
|
|
tx.Nonce = acc.Nonce
|
|
|
|
acc.Nonce += 1
|
|
|
|
self.stateManager.TransState().UpdateStateObject(acc)
|
|
|
|
|
|
|
|
tx.Sign(key.PrivateKey)
|
|
|
|
self.obj.TxPool().QueueTransaction(tx)
|
|
|
|
|
|
|
|
if contractCreation {
|
2014-10-02 08:03:15 -07:00
|
|
|
addr := tx.CreationAddress(self.World().State())
|
|
|
|
logger.Infof("Contract addr %x\n", addr)
|
2014-08-05 02:10:24 -07:00
|
|
|
|
2014-10-02 08:03:15 -07:00
|
|
|
return addr, nil
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:10:24 -07:00
|
|
|
return tx.Hash(), nil
|
2014-08-04 07:25:53 -07:00
|
|
|
}
|
2014-08-17 03:42:02 -07:00
|
|
|
|
2014-10-31 02:59:17 -07:00
|
|
|
func (self *Pipe) PushTx(tx *chain.Transaction) ([]byte, error) {
|
2014-09-22 05:51:21 -07:00
|
|
|
self.obj.TxPool().QueueTransaction(tx)
|
|
|
|
if tx.Recipient == nil {
|
2014-10-02 08:03:15 -07:00
|
|
|
addr := tx.CreationAddress(self.World().State())
|
|
|
|
logger.Infof("Contract addr %x\n", addr)
|
|
|
|
return addr, nil
|
2014-09-22 05:51:21 -07:00
|
|
|
}
|
|
|
|
return tx.Hash(), nil
|
2014-09-06 13:51:13 -07:00
|
|
|
}
|
|
|
|
|
2014-08-17 03:42:02 -07:00
|
|
|
func (self *Pipe) CompileMutan(code string) ([]byte, error) {
|
|
|
|
data, err := ethutil.Compile(code, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|