bsc/xeth/pipe.go

176 lines
4.3 KiB
Go
Raw Normal View History

2014-10-31 15:30:08 +02:00
package xeth
/*
* eXtended ETHereum
*/
2014-08-04 17:25:53 +03:00
import (
"fmt"
2014-08-04 17:25:53 +03:00
"strings"
2014-10-31 11:59:17 +02:00
"github.com/ethereum/go-ethereum/chain"
2014-10-31 13:37:43 +02:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
2014-10-31 13:56:05 +02:00
"github.com/ethereum/go-ethereum/logger"
2014-10-31 15:43:14 +02:00
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/vm"
2014-08-04 17:25:53 +03:00
)
2014-10-31 15:30:08 +02:00
var pipelogger = logger.NewLogger("XETH")
2014-08-04 17:25:53 +03:00
2014-08-05 12:10:24 +03:00
type VmVars struct {
2014-10-31 15:43:14 +02:00
State *state.State
2014-08-05 12:10:24 +03:00
}
2014-10-31 15:30:08 +02:00
type XEth struct {
2014-10-31 11:59:17 +02:00
obj chain.EthManager
2014-11-04 11:57:02 +02:00
blockManager *chain.BlockManager
2014-10-31 11:59:17 +02:00
blockChain *chain.ChainManager
2014-08-05 12:30:12 +03:00
world *World
2014-08-05 12:10:24 +03:00
Vm VmVars
2014-08-04 17:25:53 +03:00
}
2014-10-31 15:30:08 +02:00
func New(obj chain.EthManager) *XEth {
pipe := &XEth{
2014-08-04 17:25:53 +03:00
obj: obj,
2014-11-04 11:57:02 +02:00
blockManager: obj.BlockManager(),
2014-10-20 12:53:11 +03:00
blockChain: obj.ChainManager(),
2014-08-04 17:25:53 +03:00
}
pipe.world = NewWorld(pipe)
return pipe
}
2014-10-31 15:30:08 +02:00
func (self *XEth) Balance(addr []byte) *ethutil.Value {
2014-08-04 17:25:53 +03:00
return ethutil.NewValue(self.World().safeGet(addr).Balance)
}
2014-10-31 15:30:08 +02:00
func (self *XEth) Nonce(addr []byte) uint64 {
2014-08-04 17:25:53 +03:00
return self.World().safeGet(addr).Nonce
}
2014-10-31 15:30:08 +02:00
func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
2014-08-05 12:26:12 +03:00
return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price)
2014-08-04 17:25:53 +03:00
}
2014-10-31 15:30:08 +02:00
func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) {
2014-08-04 17:25:53 +03:00
var (
2014-10-31 15:43:14 +02:00
initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address())
block = self.blockChain.CurrentBlock
2014-08-04 17:25:53 +03:00
)
self.Vm.State = self.World().State().Copy()
2014-08-04 17:25:53 +03:00
2014-10-18 14:31:20 +03:00
evm := vm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()), vm.Type(ethutil.Config.VmType))
2014-08-04 17:25:53 +03:00
2014-10-18 14:31:20 +03:00
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)
2014-08-04 17:25:53 +03:00
return ret, err
}
2014-10-31 15:30:08 +02:00
func (self *XEth) Block(hash []byte) *chain.Block {
2014-08-04 17:25:53 +03:00
return self.blockChain.GetBlock(hash)
}
2014-10-31 15:30:08 +02:00
func (self *XEth) Storage(addr, storageAddr []byte) *ethutil.Value {
2014-08-04 17:25:53 +03:00
return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr))
}
2014-10-31 15:30:08 +02:00
func (self *XEth) ToAddress(priv []byte) []byte {
2014-10-31 13:37:43 +02:00
pair, err := crypto.NewKeyPairFromSec(priv)
2014-08-04 17:25:53 +03:00
if err != nil {
return nil
}
return pair.Address()
}
2014-10-31 15:30:08 +02:00
func (self *XEth) Exists(addr []byte) bool {
2014-08-04 17:34:55 +03:00
return self.World().Get(addr) != nil
}
2014-10-31 15:30:08 +02:00
func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) ([]byte, error) {
2014-08-04 17:25:53 +03: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 15:30:08 +02:00
func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) ([]byte, error) {
2014-08-04 17:25:53 +03:00
var hash []byte
var contractCreation bool
if rec == nil {
contractCreation = true
}
2014-10-31 11:59:17 +02:00
var tx *chain.Transaction
2014-08-04 17:25:53 +03:00
// Compile and assemble the given data
if contractCreation {
script, err := ethutil.Compile(string(data), false)
if err != nil {
2014-08-05 12:10:24 +03:00
return nil, err
2014-08-04 17:25:53 +03:00
}
2014-10-31 11:59:17 +02:00
tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script)
2014-08-04 17:25:53 +03: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 11:59:17 +02:00
tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data)
2014-08-04 17:25:53 +03:00
}
2014-11-04 11:57:02 +02:00
acc := self.blockManager.TransState().GetOrNewStateObject(key.Address())
2014-08-04 17:25:53 +03:00
tx.Nonce = acc.Nonce
acc.Nonce += 1
2014-11-04 11:57:02 +02:00
self.blockManager.TransState().UpdateStateObject(acc)
2014-08-04 17:25:53 +03:00
tx.Sign(key.PrivateKey)
self.obj.TxPool().QueueTransaction(tx)
if contractCreation {
2014-10-02 18:03:15 +03:00
addr := tx.CreationAddress(self.World().State())
2014-10-31 13:56:05 +02:00
pipelogger.Infof("Contract addr %x\n", addr)
2014-08-05 12:10:24 +03:00
2014-10-02 18:03:15 +03:00
return addr, nil
2014-08-04 17:25:53 +03:00
}
2014-08-05 12:10:24 +03:00
return tx.Hash(), nil
2014-08-04 17:25:53 +03:00
}
2014-08-17 13:42:02 +03:00
2014-10-31 15:30:08 +02:00
func (self *XEth) PushTx(tx *chain.Transaction) ([]byte, error) {
self.obj.TxPool().QueueTransaction(tx)
if tx.Recipient == nil {
2014-10-02 18:03:15 +03:00
addr := tx.CreationAddress(self.World().State())
2014-10-31 13:56:05 +02:00
pipelogger.Infof("Contract addr %x\n", addr)
2014-10-02 18:03:15 +03:00
return addr, nil
}
return tx.Hash(), nil
}
2014-10-31 15:30:08 +02:00
func (self *XEth) CompileMutan(code string) ([]byte, error) {
2014-08-17 13:42:02 +03:00
data, err := ethutil.Compile(code, false)
if err != nil {
return nil, err
}
return data, nil
}