xeth.go 16.0 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"
13
	"github.com/ethereum/go-ethereum/common"
O
obscuren 已提交
14 15 16
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
17
	"github.com/ethereum/go-ethereum/event"
T
Taylor Gerring 已提交
18
	"github.com/ethereum/go-ethereum/event/filter"
O
obscuren 已提交
19
	"github.com/ethereum/go-ethereum/logger"
20
	"github.com/ethereum/go-ethereum/p2p"
O
wip  
obscuren 已提交
21
	"github.com/ethereum/go-ethereum/state"
22
	"github.com/ethereum/go-ethereum/whisper"
O
obscuren 已提交
23
)
O
obscuren 已提交
24

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

// to resolve the import cycle
type Backend interface {
	BlockProcessor() *core.BlockProcessor
	ChainManager() *core.ChainManager
36
	AccountManager() *accounts.Manager
37 38
	TxPool() *core.TxPool
	PeerCount() int
O
obscuren 已提交
39
	IsListening() bool
40
	Peers() []*p2p.Peer
O
obscuren 已提交
41 42 43
	BlockDb() common.Database
	StateDb() common.Database
	ExtraDb() common.Database
44
	EventMux() *event.TypeMux
45
	Whisper() *whisper.Whisper
46 47 48 49

	IsMining() bool
	StartMining() error
	StopMining()
T
Taylor Gerring 已提交
50
	Version() string
O
obscuren 已提交
51 52
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
// 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
}

T
Taylor Gerring 已提交
73 74 75 76 77 78 79
// dummyFrontend is a non-interactive frontend that allows all
// transactions but cannot not unlock any keys.
type dummyFrontend struct{}

func (dummyFrontend) UnlockAccount([]byte) bool                  { return false }
func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true }

O
obscuren 已提交
80
type XEth struct {
O
obscuren 已提交
81 82 83
	eth            Backend
	blockProcessor *core.BlockProcessor
	chainManager   *core.ChainManager
84
	accountManager *accounts.Manager
85
	state          *State
86
	whisper        *Whisper
O
obscuren 已提交
87

88
	frontend Frontend
T
Taylor Gerring 已提交
89 90 91 92 93 94 95 96 97

	quit          chan struct{}
	filterManager *filter.FilterManager

	logMut sync.RWMutex
	logs   map[int]*logFilter

	messagesMut sync.RWMutex
	messages    map[int]*whisperFilter
O
obscuren 已提交
98 99
}

100 101 102 103
// 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 已提交
104
	xeth := &XEth{
O
obscuren 已提交
105 106 107
		eth:            eth,
		blockProcessor: eth.BlockProcessor(),
		chainManager:   eth.ChainManager(),
108
		accountManager: eth.AccountManager(),
109
		whisper:        NewWhisper(eth.Whisper()),
T
Taylor Gerring 已提交
110 111
		quit:           make(chan struct{}),
		filterManager:  filter.NewFilterManager(eth.EventMux()),
112
		frontend:       frontend,
T
Taylor Gerring 已提交
113 114
		logs:           make(map[int]*logFilter),
		messages:       make(map[int]*whisperFilter),
O
obscuren 已提交
115
	}
O
obscuren 已提交
116
	if frontend == nil {
117
		xeth.frontend = dummyFrontend{}
O
obscuren 已提交
118
	}
O
wip  
obscuren 已提交
119
	xeth.state = NewState(xeth, xeth.chainManager.TransState())
T
Taylor Gerring 已提交
120 121 122
	go xeth.start()
	go xeth.filterManager.Start()

O
obscuren 已提交
123 124 125
	return xeth
}

T
Taylor Gerring 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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 已提交
159 160 161
func (self *XEth) DefaultGas() *big.Int      { return defaultGas }
func (self *XEth) DefaultGasPrice() *big.Int { return defaultGasPrice }

T
Taylor Gerring 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
func (self *XEth) AtStateNum(num int64) *XEth {
	chain := self.Backend().ChainManager()
	var block *types.Block

	if num < 0 {
		num = chain.CurrentBlock().Number().Int64() + num + 1
	}
	block = chain.GetBlockByNumber(uint64(num))

	var st *state.StateDB
	if block != nil {
		st = state.New(block.Root(), self.Backend().StateDb())
	} else {
		st = chain.State()
	}
	return self.WithState(st)
}

O
wip  
obscuren 已提交
180
func (self *XEth) Backend() Backend { return self.eth }
O
obscuren 已提交
181
func (self *XEth) WithState(statedb *state.StateDB) *XEth {
O
wip  
obscuren 已提交
182 183 184 185 186 187 188 189 190 191 192 193
	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 }

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

O
obscuren 已提交
196
func (self *XEth) BlockByHash(strHash string) *Block {
O
obscuren 已提交
197
	hash := common.FromHex(strHash)
O
obscuren 已提交
198 199
	block := self.chainManager.GetBlock(hash)

O
obscuren 已提交
200
	return NewBlock(block)
O
obscuren 已提交
201 202
}

T
Taylor Gerring 已提交
203
func (self *XEth) EthBlockByHash(strHash string) *types.Block {
O
obscuren 已提交
204
	hash := common.FromHex(strHash)
T
Taylor Gerring 已提交
205 206 207 208 209
	block := self.chainManager.GetBlock(hash)

	return block
}

O
obscuren 已提交
210
func (self *XEth) EthTransactionByHash(hash string) *types.Transaction {
O
obscuren 已提交
211
	data, _ := self.eth.ExtraDb().Get(common.FromHex(hash))
O
obscuren 已提交
212 213 214 215 216 217
	if len(data) != 0 {
		return types.NewTransactionFromBytes(data)
	}
	return nil
}

T
Taylor Gerring 已提交
218 219 220 221 222 223
func (self *XEth) BlockByNumber(num int64) *Block {
	if num == -1 {
		return NewBlock(self.chainManager.CurrentBlock())
	}

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

T
Taylor Gerring 已提交
226 227 228 229 230 231 232 233
func (self *XEth) EthBlockByNumber(num int64) *types.Block {
	if num == -1 {
		return self.chainManager.CurrentBlock()
	}

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

O
obscuren 已提交
234
func (self *XEth) Block(v interface{}) *Block {
O
obscuren 已提交
235
	if n, ok := v.(int32); ok {
T
Taylor Gerring 已提交
236
		return self.BlockByNumber(int64(n))
O
obscuren 已提交
237 238 239
	} else if str, ok := v.(string); ok {
		return self.BlockByHash(str)
	} else if f, ok := v.(float64); ok { // Don't ask ...
T
Taylor Gerring 已提交
240
		return self.BlockByNumber(int64(f))
O
obscuren 已提交
241 242 243 244 245
	}

	return nil
}

O
obscuren 已提交
246
func (self *XEth) Accounts() []string {
247 248 249 250
	// TODO: check err?
	accounts, _ := self.eth.AccountManager().Accounts()
	accountAddresses := make([]string, len(accounts))
	for i, ac := range accounts {
251
		accountAddresses[i] = common.ToHex(ac.Address)
252 253
	}
	return accountAddresses
O
obscuren 已提交
254 255
}

O
obscuren 已提交
256
func (self *XEth) PeerCount() int {
O
obscuren 已提交
257 258 259
	return self.eth.PeerCount()
}

O
obscuren 已提交
260
func (self *XEth) IsMining() bool {
261
	return self.eth.IsMining()
O
obscuren 已提交
262 263
}

T
Taylor Gerring 已提交
264
func (self *XEth) SetMining(shouldmine bool) bool {
265
	ismining := self.eth.IsMining()
T
Taylor Gerring 已提交
266
	if shouldmine && !ismining {
267 268
		err := self.eth.StartMining()
		return err == nil
T
Taylor Gerring 已提交
269 270
	}
	if ismining && !shouldmine {
271
		self.eth.StopMining()
T
Taylor Gerring 已提交
272
	}
273
	return self.eth.IsMining()
T
Taylor Gerring 已提交
274 275
}

O
obscuren 已提交
276
func (self *XEth) IsListening() bool {
O
obscuren 已提交
277 278 279
	return self.eth.IsListening()
}

O
obscuren 已提交
280
func (self *XEth) Coinbase() string {
281
	cb, _ := self.eth.AccountManager().Coinbase()
282
	return common.ToHex(cb)
O
obscuren 已提交
283 284
}

O
obscuren 已提交
285
func (self *XEth) NumberToHuman(balance string) string {
O
obscuren 已提交
286
	b := common.Big(balance)
O
obscuren 已提交
287

O
obscuren 已提交
288
	return common.CurrencyToString(b)
O
obscuren 已提交
289 290
}

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

294
	return common.ToHex(storage.Bytes())
O
obscuren 已提交
295 296
}

O
obscuren 已提交
297
func (self *XEth) BalanceAt(addr string) string {
O
obscuren 已提交
298 299 300
	return self.State().SafeGet(addr).Balance().String()
}

O
obscuren 已提交
301
func (self *XEth) TxCountAt(address string) int {
302
	return int(self.State().SafeGet(address).Nonce())
O
obscuren 已提交
303 304
}

O
obscuren 已提交
305
func (self *XEth) CodeAt(address string) string {
306
	return common.ToHex(self.State().SafeGet(address).Code())
O
obscuren 已提交
307 308
}

O
obscuren 已提交
309
func (self *XEth) IsContract(address string) bool {
310
	return len(self.State().SafeGet(address).Code()) > 0
O
obscuren 已提交
311 312
}

O
obscuren 已提交
313
func (self *XEth) SecretToAddress(key string) string {
O
obscuren 已提交
314
	pair, err := crypto.NewKeyPairFromSec(common.FromHex(key))
O
obscuren 已提交
315 316 317 318
	if err != nil {
		return ""
	}

319
	return common.ToHex(pair.Address())
O
obscuren 已提交
320 321
}

T
Taylor Gerring 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 370 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 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
func (self *XEth) RegisterFilter(args *core.FilterOptions) int {
	var id int
	filter := core.NewFilter(self.Backend())
	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
	filter := core.NewFilter(self.Backend())

	callback := 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{})
	}

	switch word {
	case "pending":
		filter.PendingCallback = callback
	case "latest":
		filter.BlockCallback = callback
	}

	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 {
	filter := core.NewFilter(self.Backend())
	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
}

O
obscuren 已提交
437 438 439 440 441
type KeyVal struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

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

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

	return string(valuesJson)
}

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

461
	return "0x" + common.ToHex(padded)
O
obscuren 已提交
462 463
}

O
obscuren 已提交
464
func (self *XEth) FromAscii(str string) string {
O
obscuren 已提交
465
	if common.IsHex(str) {
O
obscuren 已提交
466 467 468
		str = str[2:]
	}

O
obscuren 已提交
469
	return string(bytes.Trim(common.FromHex(str), "\x00"))
O
obscuren 已提交
470 471
}

O
obscuren 已提交
472
func (self *XEth) FromNumber(str string) string {
O
obscuren 已提交
473
	if common.IsHex(str) {
O
obscuren 已提交
474 475 476
		str = str[2:]
	}

O
obscuren 已提交
477
	return common.BigD(common.FromHex(str)).String()
O
obscuren 已提交
478 479
}

O
obscuren 已提交
480
func (self *XEth) PushTx(encodedTx string) (string, error) {
O
obscuren 已提交
481
	tx := types.NewTransactionFromBytes(common.FromHex(encodedTx))
O
obscuren 已提交
482 483 484 485 486 487 488
	err := self.eth.TxPool().Add(tx)
	if err != nil {
		return "", err
	}

	if tx.To() == nil {
		addr := core.AddressFromMessage(tx)
489
		return common.ToHex(addr), nil
O
obscuren 已提交
490
	}
491
	return common.ToHex(tx.Hash()), nil
O
obscuren 已提交
492
}
493

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

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

512
	block := self.chainManager.CurrentBlock()
513
	vmenv := core.NewEnv(statedb, self.chainManager, msg, block)
514

515
	res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value)
516
	return common.ToHex(res), err
517 518
}

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

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

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

576
	state := self.chainManager.TxState()
O
obscuren 已提交
577
	nonce := state.NewNonce(from) //state.GetNonce(from)
578 579
	tx.SetNonce(nonce)

580
	if err := self.sign(tx, from, false); err != nil {
581 582
		return "", err
	}
583
	if err := self.eth.TxPool().Add(tx); err != nil {
584 585
		return "", err
	}
O
obscuren 已提交
586
	//state.IncrementNonce(from)
587 588 589 590 591

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

	if types.IsContractAddr(to) {
594
		return common.ToHex(core.AddressFromMessage(tx)), nil
595
	}
596
	return common.ToHex(tx.Hash()), nil
597
}
598

599 600 601 602 603 604 605 606 607 608
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.
609
		return self.sign(tx, from, true)
610 611 612 613 614 615 616
	} else if err != nil {
		return err
	}
	tx.SetSignatureValues(sig)
	return nil
}

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
// 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 }
T
Taylor Gerring 已提交
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 666

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
}