From 13cf5c1e6e1b06554534b644685fbcecd3d2f362 Mon Sep 17 00:00:00 2001 From: paladz <453256728@qq.com> Date: Fri, 29 Nov 2019 16:06:44 +0800 Subject: [PATCH] edit for code review --- protocol/validation/tx.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/protocol/validation/tx.go b/protocol/validation/tx.go index 7b7cb36f..face9b5c 100644 --- a/protocol/validation/tx.go +++ b/protocol/validation/tx.go @@ -541,17 +541,12 @@ func (r *ValidateTxResult) GetError() error { return r.err } -func validateTxWorker(workCh chan *validateTxWork, resultCh chan *ValidateTxResult, closeCh chan struct{}, wg *sync.WaitGroup) { - for { - select { - case work := <-workCh: - gasStatus, err := ValidateTx(work.tx, work.block) - resultCh <- &ValidateTxResult{i: work.i, gasStatus: gasStatus, err: err} - case <-closeCh: - wg.Done() - return - } +func validateTxWorker(workCh chan *validateTxWork, resultCh chan *ValidateTxResult, wg *sync.WaitGroup) { + for work := range workCh { + gasStatus, err := ValidateTx(work.tx, work.block) + resultCh <- &ValidateTxResult{i: work.i, gasStatus: gasStatus, err: err} } + wg.Done() } // ValidateTxs validates txs in async mode @@ -562,16 +557,16 @@ func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult { var wg sync.WaitGroup workCh := make(chan *validateTxWork, txSize) resultCh := make(chan *ValidateTxResult, txSize) - closeCh := make(chan struct{}) for i := 0; i <= validateWorkerNum && i < txSize; i++ { wg.Add(1) - go validateTxWorker(workCh, resultCh, closeCh, &wg) + go validateTxWorker(workCh, resultCh, &wg) } //sent the works for i, tx := range txs { workCh <- &validateTxWork{i: i, tx: tx, block: block} } + close(workCh) //collect validate results results := make([]*ValidateTxResult, txSize) @@ -580,7 +575,6 @@ func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult { results[result.i] = result } - close(closeCh) wg.Wait() return results } -- 2.11.0