xeth.go 15.9 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"
T
Taylor Gerring 已提交
9 10
	"sync"
	"time"
O
obscuren 已提交
11

12
	"github.com/ethereum/go-ethereum/accounts"
O
obscuren 已提交
13
	"github.com/ethereum/go-ethereum/common"
O
obscuren 已提交
14
	"github.com/ethereum/go-ethereum/core"
T
Taylor Gerring 已提交
15
	"github.com/ethereum/go-ethereum/core/state"
O
obscuren 已提交
16 17
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
18
	"github.com/ethereum/go-ethereum/eth"
T
Taylor Gerring 已提交
19
	"github.com/ethereum/go-ethereum/event/filter"
O
obscuren 已提交
20
	"github.com/ethereum/go-ethereum/logger"
21
	"github.com/ethereum/go-ethereum/miner"
O
obscuren 已提交
22
)
O
obscuren 已提交
23

T
Taylor Gerring 已提交
24 25 26
var (
	pipelogger       = logger.NewLogger("XETH")
	filterTickerTime = 5 * time.Minute
T
Taylor Gerring 已提交
27 28
	defaultGasPrice  = big.NewInt(10000000000000) //150000000000
	defaultGas       = big.NewInt(90000)          //500000
T
Taylor Gerring 已提交
29
)
O
obscuren 已提交
30

O
obscuren 已提交
31
type XEth struct {
T
Shuffle  
Taylor Gerring 已提交
32 33 34
	backend  *eth.Ethereum
	frontend Frontend

T
Taylor Gerring 已提交
35 36
	state   *State
	whisper *Whisper
O
obscuren 已提交
37

T
Taylor Gerring 已提交
38 39
	quit          chan struct{}
	filterManager *filter.FilterManager
O
obscuren 已提交
40

T
Taylor Gerring 已提交
41 42 43 44 45
	logMut sync.RWMutex
	logs   map[int]*logFilter

	messagesMut sync.RWMutex
	messages    map[int]*whisperFilter
T
Taylor Gerring 已提交
46 47 48

	// regmut   sync.Mutex
	// register map[string][]*interface{} // TODO improve return type
49

T
Taylor Gerring 已提交
50
	agent *miner.RemoteAgent
O
obscuren 已提交
51
}
O
obscuren 已提交
52

53 54 55
// 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.
56
func New(eth *eth.Ethereum, frontend Frontend) *XEth {
O
obscuren 已提交
57
	xeth := &XEth{
T
Taylor Gerring 已提交
58
		backend:       eth,
T
Shuffle  
Taylor Gerring 已提交
59
		frontend:      frontend,
T
Taylor Gerring 已提交
60 61 62 63 64 65
		whisper:       NewWhisper(eth.Whisper()),
		quit:          make(chan struct{}),
		filterManager: filter.NewFilterManager(eth.EventMux()),
		logs:          make(map[int]*logFilter),
		messages:      make(map[int]*whisperFilter),
		agent:         miner.NewRemoteAgent(),
O
obscuren 已提交
66
	}
67 68
	eth.Miner().Register(xeth.agent)

O
obscuren 已提交
69
	if frontend == nil {
70
		xeth.frontend = dummyFrontend{}
O
obscuren 已提交
71
	}
T
Taylor Gerring 已提交
72
	xeth.state = NewState(xeth, xeth.backend.ChainManager().TransState())
T
Shuffle  
Taylor Gerring 已提交
73

T
Taylor Gerring 已提交
74 75 76
	go xeth.start()
	go xeth.filterManager.Start()

O
obscuren 已提交
77 78 79
	return xeth
}

T
Taylor Gerring 已提交
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
func (self *XEth) start() {
	timer := time.NewTicker(2 * time.Second)
done:
	for {
		select {
		case <-timer.C:
			self.logMut.Lock()
			self.messagesMut.Lock()
			for id, filter := range self.logs {
				if time.Since(filter.timeout) > filterTickerTime {
					self.filterManager.UninstallFilter(id)
					delete(self.logs, id)
				}
			}

			for id, filter := range self.messages {
				if time.Since(filter.timeout) > filterTickerTime {
					self.Whisper().Unwatch(id)
					delete(self.messages, id)
				}
			}
			self.messagesMut.Unlock()
			self.logMut.Unlock()
		case <-self.quit:
			break done
		}
	}
}

func (self *XEth) stop() {
	close(self.quit)
}

T
Taylor Gerring 已提交
113 114 115
func (self *XEth) DefaultGas() *big.Int      { return defaultGas }
func (self *XEth) DefaultGasPrice() *big.Int { return defaultGasPrice }

T
Taylor Gerring 已提交
116
func (self *XEth) RemoteMining() *miner.RemoteAgent { return self.agent }
117

T
Taylor Gerring 已提交
118
func (self *XEth) AtStateNum(num int64) *XEth {
T
Taylor Gerring 已提交
119
	block := self.getBlockByHeight(num)
T
Taylor Gerring 已提交
120 121 122

	var st *state.StateDB
	if block != nil {
T
Taylor Gerring 已提交
123
		st = state.New(block.Root(), self.backend.StateDb())
T
Taylor Gerring 已提交
124
	} else {
T
Taylor Gerring 已提交
125
		st = self.backend.ChainManager().State()
T
Taylor Gerring 已提交
126
	}
T
Taylor Gerring 已提交
127 128

	return self.withState(st)
T
Taylor Gerring 已提交
129 130
}

T
Taylor Gerring 已提交
131
func (self *XEth) withState(statedb *state.StateDB) *XEth {
O
wip  
obscuren 已提交
132
	xeth := &XEth{
T
Taylor Gerring 已提交
133
		backend: self.backend,
O
wip  
obscuren 已提交
134 135 136 137 138
	}

	xeth.state = NewState(xeth, statedb)
	return xeth
}
T
Shuffle  
Taylor Gerring 已提交
139

O
wip  
obscuren 已提交
140 141
func (self *XEth) State() *State { return self.state }

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

T
Taylor Gerring 已提交
144 145 146
func (self *XEth) getBlockByHeight(height int64) *types.Block {
	var num uint64

147 148
	if height < 0 {
		num = self.CurrentBlock().NumberU64() + uint64(-1*height)
T
Taylor Gerring 已提交
149 150 151 152 153 154 155
	} else {
		num = uint64(height)
	}

	return self.backend.ChainManager().GetBlockByNumber(num)
}

O
obscuren 已提交
156
func (self *XEth) BlockByHash(strHash string) *Block {
O
obscuren 已提交
157
	hash := common.HexToHash(strHash)
T
Taylor Gerring 已提交
158
	block := self.backend.ChainManager().GetBlock(hash)
O
obscuren 已提交
159

O
obscuren 已提交
160
	return NewBlock(block)
O
obscuren 已提交
161 162
}

T
Taylor Gerring 已提交
163 164
func (self *XEth) EthBlockByHash(strHash string) *types.Block {
	hash := common.HexToHash(strHash)
T
Taylor Gerring 已提交
165
	block := self.backend.ChainManager().GetBlock(hash)
T
Taylor Gerring 已提交
166 167 168 169

	return block
}

O
obscuren 已提交
170
func (self *XEth) EthTransactionByHash(hash string) *types.Transaction {
T
Taylor Gerring 已提交
171
	data, _ := self.backend.ExtraDb().Get(common.FromHex(hash))
O
obscuren 已提交
172 173 174 175 176 177
	if len(data) != 0 {
		return types.NewTransactionFromBytes(data)
	}
	return nil
}

T
Taylor Gerring 已提交
178
func (self *XEth) BlockByNumber(num int64) *Block {
T
Taylor Gerring 已提交
179
	return NewBlock(self.getBlockByHeight(num))
O
obscuren 已提交
180 181
}

T
Taylor Gerring 已提交
182
func (self *XEth) EthBlockByNumber(num int64) *types.Block {
T
Taylor Gerring 已提交
183
	return self.getBlockByHeight(num)
T
Taylor Gerring 已提交
184 185
}

T
Taylor Gerring 已提交
186 187 188 189
func (self *XEth) CurrentBlock() *types.Block {
	return self.backend.ChainManager().CurrentBlock()
}

O
obscuren 已提交
190
func (self *XEth) Block(v interface{}) *Block {
O
obscuren 已提交
191
	if n, ok := v.(int32); ok {
T
Taylor Gerring 已提交
192
		return self.BlockByNumber(int64(n))
O
obscuren 已提交
193 194 195
	} else if str, ok := v.(string); ok {
		return self.BlockByHash(str)
	} else if f, ok := v.(float64); ok { // Don't ask ...
T
Taylor Gerring 已提交
196
		return self.BlockByNumber(int64(f))
O
obscuren 已提交
197 198 199 200 201
	}

	return nil
}

O
obscuren 已提交
202
func (self *XEth) Accounts() []string {
203
	// TODO: check err?
T
Taylor Gerring 已提交
204
	accounts, _ := self.backend.AccountManager().Accounts()
205 206
	accountAddresses := make([]string, len(accounts))
	for i, ac := range accounts {
207
		accountAddresses[i] = common.ToHex(ac.Address)
208 209
	}
	return accountAddresses
O
obscuren 已提交
210 211
}

212 213 214 215 216 217 218 219 220 221
func (self *XEth) DbPut(key, val []byte) bool {
	self.backend.ExtraDb().Put(key, val)
	return true
}

func (self *XEth) DbGet(key []byte) ([]byte, error) {
	val, err := self.backend.ExtraDb().Get(key)
	return val, err
}

O
obscuren 已提交
222
func (self *XEth) PeerCount() int {
T
Taylor Gerring 已提交
223
	return self.backend.PeerCount()
O
obscuren 已提交
224 225
}

O
obscuren 已提交
226
func (self *XEth) IsMining() bool {
T
Taylor Gerring 已提交
227
	return self.backend.IsMining()
O
obscuren 已提交
228 229
}

230
func (self *XEth) EthVersion() string {
231
	return fmt.Sprintf("%d", self.backend.EthVersion())
232 233
}

T
Taylor Gerring 已提交
234
func (self *XEth) NetworkVersion() string {
235
	return fmt.Sprintf("%d", self.backend.NetVersion())
236 237 238
}

func (self *XEth) WhisperVersion() string {
239
	return fmt.Sprintf("%d", self.backend.ShhVersion())
T
Taylor Gerring 已提交
240 241 242
}

func (self *XEth) ClientVersion() string {
243
	return self.backend.ClientVersion()
T
Taylor Gerring 已提交
244 245
}

T
Taylor Gerring 已提交
246
func (self *XEth) SetMining(shouldmine bool) bool {
T
Taylor Gerring 已提交
247
	ismining := self.backend.IsMining()
T
Taylor Gerring 已提交
248
	if shouldmine && !ismining {
T
Taylor Gerring 已提交
249
		err := self.backend.StartMining()
250
		return err == nil
T
Taylor Gerring 已提交
251 252
	}
	if ismining && !shouldmine {
T
Taylor Gerring 已提交
253
		self.backend.StopMining()
T
Taylor Gerring 已提交
254
	}
T
Taylor Gerring 已提交
255
	return self.backend.IsMining()
T
Taylor Gerring 已提交
256 257
}

O
obscuren 已提交
258
func (self *XEth) IsListening() bool {
T
Taylor Gerring 已提交
259
	return self.backend.IsListening()
O
obscuren 已提交
260 261
}

O
obscuren 已提交
262
func (self *XEth) Coinbase() string {
Z
zelig 已提交
263 264
	eb, _ := self.backend.Etherbase()
	return eb.Hex()
O
obscuren 已提交
265 266
}

O
obscuren 已提交
267
func (self *XEth) NumberToHuman(balance string) string {
O
obscuren 已提交
268
	b := common.Big(balance)
O
obscuren 已提交
269

O
obscuren 已提交
270
	return common.CurrencyToString(b)
O
obscuren 已提交
271 272
}

O
obscuren 已提交
273
func (self *XEth) StorageAt(addr, storageAddr string) string {
O
obscuren 已提交
274 275
	storage := self.State().SafeGet(addr).StorageString(storageAddr)

276
	return common.ToHex(storage.Bytes())
O
obscuren 已提交
277 278
}

O
obscuren 已提交
279
func (self *XEth) BalanceAt(addr string) string {
O
obscuren 已提交
280 281 282
	return self.State().SafeGet(addr).Balance().String()
}

O
obscuren 已提交
283
func (self *XEth) TxCountAt(address string) int {
284
	return int(self.State().SafeGet(address).Nonce())
O
obscuren 已提交
285 286
}

O
obscuren 已提交
287
func (self *XEth) CodeAt(address string) string {
288
	return common.ToHex(self.State().SafeGet(address).Code())
O
obscuren 已提交
289 290
}

O
obscuren 已提交
291
func (self *XEth) IsContract(address string) bool {
292
	return len(self.State().SafeGet(address).Code()) > 0
O
obscuren 已提交
293 294
}

O
obscuren 已提交
295
func (self *XEth) SecretToAddress(key string) string {
O
obscuren 已提交
296
	pair, err := crypto.NewKeyPairFromSec(common.FromHex(key))
O
obscuren 已提交
297 298 299 300
	if err != nil {
		return ""
	}

301
	return common.ToHex(pair.Address())
O
obscuren 已提交
302 303
}

T
Taylor Gerring 已提交
304 305
func (self *XEth) RegisterFilter(args *core.FilterOptions) int {
	var id int
T
Taylor Gerring 已提交
306
	filter := core.NewFilter(self.backend)
T
Taylor Gerring 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	filter.SetOptions(args)
	filter.LogsCallback = func(logs state.Logs) {
		self.logMut.Lock()
		defer self.logMut.Unlock()

		self.logs[id].add(logs...)
	}
	id = self.filterManager.InstallFilter(filter)
	self.logs[id] = &logFilter{timeout: time.Now()}

	return id
}

func (self *XEth) UninstallFilter(id int) bool {
	if _, ok := self.logs[id]; ok {
		delete(self.logs, id)
		self.filterManager.UninstallFilter(id)
		return true
	}

	return false
}

func (self *XEth) NewFilterString(word string) int {
	var id int
T
Taylor Gerring 已提交
332
	filter := core.NewFilter(self.backend)
T
Taylor Gerring 已提交
333 334 335

	switch word {
	case "pending":
336 337 338 339 340 341
		filter.PendingCallback = func(tx *types.Transaction) {
			self.logMut.Lock()
			defer self.logMut.Unlock()

			self.logs[id].add(&state.StateLog{})
		}
T
Taylor Gerring 已提交
342
	case "latest":
343 344 345 346 347 348 349 350 351
		filter.BlockCallback = func(block *types.Block, logs state.Logs) {
			self.logMut.Lock()
			defer self.logMut.Unlock()

			for _, log := range logs {
				self.logs[id].add(log)
			}
			self.logs[id].add(&state.StateLog{})
		}
T
Taylor Gerring 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	}

	id = self.filterManager.InstallFilter(filter)
	self.logs[id] = &logFilter{timeout: time.Now()}

	return id
}

func (self *XEth) FilterChanged(id int) state.Logs {
	self.logMut.Lock()
	defer self.logMut.Unlock()

	if self.logs[id] != nil {
		return self.logs[id].get()
	}

	return nil
}

func (self *XEth) Logs(id int) state.Logs {
	self.logMut.Lock()
	defer self.logMut.Unlock()

	filter := self.filterManager.GetFilter(id)
	if filter != nil {
		return filter.Find()
	}

	return nil
}

func (self *XEth) AllLogs(args *core.FilterOptions) state.Logs {
T
Taylor Gerring 已提交
384
	filter := core.NewFilter(self.backend)
T
Taylor Gerring 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	filter.SetOptions(args)

	return filter.Find()
}

func (p *XEth) NewWhisperFilter(opts *Options) int {
	var id int
	opts.Fn = func(msg WhisperMessage) {
		p.messagesMut.Lock()
		defer p.messagesMut.Unlock()
		p.messages[id].add(msg) // = append(p.messages[id], msg)
	}
	id = p.Whisper().Watch(opts)
	p.messages[id] = &whisperFilter{timeout: time.Now()}
	return id
}

func (p *XEth) UninstallWhisperFilter(id int) bool {
	if _, ok := p.messages[id]; ok {
		delete(p.messages, id)
		return true
	}

	return false
}

func (self *XEth) MessagesChanged(id int) []WhisperMessage {
	self.messagesMut.Lock()
	defer self.messagesMut.Unlock()

	if self.messages[id] != nil {
		return self.messages[id].get()
	}

	return nil
}

T
Taylor Gerring 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
// func (self *XEth) Register(args string) bool {
// 	self.regmut.Lock()
// 	defer self.regmut.Unlock()

// 	if _, ok := self.register[args]; ok {
// 		self.register[args] = nil // register with empty
// 	}
// 	return true
// }

// func (self *XEth) Unregister(args string) bool {
// 	self.regmut.Lock()
// 	defer self.regmut.Unlock()

// 	if _, ok := self.register[args]; ok {
// 		delete(self.register, args)
// 		return true
// 	}

// 	return false
// }

// // TODO improve return type
// func (self *XEth) PullWatchTx(args string) []*interface{} {
// 	self.regmut.Lock()
// 	defer self.regmut.Unlock()

// 	txs := self.register[args]
// 	self.register[args] = nil

// 	return txs
// }

O
obscuren 已提交
455 456 457 458 459
type KeyVal struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

O
obscuren 已提交
460
func (self *XEth) EachStorage(addr string) string {
O
obscuren 已提交
461 462 463 464
	var values []KeyVal
	object := self.State().SafeGet(addr)
	it := object.Trie().Iterator()
	for it.Next() {
O
obscuren 已提交
465
		values = append(values, KeyVal{common.ToHex(object.Trie().GetKey(it.Key)), common.ToHex(it.Value)})
O
obscuren 已提交
466 467 468 469 470 471 472 473 474 475
	}

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

	return string(valuesJson)
}

O
obscuren 已提交
476
func (self *XEth) ToAscii(str string) string {
O
obscuren 已提交
477
	padded := common.RightPadBytes([]byte(str), 32)
O
obscuren 已提交
478

479
	return "0x" + common.ToHex(padded)
O
obscuren 已提交
480 481
}

O
obscuren 已提交
482
func (self *XEth) FromAscii(str string) string {
O
obscuren 已提交
483
	if common.IsHex(str) {
O
obscuren 已提交
484 485 486
		str = str[2:]
	}

O
obscuren 已提交
487
	return string(bytes.Trim(common.FromHex(str), "\x00"))
O
obscuren 已提交
488 489
}

O
obscuren 已提交
490
func (self *XEth) FromNumber(str string) string {
O
obscuren 已提交
491
	if common.IsHex(str) {
O
obscuren 已提交
492 493 494
		str = str[2:]
	}

O
obscuren 已提交
495
	return common.BigD(common.FromHex(str)).String()
O
obscuren 已提交
496 497
}

O
obscuren 已提交
498
func (self *XEth) PushTx(encodedTx string) (string, error) {
O
obscuren 已提交
499
	tx := types.NewTransactionFromBytes(common.FromHex(encodedTx))
T
Taylor Gerring 已提交
500
	err := self.backend.TxPool().Add(tx)
O
obscuren 已提交
501 502 503 504 505 506
	if err != nil {
		return "", err
	}

	if tx.To() == nil {
		addr := core.AddressFromMessage(tx)
O
obscuren 已提交
507
		return addr.Hex(), nil
O
obscuren 已提交
508
	}
O
obscuren 已提交
509
	return tx.Hash().Hex(), nil
O
obscuren 已提交
510
}
511

512
func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) {
T
Taylor Gerring 已提交
513
	statedb := self.State().State() //self.eth.ChainManager().TransState()
514
	msg := callmsg{
O
obscuren 已提交
515 516
		from:     statedb.GetOrNewStateObject(common.HexToAddress(fromStr)),
		to:       common.HexToAddress(toStr),
O
obscuren 已提交
517 518 519 520
		gas:      common.Big(gasStr),
		gasPrice: common.Big(gasPriceStr),
		value:    common.Big(valueStr),
		data:     common.FromHex(dataStr),
521
	}
522 523 524 525 526 527 528 529
	if msg.gas.Cmp(big.NewInt(0)) == 0 {
		msg.gas = defaultGas
	}

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

530
	block := self.CurrentBlock()
T
Taylor Gerring 已提交
531
	vmenv := core.NewEnv(statedb, self.backend.ChainManager(), msg, block)
532

533
	res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value)
534
	return common.ToHex(res), err
535 536
}

537
func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
538
	var (
O
obscuren 已提交
539 540
		from             = common.HexToAddress(fromStr)
		to               = common.HexToAddress(toStr)
O
obscuren 已提交
541
		value            = common.NewValue(valueStr)
T
Taylor Gerring 已提交
542 543
		gas              = common.Big(gasStr)
		price            = common.Big(gasPriceStr)
544 545 546 547
		data             []byte
		contractCreation bool
	)

T
Taylor Gerring 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	// TODO if no_private_key then
	//if _, exists := p.register[args.From]; exists {
	//	p.register[args.From] = append(p.register[args.From], args)
	//} else {
	/*
		account := accounts.Get(common.FromHex(args.From))
		if account != nil {
			if account.Unlocked() {
				if !unlockAccount(account) {
					return
				}
			}

			result, _ := account.Transact(common.FromHex(args.To), common.FromHex(args.Value), common.FromHex(args.Gas), common.FromHex(args.GasPrice), common.FromHex(args.Data))
			if len(result) > 0 {
				*reply = common.ToHex(result)
			}
		} else if _, exists := p.register[args.From]; exists {
			p.register[ags.From] = append(p.register[args.From], args)
		}
	*/

T
Taylor Gerring 已提交
570 571 572 573 574 575 576 577 578 579
	// TODO: align default values to have the same type, e.g. not depend on
	// common.Value conversions later on
	if gas.Cmp(big.NewInt(0)) == 0 {
		gas = defaultGas
	}

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

O
obscuren 已提交
580
	data = common.FromHex(codeStr)
O
obscuren 已提交
581
	if len(toStr) == 0 {
582 583 584 585 586
		contractCreation = true
	}

	var tx *types.Transaction
	if contractCreation {
T
Taylor Gerring 已提交
587
		tx = types.NewContractCreationTx(value.BigInt(), gas, price, data)
588
	} else {
T
Taylor Gerring 已提交
589
		tx = types.NewTransactionMessage(to, value.BigInt(), gas, price, data)
590 591
	}

T
Taylor Gerring 已提交
592
	state := self.backend.ChainManager().TxState()
O
obscuren 已提交
593
	nonce := state.NewNonce(from)
594 595
	tx.SetNonce(nonce)

596
	if err := self.sign(tx, from, false); err != nil {
597 598
		return "", err
	}
T
Taylor Gerring 已提交
599
	if err := self.backend.TxPool().Add(tx); err != nil {
600 601
		return "", err
	}
602 603 604 605

	if contractCreation {
		addr := core.AddressFromMessage(tx)
		pipelogger.Infof("Contract addr %x\n", addr)
606

O
obscuren 已提交
607
		return core.AddressFromMessage(tx).Hex(), nil
608
	}
O
obscuren 已提交
609
	return tx.Hash().Hex(), nil
610
}
611

O
obscuren 已提交
612
func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error {
T
Taylor Gerring 已提交
613
	sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, tx.Hash().Bytes())
614 615 616 617
	if err == accounts.ErrLocked {
		if didUnlock {
			return fmt.Errorf("sender account still locked after successful unlock")
		}
O
obscuren 已提交
618
		if !self.frontend.UnlockAccount(from.Bytes()) {
619 620 621
			return fmt.Errorf("could not unlock sender account")
		}
		// retry signing, the account should now be unlocked.
622
		return self.sign(tx, from, true)
623 624 625 626 627 628 629
	} else if err != nil {
		return err
	}
	tx.SetSignatureValues(sig)
	return nil
}

630 631 632
// callmsg is the message type used for call transations.
type callmsg struct {
	from          *state.StateObject
O
obscuren 已提交
633
	to            common.Address
634 635 636 637 638 639
	gas, gasPrice *big.Int
	value         *big.Int
	data          []byte
}

// accessor boilerplate to implement core.Message
O
obscuren 已提交
640 641 642 643 644 645 646
func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil }
func (m callmsg) Nonce() uint64                 { return m.from.Nonce() }
func (m callmsg) To() *common.Address           { 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 }
T
Taylor Gerring 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

type whisperFilter struct {
	messages []WhisperMessage
	timeout  time.Time
	id       int
}

func (w *whisperFilter) add(msgs ...WhisperMessage) {
	w.messages = append(w.messages, msgs...)
}
func (w *whisperFilter) get() []WhisperMessage {
	w.timeout = time.Now()
	tmp := w.messages
	w.messages = nil
	return tmp
}

type logFilter struct {
	logs    state.Logs
	timeout time.Time
	id      int
}

func (l *logFilter) add(logs ...state.Log) {
	l.logs = append(l.logs, logs...)
}

func (l *logFilter) get() state.Logs {
	l.timeout = time.Now()
	tmp := l.logs
	l.logs = nil
	return tmp
}