github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/native/event.go (about) 1 package native 2 3 import ( 4 "crypto/sha256" 5 6 "github.com/unicornultrafoundation/go-helios/common/bigendian" 7 "github.com/unicornultrafoundation/go-helios/hash" 8 "github.com/unicornultrafoundation/go-helios/native/dag" 9 "github.com/unicornultrafoundation/go-helios/native/idx" 10 "github.com/unicornultrafoundation/go-u2u/core/types" 11 "github.com/unicornultrafoundation/go-u2u/rlp" 12 "github.com/unicornultrafoundation/go-u2u/trie" 13 ) 14 15 type EventI interface { 16 dag.Event 17 Version() uint8 18 NetForkID() uint16 19 CreationTime() Timestamp 20 MedianTime() Timestamp 21 PrevEpochHash() *hash.Hash 22 Extra() []byte 23 GasPowerLeft() GasPowerLeft 24 GasPowerUsed() uint64 25 26 HashToSign() hash.Hash 27 Locator() EventLocator 28 29 // Payload-related fields 30 31 AnyTxs() bool 32 AnyBlockVotes() bool 33 AnyEpochVote() bool 34 AnyMisbehaviourProofs() bool 35 PayloadHash() hash.Hash 36 } 37 38 type EventLocator struct { 39 BaseHash hash.Hash 40 NetForkID uint16 41 Epoch idx.Epoch 42 Seq idx.Event 43 Lamport idx.Lamport 44 Creator idx.ValidatorID 45 PayloadHash hash.Hash 46 } 47 48 type SignedEventLocator struct { 49 Locator EventLocator 50 Sig Signature 51 } 52 53 func AsSignedEventLocator(e EventPayloadI) SignedEventLocator { 54 return SignedEventLocator{ 55 Locator: e.Locator(), 56 Sig: e.Sig(), 57 } 58 } 59 60 type EventPayloadI interface { 61 EventI 62 Sig() Signature 63 64 Txs() types.Transactions 65 EpochVote() LlrEpochVote 66 BlockVotes() LlrBlockVotes 67 MisbehaviourProofs() []MisbehaviourProof 68 } 69 70 var emptyPayloadHash1 = CalcPayloadHash(&MutableEventPayload{extEventData: extEventData{version: 1}}) 71 72 func EmptyPayloadHash(version uint8) hash.Hash { 73 if version == 0 { 74 return hash.Hash(types.EmptyRootHash) 75 } else { 76 return emptyPayloadHash1 77 } 78 } 79 80 type baseEvent struct { 81 dag.BaseEvent 82 } 83 84 type mutableBaseEvent struct { 85 dag.MutableBaseEvent 86 } 87 88 type extEventData struct { 89 version uint8 90 netForkID uint16 91 creationTime Timestamp 92 medianTime Timestamp 93 prevEpochHash *hash.Hash 94 gasPowerLeft GasPowerLeft 95 gasPowerUsed uint64 96 extra []byte 97 98 anyTxs bool 99 anyBlockVotes bool 100 anyEpochVote bool 101 anyMisbehaviourProofs bool 102 payloadHash hash.Hash 103 } 104 105 type sigData struct { 106 sig Signature 107 } 108 109 type payloadData struct { 110 txs types.Transactions 111 misbehaviourProofs []MisbehaviourProof 112 113 epochVote LlrEpochVote 114 blockVotes LlrBlockVotes 115 } 116 117 type Event struct { 118 baseEvent 119 extEventData 120 121 // cache 122 _baseHash *hash.Hash 123 _locatorHash *hash.Hash 124 } 125 126 type SignedEvent struct { 127 Event 128 sigData 129 } 130 131 type EventPayload struct { 132 SignedEvent 133 payloadData 134 135 // cache 136 _size int 137 } 138 139 type MutableEventPayload struct { 140 mutableBaseEvent 141 extEventData 142 sigData 143 payloadData 144 } 145 146 func (e *Event) HashToSign() hash.Hash { 147 return *e._locatorHash 148 } 149 150 func asLocator(basehash hash.Hash, e EventI) EventLocator { 151 return EventLocator{ 152 BaseHash: basehash, 153 NetForkID: e.NetForkID(), 154 Epoch: e.Epoch(), 155 Seq: e.Seq(), 156 Lamport: e.Lamport(), 157 Creator: e.Creator(), 158 PayloadHash: e.PayloadHash(), 159 } 160 } 161 162 func (e *Event) Locator() EventLocator { 163 return asLocator(*e._baseHash, e) 164 } 165 166 func (e *EventPayload) Size() int { 167 return e._size 168 } 169 170 func (e *extEventData) Version() uint8 { return e.version } 171 172 func (e *extEventData) NetForkID() uint16 { return e.netForkID } 173 174 func (e *extEventData) CreationTime() Timestamp { return e.creationTime } 175 176 func (e *extEventData) MedianTime() Timestamp { return e.medianTime } 177 178 func (e *extEventData) PrevEpochHash() *hash.Hash { return e.prevEpochHash } 179 180 func (e *extEventData) Extra() []byte { return e.extra } 181 182 func (e *extEventData) PayloadHash() hash.Hash { return e.payloadHash } 183 184 func (e *extEventData) AnyTxs() bool { return e.anyTxs } 185 186 func (e *extEventData) AnyMisbehaviourProofs() bool { return e.anyMisbehaviourProofs } 187 188 func (e *extEventData) AnyEpochVote() bool { return e.anyEpochVote } 189 190 func (e *extEventData) AnyBlockVotes() bool { return e.anyBlockVotes } 191 192 func (e *extEventData) GasPowerLeft() GasPowerLeft { return e.gasPowerLeft } 193 194 func (e *extEventData) GasPowerUsed() uint64 { return e.gasPowerUsed } 195 196 func (e *sigData) Sig() Signature { return e.sig } 197 198 func (e *payloadData) Txs() types.Transactions { return e.txs } 199 200 func (e *payloadData) MisbehaviourProofs() []MisbehaviourProof { return e.misbehaviourProofs } 201 202 func (e *payloadData) BlockVotes() LlrBlockVotes { return e.blockVotes } 203 204 func (e *payloadData) EpochVote() LlrEpochVote { return e.epochVote } 205 206 func CalcTxHash(txs types.Transactions) hash.Hash { 207 return hash.Hash(types.DeriveSha(txs, trie.NewStackTrie(nil))) 208 } 209 210 func CalcReceiptsHash(receipts []*types.ReceiptForStorage) hash.Hash { 211 hasher := sha256.New() 212 _ = rlp.Encode(hasher, receipts) 213 return hash.BytesToHash(hasher.Sum(nil)) 214 } 215 216 func CalcMisbehaviourProofsHash(mps []MisbehaviourProof) hash.Hash { 217 hasher := sha256.New() 218 _ = rlp.Encode(hasher, mps) 219 return hash.BytesToHash(hasher.Sum(nil)) 220 } 221 222 func CalcPayloadHash(e EventPayloadI) hash.Hash { 223 if e.Version() == 0 { 224 return CalcTxHash(e.Txs()) 225 } 226 return hash.Of(hash.Of(CalcTxHash(e.Txs()).Bytes(), CalcMisbehaviourProofsHash(e.MisbehaviourProofs()).Bytes()).Bytes(), hash.Of(e.EpochVote().Hash().Bytes(), e.BlockVotes().Hash().Bytes()).Bytes()) 227 } 228 229 func (e *MutableEventPayload) SetVersion(v uint8) { e.version = v } 230 231 func (e *MutableEventPayload) SetNetForkID(v uint16) { e.netForkID = v } 232 233 func (e *MutableEventPayload) SetCreationTime(v Timestamp) { e.creationTime = v } 234 235 func (e *MutableEventPayload) SetMedianTime(v Timestamp) { e.medianTime = v } 236 237 func (e *MutableEventPayload) SetPrevEpochHash(v *hash.Hash) { e.prevEpochHash = v } 238 239 func (e *MutableEventPayload) SetExtra(v []byte) { e.extra = v } 240 241 func (e *MutableEventPayload) SetPayloadHash(v hash.Hash) { e.payloadHash = v } 242 243 func (e *MutableEventPayload) SetGasPowerLeft(v GasPowerLeft) { e.gasPowerLeft = v } 244 245 func (e *MutableEventPayload) SetGasPowerUsed(v uint64) { e.gasPowerUsed = v } 246 247 func (e *MutableEventPayload) SetSig(v Signature) { e.sig = v } 248 249 func (e *MutableEventPayload) SetTxs(v types.Transactions) { 250 e.txs = v 251 e.anyTxs = len(v) != 0 252 } 253 254 func (e *MutableEventPayload) SetMisbehaviourProofs(v []MisbehaviourProof) { 255 e.misbehaviourProofs = v 256 e.anyMisbehaviourProofs = len(v) != 0 257 } 258 259 func (e *MutableEventPayload) SetBlockVotes(v LlrBlockVotes) { 260 e.blockVotes = v 261 e.anyBlockVotes = len(v.Votes) != 0 262 } 263 264 func (e *MutableEventPayload) SetEpochVote(v LlrEpochVote) { 265 e.epochVote = v 266 e.anyEpochVote = v.Epoch != 0 && v.Vote != hash.Zero 267 } 268 269 func calcEventID(h hash.Hash) (id [24]byte) { 270 copy(id[:], h[:24]) 271 return id 272 } 273 274 func calcEventHashes(ser []byte, e EventI) (locator hash.Hash, base hash.Hash) { 275 base = hash.Of(ser) 276 if e.Version() < 1 { 277 return base, base 278 } 279 return asLocator(base, e).HashToSign(), base 280 } 281 282 func (e *MutableEventPayload) calcHashes() (locator hash.Hash, base hash.Hash) { 283 b, _ := e.immutable().Event.MarshalBinary() 284 return calcEventHashes(b, e) 285 } 286 287 func (e *MutableEventPayload) size() int { 288 b, err := e.immutable().MarshalBinary() 289 if err != nil { 290 panic("can't encode: " + err.Error()) 291 } 292 return len(b) 293 } 294 295 func (e *MutableEventPayload) HashToSign() hash.Hash { 296 h, _ := e.calcHashes() 297 return h 298 } 299 300 func (e *MutableEventPayload) Locator() EventLocator { 301 _, baseHash := e.calcHashes() 302 return asLocator(baseHash, e) 303 } 304 305 func (e *MutableEventPayload) Size() int { 306 return e.size() 307 } 308 309 func (e *MutableEventPayload) build(locatorHash hash.Hash, baseHash hash.Hash, size int) *EventPayload { 310 return &EventPayload{ 311 SignedEvent: SignedEvent{ 312 Event: Event{ 313 baseEvent: baseEvent{*e.MutableBaseEvent.Build(calcEventID(locatorHash))}, 314 extEventData: e.extEventData, 315 _baseHash: &baseHash, 316 _locatorHash: &locatorHash, 317 }, 318 sigData: e.sigData, 319 }, 320 payloadData: e.payloadData, 321 _size: size, 322 } 323 } 324 325 func (e *MutableEventPayload) immutable() *EventPayload { 326 return e.build(hash.Hash{}, hash.Hash{}, 0) 327 } 328 329 func (e *MutableEventPayload) Build() *EventPayload { 330 locatorHash, baseHash := e.calcHashes() 331 payloadSer, _ := e.immutable().MarshalBinary() 332 return e.build(locatorHash, baseHash, len(payloadSer)) 333 } 334 335 func (l EventLocator) HashToSign() hash.Hash { 336 return hash.Of(l.BaseHash.Bytes(), bigendian.Uint16ToBytes(l.NetForkID), l.Epoch.Bytes(), l.Seq.Bytes(), l.Lamport.Bytes(), l.Creator.Bytes(), l.PayloadHash.Bytes()) 337 } 338 339 func (l EventLocator) ID() hash.Event { 340 h := l.HashToSign() 341 copy(h[0:4], l.Epoch.Bytes()) 342 copy(h[4:8], l.Lamport.Bytes()) 343 return hash.Event(h) 344 }