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

types: Update RLP encoding of EIP-6110 deposit reqs #12206

Closed
wants to merge 5 commits into from
Closed
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
71 changes: 33 additions & 38 deletions core/types/deposit_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import (
"github.com/erigontech/erigon-lib/common/hexutil"
"github.com/erigontech/erigon-lib/common/hexutility"

rlp2 "github.com/erigontech/erigon-lib/rlp"
"github.com/erigontech/erigon/accounts/abi"
"github.com/erigontech/erigon/rlp"
)

const (
Expand Down Expand Up @@ -70,37 +68,42 @@ type DepositRequestJson struct {

func (d *DepositRequest) RequestType() byte { return DepositRequestType }
func (d *DepositRequest) EncodeRLP(w io.Writer) (err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EncodeRLP/DecodeRLP are no longer accurate because it's not RLP anymore.

Copy link
Contributor Author

@somnathb1 somnathb1 Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. It was just following suit till now anyways and never was the full RLP encoding. This will be removed in subsequent PRs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Furthermore, the RLP encoding of the flat version would be almost what it is here, and would still work with Engine API and block storage, hence leaving it for this PR.

var buf bytes.Buffer
bb := make([]byte, 10)
if err = rlp.Encode(&buf, d.Pubkey); err != nil {
b := []byte{}
b = append(b, DepositRequestType)
b = append(b, d.Pubkey[:]...)
b = append(b, d.WithdrawalCredentials.Bytes()...)
b = binary.LittleEndian.AppendUint64(b, d.Amount)
b = append(b, d.Signature[:]...)
b = binary.LittleEndian.AppendUint64(b, d.Index)

if _, err = w.Write(b); err != nil {
return err
}
if err = rlp.Encode(&buf, d.WithdrawalCredentials); err != nil {
return err
}
if err = rlp.EncodeInt(d.Amount, &buf, bb); err != nil {
return err
}
if err = rlp.Encode(&buf, d.Signature); err != nil {
return err
}
if err = rlp.EncodeInt(d.Index, &buf, bb); err != nil {
return err
}
rlp2.EncodeListPrefix(buf.Len(), bb)
if _, err = w.Write([]byte{DepositRequestType}); err != nil {
return err
}
if _, err = w.Write(bb[0:2]); err != nil {
return err
}
if _, err = w.Write(buf.Bytes()); err != nil {
return err
}

return
}
func (d *DepositRequest) DecodeRLP(input []byte) error { return rlp.DecodeBytes(input[1:], d) }
func (d *DepositRequest) DecodeRLP(input []byte) error {
if len(input) != d.EncodingSize() {
return errors.New("Error decoding Deposit Request RLP - size mismatch in input")
}
i := 1
d.Pubkey = [BLSPubKeyLen]byte(input[i : i+BLSPubKeyLen])
i += BLSPubKeyLen
d.WithdrawalCredentials = libcommon.Hash(input[i : i+WithdrawalCredentialsLen])
i += WithdrawalCredentialsLen
d.Amount = binary.LittleEndian.Uint64(input[i : i+8])
i += 8
d.Signature = [BLSSigLen]byte(input[i : i+BLSSigLen])
i += BLSSigLen
d.Index = binary.LittleEndian.Uint64(input[i : i+8])
return nil
}

func (d *DepositRequest) Encode() []byte {
b := bytes.NewBuffer([]byte{})
d.EncodeRLP(b)
return b.Bytes()
}

func (d *DepositRequest) copy() Request {
return &DepositRequest{
Pubkey: d.Pubkey,
Expand All @@ -112,15 +115,7 @@ func (d *DepositRequest) copy() Request {
}

func (d *DepositRequest) EncodingSize() (encodingSize int) {
encodingSize++
encodingSize += rlp.IntLenExcludingHead(d.Amount)
encodingSize++
encodingSize += rlp.IntLenExcludingHead(d.Index)

encodingSize += 180 // 1 + 48 + 1 + 32 + 1 + 1 + 96 (0x80 + pLen, 0x80 + wLen, 0xb8 + 2 + sLen)
encodingSize += rlp2.ListPrefixLen(encodingSize)
encodingSize += 1 //RequestType
return
return 1 + BLSPubKeyLen + WithdrawalCredentialsLen + 8 + BLSSigLen + 8 //
}

func (d *DepositRequest) MarshalJSON() ([]byte, error) {
Expand Down
1 change: 1 addition & 0 deletions tests/exec_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestExecutionSpec(t *testing.T) {
bt.skipLoad(`^prague/eip2935_historical_block_hashes_from_state/block_hashes/block_hashes_history.json`)
bt.skipLoad(`^prague/eip7251_consolidations/`)
bt.skipLoad(`^prague/eip7685_general_purpose_el_requests/`)
bt.skipLoad(`^prague/eip6110_deposits/`)
bt.skipLoad(`^prague/eip7002_el_triggerable_withdrawals/`)
checkStateRoot := true

Expand Down
Loading