Skip to content

Commit d161ae4

Browse files
committed
[ETCM-921] Clean magic number and add unit test for PrefixedRLPEncodable
1 parent a14b927 commit d161ae4

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

rlp/src/main/scala/io/iohk/ethereum/rlp/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ package object rlp {
5858
* @param prefixedRLPEncodeable the RLPEncodable to prefix with
5959
*/
6060
case class PrefixedRLPEncodable(prefix: Byte, prefixedRLPEncodeable: RLPEncodeable) extends RLPEncodeable {
61-
require(prefix <= 0x07f, "prefix should be lower than 0x7f")
61+
require(prefix >= 0, "prefix should be in the range [0; 0x7f]")
6262
}
6363

6464
trait RLPEncoder[T] {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ sealed trait Transaction extends Product with Serializable {
2323

2424
object Transaction {
2525
val Type01: Byte = 1.toByte
26+
27+
val MinAllowedType: Byte = 0
28+
val MaxAllowedType: Byte = 0x7f
29+
2630
val LegacyThresholdLowerBound: Int = 0xc0
2731
val LegacyThresholdUpperBound: Int = 0xfe
2832

2933
def withGasLimit(gl: BigInt): Transaction => Transaction = {
3034
case tx: LegacyTransaction => tx.copy(gasLimit = gl)
3135
case tx: TransactionWithAccessList => tx.copy(gasLimit = gl)
3236
}
37+
38+
implicit class TransactionTypeValidator(val transactionType: Byte) extends AnyVal {
39+
def isValidTransactionType: Boolean = transactionType >= MinAllowedType && transactionType <= MaxAllowedType
40+
}
41+
42+
implicit class ByteArrayTransactionTypeValidator(val binaryData: Array[Byte]) extends AnyVal {
43+
def isValidTransactionType: Boolean = binaryData.length == 1 && binaryData.head.isValidTransactionType
44+
}
3345
}
3446

3547
sealed trait TypedTransaction extends Transaction

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ object BaseETH6XMessages {
161161
object TypedTransaction {
162162
implicit class TypedTransactionsRLPAggregator(val encodables: Seq[RLPEncodeable]) extends AnyVal {
163163

164+
import Transaction.ByteArrayTransactionTypeValidator
165+
164166
/** Convert a Seq of RLPEncodable containing TypedTransaction informations into a Seq of
165167
* Prefixed RLPEncodable.
166168
*
@@ -181,7 +183,7 @@ object BaseETH6XMessages {
181183
def toTypedRLPEncodables: Seq[RLPEncodeable] =
182184
encodables match {
183185
case Seq() => Seq()
184-
case Seq(RLPValue(v), rlpList: RLPList, tail @ _*) if v.length == 1 && v.head < 0x7f =>
186+
case Seq(RLPValue(v), rlpList: RLPList, tail @ _*) if v.isValidTransactionType =>
185187
PrefixedRLPEncodable(v.head, rlpList) +: tail.toTypedRLPEncodables
186188
case Seq(head, tail @ _*) => head +: tail.toTypedRLPEncodables
187189
}

0 commit comments

Comments
 (0)