Skip to content

Commit

Permalink
Remove obsolete EIP-6110-7002-7251 types, encoding, and checks (#752)
Browse files Browse the repository at this point in the history
* Remove obsolete EIP-6110-7002-7251 types

* Keep blocks_rlp.nim
  • Loading branch information
jangko authored Oct 22, 2024
1 parent 171531f commit 66297c5
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 243 deletions.
45 changes: 0 additions & 45 deletions eth/common/blocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,16 @@ type
address* : Address
amount* : uint64

DepositRequest* = object # EIP-6110
pubkey* : Bytes48
withdrawalCredentials*: Bytes32
amount* : uint64
signature* : Bytes96
index* : uint64

WithdrawalRequest* = object # EIP-7002
sourceAddress* : Address
validatorPubkey*: Bytes48
amount* : uint64

ConsolidationRequest* = object # EIP-7251
sourceAddress*: Address
sourcePubkey* : Bytes48
targetPubkey* : Bytes48

RequestType* = enum
DepositRequestType # EIP-6110
WithdrawalRequestType # EIP-7002
ConsolidationRequestType # EIP-7251

Request* = object
case requestType*: RequestType
of DepositRequestType:
deposit*: DepositRequest
of WithdrawalRequestType:
withdrawal*: WithdrawalRequest
of ConsolidationRequestType:
consolidation*: ConsolidationRequest

BlockBody* = object
transactions*: seq[Transaction]
uncles*: seq[Header]
withdrawals*: Opt[seq[Withdrawal]] # EIP-4895
requests*: Opt[seq[Request]] # EIP-7865

Block* = object
header* : Header
transactions*: seq[Transaction]
uncles* : seq[Header]
withdrawals*: Opt[seq[Withdrawal]] # EIP-4895
requests*: Opt[seq[Request]] # EIP-7865

const
EMPTY_UNCLE_HASH* = hash32"1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
Expand All @@ -77,15 +44,3 @@ func init*(T: type Block, header: Header, body: BlockBody): T =
template txs*(blk: Block): seq[Transaction] =
# Legacy name emulation
blk.transactions

func `==`*(a, b: Request): bool =
if a.requestType != b.requestType:
return false

case a.requestType
of DepositRequestType:
a.deposit == b.deposit
of WithdrawalRequestType:
a.withdrawal == b.withdrawal
of ConsolidationRequestType:
a.consolidation == b.consolidation
143 changes: 0 additions & 143 deletions eth/common/blocks_rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,146 +12,3 @@ import ./[addresses_rlp, blocks, base_rlp, hashes_rlp], ../rlp
from stew/objects import checkedEnumAssign

export addresses_rlp, blocks, base_rlp, hashes_rlp, rlp

proc append*(rlpWriter: var RlpWriter, request: DepositRequest) =
rlpWriter.appendRawBytes([DepositRequestType.byte])
rlpWriter.startList(5)
rlpWriter.append(request.pubkey)
rlpWriter.append(request.withdrawalCredentials)
rlpWriter.append(request.amount)
rlpWriter.append(request.signature)
rlpWriter.append(request.index)

proc read*(rlp: var Rlp, T: type DepositRequest): T {.raises: [RlpError].} =
if not rlp.hasData:
raise (ref MalformedRlpError)(
msg: "DepositRequestType expected but source RLP is empty"
)
let reqType = rlp.readRawByte()
if reqType != DepositRequestType:
raise (ref UnsupportedRlpError)(msg: "Unexpected DepositRequestType: " & $reqType)

var res: DepositRequest
rlp.tryEnterList()
rlp.read(res.pubkey)
rlp.read(res.withdrawalCredentials)
rlp.read(res.amount)
rlp.read(res.signature)
rlp.read(res.index)
if rlp.hasData:
raise (ref MalformedRlpError)(msg: "Extra data after DepositRequest")
res

proc append*(rlpWriter: var RlpWriter, request: WithdrawalRequest) =
rlpWriter.appendRawBytes([WithdrawalRequestType.byte])
rlpWriter.startList(3)
rlpWriter.append(request.sourceAddress)
rlpWriter.append(request.validatorPubkey)
rlpWriter.append(request.amount)

proc read*(rlp: var Rlp, T: type WithdrawalRequest): T {.raises: [RlpError].} =
if not rlp.hasData:
raise (ref MalformedRlpError)(
msg: "WithdrawalRequestType expected but source RLP is empty"
)
let reqType = rlp.readRawByte()
if reqType != WithdrawalRequestType:
raise
(ref UnsupportedRlpError)(msg: "Unexpected WithdrawalRequestType: " & $reqType)

var res: WithdrawalRequest
rlp.tryEnterList()
rlp.read(res.sourceAddress)
rlp.read(res.validatorPubkey)
rlp.read(res.amount)
if rlp.hasData:
raise (ref MalformedRlpError)(msg: "Extra data after WithdrawalRequest")
res

proc append*(rlpWriter: var RlpWriter, request: ConsolidationRequest) =
rlpWriter.appendRawBytes([ConsolidationRequestType.byte])
rlpWriter.startList(3)
rlpWriter.append(request.sourceAddress)
rlpWriter.append(request.sourcePubkey)
rlpWriter.append(request.targetPubkey)

proc read*(rlp: var Rlp, T: type ConsolidationRequest): T {.raises: [RlpError].} =
if not rlp.hasData:
raise (ref MalformedRlpError)(
msg: "ConsolidationRequestType expected but source RLP is empty"
)
let reqType = rlp.readRawByte()
if reqType != ConsolidationRequestType:
raise
(ref UnsupportedRlpError)(msg: "Unexpected ConsolidationRequestType: " & $reqType)

var res: ConsolidationRequest
rlp.tryEnterList()
rlp.read(res.sourceAddress)
rlp.read(res.sourcePubkey)
rlp.read(res.targetPubkey)
if rlp.hasData:
raise (ref MalformedRlpError)(msg: "Extra data after ConsolidationRequest")
res

proc append*(rlpWriter: var RlpWriter, request: Request) =
case request.requestType
of DepositRequestType:
rlpWriter.append(request.deposit)
of WithdrawalRequestType:
rlpWriter.append(request.withdrawal)
of ConsolidationRequestType:
rlpWriter.append(request.consolidation)

proc append*(rlpWriter: var RlpWriter, reqs: seq[Request] | openArray[Request]) =
rlpWriter.startList(reqs.len)
for req in reqs:
rlpWriter.append(rlp.encode(req))

proc read*(rlp: var Rlp, T: type Request): T {.raises: [RlpError].} =
if not rlp.hasData:
raise newException(MalformedRlpError, "Request expected but source RLP is empty")
if not rlp.isSingleByte:
raise newException(
MalformedRlpError, "RequestType byte is out of range, must be 0x00 to 0x7f"
)

let reqType = rlp.getByteValue
rlp.position += 1

var reqVal: RequestType
if checkedEnumAssign(reqVal, reqType):
result = Request(requestType: reqVal)
rlp.tryEnterList()
case reqVal
of DepositRequestType:
rlp.read(result.deposit.pubkey)
rlp.read(result.deposit.withdrawalCredentials)
rlp.read(result.deposit.amount)
rlp.read(result.deposit.signature)
rlp.read(result.deposit.index)
of WithdrawalRequestType:
rlp.read(result.withdrawal.sourceAddress)
rlp.read(result.withdrawal.validatorPubkey)
rlp.read(result.withdrawal.amount)
of ConsolidationRequestType:
rlp.read(result.consolidation.sourceAddress)
rlp.read(result.consolidation.sourcePubkey)
rlp.read(result.consolidation.targetPubkey)
else:
raise (ref UnsupportedRlpError)(msg: "Unexpected RequestType: " & $reqType)

proc read*(
rlp: var Rlp, T: (type seq[Request]) | (type openArray[Request])
): seq[Request] {.raises: [RlpError].} =
if not rlp.isList:
raise newException(
RlpTypeMismatch, "Requests list expected, but source RLP is not a list"
)

var reqs: seq[Request]
for item in rlp:
var rr = rlpFromBytes(rlp.read(seq[byte]))
reqs.add rr.read(Request)

reqs
3 changes: 0 additions & 3 deletions eth/common/eth_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ type
EthAccount* = Account
EthAddress* = Address
EthBlock* = Block
EthConsolidationRequest* = ConsolidationRequest
EthDepositRequest* = DepositRequest
EthHash32* = Hash32
EthHeader* = Header
EthTransaction* = Transaction
EthReceipt* = Receipt
EthWithdrawal* = Withdrawal
EthWithdrawalRequest* = WithdrawalRequest

func init*(T: type BlockHashOrNumber, str: string): T {.raises: [ValueError].} =
if str.startsWith "0x":
Expand Down
4 changes: 2 additions & 2 deletions eth/common/eth_types_rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import
"."/[
accounts_rlp, addresses_rlp, base_rlp, blocks_rlp, eth_types, hashes_rlp,
accounts_rlp, addresses_rlp, base_rlp, eth_types, hashes_rlp,
headers_rlp, receipts_rlp, times_rlp, transactions_rlp,
],
../rlp

export
accounts_rlp, addresses_rlp, base_rlp, blocks_rlp, eth_types, hashes_rlp,
accounts_rlp, addresses_rlp, base_rlp, eth_types, hashes_rlp,
headers_rlp, receipts_rlp, times_rlp, transactions_rlp, rlp

proc append*(rlpWriter: var RlpWriter, value: BlockHashOrNumber) =
Expand Down
50 changes: 0 additions & 50 deletions tests/common/test_eth_types_rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -246,53 +246,3 @@ template genTestOpt(TT) =

genTestOpt(BlockBodyOpt)
genTestOpt(EthBlockOpt)

suite "EIP-7865 tests":
const reqs = [
Request(
requestType: DepositRequestType,
deposit: DepositRequest(
pubkey : bytes48"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
withdrawalCredentials: bytes32"0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
amount : 1,
signature : bytes96"0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
index : 3,
)
),
Request(
requestType: WithdrawalRequestType,
withdrawal: WithdrawalRequest(
sourceAddress : address"0xDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
validatorPubkey: bytes48"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
amount : 7,
)
),
Request(
requestType: ConsolidationRequestType,
consolidation: ConsolidationRequest(
sourceAddress: address"0xEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE",
sourcePubkey : bytes48"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
targetPubkey : bytes48"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
)
)
]

test "rlp roundtrip":
let
body = BlockBody(
withdrawals: Opt.some(@[Withdrawal()]),
requests: Opt.some(@reqs)
)

blk = EthBlock(
withdrawals: Opt.some(@[Withdrawal()]),
requests: Opt.some(@reqs)
)

encodedBody = rlp.encode(body)
encodedBlock = rlp.encode(blk)
decodedBody = rlp.decode(encodedBody, BlockBody)
decodedBlk = rlp.decode(encodedBlock, EthBlock)

check decodedBody == body
check decodedBlk == blk

0 comments on commit 66297c5

Please sign in to comment.