worker.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package miner
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "sync"
  22. "sync/atomic"
  23. "time"
  24. "github.com/ethereum/go-ethereum/accounts"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/event"
  32. "github.com/ethereum/go-ethereum/logger"
  33. "github.com/ethereum/go-ethereum/logger/glog"
  34. "github.com/ethereum/go-ethereum/params"
  35. "github.com/ethereum/go-ethereum/pow"
  36. "gopkg.in/fatih/set.v0"
  37. )
  38. var jsonlogger = logger.NewJsonLogger()
  39. const (
  40. resultQueueSize = 10
  41. miningLogAtDepth = 5
  42. )
  43. // Agent can register themself with the worker
  44. type Agent interface {
  45. Work() chan<- *Work
  46. SetReturnCh(chan<- *Result)
  47. Stop()
  48. Start()
  49. GetHashRate() int64
  50. }
  51. // Work is the workers current environment and holds
  52. // all of the current state information
  53. type Work struct {
  54. config *params.ChainConfig
  55. signer types.Signer
  56. state *state.StateDB // apply state changes here
  57. ancestors *set.Set // ancestor set (used for checking uncle parent validity)
  58. family *set.Set // family set (used for checking uncle invalidity)
  59. uncles *set.Set // uncle set
  60. tcount int // tx count in cycle
  61. ownedAccounts *set.Set
  62. lowGasTxs types.Transactions
  63. failedTxs types.Transactions
  64. Block *types.Block // the new block
  65. header *types.Header
  66. txs []*types.Transaction
  67. receipts []*types.Receipt
  68. createdAt time.Time
  69. }
  70. type Result struct {
  71. Work *Work
  72. Block *types.Block
  73. }
  74. // worker is the main object which takes care of applying messages to the new state
  75. type worker struct {
  76. config *params.ChainConfig
  77. mu sync.Mutex
  78. // update loop
  79. mux *event.TypeMux
  80. events event.Subscription
  81. wg sync.WaitGroup
  82. agents map[Agent]struct{}
  83. recv chan *Result
  84. pow pow.PoW
  85. eth Backend
  86. chain *core.BlockChain
  87. proc core.Validator
  88. chainDb ethdb.Database
  89. coinbase common.Address
  90. gasPrice *big.Int
  91. extra []byte
  92. currentMu sync.Mutex
  93. current *Work
  94. uncleMu sync.Mutex
  95. possibleUncles map[common.Hash]*types.Block
  96. txQueueMu sync.Mutex
  97. txQueue map[common.Hash]*types.Transaction
  98. unconfirmed *unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations
  99. // atomic status counters
  100. mining int32
  101. atWork int32
  102. fullValidation bool
  103. }
  104. func newWorker(config *params.ChainConfig, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker {
  105. worker := &worker{
  106. config: config,
  107. eth: eth,
  108. mux: mux,
  109. chainDb: eth.ChainDb(),
  110. recv: make(chan *Result, resultQueueSize),
  111. gasPrice: new(big.Int),
  112. chain: eth.BlockChain(),
  113. proc: eth.BlockChain().Validator(),
  114. possibleUncles: make(map[common.Hash]*types.Block),
  115. coinbase: coinbase,
  116. txQueue: make(map[common.Hash]*types.Transaction),
  117. agents: make(map[Agent]struct{}),
  118. unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), 5),
  119. fullValidation: false,
  120. }
  121. worker.events = worker.mux.Subscribe(core.ChainHeadEvent{}, core.ChainSideEvent{}, core.TxPreEvent{})
  122. go worker.update()
  123. go worker.wait()
  124. worker.commitNewWork()
  125. return worker
  126. }
  127. func (self *worker) setEtherbase(addr common.Address) {
  128. self.mu.Lock()
  129. defer self.mu.Unlock()
  130. self.coinbase = addr
  131. }
  132. func (self *worker) setExtra(extra []byte) {
  133. self.mu.Lock()
  134. defer self.mu.Unlock()
  135. self.extra = extra
  136. }
  137. func (self *worker) pending() (*types.Block, *state.StateDB) {
  138. self.currentMu.Lock()
  139. defer self.currentMu.Unlock()
  140. if atomic.LoadInt32(&self.mining) == 0 {
  141. return types.NewBlock(
  142. self.current.header,
  143. self.current.txs,
  144. nil,
  145. self.current.receipts,
  146. ), self.current.state.Copy()
  147. }
  148. return self.current.Block, self.current.state.Copy()
  149. }
  150. func (self *worker) pendingBlock() *types.Block {
  151. self.currentMu.Lock()
  152. defer self.currentMu.Unlock()
  153. if atomic.LoadInt32(&self.mining) == 0 {
  154. return types.NewBlock(
  155. self.current.header,
  156. self.current.txs,
  157. nil,
  158. self.current.receipts,
  159. )
  160. }
  161. return self.current.Block
  162. }
  163. func (self *worker) start() {
  164. self.mu.Lock()
  165. defer self.mu.Unlock()
  166. atomic.StoreInt32(&self.mining, 1)
  167. // spin up agents
  168. for agent := range self.agents {
  169. agent.Start()
  170. }
  171. }
  172. func (self *worker) stop() {
  173. self.wg.Wait()
  174. self.mu.Lock()
  175. defer self.mu.Unlock()
  176. if atomic.LoadInt32(&self.mining) == 1 {
  177. // Stop all agents.
  178. for agent := range self.agents {
  179. agent.Stop()
  180. // Remove CPU agents.
  181. if _, ok := agent.(*CpuAgent); ok {
  182. delete(self.agents, agent)
  183. }
  184. }
  185. }
  186. atomic.StoreInt32(&self.mining, 0)
  187. atomic.StoreInt32(&self.atWork, 0)
  188. }
  189. func (self *worker) register(agent Agent) {
  190. self.mu.Lock()
  191. defer self.mu.Unlock()
  192. self.agents[agent] = struct{}{}
  193. agent.SetReturnCh(self.recv)
  194. }
  195. func (self *worker) unregister(agent Agent) {
  196. self.mu.Lock()
  197. defer self.mu.Unlock()
  198. delete(self.agents, agent)
  199. agent.Stop()
  200. }
  201. func (self *worker) update() {
  202. for event := range self.events.Chan() {
  203. // A real event arrived, process interesting content
  204. switch ev := event.Data.(type) {
  205. case core.ChainHeadEvent:
  206. self.commitNewWork()
  207. case core.ChainSideEvent:
  208. self.uncleMu.Lock()
  209. self.possibleUncles[ev.Block.Hash()] = ev.Block
  210. self.uncleMu.Unlock()
  211. case core.TxPreEvent:
  212. // Apply transaction to the pending state if we're not mining
  213. if atomic.LoadInt32(&self.mining) == 0 {
  214. self.currentMu.Lock()
  215. acc, _ := types.Sender(self.current.signer, ev.Tx)
  216. txs := map[common.Address]types.Transactions{acc: types.Transactions{ev.Tx}}
  217. txset := types.NewTransactionsByPriceAndNonce(txs)
  218. self.current.commitTransactions(self.mux, txset, self.gasPrice, self.chain)
  219. self.currentMu.Unlock()
  220. }
  221. }
  222. }
  223. }
  224. func (self *worker) wait() {
  225. for {
  226. mustCommitNewWork := true
  227. for result := range self.recv {
  228. atomic.AddInt32(&self.atWork, -1)
  229. if result == nil {
  230. continue
  231. }
  232. block := result.Block
  233. work := result.Work
  234. if self.fullValidation {
  235. if _, err := self.chain.InsertChain(types.Blocks{block}); err != nil {
  236. glog.V(logger.Error).Infoln("mining err", err)
  237. continue
  238. }
  239. go self.mux.Post(core.NewMinedBlockEvent{Block: block})
  240. } else {
  241. work.state.Commit(self.config.IsEIP158(block.Number()))
  242. parent := self.chain.GetBlock(block.ParentHash(), block.NumberU64()-1)
  243. if parent == nil {
  244. glog.V(logger.Error).Infoln("Invalid block found during mining")
  245. continue
  246. }
  247. auxValidator := self.eth.BlockChain().AuxValidator()
  248. if err := core.ValidateHeader(self.config, auxValidator, block.Header(), parent.Header(), true, false); err != nil && err != core.BlockFutureErr {
  249. glog.V(logger.Error).Infoln("Invalid header on mined block:", err)
  250. continue
  251. }
  252. stat, err := self.chain.WriteBlock(block)
  253. if err != nil {
  254. glog.V(logger.Error).Infoln("error writing block to chain", err)
  255. continue
  256. }
  257. // update block hash since it is now available and not when the receipt/log of individual transactions were created
  258. for _, r := range work.receipts {
  259. for _, l := range r.Logs {
  260. l.BlockHash = block.Hash()
  261. }
  262. }
  263. for _, log := range work.state.Logs() {
  264. log.BlockHash = block.Hash()
  265. }
  266. // check if canon block and write transactions
  267. if stat == core.CanonStatTy {
  268. // This puts transactions in a extra db for rpc
  269. core.WriteTransactions(self.chainDb, block)
  270. // store the receipts
  271. core.WriteReceipts(self.chainDb, work.receipts)
  272. // Write map map bloom filters
  273. core.WriteMipmapBloom(self.chainDb, block.NumberU64(), work.receipts)
  274. // implicit by posting ChainHeadEvent
  275. mustCommitNewWork = false
  276. }
  277. // broadcast before waiting for validation
  278. go func(block *types.Block, logs []*types.Log, receipts []*types.Receipt) {
  279. self.mux.Post(core.NewMinedBlockEvent{Block: block})
  280. self.mux.Post(core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
  281. if stat == core.CanonStatTy {
  282. self.mux.Post(core.ChainHeadEvent{Block: block})
  283. self.mux.Post(logs)
  284. }
  285. if err := core.WriteBlockReceipts(self.chainDb, block.Hash(), block.NumberU64(), receipts); err != nil {
  286. glog.V(logger.Warn).Infoln("error writing block receipts:", err)
  287. }
  288. }(block, work.state.Logs(), work.receipts)
  289. }
  290. // Insert the block into the set of pending ones to wait for confirmations
  291. self.unconfirmed.Insert(block.NumberU64(), block.Hash())
  292. if mustCommitNewWork {
  293. self.commitNewWork()
  294. }
  295. }
  296. }
  297. }
  298. // push sends a new work task to currently live miner agents.
  299. func (self *worker) push(work *Work) {
  300. if atomic.LoadInt32(&self.mining) != 1 {
  301. return
  302. }
  303. for agent := range self.agents {
  304. atomic.AddInt32(&self.atWork, 1)
  305. if ch := agent.Work(); ch != nil {
  306. ch <- work
  307. }
  308. }
  309. }
  310. // makeCurrent creates a new environment for the current cycle.
  311. func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error {
  312. state, err := self.chain.StateAt(parent.Root())
  313. if err != nil {
  314. return err
  315. }
  316. work := &Work{
  317. config: self.config,
  318. signer: types.NewEIP155Signer(self.config.ChainId),
  319. state: state,
  320. ancestors: set.New(),
  321. family: set.New(),
  322. uncles: set.New(),
  323. header: header,
  324. createdAt: time.Now(),
  325. }
  326. // when 08 is processed ancestors contain 07 (quick block)
  327. for _, ancestor := range self.chain.GetBlocksFromHash(parent.Hash(), 7) {
  328. for _, uncle := range ancestor.Uncles() {
  329. work.family.Add(uncle.Hash())
  330. }
  331. work.family.Add(ancestor.Hash())
  332. work.ancestors.Add(ancestor.Hash())
  333. }
  334. accounts := self.eth.AccountManager().Accounts()
  335. // Keep track of transactions which return errors so they can be removed
  336. work.tcount = 0
  337. work.ownedAccounts = accountAddressesSet(accounts)
  338. self.current = work
  339. return nil
  340. }
  341. func (w *worker) setGasPrice(p *big.Int) {
  342. w.mu.Lock()
  343. defer w.mu.Unlock()
  344. // calculate the minimal gas price the miner accepts when sorting out transactions.
  345. const pct = int64(90)
  346. w.gasPrice = gasprice(p, pct)
  347. w.mux.Post(core.GasPriceChanged{Price: w.gasPrice})
  348. }
  349. func (self *worker) commitNewWork() {
  350. self.mu.Lock()
  351. defer self.mu.Unlock()
  352. self.uncleMu.Lock()
  353. defer self.uncleMu.Unlock()
  354. self.currentMu.Lock()
  355. defer self.currentMu.Unlock()
  356. tstart := time.Now()
  357. parent := self.chain.CurrentBlock()
  358. tstamp := tstart.Unix()
  359. if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) >= 0 {
  360. tstamp = parent.Time().Int64() + 1
  361. }
  362. // this will ensure we're not going off too far in the future
  363. if now := time.Now().Unix(); tstamp > now+4 {
  364. wait := time.Duration(tstamp-now) * time.Second
  365. glog.V(logger.Info).Infoln("We are too far in the future. Waiting for", wait)
  366. time.Sleep(wait)
  367. }
  368. num := parent.Number()
  369. header := &types.Header{
  370. ParentHash: parent.Hash(),
  371. Number: num.Add(num, common.Big1),
  372. Difficulty: core.CalcDifficulty(self.config, uint64(tstamp), parent.Time().Uint64(), parent.Number(), parent.Difficulty()),
  373. GasLimit: core.CalcGasLimit(parent),
  374. GasUsed: new(big.Int),
  375. Coinbase: self.coinbase,
  376. Extra: self.extra,
  377. Time: big.NewInt(tstamp),
  378. }
  379. // If we are care about TheDAO hard-fork check whether to override the extra-data or not
  380. if daoBlock := self.config.DAOForkBlock; daoBlock != nil {
  381. // Check whether the block is among the fork extra-override range
  382. limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
  383. if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
  384. // Depending whether we support or oppose the fork, override differently
  385. if self.config.DAOForkSupport {
  386. header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
  387. } else if bytes.Compare(header.Extra, params.DAOForkBlockExtra) == 0 {
  388. header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
  389. }
  390. }
  391. }
  392. // Could potentially happen if starting to mine in an odd state.
  393. err := self.makeCurrent(parent, header)
  394. if err != nil {
  395. glog.V(logger.Info).Infoln("Could not create new env for mining, retrying on next block.")
  396. return
  397. }
  398. // Create the current work task and check any fork transitions needed
  399. work := self.current
  400. if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 {
  401. core.ApplyDAOHardFork(work.state)
  402. }
  403. pending, err := self.eth.TxPool().Pending()
  404. if err != nil {
  405. glog.Errorf("Could not fetch pending transactions: %v", err)
  406. return
  407. }
  408. txs := types.NewTransactionsByPriceAndNonce(pending)
  409. work.commitTransactions(self.mux, txs, self.gasPrice, self.chain)
  410. self.eth.TxPool().RemoveBatch(work.lowGasTxs)
  411. self.eth.TxPool().RemoveBatch(work.failedTxs)
  412. // compute uncles for the new block.
  413. var (
  414. uncles []*types.Header
  415. badUncles []common.Hash
  416. )
  417. for hash, uncle := range self.possibleUncles {
  418. if len(uncles) == 2 {
  419. break
  420. }
  421. if err := self.commitUncle(work, uncle.Header()); err != nil {
  422. if glog.V(logger.Ridiculousness) {
  423. glog.V(logger.Detail).Infof("Bad uncle found and will be removed (%x)\n", hash[:4])
  424. glog.V(logger.Detail).Infoln(uncle)
  425. }
  426. badUncles = append(badUncles, hash)
  427. } else {
  428. glog.V(logger.Debug).Infof("commiting %x as uncle\n", hash[:4])
  429. uncles = append(uncles, uncle.Header())
  430. }
  431. }
  432. for _, hash := range badUncles {
  433. delete(self.possibleUncles, hash)
  434. }
  435. if atomic.LoadInt32(&self.mining) == 1 {
  436. // commit state root after all state transitions.
  437. core.AccumulateRewards(work.state, header, uncles)
  438. header.Root = work.state.IntermediateRoot(self.config.IsEIP158(header.Number))
  439. }
  440. // create the new block whose nonce will be mined.
  441. work.Block = types.NewBlock(header, work.txs, uncles, work.receipts)
  442. // We only care about logging if we're actually mining.
  443. if atomic.LoadInt32(&self.mining) == 1 {
  444. glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", work.Block.Number(), work.tcount, len(uncles), time.Since(tstart))
  445. self.unconfirmed.Shift(work.Block.NumberU64() - 1)
  446. }
  447. self.push(work)
  448. }
  449. func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
  450. hash := uncle.Hash()
  451. if work.uncles.Has(hash) {
  452. return core.UncleError("Uncle not unique")
  453. }
  454. if !work.ancestors.Has(uncle.ParentHash) {
  455. return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
  456. }
  457. if work.family.Has(hash) {
  458. return core.UncleError(fmt.Sprintf("Uncle already in family (%x)", hash))
  459. }
  460. work.uncles.Add(uncle.Hash())
  461. return nil
  462. }
  463. func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, gasPrice *big.Int, bc *core.BlockChain) {
  464. gp := new(core.GasPool).AddGas(env.header.GasLimit)
  465. var coalescedLogs []*types.Log
  466. for {
  467. // Retrieve the next transaction and abort if all done
  468. tx := txs.Peek()
  469. if tx == nil {
  470. break
  471. }
  472. // Error may be ignored here. The error has already been checked
  473. // during transaction acceptance is the transaction pool.
  474. //
  475. // We use the eip155 signer regardless of the current hf.
  476. from, _ := types.Sender(env.signer, tx)
  477. // Check whether the tx is replay protected. If we're not in the EIP155 hf
  478. // phase, start ignoring the sender until we do.
  479. if tx.Protected() && !env.config.IsEIP155(env.header.Number) {
  480. glog.V(logger.Detail).Infof("Transaction (%x) is replay protected, but we haven't yet hardforked. Transaction will be ignored until we hardfork.\n", tx.Hash())
  481. txs.Pop()
  482. continue
  483. }
  484. // Ignore any transactions (and accounts subsequently) with low gas limits
  485. if tx.GasPrice().Cmp(gasPrice) < 0 && !env.ownedAccounts.Has(from) {
  486. // Pop the current low-priced transaction without shifting in the next from the account
  487. glog.V(logger.Info).Infof("Transaction (%x) below gas price (tx=%v ask=%v). All sequential txs from this address(%x) will be ignored\n", tx.Hash().Bytes()[:4], common.CurrencyToString(tx.GasPrice()), common.CurrencyToString(gasPrice), from[:4])
  488. env.lowGasTxs = append(env.lowGasTxs, tx)
  489. txs.Pop()
  490. continue
  491. }
  492. // Start executing the transaction
  493. env.state.StartRecord(tx.Hash(), common.Hash{}, env.tcount)
  494. err, logs := env.commitTransaction(tx, bc, gp)
  495. switch {
  496. case core.IsGasLimitErr(err):
  497. // Pop the current out-of-gas transaction without shifting in the next from the account
  498. glog.V(logger.Detail).Infof("Gas limit reached for (%x) in this block. Continue to try smaller txs\n", from[:4])
  499. txs.Pop()
  500. case err != nil:
  501. // Pop the current failed transaction without shifting in the next from the account
  502. glog.V(logger.Detail).Infof("Transaction (%x) failed, will be removed: %v\n", tx.Hash().Bytes()[:4], err)
  503. env.failedTxs = append(env.failedTxs, tx)
  504. txs.Pop()
  505. default:
  506. // Everything ok, collect the logs and shift in the next transaction from the same account
  507. coalescedLogs = append(coalescedLogs, logs...)
  508. env.tcount++
  509. txs.Shift()
  510. }
  511. }
  512. if len(coalescedLogs) > 0 || env.tcount > 0 {
  513. // make a copy, the state caches the logs and these logs get "upgraded" from pending to mined
  514. // logs by filling in the block hash when the block was mined by the local miner. This can
  515. // cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed.
  516. cpy := make([]*types.Log, len(coalescedLogs))
  517. for i, l := range coalescedLogs {
  518. cpy[i] = new(types.Log)
  519. *cpy[i] = *l
  520. }
  521. go func(logs []*types.Log, tcount int) {
  522. if len(logs) > 0 {
  523. mux.Post(core.PendingLogsEvent{Logs: logs})
  524. }
  525. if tcount > 0 {
  526. mux.Post(core.PendingStateEvent{})
  527. }
  528. }(cpy, env.tcount)
  529. }
  530. }
  531. func (env *Work) commitTransaction(tx *types.Transaction, bc *core.BlockChain, gp *core.GasPool) (error, []*types.Log) {
  532. snap := env.state.Snapshot()
  533. receipt, _, err := core.ApplyTransaction(env.config, bc, gp, env.state, env.header, tx, env.header.GasUsed, vm.Config{})
  534. if err != nil {
  535. env.state.RevertToSnapshot(snap)
  536. return err, nil
  537. }
  538. env.txs = append(env.txs, tx)
  539. env.receipts = append(env.receipts, receipt)
  540. return nil, receipt.Logs
  541. }
  542. // TODO: remove or use
  543. func (self *worker) HashRate() int64 {
  544. return 0
  545. }
  546. // gasprice calculates a reduced gas price based on the pct
  547. // XXX Use big.Rat?
  548. func gasprice(price *big.Int, pct int64) *big.Int {
  549. p := new(big.Int).Set(price)
  550. p.Div(p, big.NewInt(100))
  551. p.Mul(p, big.NewInt(pct))
  552. return p
  553. }
  554. func accountAddressesSet(accounts []accounts.Account) *set.Set {
  555. accountSet := set.New()
  556. for _, account := range accounts {
  557. accountSet.Add(account.Address)
  558. }
  559. return accountSet
  560. }