github.com/phillinzzz/newBsc@v1.1.6/core/receipt_processor.go (about)

     1  package core
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  
     7  	"github.com/phillinzzz/newBsc/core/types"
     8  )
     9  
    10  type ReceiptProcessor interface {
    11  	Apply(receipt *types.Receipt)
    12  }
    13  
    14  var (
    15  	_ ReceiptProcessor = (*ReceiptBloomGenerator)(nil)
    16  	_ ReceiptProcessor = (*AsyncReceiptBloomGenerator)(nil)
    17  )
    18  
    19  func NewReceiptBloomGenerator() *ReceiptBloomGenerator {
    20  	return &ReceiptBloomGenerator{}
    21  }
    22  
    23  type ReceiptBloomGenerator struct {
    24  }
    25  
    26  func (p *ReceiptBloomGenerator) Apply(receipt *types.Receipt) {
    27  	receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
    28  }
    29  
    30  func NewAsyncReceiptBloomGenerator(txNums int) *AsyncReceiptBloomGenerator {
    31  	generator := &AsyncReceiptBloomGenerator{
    32  		receipts: make(chan *types.Receipt, txNums),
    33  	}
    34  	generator.startWorker()
    35  	return generator
    36  }
    37  
    38  type AsyncReceiptBloomGenerator struct {
    39  	receipts chan *types.Receipt
    40  	wg       sync.WaitGroup
    41  	isClosed bool
    42  }
    43  
    44  func (p *AsyncReceiptBloomGenerator) startWorker() {
    45  	p.wg.Add(1)
    46  	go func() {
    47  		defer p.wg.Done()
    48  		for receipt := range p.receipts {
    49  			if receipt != nil && bytes.Equal(receipt.Bloom[:], types.EmptyBloom[:]) {
    50  				receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
    51  			}
    52  		}
    53  	}()
    54  }
    55  
    56  func (p *AsyncReceiptBloomGenerator) Apply(receipt *types.Receipt) {
    57  	if !p.isClosed {
    58  		p.receipts <- receipt
    59  	}
    60  }
    61  
    62  func (p *AsyncReceiptBloomGenerator) Close() {
    63  	close(p.receipts)
    64  	p.isClosed = true
    65  	p.wg.Wait()
    66  }