github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/store_llr_epoch.go (about) 1 package gossip 2 3 import ( 4 "github.com/unicornultrafoundation/go-helios/hash" 5 "github.com/unicornultrafoundation/go-helios/native/idx" 6 "github.com/unicornultrafoundation/go-helios/native/pos" 7 "github.com/unicornultrafoundation/go-u2u/common" 8 "github.com/unicornultrafoundation/go-u2u/rlp" 9 10 "github.com/unicornultrafoundation/go-u2u/native" 11 ) 12 13 const ( 14 maxEpochPackVotes = 10000 15 ) 16 17 func (s *Store) SetEpochVote(ev native.LlrSignedEpochVote) { 18 s.rlp.Set(s.table.LlrEpochVotes, append(ev.Val.Epoch.Bytes(), ev.Signed.Locator.ID().Bytes()...), &ev) 19 } 20 21 func (s *Store) HasEpochVote(epoch idx.Epoch, id hash.Event) bool { 22 ok, _ := s.table.LlrEpochVotes.Has(append(epoch.Bytes(), id.Bytes()...)) 23 return ok 24 } 25 26 func (s *Store) iterateEpochVotesRLP(prefix []byte, f func(ev rlp.RawValue) bool) { 27 it := s.table.LlrEpochVotes.NewIterator(prefix, nil) 28 defer it.Release() 29 for it.Next() { 30 if !f(common.CopyBytes(it.Value())) { 31 break 32 } 33 } 34 } 35 36 func (s *Store) flushLlrEpochVoteWeight(cKey VotesCacheID, value VotesCacheValue) { 37 key := append(cKey.Epoch.Bytes(), cKey.V[:]...) 38 s.flushLlrVoteWeight(s.table.LlrEpochVoteIndex, key, value.weight, value.set) 39 } 40 41 func (s *Store) AddLlrEpochVoteWeight(epoch idx.Epoch, ev hash.Hash, val idx.Validator, vals idx.Validator, diff pos.Weight) pos.Weight { 42 key := append(epoch.Bytes(), ev[:]...) 43 cKey := VotesCacheID{ 44 Block: 0, 45 Epoch: epoch, 46 V: ev, 47 } 48 return s.addLlrVoteWeight(s.cache.LlrEpochVoteIndex, s.table.LlrEpochVoteIndex, cKey, key, val, vals, diff) 49 } 50 51 func (s *Store) SetLlrEpochResult(epoch idx.Epoch, ev hash.Hash) { 52 err := s.table.LlrEpochResults.Put(epoch.Bytes(), ev.Bytes()) 53 if err != nil { 54 s.Log.Crit("Failed to put key-value", "err", err) 55 } 56 } 57 58 func (s *Store) GetLlrEpochResult(epoch idx.Epoch) *hash.Hash { 59 evB, err := s.table.LlrEpochResults.Get(epoch.Bytes()) 60 if err != nil { 61 s.Log.Crit("Failed to get key-value", "err", err) 62 } 63 if evB == nil { 64 return nil 65 } 66 ev := hash.BytesToHash(evB) 67 return &ev 68 } 69 70 type LlrIdxFullEpochRecordRLP struct { 71 RecordRLP rlp.RawValue 72 Idx idx.Epoch 73 } 74 75 type LlrEpochPackRLP struct { 76 VotesRLP []rlp.RawValue 77 Record LlrIdxFullEpochRecordRLP 78 } 79 80 func (s *Store) IterateEpochPacksRLP(start idx.Epoch, f func(epoch idx.Epoch, ep rlp.RawValue) bool) { 81 for epoch := start; ; epoch++ { 82 key := epoch.Bytes() 83 val, err := s.table.BlockEpochStateHistory.Get(key) 84 if err != nil { 85 s.Log.Crit("Failed to get block-epoch state", "err", err) 86 } 87 if val == nil { 88 break 89 } 90 evs := make([]rlp.RawValue, 0, 20) 91 s.iterateEpochVotesRLP(key, func(ev rlp.RawValue) bool { 92 evs = append(evs, ev) 93 return len(evs) < maxEpochPackVotes 94 }) 95 if len(evs) == 0 { 96 continue 97 } 98 ep := &LlrEpochPackRLP{ 99 VotesRLP: evs, 100 Record: LlrIdxFullEpochRecordRLP{ 101 RecordRLP: val, 102 Idx: epoch, 103 }, 104 } 105 encoded, err := rlp.EncodeToBytes(ep) 106 if err != nil { 107 s.Log.Crit("Failed to encode BR", "err", err) 108 } 109 if !f(epoch, encoded) { 110 break 111 } 112 } 113 }