tx_cacher.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2018 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 core
  17. import (
  18. "runtime"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. )
  21. // senderCacher is a concurrent transaction sender recoverer and cacher.
  22. var senderCacher = newTxSenderCacher(runtime.NumCPU())
  23. // txSenderCacherRequest is a request for recovering transaction senders with a
  24. // specific signature scheme and caching it into the transactions themselves.
  25. //
  26. // The inc field defines the number of transactions to skip after each recovery,
  27. // which is used to feed the same underlying input array to different threads but
  28. // ensure they process the early transactions fast.
  29. type txSenderCacherRequest struct {
  30. signer types.Signer
  31. txs []*types.Transaction
  32. inc int
  33. }
  34. // txSenderCacher is a helper structure to concurrently ecrecover transaction
  35. // senders from digital signatures on background threads.
  36. type txSenderCacher struct {
  37. threads int
  38. tasks chan *txSenderCacherRequest
  39. }
  40. // newTxSenderCacher creates a new transaction sender background cacher and starts
  41. // as many processing goroutines as allowed by the GOMAXPROCS on construction.
  42. func newTxSenderCacher(threads int) *txSenderCacher {
  43. cacher := &txSenderCacher{
  44. tasks: make(chan *txSenderCacherRequest, threads),
  45. threads: threads,
  46. }
  47. for i := 0; i < threads; i++ {
  48. go cacher.cache()
  49. }
  50. return cacher
  51. }
  52. // cache is an infinite loop, caching transaction senders from various forms of
  53. // data structures.
  54. func (cacher *txSenderCacher) cache() {
  55. for task := range cacher.tasks {
  56. for i := 0; i < len(task.txs); i += task.inc {
  57. types.Sender(task.signer, task.txs[i])
  58. }
  59. }
  60. }
  61. // recover recovers the senders from a batch of transactions and caches them
  62. // back into the same data structures. There is no validation being done, nor
  63. // any reaction to invalid signatures. That is up to calling code later.
  64. func (cacher *txSenderCacher) recover(signer types.Signer, txs []*types.Transaction) {
  65. // If there's nothing to recover, abort
  66. if len(txs) == 0 {
  67. return
  68. }
  69. // Ensure we have meaningful task sizes and schedule the recoveries
  70. tasks := cacher.threads
  71. if len(txs) < tasks*4 {
  72. tasks = (len(txs) + 3) / 4
  73. }
  74. for i := 0; i < tasks; i++ {
  75. cacher.tasks <- &txSenderCacherRequest{
  76. signer: signer,
  77. txs: txs[i:],
  78. inc: tasks,
  79. }
  80. }
  81. }
  82. // recoverFromBlocks recovers the senders from a batch of blocks and caches them
  83. // back into the same data structures. There is no validation being done, nor
  84. // any reaction to invalid signatures. That is up to calling code later.
  85. func (cacher *txSenderCacher) recoverFromBlocks(signer types.Signer, blocks []*types.Block) {
  86. count := 0
  87. for _, block := range blocks {
  88. count += len(block.Transactions())
  89. }
  90. txs := make([]*types.Transaction, 0, count)
  91. for _, block := range blocks {
  92. txs = append(txs, block.Transactions()...)
  93. }
  94. cacher.recover(signer, txs)
  95. }