Skip to content

Commit

Permalink
rlp: fix sidecar encoding (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed May 14, 2024
1 parent f04a48b commit 7be4557
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
46 changes: 38 additions & 8 deletions core/types/blob_sidecars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"

libcommon "github.com/ledgerwatch/erigon-lib/common"
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
"github.com/ledgerwatch/erigon/rlp"
)

Expand Down Expand Up @@ -65,18 +66,31 @@ func (s *BlobSidecar) EncodeRLP(w io.Writer) error {
if err := s.BlobTxSidecar.EncodeRLP(w); err != nil {
return err
}
if err := rlp.Encode(w, s.BlockNumber); err != nil {

if err := rlp.EncodeBigInt(s.BlockNumber, w, b[:]); err != nil {
return err
}

b[0] = 128 + 32
if _, err := w.Write(b[:1]); err != nil {
return err
}
if _, err := w.Write(s.BlockHash.Bytes()); err != nil {
return err
}
if err := rlp.Encode(w, s.BlockHash); err != nil {

if err := rlp.EncodeInt(s.TxIndex, w, b[:]); err != nil {
return err
}
if err := rlp.Encode(w, s.TxIndex); err != nil {

b[0] = 128 + 32
if _, err := w.Write(b[:1]); err != nil {
return err
}
if err := rlp.Encode(w, s.TxHash); err != nil {
if _, err := w.Write(s.TxHash.Bytes()); err != nil {
return err
}

return nil
}

Expand All @@ -89,14 +103,20 @@ func (sc *BlobSidecar) DecodeRLP(s *rlp.Stream) error {
if err := sc.BlobTxSidecar.DecodeRLP(s); err != nil {
return err
}

var b []byte
if b, err = s.Bytes(); err != nil {

if b, err = s.Uint256Bytes(); err != nil {
return err
}
sc.BlockNumber = new(big.Int).SetBytes(b)

if b, err = s.Bytes(); err != nil {
return err
}
if len(b) != 32 {
return fmt.Errorf("invalid block hash length: %d", len(b))
}
sc.BlockHash = libcommon.BytesToHash(b)

if sc.TxIndex, err = s.Uint(); err != nil {
Expand All @@ -106,6 +126,9 @@ func (sc *BlobSidecar) DecodeRLP(s *rlp.Stream) error {
if b, err = s.Bytes(); err != nil {
return err
}
if len(b) != 32 {
return fmt.Errorf("invalid tx hash length: %d", len(b))
}
sc.TxHash = libcommon.BytesToHash(b)

if err = s.ListEnd(); err != nil {
Expand All @@ -116,10 +139,17 @@ func (sc *BlobSidecar) DecodeRLP(s *rlp.Stream) error {

func (s *BlobSidecar) payloadSize() int {
size := s.BlobTxSidecar.payloadSize()
size += rlp2.ListPrefixLen(size) // size of payload size encoding

size++
size += rlp.BigIntLenExcludingHead(s.BlockNumber)
size += 32
size += 8
size += 32

size += rlp2.StringLen(s.BlockHash.Bytes())

size++
size += rlp.IntLenExcludingHead(s.TxIndex)

size += rlp2.StringLen(s.TxHash.Bytes())
return size
}

Expand Down
2 changes: 1 addition & 1 deletion core/types/blob_sidecars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
blockNumber = big.NewInt(123456)
blockHash = libcommon.HexToHash("0x1")
txHash = libcommon.HexToHash("0x2")
txIndex = uint64(0)
txIndex = uint64(100)
)

// generate newRandBlobTxSidecar
Expand Down

0 comments on commit 7be4557

Please sign in to comment.