This repository was archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
96 lines (87 loc) · 2.48 KB
/
Copy pathhelper.go
File metadata and controls
96 lines (87 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package dbft
import (
"io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/txhsl/tpke"
"golang.org/x/crypto/sha3"
)
func EncodeSignatureShare(s *tpke.SignatureShare) []byte {
return s.ToBytes()
}
func DecodeSignatureShare(b []byte) *tpke.SignatureShare {
s, err := tpke.BytesToSigShare(b)
if err != nil {
panic("failed to decode sig share")
}
return s
}
func DecodeSignature(b []byte) *tpke.Signature {
s, err := tpke.BytesToSig(b)
if err != nil {
panic("failed to decode sig")
}
return s
}
func EncodeDecryptionShare(ss []*tpke.DecryptionShare) [][]byte {
bs := make([][]byte, len(ss))
for i := 0; i < len(ss); i++ {
bs[i] = ss[i].ToBytes()
}
return bs
}
func DecodeDecryptionShare(bs [][]byte) []*tpke.DecryptionShare {
ss := make([]*tpke.DecryptionShare, len(bs))
for i := 0; i < len(bs); i++ {
s, err := tpke.BytesToDecryptionShare(bs[i])
if err != nil {
panic("failed to decode share")
}
ss[i] = s
}
return ss
}
// WorkerSealHash returns the hash of a header prior to it being sealed. WorkerSealHash is
// override to exclude those header fields that will be changed by dBFT during
// block sealing: MixDigest, Nonce and last [crypto.SignatureLength] bytes of
// Extra.
//
// Be careful no to use WorkerSealHash anywhere where "the honest" WorkerSealHash is required.
func WorkerSealHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewLegacyKeccak256()
encodeUnchangeableHeader(hasher, header)
hasher.(crypto.KeccakState).Read(hash[:])
return hash
}
// encodeUnchangeableHeader encodes those header fields that won't be changed by
// dBFT during block sealing: every header field except MixDigest, Nonce and last
// [crypto.SignatureLength] bytes of Extra.
func encodeUnchangeableHeader(w io.Writer, header *types.Header) {
enc := []interface{}{
header.ParentHash,
header.UncleHash,
header.Coinbase,
header.Root,
header.TxHash,
header.ReceiptHash,
header.Bloom,
header.Difficulty,
header.Number,
header.GasLimit,
header.GasUsed,
header.Time,
// Do not include validators addresses into hashable part.
header.Extra, // Yes, this will panic if extra is too short.
}
if header.BaseFee != nil {
enc = append(enc, header.BaseFee)
}
if header.WithdrawalsHash != nil {
panic("unexpected withdrawal hash value in dbft")
}
if err := rlp.Encode(w, enc); err != nil {
panic("can't encode: " + err.Error())
}
}