Skip to content

Commit 1bfd580

Browse files
committed
[ETCM-921] read chainId from rlp transaction instead of config
1 parent ec940a7 commit 1bfd580

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.iohk.ethereum.network.p2p.messages.BaseETH6XMessages.SignedTransaction
2222
import io.iohk.ethereum.rlp.RLPImplicitConversions._
2323
import io.iohk.ethereum.rlp.RLPImplicits._
2424
import io.iohk.ethereum.rlp.{encode => rlpEncode, _}
25+
import io.iohk.ethereum.utils.Config
2526

2627
object SignedTransaction {
2728

@@ -86,13 +87,14 @@ object SignedTransaction {
8687

8788
private def calculateSender(tx: SignedTransaction): Option[Address] = Try {
8889
val ECDSASignature(_, _, v) = tx.signature
89-
val bytesToSign: Array[Byte] = if (v == ECDSASignature.negativePointSign || v == ECDSASignature.positivePointSign) {
90-
generalTransactionBytes(tx.tx)
91-
} else {
92-
chainSpecificTransactionBytes(tx.tx, chainId)
90+
// chainId specific code that will be refactored with the Signer feature (ETCM-1096)
91+
val chainIdOpt = extractChainId(tx)
92+
val bytesToSign: Array[Byte] = chainIdOpt match {
93+
case None => generalTransactionBytes(tx.tx)
94+
case Some(chainId) => chainSpecificTransactionBytes(tx.tx, chainId)
9395
}
9496

95-
val recoveredPublicKey: Option[Array[Byte]] = tx.signature.publicKey(bytesToSign, Some(chainId))
97+
val recoveredPublicKey: Option[Array[Byte]] = tx.signature.publicKey(bytesToSign, chainIdOpt)
9698

9799
for {
98100
key <- recoveredPublicKey
@@ -142,6 +144,17 @@ object SignedTransaction {
142144
)
143145
}
144146

147+
private def extractChainId(stx: SignedTransaction): Option[Byte] = {
148+
val chainIdOpt: Option[BigInt] = stx.tx match {
149+
case _: LegacyTransaction
150+
if stx.signature.v == ECDSASignature.negativePointSign || stx.signature.v == ECDSASignature.positivePointSign =>
151+
None
152+
case _: LegacyTransaction => Some(Config.blockchains.blockchainConfig.chainId)
153+
case twal: TransactionWithAccessList => Some(twal.chainId)
154+
}
155+
chainIdOpt.map(_.toByte)
156+
}
157+
145158
val byteArraySerializable: ByteArraySerializable[SignedTransaction] = new ByteArraySerializable[SignedTransaction] {
146159

147160
override def fromBytes(bytes: Array[Byte]): SignedTransaction = bytes.toSignedTransaction

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ case class LegacyTransaction(
7171

7272
object TransactionWithAccessList {
7373
def apply(
74+
chainId: BigInt,
7475
nonce: BigInt,
7576
gasPrice: BigInt,
7677
gasLimit: BigInt,
@@ -79,10 +80,11 @@ object TransactionWithAccessList {
7980
payload: ByteString,
8081
accessList: List[AccessListItem]
8182
): TransactionWithAccessList =
82-
TransactionWithAccessList(nonce, gasPrice, gasLimit, Some(receivingAddress), value, payload, accessList)
83+
TransactionWithAccessList(chainId, nonce, gasPrice, gasLimit, Some(receivingAddress), value, payload, accessList)
8384
}
8485

8586
case class TransactionWithAccessList(
87+
chainId: BigInt,
8688
nonce: BigInt,
8789
gasPrice: BigInt,
8890
gasLimit: BigInt,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ object BaseETH6XMessages {
155155

156156
object SignedTransactions {
157157

158-
lazy val chainId: Byte = Config.blockchains.blockchainConfig.chainId
159-
160158
implicit class SignedTransactionEnc(val signedTx: SignedTransaction) extends RLPSerializable {
161159

162160
override def toBytes: Array[Byte] =
@@ -171,9 +169,9 @@ object BaseETH6XMessages {
171169
.map(_.toArray)
172170
.getOrElse(Array.emptyByteArray)
173171
signedTx.tx match {
174-
case TransactionWithAccessList(nonce, gasPrice, gasLimit, _, value, payload, accessList) =>
172+
case TransactionWithAccessList(chainId, nonce, gasPrice, gasLimit, _, value, payload, accessList) =>
175173
RLPList(
176-
chainId, // TODO improve how chainid is preserved in transactions (ETCM-1096)
174+
chainId,
177175
nonce,
178176
gasPrice,
179177
gasLimit,
@@ -220,7 +218,7 @@ object BaseETH6XMessages {
220218

221219
def toSignedTransaction: SignedTransaction = rlpEncodeable match {
222220
case RLPList(
223-
_, // TODO improve how chainid is preserved in transactions (ETCM-1096)
221+
chainId,
224222
nonce,
225223
gasPrice,
226224
gasLimit,
@@ -235,6 +233,7 @@ object BaseETH6XMessages {
235233
val receivingAddressOpt = if (receivingAddress.bytes.isEmpty) None else Some(Address(receivingAddress.bytes))
236234
SignedTransaction(
237235
TransactionWithAccessList(
236+
chainId,
238237
nonce,
239238
gasPrice,
240239
gasLimit,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ trait ObjectGenerators {
107107
)
108108

109109
def typedTransactionGen(): Gen[TransactionWithAccessList] = for {
110+
chainId <- bigIntGen
110111
nonce <- bigIntGen
111112
gasPrice <- bigIntGen
112113
gasLimit <- bigIntGen
@@ -115,6 +116,7 @@ trait ObjectGenerators {
115116
payload <- byteStringOfLengthNGen(256)
116117
accessList <- Gen.listOf(accessListItemGen())
117118
} yield TransactionWithAccessList(
119+
chainId,
118120
nonce,
119121
gasPrice,
120122
gasLimit,

src/test/scala/io/iohk/ethereum/consensus/blocks/BlockGeneratorSpec.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
631631
)
632632

633633
val typedTransaction: TypedTransaction = TransactionWithAccessList(
634+
chainId = 61, // ethereum classic mainnet
634635
nonce = 0,
635636
gasPrice = 1,
636637
gasLimit = txGasLimit,

src/test/scala/io/iohk/ethereum/domain/TransactionSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.iohk.ethereum.ObjectGenerators
1111
import io.iohk.ethereum.crypto.ECDSASignature
1212
import io.iohk.ethereum.network.p2p.messages.BaseETH6XMessages.SignedTransactions
1313
import io.iohk.ethereum.security.SecureRandomBuilder
14+
import io.iohk.ethereum.utils.Config
1415
import io.iohk.ethereum.utils.Hex
1516
import io.iohk.ethereum.vm.utils.MockVmInput
1617

@@ -43,6 +44,7 @@ class TransactionSpec
4344

4445
val toAddr: Address = Address.apply("b94f5374fce5edbc8e2a8697c15331677e6ebf0b")
4546
val tx: TransactionWithAccessList = TransactionWithAccessList(
47+
1, // ethereum mainnet, used by the core-geth test
4648
3,
4749
1,
4850
25000,

0 commit comments

Comments
 (0)