Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blob: fix some sidecar RLP encode/decode issues; #390

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions core/types/blob_sidecars_test.go

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions eth/protocols/eth/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"io"
"math/big"

rlp2 "github.com/ledgerwatch/erigon-lib/rlp"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/direct"
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
rlp2 "github.com/ledgerwatch/erigon-lib/rlp"

"github.com/ledgerwatch/erigon/core/forkid"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rlp"
Expand Down Expand Up @@ -284,6 +284,14 @@ func (nbp NewBlockPacket) EncodeRLP(w io.Writer) error {
}
}
encodingSize += tdLen
if len(nbp.Sidecars) > 0 {
sidecarsLen := 0
for _, item := range nbp.Sidecars {
size := item.EncodingSize()
sidecarsLen += rlp2.ListPrefixLen(size) + size
}
encodingSize += rlp2.ListPrefixLen(sidecarsLen) + sidecarsLen
}
var b [33]byte
// prefix
if err := types.EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil {
Expand Down
89 changes: 89 additions & 0 deletions eth/protocols/eth/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"math/big"
"testing"

"github.com/holiman/uint256"
types2 "github.com/ledgerwatch/erigon-lib/types"
"github.com/ledgerwatch/erigon/params"
"github.com/stretchr/testify/require"

libcommon "github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/erigon/common"
Expand Down Expand Up @@ -229,3 +234,87 @@ func TestEth66Messages(t *testing.T) {
}
}
}

func TestNewBlockPacket_EncodeDecode(t *testing.T) {
dynamicTx := &types.DynamicFeeTransaction{
CommonTx: types.CommonTx{
Nonce: 0,
Gas: 25000,
To: &libcommon.Address{0x03, 0x04, 0x05},
Value: uint256.NewInt(99), // wei amount
Data: make([]byte, 50),
},
ChainID: uint256.NewInt(716),
Tip: uint256.NewInt(22),
FeeCap: uint256.NewInt(5),
AccessList: types2.AccessList{},
}
txSidecar := types.BlobTxSidecar{
Blobs: types.Blobs{types.Blob{}, types.Blob{}},
Commitments: types.BlobKzgs{types.KZGCommitment{}, types.KZGCommitment{}},
Proofs: types.KZGProofs{types.KZGProof{}, types.KZGProof{}},
}
blobTx := &types.BlobTx{
DynamicFeeTransaction: *dynamicTx,
MaxFeePerBlobGas: uint256.NewInt(5),
BlobVersionedHashes: []libcommon.Hash{{}},
}
blobGasUsed := uint64(params.BlobTxBlobGasPerBlob)
excessBlobGas := uint64(0)
withdrawalsHash := libcommon.Hash{}
header := &types.Header{
ParentHash: libcommon.HexToHash("0x85a8f2a2d4d2b3e73154d8ed1b5deb7c7c395dde7b934058bcc5d0efb69dff60"),
UncleHash: types.EmptyUncleHash,
Coinbase: libcommon.HexToAddress("0x76d76ee8823de52a1a431884c2ca930c5e72bff3 "),
Root: types.EmptyRootHash,
TxHash: libcommon.HexToHash("0xa84b1b157b86ed7e1352fa8e6518cd31e9be686bd27b1fe79d7ae6ebc02b29bb"),
ReceiptHash: libcommon.HexToHash("https://testnet.bscscan.com/tx/0x5ebb92a3a3660c18655a29be937ac2704b807545996672836ea907e2685bc8fc"),
Bloom: types.Bloom{},
Difficulty: new(big.Int).SetUint64(2),
Number: new(big.Int).SetUint64(1),
GasLimit: 69998932,
GasUsed: 998363,
Time: 1715485382,
Extra: make([]byte, 64),
MixDigest: libcommon.Hash{},
Nonce: types.BlockNonce{},
BaseFee: new(big.Int).SetUint64(0),
WithdrawalsHash: &withdrawalsHash,
BlobGasUsed: &blobGasUsed,
ExcessBlobGas: &excessBlobGas,
}

tests := []struct {
msg NewBlockPacket
}{
{msg: NewBlockPacket{
Block: types.NewBlockWithHeader(header),
TD: new(big.Int).SetUint64(1),
}},
{msg: NewBlockPacket{
Block: types.NewBlock(header, []types.Transaction{dynamicTx}, nil, nil, types.Withdrawals{}),
TD: new(big.Int).SetUint64(1),
}},
{msg: NewBlockPacket{
Block: types.NewBlock(header, []types.Transaction{dynamicTx, blobTx}, nil, nil, types.Withdrawals{}),
TD: new(big.Int).SetUint64(1),
Sidecars: types.BlobSidecars{&types.BlobSidecar{
BlobTxSidecar: txSidecar,
BlockNumber: new(big.Int).SetUint64(1),
BlockHash: libcommon.Hash{},
TxIndex: 1,
TxHash: libcommon.Hash{},
}},
}},
}

for _, item := range tests {
item.msg.Block.Size()
enc, err := rlp.EncodeToBytes(item.msg)
require.NoError(t, err)
var actual NewBlockPacket
err = rlp.DecodeBytes(enc, &actual)
require.NoError(t, err)
require.Equal(t, item.msg, actual)
}
}
Loading