github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/native/native_llr.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/idx" 9 ) 10 11 type LlrBlockVotes struct { 12 Start idx.Block 13 Epoch idx.Epoch 14 Votes []hash.Hash 15 } 16 17 func (bvs LlrBlockVotes) LastBlock() idx.Block { 18 return bvs.Start + idx.Block(len(bvs.Votes)) - 1 19 } 20 21 type LlrEpochVote struct { 22 Epoch idx.Epoch 23 Vote hash.Hash 24 } 25 26 type LlrSignedBlockVotes struct { 27 Signed SignedEventLocator 28 TxsAndMisbehaviourProofsHash hash.Hash 29 EpochVoteHash hash.Hash 30 Val LlrBlockVotes 31 } 32 33 type LlrSignedEpochVote struct { 34 Signed SignedEventLocator 35 TxsAndMisbehaviourProofsHash hash.Hash 36 BlockVotesHash hash.Hash 37 Val LlrEpochVote 38 } 39 40 func AsSignedBlockVotes(e EventPayloadI) LlrSignedBlockVotes { 41 return LlrSignedBlockVotes{ 42 Signed: AsSignedEventLocator(e), 43 TxsAndMisbehaviourProofsHash: hash.Of(CalcTxHash(e.Txs()).Bytes(), CalcMisbehaviourProofsHash(e.MisbehaviourProofs()).Bytes()), 44 EpochVoteHash: e.EpochVote().Hash(), 45 Val: e.BlockVotes(), 46 } 47 } 48 49 func AsSignedEpochVote(e EventPayloadI) LlrSignedEpochVote { 50 return LlrSignedEpochVote{ 51 Signed: AsSignedEventLocator(e), 52 TxsAndMisbehaviourProofsHash: hash.Of(CalcTxHash(e.Txs()).Bytes(), CalcMisbehaviourProofsHash(e.MisbehaviourProofs()).Bytes()), 53 BlockVotesHash: e.BlockVotes().Hash(), 54 Val: e.EpochVote(), 55 } 56 } 57 58 func (r SignedEventLocator) Size() uint64 { 59 return uint64(len(r.Sig)) + 3*32 + 4*4 60 } 61 62 func (bvs LlrSignedBlockVotes) Size() uint64 { 63 return bvs.Signed.Size() + uint64(len(bvs.Val.Votes))*32 + 32*2 + 8 + 4 64 } 65 66 func (ers LlrEpochVote) Hash() hash.Hash { 67 hasher := sha256.New() 68 hasher.Write(ers.Epoch.Bytes()) 69 hasher.Write(ers.Vote.Bytes()) 70 return hash.BytesToHash(hasher.Sum(nil)) 71 } 72 73 func (bvs LlrBlockVotes) Hash() hash.Hash { 74 hasher := sha256.New() 75 hasher.Write(bvs.Start.Bytes()) 76 hasher.Write(bvs.Epoch.Bytes()) 77 hasher.Write(bigendian.Uint32ToBytes(uint32(len(bvs.Votes)))) 78 for _, bv := range bvs.Votes { 79 hasher.Write(bv.Bytes()) 80 } 81 return hash.BytesToHash(hasher.Sum(nil)) 82 } 83 84 func (bvs LlrSignedBlockVotes) CalcPayloadHash() hash.Hash { 85 return hash.Of(bvs.TxsAndMisbehaviourProofsHash.Bytes(), hash.Of(bvs.EpochVoteHash.Bytes(), bvs.Val.Hash().Bytes()).Bytes()) 86 } 87 88 func (ev LlrSignedEpochVote) CalcPayloadHash() hash.Hash { 89 return hash.Of(ev.TxsAndMisbehaviourProofsHash.Bytes(), hash.Of(ev.Val.Hash().Bytes(), ev.BlockVotesHash.Bytes()).Bytes()) 90 } 91 92 func (ev LlrSignedEpochVote) Size() uint64 { 93 return ev.Signed.Size() + 32 + 32*2 + 4 + 4 94 }