Skip to content

Commit

Permalink
[ETCM-911] Replace LegacyTX with trait Transaction in SignedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dzajkowski committed Jul 20, 2021
1 parent ded1e7b commit a00f9fb
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
Expand Up @@ -165,7 +165,7 @@ object SignedTransaction {
}
}

case class SignedTransaction(tx: LegacyTransaction, signature: ECDSASignature) {
case class SignedTransaction(tx: Transaction, signature: ECDSASignature) {

def safeSenderIsEqualTo(address: Address): Boolean =
SignedTransaction.getSender(this).contains(address)
Expand Down
30 changes: 28 additions & 2 deletions src/main/scala/io/iohk/ethereum/domain/Transaction.scala
Expand Up @@ -4,7 +4,7 @@ import akka.util.ByteString

import org.bouncycastle.util.encoders.Hex

sealed trait Transaction {
sealed trait Transaction extends Product with Serializable {
def nonce: BigInt
def gasPrice: BigInt
def gasLimit: BigInt
Expand All @@ -25,6 +25,32 @@ object Transaction {
val Type01: Int = 1
val LegacyThresholdLowerBound: Int = 0xc0
val LegacyThresholdUpperBound: Int = 0xfe

def typed(
nonce: BigInt,
gasPrice: BigInt,
gasLimit: BigInt,
receivingAddress: Address,
value: BigInt,
payload: ByteString,
accessList: Option[List[AccessListItem]]
): Transaction =
TransactionWithAccessList(nonce, gasPrice, gasLimit, Some(receivingAddress), value, payload, accessList)

def legacy(
nonce: BigInt,
gasPrice: BigInt,
gasLimit: BigInt,
receivingAddress: Address,
value: BigInt,
payload: ByteString
): Transaction =
LegacyTransaction(nonce, gasPrice, gasLimit, Some(receivingAddress), value, payload)

def withGasLimit(gl: BigInt): Transaction => Transaction = {
case tx: LegacyTransaction => tx.copy(gasLimit = gl)
case tx: TransactionWithAccessList => tx.copy(gasLimit = gl)
}
}

sealed trait TypedTransaction extends Transaction
Expand Down Expand Up @@ -73,7 +99,7 @@ case class TransactionWithAccessList(
receivingAddress: Option[Address],
value: BigInt,
payload: ByteString,
accessList: List[AccessListItem]
accessList: Option[List[AccessListItem]]
) extends TypedTransaction {
override def toString: String =
s"TransactionWithAccessList {" +
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala
Expand Up @@ -98,14 +98,14 @@ class BlockPreparator(
* @param tx Target transaction
* @return Upfront cost
*/
private[ledger] def calculateUpfrontGas(tx: LegacyTransaction): UInt256 = UInt256(tx.gasLimit * tx.gasPrice)
private[ledger] def calculateUpfrontGas(tx: Transaction): UInt256 = UInt256(tx.gasLimit * tx.gasPrice)

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

/** Increments account nonce by 1 stated in YP equation (69) and
Expand Down
20 changes: 13 additions & 7 deletions src/main/scala/io/iohk/ethereum/ledger/StxLedger.scala
@@ -1,13 +1,15 @@
package io.iohk.ethereum.ledger

import scala.annotation.tailrec

import io.iohk.ethereum.db.storage.EvmCodeStorage
import io.iohk.ethereum.domain.Account
import io.iohk.ethereum.domain.BlockHeader
import io.iohk.ethereum.domain.BlockchainImpl
import io.iohk.ethereum.domain.BlockchainReader
import io.iohk.ethereum.domain.SignedTransactionWithSender
import io.iohk.ethereum.domain.{
Account,
BlockHeader,
BlockchainImpl,
BlockchainReader,
SignedTransactionWithSender,
Transaction
}
import io.iohk.ethereum.ledger.TxResult
import io.iohk.ethereum.nodebuilder.BlockchainConfigBuilder
import io.iohk.ethereum.vm.EvmConfig
Expand Down Expand Up @@ -68,7 +70,11 @@ class StxLedger(
highLimit
} else {
StxLedger.binaryChop(lowLimit, highLimit) { gasLimit =>
simulateTransaction(stx.copy(tx = tx.copy(tx = tx.tx.copy(gasLimit = gasLimit))), blockHeader, world).vmError
simulateTransaction(
stx.copy(tx = tx.copy(tx = Transaction.withGasLimit(gasLimit)(tx.tx))),
blockHeader,
world
).vmError
}
}
}
Expand Down
32 changes: 22 additions & 10 deletions src/main/scala/io/iohk/ethereum/utils/Picklers.scala
@@ -1,19 +1,22 @@
package io.iohk.ethereum.utils

import akka.util.ByteString

import boopickle.DefaultBasic._
import boopickle.Pickler

import io.iohk.ethereum.crypto.ECDSASignature
import io.iohk.ethereum.domain.Address
import io.iohk.ethereum.domain.BlockBody
import io.iohk.ethereum.domain.BlockHeader
import io.iohk.ethereum.domain.{
AccessListItem,
Address,
BlockBody,
BlockHeader,
Checkpoint,
LegacyTransaction,
SignedTransaction,
Transaction,
TransactionWithAccessList
}
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields._
import io.iohk.ethereum.domain.Checkpoint
import io.iohk.ethereum.domain.LegacyTransaction
import io.iohk.ethereum.domain.SignedTransaction

object Picklers {
implicit val byteStringPickler: Pickler[ByteString] =
Expand All @@ -30,9 +33,18 @@ object Picklers {

implicit val addressPickler: Pickler[Address] =
transformPickler[Address, ByteString](bytes => Address(bytes))(address => address.bytes)
implicit val transactionPickler: Pickler[LegacyTransaction] = generatePickler[LegacyTransaction]
implicit val accessListItemPickler: Pickler[AccessListItem] = generatePickler[AccessListItem]

implicit val legacyTransactionPickler: Pickler[LegacyTransaction] = generatePickler[LegacyTransaction]
implicit val transactionWithAccessListPickler: Pickler[TransactionWithAccessList] =
generatePickler[TransactionWithAccessList]

implicit val transactionPickler: Pickler[Transaction] = compositePickler[Transaction]
.addConcreteType[LegacyTransaction]
.addConcreteType[TransactionWithAccessList]

implicit val signedTransactionPickler: Pickler[SignedTransaction] =
transformPickler[SignedTransaction, (LegacyTransaction, ECDSASignature)] { case (tx, signature) =>
transformPickler[SignedTransaction, (Transaction, ECDSASignature)] { case (tx, signature) =>
new SignedTransaction(tx, signature)
}(stx => (stx.tx, stx.signature))

Expand Down
Expand Up @@ -242,7 +242,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
txsExecResult.isRight shouldBe true
val BlockResult(resultingWorldState, resultingGasUsed, resultingReceipts) = txsExecResult.toOption.get

val transaction: LegacyTransaction = validStxSignedByOrigin.tx
val transaction: Transaction = validStxSignedByOrigin.tx
// Check valid world
val minerPaymentForTxs = UInt256(transaction.gasLimit * transaction.gasPrice)
val changes = Seq(
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/io/iohk/ethereum/ledger/StxLedgerSpec.scala
Expand Up @@ -47,7 +47,7 @@ class StxLedgerSpec extends AnyFlatSpec with Matchers with Logger {

// Execute transaction with gasLimit lesser by one that estimated minimum
val errorExecResult: TxResult = mining.blockPreparator.executeTransaction(
stx.copy(tx = stx.tx.copy(gasLimit = estimationResult - 1)),
stx.copy(tx = Transaction.withGasLimit(estimationResult - 1)(stx.tx)),
fromAddress,
genesisHeader,
worldWithAccount
Expand Down

0 comments on commit a00f9fb

Please sign in to comment.