fix eip2718 bug

- fixes bug in rlp decoder of Receipt
- fixes bug in rlp decoder of Transaction
- refactor rlpEncode for AccessListTx in eth/common/transaction.nim
This commit is contained in:
jangko 2021-05-15 14:26:51 +07:00
parent b6b6f3dec7
commit 343d4f25e1
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 17 additions and 9 deletions

View File

@ -369,7 +369,10 @@ proc read*(rlp: var Rlp, T: type Transaction): T =
else: else:
let bytes = rlp.read(Blob) let bytes = rlp.read(Blob)
var rr = rlpFromBytes(bytes) var rr = rlpFromBytes(bytes)
assert(rr.read(int) == 1) let txType = rr.read(int)
if txType != 1:
raise newException(UnsupportedRlpError,
"TxType expect 1 got " & $txType)
result = Transaction( result = Transaction(
txType: AccessListTxType, txType: AccessListTxType,
accessListTx: rr.read(AccessListTx) accessListTx: rr.read(AccessListTx)
@ -427,7 +430,10 @@ proc read*(rlp: var Rlp, T: type Receipt): T =
else: else:
let bytes = rlp.read(Blob) let bytes = rlp.read(Blob)
var rr = rlpFromBytes(bytes) var rr = rlpFromBytes(bytes)
assert(rr.read(int) == 1) let recType = rr.read(int)
if recType != 1:
raise newException(UnsupportedRlpError,
"TxType expect 1 got " & $recType)
result = Receipt( result = Receipt(
receiptType: AccessListReceiptType, receiptType: AccessListReceiptType,
accessListReceipt: rr.read(AccessListReceipt) accessListReceipt: rr.read(AccessListReceipt)

View File

@ -87,11 +87,7 @@ func rlpEncodeEIP155*(tx: LegacyTx): auto =
S: 0.u256 S: 0.u256
)) ))
func txHashNoSignature*(tx: LegacyTx): Hash256 = func rlpEncode*(tx: AccessListTx): auto =
# Hash transaction without signature
return keccak256.digest(if tx.V >= EIP155_CHAIN_ID_OFFSET: tx.rlpEncodeEIP155 else: tx.rlpEncode)
func txHashNoSignature*(tx: AccessListTx): Hash256 =
let unsignedTx = AccessListUnsignedTx( let unsignedTx = AccessListUnsignedTx(
chainId: tx.chainId, chainId: tx.chainId,
nonce: tx.nonce, nonce: tx.nonce,
@ -106,8 +102,14 @@ func txHashNoSignature*(tx: AccessListTx): Hash256 =
var rw = initRlpWriter() var rw = initRlpWriter()
rw.append(1) rw.append(1)
rw.append(unsignedTx) rw.append(unsignedTx)
let rlpBytes = rlp.encode(rw.finish()) rlp.encode(rw.finish())
return keccak256.digest(rlpBytes)
func txHashNoSignature*(tx: LegacyTx): Hash256 =
# Hash transaction without signature
keccak256.digest(if tx.V >= EIP155_CHAIN_ID_OFFSET: tx.rlpEncodeEIP155 else: tx.rlpEncode)
func txHashNoSignature*(tx: AccessListTx): Hash256 =
keccak256.digest(tx.rlpEncode)
func txHashNoSignature*(tx: Transaction): Hash256 = func txHashNoSignature*(tx: Transaction): Hash256 =
if tx.txType == LegacyTxType: if tx.txType == LegacyTxType: