xeth.go 15.6 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 147 148 149 150 151 152 153 154 155 156 157 158 159
func (self *XEth) getBlockByHeight(height int64) *types.Block {
	var num uint64

	// -1 means "latest"
	// -2 means "pending", which has no blocknum
	if height <= -2 {
		return &types.Block{}
	} else if height == -1 {
		num = self.CurrentBlock().NumberU64()
	} else {
		num = uint64(height)
	}

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

O
obscuren 已提交
160
func (self *XEth) BlockByHash(strHash string) *Block {
O
obscuren 已提交
161
	hash := common.HexToHash(strHash)
T
Taylor Gerring 已提交
162
	block := self.backend.ChainManager().GetBlock(hash)
O
obscuren 已提交
163

O
obscuren 已提交
164
	return NewBlock(block)
O
obscuren 已提交
165 166
}

T
Taylor Gerring 已提交
167
func (self *XEth) EthBlockByHash(strHash string) *types.Block {
O
obscuren 已提交
168
	hash := common.HexToHash(strHash)
T
Taylor Gerring 已提交
169
	block := self.backend.ChainManager().GetBlock(hash)
T
Taylor Gerring 已提交
170 171 172 173

	return block
}

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

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

T
Taylor Gerring 已提交
186
func (self *XEth) EthBlockByNumber(num int64) *types.Block {
T
Taylor Gerring 已提交
187
	return self.getBlockByHeight(num)
T
Taylor Gerring 已提交
188 189
}

T
Taylor Gerring 已提交
190 191 192 193
func (self *XEth) CurrentBlock() *types.Block {
	return self.backend.ChainManager().CurrentBlock()
}

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

	return nil
}

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

O
obscuren 已提交
216
func (self *XEth) PeerCount() int {
T
Taylor Gerring 已提交
217
	return self.backend.PeerCount()
O
obscuren 已提交
218 219
}

O
obscuren 已提交
220
func (self *XEth) IsMining() bool {
T
Taylor Gerring 已提交
221
	return self.backend.IsMining()
O
obscuren 已提交
222 223
}

T
Taylor Gerring 已提交
224 225 226 227 228 229 230 231
func (self *XEth) NetworkVersion() string {
	return string(self.backend.ProtocolVersion())
}

func (self *XEth) ClientVersion() string {
	return self.backend.Version()
}

T
Taylor Gerring 已提交
232
func (self *XEth) SetMining(shouldmine bool) bool {
T
Taylor Gerring 已提交
233
	ismining := self.backend.IsMining()
T
Taylor Gerring 已提交
234
	if shouldmine && !ismining {
T
Taylor Gerring 已提交
235
		err := self.backend.StartMining()
236
		return err == nil
T
Taylor Gerring 已提交
237 238
	}
	if ismining && !shouldmine {
T
Taylor Gerring 已提交
239
		self.backend.StopMining()
T
Taylor Gerring 已提交
240
	}
T
Taylor Gerring 已提交
241
	return self.backend.IsMining()
T
Taylor Gerring 已提交
242 243
}

O
obscuren 已提交
244
func (self *XEth) IsListening() bool {
T
Taylor Gerring 已提交
245
	return self.backend.IsListening()
O
obscuren 已提交
246 247
}

O
obscuren 已提交
248
func (self *XEth) Coinbase() string {
T
Taylor Gerring 已提交
249
	cb, _ := self.backend.AccountManager().Coinbase()
250
	return common.ToHex(cb)
O
obscuren 已提交
251 252
}

O
obscuren 已提交
253
func (self *XEth) NumberToHuman(balance string) string {
O
obscuren 已提交
254
	b := common.Big(balance)
O
obscuren 已提交
255

O
obscuren 已提交
256
	return common.CurrencyToString(b)
O
obscuren 已提交
257 258
}

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

262
	return common.ToHex(storage.Bytes())
O
obscuren 已提交
263 264
}

O
obscuren 已提交
265
func (self *XEth) BalanceAt(addr string) string {
O
obscuren 已提交
266 267 268
	return self.State().SafeGet(addr).Balance().String()
}

O
obscuren 已提交
269
func (self *XEth) TxCountAt(address string) int {
270
	return int(self.State().SafeGet(address).Nonce())
O
obscuren 已提交
271 272
}

O
obscuren 已提交
273
func (self *XEth) CodeAt(address string) string {
274
	return common.ToHex(self.State().SafeGet(address).Code())
O
obscuren 已提交
275 276
}

O
obscuren 已提交
277
func (self *XEth) IsContract(address string) bool {
278
	return len(self.State().SafeGet(address).Code()) > 0
O
obscuren 已提交
279 280
}

O
obscuren 已提交
281
func (self *XEth) SecretToAddress(key string) string {
O
obscuren 已提交
282
	pair, err := crypto.NewKeyPairFromSec(common.FromHex(key))
O
obscuren 已提交
283 284 285 286
	if err != nil {
		return ""
	}

287
	return common.ToHex(pair.Address())
O
obscuren 已提交
288 289
}

T
Taylor Gerring 已提交
290 291
func (self *XEth) RegisterFilter(args *core.FilterOptions) int {
	var id int
T
Taylor Gerring 已提交
292
	filter := core.NewFilter(self.backend)
T
Taylor Gerring 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	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 已提交
318
	filter := core.NewFilter(self.backend)
T
Taylor Gerring 已提交
319 320 321

	switch word {
	case "pending":
322 323 324 325 326 327
		filter.PendingCallback = func(tx *types.Transaction) {
			self.logMut.Lock()
			defer self.logMut.Unlock()

			self.logs[id].add(&state.StateLog{})
		}
T
Taylor Gerring 已提交
328
	case "latest":
329 330 331 332 333 334 335 336 337
		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 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	}

	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 已提交
370
	filter := core.NewFilter(self.backend)
T
Taylor Gerring 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	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 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
// 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 已提交
441 442 443 444 445
type KeyVal struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

O
obscuren 已提交
446
func (self *XEth) EachStorage(addr string) string {
O
obscuren 已提交
447 448 449 450
	var values []KeyVal
	object := self.State().SafeGet(addr)
	it := object.Trie().Iterator()
	for it.Next() {
451
		values = append(values, KeyVal{common.ToHex(it.Key), common.ToHex(it.Value)})
O
obscuren 已提交
452 453 454 455 456 457 458 459 460 461
	}

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

	return string(valuesJson)
}

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

465
	return "0x" + common.ToHex(padded)
O
obscuren 已提交
466 467
}

O
obscuren 已提交
468
func (self *XEth) FromAscii(str string) string {
O
obscuren 已提交
469
	if common.IsHex(str) {
O
obscuren 已提交
470 471 472
		str = str[2:]
	}

O
obscuren 已提交
473
	return string(bytes.Trim(common.FromHex(str), "\x00"))
O
obscuren 已提交
474 475
}

O
obscuren 已提交
476
func (self *XEth) FromNumber(str string) string {
O
obscuren 已提交
477
	if common.IsHex(str) {
O
obscuren 已提交
478 479 480
		str = str[2:]
	}

O
obscuren 已提交
481
	return common.BigD(common.FromHex(str)).String()
O
obscuren 已提交
482 483
}

O
obscuren 已提交
484
func (self *XEth) PushTx(encodedTx string) (string, error) {
O
obscuren 已提交
485
	tx := types.NewTransactionFromBytes(common.FromHex(encodedTx))
T
Taylor Gerring 已提交
486
	err := self.backend.TxPool().Add(tx)
O
obscuren 已提交
487 488 489 490 491 492
	if err != nil {
		return "", err
	}

	if tx.To() == nil {
		addr := core.AddressFromMessage(tx)
O
obscuren 已提交
493
		return addr.Hex(), nil
O
obscuren 已提交
494
	}
O
obscuren 已提交
495
	return tx.Hash().Hex(), nil
O
obscuren 已提交
496
}
497

498
func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) {
T
Taylor Gerring 已提交
499
	statedb := self.State().State() //self.eth.ChainManager().TransState()
500
	msg := callmsg{
O
obscuren 已提交
501 502
		from:     statedb.GetOrNewStateObject(common.HexToAddress(fromStr)),
		to:       common.HexToAddress(toStr),
O
obscuren 已提交
503 504 505 506
		gas:      common.Big(gasStr),
		gasPrice: common.Big(gasPriceStr),
		value:    common.Big(valueStr),
		data:     common.FromHex(dataStr),
507
	}
508 509 510 511 512 513 514 515
	if msg.gas.Cmp(big.NewInt(0)) == 0 {
		msg.gas = defaultGas
	}

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

516
	block := self.CurrentBlock()
T
Taylor Gerring 已提交
517
	vmenv := core.NewEnv(statedb, self.backend.ChainManager(), msg, block)
518

519
	res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value)
520
	return common.ToHex(res), err
521 522
}

523
func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
524
	var (
O
obscuren 已提交
525 526
		from             = common.HexToAddress(fromStr)
		to               = common.HexToAddress(toStr)
O
obscuren 已提交
527
		value            = common.NewValue(valueStr)
T
Taylor Gerring 已提交
528 529
		gas              = common.Big(gasStr)
		price            = common.Big(gasPriceStr)
530 531 532 533
		data             []byte
		contractCreation bool
	)

T
Taylor Gerring 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	// 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 已提交
556 557 558 559 560 561 562 563 564 565
	// 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 已提交
566
	data = common.FromHex(codeStr)
O
obscuren 已提交
567
	if len(toStr) == 0 {
568 569 570 571 572
		contractCreation = true
	}

	var tx *types.Transaction
	if contractCreation {
T
Taylor Gerring 已提交
573
		tx = types.NewContractCreationTx(value.BigInt(), gas, price, data)
574
	} else {
T
Taylor Gerring 已提交
575
		tx = types.NewTransactionMessage(to, value.BigInt(), gas, price, data)
576 577
	}

T
Taylor Gerring 已提交
578
	state := self.backend.ChainManager().TxState()
O
obscuren 已提交
579
	nonce := state.NewNonce(from)
580 581
	tx.SetNonce(nonce)

582
	if err := self.sign(tx, from, false); err != nil {
583 584
		return "", err
	}
T
Taylor Gerring 已提交
585
	if err := self.backend.TxPool().Add(tx); err != nil {
586 587
		return "", err
	}
588 589 590 591

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

O
obscuren 已提交
593
		return core.AddressFromMessage(tx).Hex(), nil
594
	}
O
obscuren 已提交
595
	return tx.Hash().Hex(), nil
596
}
597

O
obscuren 已提交
598
func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error {
T
Taylor Gerring 已提交
599
	sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, tx.Hash().Bytes())
600 601 602 603
	if err == accounts.ErrLocked {
		if didUnlock {
			return fmt.Errorf("sender account still locked after successful unlock")
		}
O
obscuren 已提交
604
		if !self.frontend.UnlockAccount(from.Bytes()) {
605 606 607
			return fmt.Errorf("could not unlock sender account")
		}
		// retry signing, the account should now be unlocked.
608
		return self.sign(tx, from, true)
609 610 611 612 613 614 615
	} else if err != nil {
		return err
	}
	tx.SetSignatureValues(sig)
	return nil
}

616 617 618
// callmsg is the message type used for call transations.
type callmsg struct {
	from          *state.StateObject
O
obscuren 已提交
619
	to            common.Address
620 621 622 623 624 625
	gas, gasPrice *big.Int
	value         *big.Int
	data          []byte
}

// accessor boilerplate to implement core.Message
O
obscuren 已提交
626 627 628 629 630 631 632
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 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

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
}