Skip to content

Commit 6b69a2a

Browse files
authored
[ETCM-921] Add optional access lists to transactions (#1040)
* [ETCM-911] Rename Transaction to LegacyTransaction * [ETCM-921] Introduce TypedTransaction ADT * [ETCM-911] Use Transaction ADT in tooling * [ETCM-911] Adjust unit tests with Transaction ADT * [ETCM-911] Apply formatting rules * Apply PR remarks
1 parent 41d2442 commit 6b69a2a

34 files changed

+297
-215
lines changed

src/benchmark/scala/io/iohk/ethereum/rlp/RLPSpeedSuite.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import org.scalacheck.Gen
1111
import org.scalatest.funsuite.AnyFunSuite
1212
import org.scalatestplus.scalacheck.{ScalaCheckDrivenPropertyChecks, ScalaCheckPropertyChecks}
1313

14-
/**
15-
* Tests based on
14+
/** Tests based on
1615
* - https://github.com/cryptape/ruby-rlp/blob/master/test/speed.rb
1716
* - https://github.com/ethereum/pyrlp/blob/develop/tests/speed.py
1817
*/
@@ -63,21 +62,21 @@ class RLPSpeedSuite
6362
}
6463

6564
def doTestSerialize[T](toSerialize: T, encode: T => Array[Byte], rounds: Int): Array[Byte] = {
66-
(1 until rounds).foreach(_ => {
65+
(1 until rounds).foreach { _ =>
6766
encode(toSerialize)
68-
})
67+
}
6968
encode(toSerialize)
7069
}
7170

7271
def doTestDeserialize[T](serialized: Array[Byte], decode: Array[Byte] => T, rounds: Int): T = {
73-
(1 until rounds).foreach(_ => {
72+
(1 until rounds).foreach { _ =>
7473
decode(serialized)
75-
})
74+
}
7675
decode(serialized)
7776
}
7877

7978
val validTransaction = SignedTransaction(
80-
Transaction(
79+
LegacyTransaction(
8180
nonce = 172320,
8281
gasPrice = BigInt("50000000000"),
8382
gasLimit = 90000,

src/main/scala/io/iohk/ethereum/consensus/validators/std/StdSignedTransactionValidator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class StdSignedTransactionValidator(blockchainConfig: BlockchainConfig) extends
4242
* @return Either the validated transaction or TransactionSyntaxError if an error was detected
4343
*/
4444
private def checkSyntacticValidity(stx: SignedTransaction): Either[SignedTransactionError, SignedTransactionValid] = {
45-
import Transaction._
45+
import LegacyTransaction._
4646
import stx._
4747
import stx.tx._
4848

src/main/scala/io/iohk/ethereum/domain/SignedTransaction.scala

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ object SignedTransaction {
5050
val valueForEmptyS = 0
5151

5252
def apply(
53-
tx: Transaction,
53+
tx: LegacyTransaction,
5454
pointSign: Byte,
5555
signatureRandom: ByteString,
5656
signature: ByteString,
@@ -64,7 +64,12 @@ object SignedTransaction {
6464
SignedTransaction(tx, txSignature)
6565
}
6666

67-
def apply(tx: Transaction, pointSign: Byte, signatureRandom: ByteString, signature: ByteString): SignedTransaction = {
67+
def apply(
68+
tx: LegacyTransaction,
69+
pointSign: Byte,
70+
signatureRandom: ByteString,
71+
signature: ByteString
72+
): SignedTransaction = {
6873
val txSignature = ECDSASignature(
6974
r = new BigInteger(1, signatureRandom.toArray),
7075
s = new BigInteger(1, signature.toArray),
@@ -73,11 +78,14 @@ object SignedTransaction {
7378
SignedTransaction(tx, txSignature)
7479
}
7580

76-
def sign(tx: Transaction, keyPair: AsymmetricCipherKeyPair, chainId: Option[Byte]): SignedTransactionWithSender = {
81+
def sign(
82+
tx: LegacyTransaction,
83+
keyPair: AsymmetricCipherKeyPair,
84+
chainId: Option[Byte]
85+
): SignedTransaction = {
7786
val bytes = bytesToSign(tx, chainId)
7887
val sig = ECDSASignature.sign(bytes, keyPair, chainId)
79-
val address = Address(keyPair)
80-
SignedTransactionWithSender(tx, sig, address)
88+
SignedTransaction(tx, sig)
8189
}
8290

8391
private def bytesToSign(tx: Transaction, chainId: Option[Byte]): Array[Byte] =
@@ -157,7 +165,7 @@ object SignedTransaction {
157165
}
158166
}
159167

160-
case class SignedTransaction(tx: Transaction, signature: ECDSASignature) {
168+
case class SignedTransaction(tx: LegacyTransaction, signature: ECDSASignature) {
161169

162170
def safeSenderIsEqualTo(address: Address): Boolean =
163171
SignedTransaction.getSender(this).contains(address)
@@ -184,6 +192,6 @@ object SignedTransactionWithSender {
184192
sender.fold(acc)(addr => SignedTransactionWithSender(stx, addr) :: acc)
185193
}
186194

187-
def apply(transaction: Transaction, signature: ECDSASignature, sender: Address): SignedTransactionWithSender =
195+
def apply(transaction: LegacyTransaction, signature: ECDSASignature, sender: Address): SignedTransactionWithSender =
188196
SignedTransactionWithSender(SignedTransaction(transaction, signature), sender)
189197
}

src/main/scala/io/iohk/ethereum/domain/Transaction.scala

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@ import akka.util.ByteString
44

55
import org.bouncycastle.util.encoders.Hex
66

7+
sealed trait Transaction {
8+
def nonce: BigInt
9+
def gasPrice: BigInt
10+
def gasLimit: BigInt
11+
def receivingAddress: Option[Address]
12+
def value: BigInt
13+
def payload: ByteString
14+
15+
def isContractInit: Boolean = receivingAddress.isEmpty
16+
17+
protected def receivingAddressString: String =
18+
receivingAddress.map(_.toString).getOrElse("[Contract creation]")
19+
20+
protected def payloadString: String =
21+
s"${if (isContractInit) "ContractInit: " else "TransactionData: "}${Hex.toHexString(payload.toArray[Byte])}"
22+
}
23+
724
object Transaction {
25+
val Type01: Int = 1
26+
val LegacyThresholdLowerBound: Int = 0xc0
27+
val LegacyThresholdUpperBound: Int = 0xfe
28+
}
29+
30+
sealed trait TypedTransaction extends Transaction
31+
32+
object LegacyTransaction {
833

934
val NonceLength = 32
1035
val GasLength = 32
@@ -17,33 +42,49 @@ object Transaction {
1742
receivingAddress: Address,
1843
value: BigInt,
1944
payload: ByteString
20-
): Transaction =
21-
Transaction(nonce, gasPrice, gasLimit, Some(receivingAddress), value, payload)
45+
): LegacyTransaction =
46+
LegacyTransaction(nonce, gasPrice, gasLimit, Some(receivingAddress), value, payload)
2247

2348
}
2449

25-
case class Transaction(
50+
case class LegacyTransaction(
2651
nonce: BigInt,
2752
gasPrice: BigInt,
2853
gasLimit: BigInt,
2954
receivingAddress: Option[Address],
3055
value: BigInt,
3156
payload: ByteString
32-
) {
33-
34-
def isContractInit: Boolean = receivingAddress.isEmpty
35-
36-
override def toString: String = {
37-
val receivingAddressString =
38-
receivingAddress.map(addr => Hex.toHexString(addr.toArray)).getOrElse("[Contract creation]")
57+
) extends Transaction {
58+
override def toString: String =
59+
s"LegacyTransaction {" +
60+
s"nonce: $nonce " +
61+
s"gasPrice: $gasPrice " +
62+
s"gasLimit: $gasLimit " +
63+
s"receivingAddress: $receivingAddressString " +
64+
s"value: $value wei " +
65+
s"payload: $payloadString " +
66+
s"}"
67+
}
3968

40-
s"Transaction {" +
69+
case class TransactionWithAccessList(
70+
nonce: BigInt,
71+
gasPrice: BigInt,
72+
gasLimit: BigInt,
73+
receivingAddress: Option[Address],
74+
value: BigInt,
75+
payload: ByteString,
76+
accessList: List[AccessListItem]
77+
) extends TypedTransaction {
78+
override def toString: String =
79+
s"TransactionWithAccessList {" +
4180
s"nonce: $nonce " +
4281
s"gasPrice: $gasPrice " +
4382
s"gasLimit: $gasLimit " +
4483
s"receivingAddress: $receivingAddressString " +
4584
s"value: $value wei " +
46-
s"payload: ${if (isContractInit) "ContractInit: " else "TransactionData: "}${Hex.toHexString(payload.toArray[Byte])} " +
85+
s"payload: $payloadString " +
86+
s"accessList: $accessList" +
4787
s"}"
48-
}
4988
}
89+
90+
case class AccessListItem(address: Address, storageKeys: List[BigInt]) // bytes32

src/main/scala/io/iohk/ethereum/faucet/jsonrpc/WalletService.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import cats.data.EitherT
77
import monix.eval.Task
88

99
import io.iohk.ethereum.domain.Address
10-
import io.iohk.ethereum.domain.Transaction
10+
import io.iohk.ethereum.domain.LegacyTransaction
1111
import io.iohk.ethereum.faucet.FaucetConfig
1212
import io.iohk.ethereum.jsonrpc.client.RpcClient.RpcError
1313
import io.iohk.ethereum.keystore.KeyStore
@@ -36,7 +36,7 @@ class WalletService(walletRpcClient: WalletRpcClient, keyStore: KeyStore, config
3636

3737
private def prepareTx(wallet: Wallet, targetAddress: Address, nonce: BigInt): ByteString = {
3838
val transaction =
39-
Transaction(nonce, config.txGasPrice, config.txGasLimit, Some(targetAddress), config.txValue, ByteString())
39+
LegacyTransaction(nonce, config.txGasPrice, config.txGasLimit, Some(targetAddress), config.txValue, ByteString())
4040

4141
val stx = wallet.signTx(transaction, None)
4242
ByteString(rlp.encode(stx.tx.toRLPEncodable))

src/main/scala/io/iohk/ethereum/jsonrpc/EthInfoService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class EthInfoService(
175175

176176
val toAddress = req.tx.to.map(Address.apply)
177177

178-
val tx = Transaction(0, req.tx.gasPrice, gasLimit, toAddress, req.tx.value, req.tx.data)
178+
val tx = LegacyTransaction(0, req.tx.gasPrice, gasLimit, toAddress, req.tx.value, req.tx.data)
179179
val fakeSignature = ECDSASignature(0, 0, 0.toByte)
180180
SignedTransactionWithSender(tx, fakeSignature, fromAddress)
181181
}

src/main/scala/io/iohk/ethereum/jsonrpc/TransactionRequest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.iohk.ethereum.jsonrpc
33
import akka.util.ByteString
44

55
import io.iohk.ethereum.domain.Address
6-
import io.iohk.ethereum.domain.Transaction
6+
import io.iohk.ethereum.domain.LegacyTransaction
77
import io.iohk.ethereum.utils.Config
88

99
case class TransactionRequest(
@@ -19,8 +19,8 @@ case class TransactionRequest(
1919
private val defaultGasPrice: BigInt = 2 * BigInt(10).pow(10)
2020
private val defaultGasLimit: BigInt = 90000
2121

22-
def toTransaction(defaultNonce: BigInt): Transaction =
23-
Transaction(
22+
def toTransaction(defaultNonce: BigInt): LegacyTransaction =
23+
LegacyTransaction(
2424
nonce = nonce.getOrElse(defaultNonce),
2525
gasPrice = gasPrice.getOrElse(defaultGasPrice),
2626
gasLimit = gasLimit.getOrElse(defaultGasLimit),

src/main/scala/io/iohk/ethereum/keystore/Wallet.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import org.bouncycastle.crypto.AsymmetricCipherKeyPair
66

77
import io.iohk.ethereum.crypto._
88
import io.iohk.ethereum.domain.Address
9+
import io.iohk.ethereum.domain.LegacyTransaction
910
import io.iohk.ethereum.domain.SignedTransaction
1011
import io.iohk.ethereum.domain.SignedTransactionWithSender
11-
import io.iohk.ethereum.domain.Transaction
1212

1313
case class Wallet(address: Address, prvKey: ByteString) {
1414
lazy val keyPair: AsymmetricCipherKeyPair = keyPairFromPrvKey(prvKey.toArray)
1515

16-
def signTx(tx: Transaction, chainId: Option[Byte]): SignedTransactionWithSender =
17-
SignedTransaction.sign(tx, keyPair, chainId)
16+
def signTx(tx: LegacyTransaction, chainId: Option[Byte]): SignedTransactionWithSender =
17+
SignedTransactionWithSender(SignedTransaction.sign(tx, keyPair, chainId), Address(keyPair))
1818
}

src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class BlockPreparator(
9999
* @param tx Target transaction
100100
* @return Upfront cost
101101
*/
102-
private[ledger] def calculateUpfrontGas(tx: Transaction): UInt256 = UInt256(tx.gasLimit * tx.gasPrice)
102+
private[ledger] def calculateUpfrontGas(tx: LegacyTransaction): UInt256 = UInt256(tx.gasLimit * tx.gasPrice)
103103

104104
/** v0 ≡ Tg (Tx gas limit) * Tp (Tx gas price) + Tv (Tx value). See YP equation number (65)
105105
*
106106
* @param tx Target transaction
107107
* @return Upfront cost
108108
*/
109-
private[ledger] def calculateUpfrontCost(tx: Transaction): UInt256 =
109+
private[ledger] def calculateUpfrontCost(tx: LegacyTransaction): UInt256 =
110110
UInt256(calculateUpfrontGas(tx) + tx.value)
111111

112112
/** Increments account nonce by 1 stated in YP equation (69) and

src/main/scala/io/iohk/ethereum/network/p2p/messages/BaseETH6XMessages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ object BaseETH6XMessages {
187187
) =>
188188
val receivingAddressOpt = if (receivingAddress.bytes.isEmpty) None else Some(Address(receivingAddress.bytes))
189189
SignedTransaction(
190-
Transaction(nonce, gasPrice, gasLimit, receivingAddressOpt, value, payload),
190+
LegacyTransaction(nonce, gasPrice, gasLimit, receivingAddressOpt, value, payload),
191191
(pointSign: Int).toByte,
192192
signatureRandom,
193193
signature,

src/main/scala/io/iohk/ethereum/utils/Picklers.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import io.iohk.ethereum.domain.BlockHeader
1212
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields
1313
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields._
1414
import io.iohk.ethereum.domain.Checkpoint
15+
import io.iohk.ethereum.domain.LegacyTransaction
1516
import io.iohk.ethereum.domain.SignedTransaction
16-
import io.iohk.ethereum.domain.Transaction
1717

1818
object Picklers {
1919
implicit val byteStringPickler: Pickler[ByteString] =
@@ -30,9 +30,9 @@ object Picklers {
3030

3131
implicit val addressPickler: Pickler[Address] =
3232
transformPickler[Address, ByteString](bytes => Address(bytes))(address => address.bytes)
33-
implicit val transactionPickler: Pickler[Transaction] = generatePickler[Transaction]
33+
implicit val transactionPickler: Pickler[LegacyTransaction] = generatePickler[LegacyTransaction]
3434
implicit val signedTransactionPickler: Pickler[SignedTransaction] =
35-
transformPickler[SignedTransaction, (Transaction, ECDSASignature)] { case (tx, signature) =>
35+
transformPickler[SignedTransaction, (LegacyTransaction, ECDSASignature)] { case (tx, signature) =>
3636
new SignedTransaction(tx, signature)
3737
}(stx => (stx.tx, stx.signature))
3838

src/test/scala/io/iohk/ethereum/BlockHelpers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object BlockHelpers extends SecureRandomBuilder {
2323
unixTimestamp = 0
2424
)
2525

26-
val defaultTx: Transaction = Transaction(
26+
val defaultTx: LegacyTransaction = LegacyTransaction(
2727
nonce = 42,
2828
gasPrice = 1,
2929
gasLimit = 90000,
@@ -62,7 +62,7 @@ object BlockHelpers extends SecureRandomBuilder {
6262
val tx = defaultTx.copy(payload = randomHash())
6363
val stx = SignedTransaction.sign(tx, keyPair, None)
6464

65-
Block(header, BlockBody(List(stx.tx), List(ommer)))
65+
Block(header, BlockBody(List(stx), List(ommer)))
6666
}
6767

6868
def updateHeader(block: Block, updater: BlockHeader => BlockHeader): Block =

0 commit comments

Comments
 (0)