plugeth/ethpipe/pipe.go

160 lines
4.0 KiB
Go
Raw Normal View History

2014-08-04 14:25:53 +00:00
package ethpipe
import (
"strings"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethvm"
)
var logger = ethlog.NewLogger("PIPE")
2014-08-05 09:10:24 +00:00
type VmVars struct {
State *ethstate.State
}
2014-08-04 14:25:53 +00:00
type Pipe struct {
obj ethchain.EthManager
stateManager *ethchain.StateManager
blockChain *ethchain.BlockChain
2014-08-05 09:30:12 +00:00
world *World
2014-08-05 09:10:24 +00:00
Vm VmVars
2014-08-04 14:25:53 +00:00
}
func New(obj ethchain.EthManager) *Pipe {
pipe := &Pipe{
obj: obj,
stateManager: obj.StateManager(),
blockChain: obj.BlockChain(),
}
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 09:26:12 +00:00
return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price)
2014-08-04 14:25:53 +00:00
}
2014-08-05 09:26:12 +00:00
func (self *Pipe) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
2014-08-04 14:25:53 +00:00
var (
2014-08-05 09:10:24 +00:00
initiator = ethstate.NewStateObject([]byte{0})
block = self.blockChain.CurrentBlock
stateObject = object.StateObject
2014-08-04 14:25:53 +00:00
)
2014-08-05 09:10:24 +00:00
if self.Vm.State == nil {
self.Vm.State = self.World().State().Copy()
}
2014-08-04 14:25:53 +00:00
2014-08-05 09:10:24 +00:00
vm := ethvm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()))
2014-08-04 14:25:53 +00:00
closure := ethvm.NewClosure(&ethstate.Message{}, initiator, stateObject, object.Code, gas.BigInt(), price.BigInt())
2014-08-04 14:25:53 +00:00
ret, _, err := closure.Call(vm, data)
return ret, err
}
func (self *Pipe) Block(hash []byte) *ethchain.Block {
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 {
pair, err := ethcrypto.NewKeyPairFromSec(priv)
if err != nil {
return nil
}
return pair.Address()
}
2014-08-04 14:34:55 +00:00
func (self *Pipe) Exists(addr []byte) bool {
return self.World().Get(addr) != nil
}
2014-08-05 09:10:24 +00:00
func (self *Pipe) TransactString(key *ethcrypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) ([]byte, error) {
2014-08-04 14:25:53 +00: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-08-05 09:10:24 +00:00
func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) ([]byte, error) {
2014-08-04 14:25:53 +00:00
var hash []byte
var contractCreation bool
if rec == nil {
contractCreation = true
}
var tx *ethchain.Transaction
// Compile and assemble the given data
if contractCreation {
script, err := ethutil.Compile(string(data), false)
if err != nil {
2014-08-05 09:10:24 +00:00
return nil, err
2014-08-04 14:25:53 +00:00
}
tx = ethchain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script)
} 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
})
tx = ethchain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data)
}
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 {
logger.Infof("Contract addr %x", tx.CreationAddress())
2014-08-05 09:10:24 +00:00
return tx.CreationAddress(), nil
2014-08-04 14:25:53 +00:00
}
2014-08-05 09:10:24 +00:00
return tx.Hash(), nil
2014-08-04 14:25:53 +00:00
}
2014-08-17 10:42:02 +00:00
func (self *Pipe) CompileMutan(code string) ([]byte, error) {
data, err := ethutil.Compile(code, false)
if err != nil {
return nil, err
}
return data, nil
}