xeth.go 10.5 KB
Newer Older
1
// eXtended ETHereum
O
obscuren 已提交
2 3
package xeth

O
obscuren 已提交
4 5 6
import (
	"bytes"
	"encoding/json"
7
	"fmt"
8
	"math/big"
O
obscuren 已提交
9

10
	"github.com/ethereum/go-ethereum/accounts"
O
obscuren 已提交
11 12 13 14
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/ethutil"
15
	"github.com/ethereum/go-ethereum/event"
O
obscuren 已提交
16
	"github.com/ethereum/go-ethereum/logger"
17
	"github.com/ethereum/go-ethereum/p2p"
O
wip  
obscuren 已提交
18
	"github.com/ethereum/go-ethereum/state"
19
	"github.com/ethereum/go-ethereum/whisper"
O
obscuren 已提交
20
)
O
obscuren 已提交
21

O
obscuren 已提交
22
var pipelogger = logger.NewLogger("XETH")
O
obscuren 已提交
23 24 25 26 27

// to resolve the import cycle
type Backend interface {
	BlockProcessor() *core.BlockProcessor
	ChainManager() *core.ChainManager
28
	AccountManager() *accounts.Manager
29 30
	TxPool() *core.TxPool
	PeerCount() int
O
obscuren 已提交
31
	IsListening() bool
32
	Peers() []*p2p.Peer
33 34
	BlockDb() ethutil.Database
	StateDb() ethutil.Database
O
obscuren 已提交
35
	ExtraDb() ethutil.Database
36
	EventMux() *event.TypeMux
37
	Whisper() *whisper.Whisper
38 39 40 41

	IsMining() bool
	StartMining() error
	StopMining()
O
obscuren 已提交
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// Frontend should be implemented by users of XEth. Its methods are
// called whenever XEth makes a decision that requires user input.
type Frontend interface {
	// UnlockAccount is called when a transaction needs to be signed
	// but the key corresponding to the transaction's sender is
	// locked.
	//
	// It should unlock the account with the given address and return
	// true if unlocking succeeded.
	UnlockAccount(address []byte) bool

	// This is called for all transactions inititated through
	// Transact. It should prompt the user to confirm the transaction
	// and return true if the transaction was acknowledged.
	//
	// ConfirmTransaction is not used for Call transactions
	// because they cannot change any state.
	ConfirmTransaction(tx *types.Transaction) bool
}

O
obscuren 已提交
64
type XEth struct {
O
obscuren 已提交
65 66 67
	eth            Backend
	blockProcessor *core.BlockProcessor
	chainManager   *core.ChainManager
68
	accountManager *accounts.Manager
69
	state          *State
70
	whisper        *Whisper
O
obscuren 已提交
71

72
	frontend Frontend
O
obscuren 已提交
73 74
}

75 76 77
// dummyFrontend is a non-interactive frontend that allows all
// transactions but cannot not unlock any keys.
type dummyFrontend struct{}
O
obscuren 已提交
78

79 80
func (dummyFrontend) UnlockAccount([]byte) bool                  { return false }
func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true }
O
obscuren 已提交
81

82 83 84 85
// New creates an XEth that uses the given frontend.
// If a nil Frontend is provided, a default frontend which
// confirms all transactions will be used.
func New(eth Backend, frontend Frontend) *XEth {
O
obscuren 已提交
86
	xeth := &XEth{
O
obscuren 已提交
87 88 89
		eth:            eth,
		blockProcessor: eth.BlockProcessor(),
		chainManager:   eth.ChainManager(),
90
		accountManager: eth.AccountManager(),
91
		whisper:        NewWhisper(eth.Whisper()),
92
		frontend:       frontend,
O
obscuren 已提交
93
	}
O
obscuren 已提交
94
	if frontend == nil {
95
		xeth.frontend = dummyFrontend{}
O
obscuren 已提交
96
	}
O
wip  
obscuren 已提交
97
	xeth.state = NewState(xeth, xeth.chainManager.TransState())
O
obscuren 已提交
98 99 100
	return xeth
}

O
wip  
obscuren 已提交
101
func (self *XEth) Backend() Backend { return self.eth }
O
obscuren 已提交
102
func (self *XEth) WithState(statedb *state.StateDB) *XEth {
O
wip  
obscuren 已提交
103 104 105 106 107 108 109 110 111 112 113 114
	xeth := &XEth{
		eth:            self.eth,
		blockProcessor: self.blockProcessor,
		chainManager:   self.chainManager,
		whisper:        self.whisper,
	}

	xeth.state = NewState(xeth, statedb)
	return xeth
}
func (self *XEth) State() *State { return self.state }

115
func (self *XEth) Whisper() *Whisper { return self.whisper }
O
obscuren 已提交
116

O
obscuren 已提交
117
func (self *XEth) BlockByHash(strHash string) *Block {
O
obscuren 已提交
118 119 120
	hash := fromHex(strHash)
	block := self.chainManager.GetBlock(hash)

O
obscuren 已提交
121
	return NewBlock(block)
O
obscuren 已提交
122 123
}

T
Taylor Gerring 已提交
124 125 126 127 128 129 130
func (self *XEth) EthBlockByHash(strHash string) *types.Block {
	hash := fromHex(strHash)
	block := self.chainManager.GetBlock(hash)

	return block
}

O
obscuren 已提交
131 132 133 134 135 136 137 138
func (self *XEth) EthTransactionByHash(hash string) *types.Transaction {
	data, _ := self.eth.ExtraDb().Get(fromHex(hash))
	if len(data) != 0 {
		return types.NewTransactionFromBytes(data)
	}
	return nil
}

T
Taylor Gerring 已提交
139 140 141 142 143 144
func (self *XEth) BlockByNumber(num int64) *Block {
	if num == -1 {
		return NewBlock(self.chainManager.CurrentBlock())
	}

	return NewBlock(self.chainManager.GetBlockByNumber(uint64(num)))
O
obscuren 已提交
145 146
}

T
Taylor Gerring 已提交
147 148 149 150 151 152 153 154
func (self *XEth) EthBlockByNumber(num int64) *types.Block {
	if num == -1 {
		return self.chainManager.CurrentBlock()
	}

	return self.chainManager.GetBlockByNumber(uint64(num))
}

O
obscuren 已提交
155
func (self *XEth) Block(v interface{}) *Block {
O
obscuren 已提交
156
	if n, ok := v.(int32); ok {
T
Taylor Gerring 已提交
157
		return self.BlockByNumber(int64(n))
O
obscuren 已提交
158 159 160
	} else if str, ok := v.(string); ok {
		return self.BlockByHash(str)
	} else if f, ok := v.(float64); ok { // Don't ask ...
T
Taylor Gerring 已提交
161
		return self.BlockByNumber(int64(f))
O
obscuren 已提交
162 163 164 165 166
	}

	return nil
}

O
obscuren 已提交
167
func (self *XEth) Accounts() []string {
168 169 170 171 172 173 174
	// TODO: check err?
	accounts, _ := self.eth.AccountManager().Accounts()
	accountAddresses := make([]string, len(accounts))
	for i, ac := range accounts {
		accountAddresses[i] = toHex(ac.Address)
	}
	return accountAddresses
O
obscuren 已提交
175 176
}

O
obscuren 已提交
177
func (self *XEth) PeerCount() int {
O
obscuren 已提交
178 179 180
	return self.eth.PeerCount()
}

O
obscuren 已提交
181
func (self *XEth) IsMining() bool {
182
	return self.eth.IsMining()
O
obscuren 已提交
183 184
}

T
Taylor Gerring 已提交
185
func (self *XEth) SetMining(shouldmine bool) bool {
186
	ismining := self.eth.IsMining()
T
Taylor Gerring 已提交
187
	if shouldmine && !ismining {
188 189
		err := self.eth.StartMining()
		return err == nil
T
Taylor Gerring 已提交
190 191
	}
	if ismining && !shouldmine {
192
		self.eth.StopMining()
T
Taylor Gerring 已提交
193
	}
194
	return self.eth.IsMining()
T
Taylor Gerring 已提交
195 196
}

O
obscuren 已提交
197
func (self *XEth) IsListening() bool {
O
obscuren 已提交
198 199 200
	return self.eth.IsListening()
}

O
obscuren 已提交
201
func (self *XEth) Coinbase() string {
202 203
	cb, _ := self.eth.AccountManager().Coinbase()
	return toHex(cb)
O
obscuren 已提交
204 205
}

O
obscuren 已提交
206
func (self *XEth) NumberToHuman(balance string) string {
O
obscuren 已提交
207 208 209 210 211
	b := ethutil.Big(balance)

	return ethutil.CurrencyToString(b)
}

O
obscuren 已提交
212
func (self *XEth) StorageAt(addr, storageAddr string) string {
O
obscuren 已提交
213 214 215 216 217
	storage := self.State().SafeGet(addr).StorageString(storageAddr)

	return toHex(storage.Bytes())
}

O
obscuren 已提交
218
func (self *XEth) BalanceAt(addr string) string {
O
obscuren 已提交
219 220 221
	return self.State().SafeGet(addr).Balance().String()
}

O
obscuren 已提交
222
func (self *XEth) TxCountAt(address string) int {
223
	return int(self.State().SafeGet(address).Nonce())
O
obscuren 已提交
224 225
}

O
obscuren 已提交
226
func (self *XEth) CodeAt(address string) string {
227
	return toHex(self.State().SafeGet(address).Code())
O
obscuren 已提交
228 229
}

O
obscuren 已提交
230
func (self *XEth) IsContract(address string) bool {
231
	return len(self.State().SafeGet(address).Code()) > 0
O
obscuren 已提交
232 233
}

O
obscuren 已提交
234
func (self *XEth) SecretToAddress(key string) string {
O
obscuren 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247
	pair, err := crypto.NewKeyPairFromSec(fromHex(key))
	if err != nil {
		return ""
	}

	return toHex(pair.Address())
}

type KeyVal struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

O
obscuren 已提交
248
func (self *XEth) EachStorage(addr string) string {
O
obscuren 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	var values []KeyVal
	object := self.State().SafeGet(addr)
	it := object.Trie().Iterator()
	for it.Next() {
		values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)})
	}

	valuesJson, err := json.Marshal(values)
	if err != nil {
		return ""
	}

	return string(valuesJson)
}

O
obscuren 已提交
264
func (self *XEth) ToAscii(str string) string {
O
obscuren 已提交
265 266 267 268 269
	padded := ethutil.RightPadBytes([]byte(str), 32)

	return "0x" + toHex(padded)
}

O
obscuren 已提交
270
func (self *XEth) FromAscii(str string) string {
O
obscuren 已提交
271 272 273 274 275 276 277
	if ethutil.IsHex(str) {
		str = str[2:]
	}

	return string(bytes.Trim(fromHex(str), "\x00"))
}

O
obscuren 已提交
278
func (self *XEth) FromNumber(str string) string {
O
obscuren 已提交
279 280 281 282 283 284 285
	if ethutil.IsHex(str) {
		str = str[2:]
	}

	return ethutil.BigD(fromHex(str)).String()
}

O
obscuren 已提交
286
func (self *XEth) PushTx(encodedTx string) (string, error) {
O
obscuren 已提交
287 288 289 290 291 292 293 294 295 296 297 298
	tx := types.NewTransactionFromBytes(fromHex(encodedTx))
	err := self.eth.TxPool().Add(tx)
	if err != nil {
		return "", err
	}

	if tx.To() == nil {
		addr := core.AddressFromMessage(tx)
		return toHex(addr), nil
	}
	return toHex(tx.Hash()), nil
}
299

300 301 302 303
var (
	defaultGasPrice = big.NewInt(10000000000000)
	defaultGas      = big.NewInt(90000)
)
304

305
func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) {
306 307 308 309 310 311 312 313
	statedb := self.State().State() //self.chainManager.TransState()
	msg := callmsg{
		from:     statedb.GetOrNewStateObject(fromHex(fromStr)),
		to:       fromHex(toStr),
		gas:      ethutil.Big(gasStr),
		gasPrice: ethutil.Big(gasPriceStr),
		value:    ethutil.Big(valueStr),
		data:     fromHex(dataStr),
314
	}
315 316 317 318 319 320 321 322
	if msg.gas.Cmp(big.NewInt(0)) == 0 {
		msg.gas = defaultGas
	}

	if msg.gasPrice.Cmp(big.NewInt(0)) == 0 {
		msg.gasPrice = defaultGasPrice
	}

323
	block := self.chainManager.CurrentBlock()
324
	vmenv := core.NewEnv(statedb, self.chainManager, msg, block)
325

326 327
	res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value)
	return toHex(res), err
328 329
}

330
func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
331
	var (
332
		from             []byte
333 334 335 336 337 338 339 340
		to               []byte
		value            = ethutil.NewValue(valueStr)
		gas              = ethutil.NewValue(gasStr)
		price            = ethutil.NewValue(gasPriceStr)
		data             []byte
		contractCreation bool
	)

341
	from = fromHex(fromStr)
342 343 344 345 346 347 348 349 350 351 352 353 354
	data = fromHex(codeStr)
	to = fromHex(toStr)
	if len(to) == 0 {
		contractCreation = true
	}

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

355
	state := self.chainManager.TxState()
O
obscuren 已提交
356
	nonce := state.NewNonce(from) //state.GetNonce(from)
357 358
	tx.SetNonce(nonce)

359
	if err := self.sign(tx, from, false); err != nil {
360 361
		return "", err
	}
362
	if err := self.eth.TxPool().Add(tx); err != nil {
363 364
		return "", err
	}
O
obscuren 已提交
365
	//state.IncrementNonce(from)
366 367 368 369 370

	if contractCreation {
		addr := core.AddressFromMessage(tx)
		pipelogger.Infof("Contract addr %x\n", addr)
	}
371 372 373 374 375 376

	if types.IsContractAddr(to) {
		return toHex(core.AddressFromMessage(tx)), nil
	}
	return toHex(tx.Hash()), nil
}
377

378 379 380 381 382 383 384 385 386 387
func (self *XEth) sign(tx *types.Transaction, from []byte, didUnlock bool) error {
	sig, err := self.accountManager.Sign(accounts.Account{Address: from}, tx.Hash())
	if err == accounts.ErrLocked {
		if didUnlock {
			return fmt.Errorf("sender account still locked after successful unlock")
		}
		if !self.frontend.UnlockAccount(from) {
			return fmt.Errorf("could not unlock sender account")
		}
		// retry signing, the account should now be unlocked.
388
		return self.sign(tx, from, true)
389 390 391 392 393 394 395
	} else if err != nil {
		return err
	}
	tx.SetSignatureValues(sig)
	return nil
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
// callmsg is the message type used for call transations.
type callmsg struct {
	from          *state.StateObject
	to            []byte
	gas, gasPrice *big.Int
	value         *big.Int
	data          []byte
}

// accessor boilerplate to implement core.Message
func (m callmsg) From() []byte       { return m.from.Address() }
func (m callmsg) Nonce() uint64      { return m.from.Nonce() }
func (m callmsg) To() []byte         { return m.to }
func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
func (m callmsg) Gas() *big.Int      { return m.gas }
func (m callmsg) Value() *big.Int    { return m.value }
func (m callmsg) Data() []byte       { return m.data }