Skip to content

Commit

Permalink
added random field to block type (#2962)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Nov 15, 2021
1 parent 9e5ae33 commit 0b1027e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0b1027e

Please sign in to comment.