github.com/klaytn/klaytn@v1.12.1/blockchain/tx_cacher.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2018 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/tx_cacher.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package blockchain 22 23 import ( 24 "math" 25 "runtime" 26 27 "github.com/klaytn/klaytn/blockchain/types" 28 ) 29 30 // senderCacher is a concurrent transaction sender recoverer and cacher. 31 var senderCacher = newTxSenderCacher(calcNumSenderCachers()) 32 33 func calcNumSenderCachers() int { 34 numWorkers := math.Ceil(float64(runtime.NumCPU()) * 2.0 / 3.0) 35 return int(numWorkers) 36 } 37 38 // txSenderCacherRequest is a request for recovering transaction senders with a 39 // specific signature scheme and caching it into the transactions themselves. 40 // 41 // The inc field defines the number of transactions to skip after each recovery, 42 // which is used to feed the same underlying input array to different threads but 43 // ensure they process the early transactions fast. 44 type txSenderCacherRequest struct { 45 signer types.Signer 46 txs []*types.Transaction 47 inc int 48 } 49 50 // txSenderCacher is a helper structure to concurrently ecrecover transaction 51 // senders from digital signatures on background threads. 52 type txSenderCacher struct { 53 threads int 54 tasks chan *txSenderCacherRequest 55 } 56 57 // newTxSenderCacher creates a new transaction sender background cacher and starts 58 // as many procesing goroutines as allowed by the GOMAXPROCS on construction. 59 func newTxSenderCacher(threads int) *txSenderCacher { 60 cacher := &txSenderCacher{ 61 tasks: make(chan *txSenderCacherRequest, threads), 62 threads: threads, 63 } 64 for i := 0; i < threads; i++ { 65 go cacher.cache() 66 } 67 return cacher 68 } 69 70 // cacheSender calls SenderXXX functions based on the tx types. 71 // If a legacy transaction, it calls SenderFrom() to cache an address into `Transaction.from`. 72 // Otherwise, it calls SenderPubkey() to cache a pubkey into `Transaction.from`. 73 // In addition, if a transaction is a fee-delegated transaction, it also caches a pubkey into `Transaction.feePayer`. 74 func cacheSender(signer types.Signer, tx *types.Transaction) { 75 if tx.IsEthereumTransaction() { 76 types.SenderFrom(signer, tx) 77 return 78 } 79 80 types.SenderPubkey(signer, tx) 81 82 if tx.IsFeeDelegatedTransaction() { 83 types.SenderFeePayerPubkey(signer, tx) 84 } 85 } 86 87 // cache is an infinite loop, caching transaction senders from various forms of 88 // data structures. 89 func (cacher *txSenderCacher) cache() { 90 for task := range cacher.tasks { 91 for i := 0; i < len(task.txs); i += task.inc { 92 cacheSender(task.signer, task.txs[i]) 93 } 94 } 95 } 96 97 // recover recovers the senders from a batch of transactions and caches them 98 // back into the same data structures. There is no validation being done, nor 99 // any reaction to invalid signatures. That is up to calling code later. 100 func (cacher *txSenderCacher) recover(signer types.Signer, txs []*types.Transaction) { 101 // If there's nothing to recover, abort 102 if len(txs) == 0 { 103 return 104 } 105 // Ensure we have meaningful task sizes and schedule the recoveries 106 tasks := cacher.threads 107 if len(txs) < tasks*4 { 108 tasks = (len(txs) + 3) / 4 109 } 110 for i := 0; i < tasks; i++ { 111 cacher.tasks <- &txSenderCacherRequest{ 112 signer: signer, 113 txs: txs[i:], 114 inc: tasks, 115 } 116 } 117 } 118 119 // recoverFromBlocks recovers the senders from a batch of blocks and caches them 120 // back into the same data structures. There is no validation being done, nor 121 // any reaction to invalid signatures. That is up to calling code later. 122 func (cacher *txSenderCacher) recoverFromBlocks(signer types.Signer, blocks []*types.Block) { 123 count := 0 124 for _, block := range blocks { 125 count += len(block.Transactions()) 126 } 127 txs := make([]*types.Transaction, 0, count) 128 for _, block := range blocks { 129 txs = append(txs, block.Transactions()...) 130 } 131 cacher.recover(signer, txs) 132 }