added random field to block type

This commit is contained in:
Giulio Rebuffo 2021-11-14 22:46:28 +01:00
parent 9e5ae33eb0
commit 2f275cb16d
2 changed files with 68 additions and 0 deletions

View File

@ -96,7 +96,9 @@ type Header struct {
MixDigest common.Hash `json:"mixHash"`
Nonce BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas"`
Random common.Hash `json:"random"`
Eip1559 bool // to avoid relying on BaseFee != nil for that
Eip3675 bool // to avoid relying on Random != nil for that
Seal []rlp.RawValue // AuRa POA network field
WithSeal bool // to avoid relying on Seal != nil for that
}
@ -243,6 +245,10 @@ func (h Header) EncodeRLP(w io.Writer) error {
encodingSize += baseFeeLen
}
if h.Eip3675 {
encodingSize += common.HashLength + 1
}
var b [33]byte
// Prefix
if err := EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil {
@ -401,6 +407,15 @@ func (h Header) EncodeRLP(w io.Writer) error {
}
}
}
if h.Eip3675 {
if _, err := w.Write([]byte{128 + common.HashLength}); err != nil {
return err
}
if _, err := w.Write(h.Random.Bytes()); err != nil {
return err
}
}
return nil
}
@ -529,6 +544,23 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
}
h.Eip1559 = true
h.BaseFee = new(big.Int).SetBytes(b)
if b, err = s.Bytes(); err != nil {
if errors.Is(err, rlp.EOL) {
h.Random = common.Hash{}
h.Eip3675 = false
if err := s.ListEnd(); err != nil {
return fmt.Errorf("close header struct (no random): %w", err)
}
return nil
}
return fmt.Errorf("read BaseFee: %w", err)
}
if len(b) != common.HashLength {
return fmt.Errorf("wrong size for BaseFee: %d", len(b))
}
h.Eip3675 = true
h.Random = common.BytesToHash(b)
}
if err := s.ListEnd(); err != nil {
return fmt.Errorf("close header struct: %w", err)

View File

@ -214,6 +214,42 @@ func TestUncleHash(t *testing.T) {
}
}
func TestRandom(t *testing.T) {
check := func(f string, got, want interface{}) {
if !reflect.DeepEqual(got, want) {
t.Errorf("%s mismatch: got %v, want %v", f, got, want)
}
}
header := Header{
Difficulty: math.BigPow(11, 11),
Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000001"),
GasLimit: 12345678,
GasUsed: 1476322,
Time: 9876543,
BaseFee: common.Big1,
Random: common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"),
Eip1559: true,
Eip3675: true,
}
var buf bytes.Buffer
err := header.EncodeRLP(&buf)
if err != nil {
t.Fatalf("err during encododing: %s", err.Error())
}
var decodedHeader Header
decodedHeader.DecodeRLP(rlp.NewStream(&buf, 0))
check("Difficulty", decodedHeader.Difficulty, math.BigPow(11, 11))
check("GasLimit", decodedHeader.GasLimit, uint64(12345678))
check("GasUsed", decodedHeader.GasUsed, uint64(1476322))
check("Coinbase", decodedHeader.Coinbase, common.HexToAddress("0x0000000000000000000000000000000000000001"))
check("BaseFee", decodedHeader.BaseFee, common.Big1)
check("Random", decodedHeader.Random, common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
check("Time", decodedHeader.Time, uint64(9876543))
}
var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000))
func BenchmarkEncodeBlock(b *testing.B) {