github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/native/iblockproc/decided_state.go (about)

     1  package iblockproc
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"math/big"
     6  
     7  	"github.com/unicornultrafoundation/go-helios/hash"
     8  	"github.com/unicornultrafoundation/go-helios/native/idx"
     9  	"github.com/unicornultrafoundation/go-helios/native/pos"
    10  	"github.com/unicornultrafoundation/go-helios/types"
    11  	"github.com/unicornultrafoundation/go-u2u/rlp"
    12  
    13  	"github.com/unicornultrafoundation/go-u2u/native"
    14  	"github.com/unicornultrafoundation/go-u2u/u2u"
    15  )
    16  
    17  type ValidatorBlockState struct {
    18  	LastEvent        EventInfo
    19  	Uptime           native.Timestamp
    20  	LastOnlineTime   native.Timestamp
    21  	LastGasPowerLeft native.GasPowerLeft
    22  	LastBlock        idx.Block
    23  	DirtyGasRefund   uint64
    24  	Originated       *big.Int
    25  }
    26  
    27  type EventInfo struct {
    28  	ID           hash.Event
    29  	GasPowerLeft native.GasPowerLeft
    30  	Time         native.Timestamp
    31  }
    32  
    33  type ValidatorEpochState struct {
    34  	GasRefund      uint64
    35  	PrevEpochEvent EventInfo
    36  }
    37  
    38  type BlockCtx struct {
    39  	Idx     idx.Block
    40  	Time    native.Timestamp
    41  	Atropos hash.Event
    42  }
    43  
    44  type BlockState struct {
    45  	LastBlock          BlockCtx
    46  	FinalizedStateRoot hash.Hash
    47  
    48  	EpochGas        uint64
    49  	EpochCheaters   types.Cheaters
    50  	CheatersWritten uint32
    51  
    52  	ValidatorStates       []ValidatorBlockState
    53  	NextValidatorProfiles ValidatorProfiles
    54  
    55  	DirtyRules *u2u.Rules `rlp:"nil"` // nil means that there's no changes compared to epoch rules
    56  
    57  	AdvanceEpochs idx.Epoch
    58  }
    59  
    60  func (bs BlockState) Copy() BlockState {
    61  	cp := bs
    62  	cp.EpochCheaters = make(types.Cheaters, len(bs.EpochCheaters))
    63  	copy(cp.EpochCheaters, bs.EpochCheaters)
    64  	cp.ValidatorStates = make([]ValidatorBlockState, len(bs.ValidatorStates))
    65  	copy(cp.ValidatorStates, bs.ValidatorStates)
    66  	for i := range cp.ValidatorStates {
    67  		cp.ValidatorStates[i].Originated = new(big.Int).Set(cp.ValidatorStates[i].Originated)
    68  	}
    69  	cp.NextValidatorProfiles = bs.NextValidatorProfiles.Copy()
    70  	if bs.DirtyRules != nil {
    71  		rules := bs.DirtyRules.Copy()
    72  		cp.DirtyRules = &rules
    73  	}
    74  	return cp
    75  }
    76  
    77  func (bs *BlockState) GetValidatorState(id idx.ValidatorID, validators *pos.Validators) *ValidatorBlockState {
    78  	validatorIdx := validators.GetIdx(id)
    79  	return &bs.ValidatorStates[validatorIdx]
    80  }
    81  
    82  func (bs BlockState) Hash() hash.Hash {
    83  	hasher := sha256.New()
    84  	err := rlp.Encode(hasher, &bs)
    85  	if err != nil {
    86  		panic("can't hash: " + err.Error())
    87  	}
    88  	return hash.BytesToHash(hasher.Sum(nil))
    89  }
    90  
    91  type EpochStateV1 struct {
    92  	Epoch          idx.Epoch
    93  	EpochStart     native.Timestamp
    94  	PrevEpochStart native.Timestamp
    95  
    96  	EpochStateRoot hash.Hash
    97  
    98  	Validators        *pos.Validators
    99  	ValidatorStates   []ValidatorEpochState
   100  	ValidatorProfiles ValidatorProfiles
   101  
   102  	Rules u2u.Rules
   103  }
   104  
   105  type EpochState EpochStateV1
   106  
   107  func (es *EpochState) GetValidatorState(id idx.ValidatorID, validators *pos.Validators) *ValidatorEpochState {
   108  	validatorIdx := validators.GetIdx(id)
   109  	return &es.ValidatorStates[validatorIdx]
   110  }
   111  
   112  func (es EpochState) Duration() native.Timestamp {
   113  	return es.EpochStart - es.PrevEpochStart
   114  }
   115  
   116  func (es EpochState) Hash() hash.Hash {
   117  	var hashed interface{}
   118  	if es.Rules.Upgrades.London {
   119  		hashed = &es
   120  	} else {
   121  		es0 := EpochStateV0{
   122  			Epoch:             es.Epoch,
   123  			EpochStart:        es.EpochStart,
   124  			PrevEpochStart:    es.PrevEpochStart,
   125  			EpochStateRoot:    es.EpochStateRoot,
   126  			Validators:        es.Validators,
   127  			ValidatorStates:   make([]ValidatorEpochStateV0, len(es.ValidatorStates)),
   128  			ValidatorProfiles: es.ValidatorProfiles,
   129  			Rules:             es.Rules,
   130  		}
   131  		for i, v := range es.ValidatorStates {
   132  			es0.ValidatorStates[i].GasRefund = v.GasRefund
   133  			es0.ValidatorStates[i].PrevEpochEvent = v.PrevEpochEvent.ID
   134  		}
   135  		hashed = &es0
   136  	}
   137  	hasher := sha256.New()
   138  	err := rlp.Encode(hasher, hashed)
   139  	if err != nil {
   140  		panic("can't hash: " + err.Error())
   141  	}
   142  	return hash.BytesToHash(hasher.Sum(nil))
   143  }
   144  
   145  func (es EpochState) Copy() EpochState {
   146  	cp := es
   147  	cp.ValidatorStates = make([]ValidatorEpochState, len(es.ValidatorStates))
   148  	copy(cp.ValidatorStates, es.ValidatorStates)
   149  	cp.ValidatorProfiles = es.ValidatorProfiles.Copy()
   150  	if es.Rules != (u2u.Rules{}) {
   151  		cp.Rules = es.Rules.Copy()
   152  	}
   153  	return cp
   154  }