github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/blockproc/eventmodule/confirmed_events_processor.go (about)

     1  package eventmodule
     2  
     3  import (
     4  	"github.com/unicornultrafoundation/go-u2u/gossip/blockproc"
     5  	"github.com/unicornultrafoundation/go-u2u/native"
     6  	"github.com/unicornultrafoundation/go-u2u/native/iblockproc"
     7  )
     8  
     9  type ValidatorEventsModule struct{}
    10  
    11  func New() *ValidatorEventsModule {
    12  	return &ValidatorEventsModule{}
    13  }
    14  
    15  func (m *ValidatorEventsModule) Start(bs iblockproc.BlockState, es iblockproc.EpochState) blockproc.ConfirmedEventsProcessor {
    16  	return &ValidatorEventsProcessor{
    17  		es:                     es,
    18  		bs:                     bs,
    19  		validatorHighestEvents: make(native.EventIs, es.Validators.Len()),
    20  	}
    21  }
    22  
    23  type ValidatorEventsProcessor struct {
    24  	es                     iblockproc.EpochState
    25  	bs                     iblockproc.BlockState
    26  	validatorHighestEvents native.EventIs
    27  }
    28  
    29  func (p *ValidatorEventsProcessor) ProcessConfirmedEvent(e native.EventI) {
    30  	creatorIdx := p.es.Validators.GetIdx(e.Creator())
    31  	prev := p.validatorHighestEvents[creatorIdx]
    32  	if prev == nil || e.Seq() > prev.Seq() {
    33  		p.validatorHighestEvents[creatorIdx] = e
    34  	}
    35  	p.bs.EpochGas += e.GasPowerUsed()
    36  }
    37  
    38  func (p *ValidatorEventsProcessor) Finalize(block iblockproc.BlockCtx, _ bool) iblockproc.BlockState {
    39  	for _, v := range p.bs.EpochCheaters {
    40  		creatorIdx := p.es.Validators.GetIdx(v)
    41  		p.validatorHighestEvents[creatorIdx] = nil
    42  	}
    43  	for creatorIdx, e := range p.validatorHighestEvents {
    44  		if e == nil {
    45  			continue
    46  		}
    47  		info := p.bs.ValidatorStates[creatorIdx]
    48  		if block.Idx <= info.LastBlock+p.es.Rules.Economy.BlockMissedSlack {
    49  			prevOnlineTime := info.LastOnlineTime
    50  			if p.es.Rules.Upgrades.Berlin {
    51  				prevOnlineTime = native.MaxTimestamp(info.LastOnlineTime, p.es.EpochStart)
    52  			}
    53  			if e.MedianTime() > prevOnlineTime {
    54  				info.Uptime += e.MedianTime() - prevOnlineTime
    55  			}
    56  		}
    57  		info.LastGasPowerLeft = e.GasPowerLeft()
    58  		info.LastOnlineTime = e.MedianTime()
    59  		info.LastBlock = block.Idx
    60  		info.LastEvent = iblockproc.EventInfo{
    61  			ID:           e.ID(),
    62  			GasPowerLeft: e.GasPowerLeft(),
    63  			Time:         e.MedianTime(),
    64  		}
    65  		p.bs.ValidatorStates[creatorIdx] = info
    66  	}
    67  	return p.bs
    68  }