Remove obsolete EIP-6110-7002-7251 types, encoding, and checks (#752)

* Remove obsolete EIP-6110-7002-7251 types

* Keep blocks_rlp.nim
This commit is contained in:
andri lim 2024-10-22 14:00:06 +07:00 committed by GitHub
parent 171531fbf1
commit 66297c5c0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 2 additions and 243 deletions

View File

@ -18,49 +18,16 @@ type
address* : Address address* : Address
amount* : uint64 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 BlockBody* = object
transactions*: seq[Transaction] transactions*: seq[Transaction]
uncles*: seq[Header] uncles*: seq[Header]
withdrawals*: Opt[seq[Withdrawal]] # EIP-4895 withdrawals*: Opt[seq[Withdrawal]] # EIP-4895
requests*: Opt[seq[Request]] # EIP-7865
Block* = object Block* = object
header* : Header header* : Header
transactions*: seq[Transaction] transactions*: seq[Transaction]
uncles* : seq[Header] uncles* : seq[Header]
withdrawals*: Opt[seq[Withdrawal]] # EIP-4895 withdrawals*: Opt[seq[Withdrawal]] # EIP-4895
requests*: Opt[seq[Request]] # EIP-7865
const const
EMPTY_UNCLE_HASH* = hash32"1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" EMPTY_UNCLE_HASH* = hash32"1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
@ -77,15 +44,3 @@ func init*(T: type Block, header: Header, body: BlockBody): T =
template txs*(blk: Block): seq[Transaction] = template txs*(blk: Block): seq[Transaction] =
# Legacy name emulation # Legacy name emulation
blk.transactions 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

View File

@ -12,146 +12,3 @@ import ./[addresses_rlp, blocks, base_rlp, hashes_rlp], ../rlp
from stew/objects import checkedEnumAssign from stew/objects import checkedEnumAssign
export addresses_rlp, blocks, base_rlp, hashes_rlp, rlp 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

View File

@ -33,14 +33,11 @@ type
EthAccount* = Account EthAccount* = Account
EthAddress* = Address EthAddress* = Address
EthBlock* = Block EthBlock* = Block
EthConsolidationRequest* = ConsolidationRequest
EthDepositRequest* = DepositRequest
EthHash32* = Hash32 EthHash32* = Hash32
EthHeader* = Header EthHeader* = Header
EthTransaction* = Transaction EthTransaction* = Transaction
EthReceipt* = Receipt EthReceipt* = Receipt
EthWithdrawal* = Withdrawal EthWithdrawal* = Withdrawal
EthWithdrawalRequest* = WithdrawalRequest
func init*(T: type BlockHashOrNumber, str: string): T {.raises: [ValueError].} = func init*(T: type BlockHashOrNumber, str: string): T {.raises: [ValueError].} =
if str.startsWith "0x": if str.startsWith "0x":

View File

@ -6,13 +6,13 @@
import 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, headers_rlp, receipts_rlp, times_rlp, transactions_rlp,
], ],
../rlp ../rlp
export 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 headers_rlp, receipts_rlp, times_rlp, transactions_rlp, rlp
proc append*(rlpWriter: var RlpWriter, value: BlockHashOrNumber) = proc append*(rlpWriter: var RlpWriter, value: BlockHashOrNumber) =

View File

@ -246,53 +246,3 @@ template genTestOpt(TT) =
genTestOpt(BlockBodyOpt) genTestOpt(BlockBodyOpt)
genTestOpt(EthBlockOpt) 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