Skip to content

Commit

Permalink
[ETCM-911] Adjust unit tests with Transaction ADT
Browse files Browse the repository at this point in the history
  • Loading branch information
dzajkowski committed Jul 7, 2021
1 parent 21f834e commit e7d3aaf
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/test/scala/io/iohk/ethereum/BlockHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object BlockHelpers extends SecureRandomBuilder {
val tx = defaultTx.copy(payload = randomHash())
val stx = SignedTransaction.sign(tx, keyPair, None)

Block(header, BlockBody(List(stx.tx), List(ommer)))
Block(header, BlockBody(List(stx), List(ommer)))
}

def updateHeader(block: Block, updater: BlockHeader => BlockHeader): Block =
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/io/iohk/ethereum/ObjectGenerators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ trait ObjectGenerators {
val txsSeqGen = Gen.listOfN(length, transactionGen())
txsSeqGen.map { txs =>
txs.map { tx =>
SignedTransaction.sign(tx, senderKeys, chainId).tx
SignedTransaction.sign(tx, senderKeys, chainId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
it should "generate correct block with transactions" in new TestSetup {
val pendingBlock =
blockGenerator
.generateBlock(bestBlock.get, Seq(signedTransaction.tx), Address(testAddress), blockGenerator.emptyX, None)
.generateBlock(bestBlock.get, Seq(signedTransaction), Address(testAddress), blockGenerator.emptyX, None)
.pendingBlock

//mined with mantis + ethminer
Expand All @@ -91,7 +91,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
it should "be possible to simulate transaction, on world returned with pending block" in new TestSetup {
val pendingBlock =
blockGenerator
.generateBlock(bestBlock.get, Seq(signedTransaction.tx), Address(testAddress), blockGenerator.emptyX, None)
.generateBlock(bestBlock.get, Seq(signedTransaction), Address(testAddress), blockGenerator.emptyX, None)
.pendingBlock

//mined with mantis + ethminer
Expand All @@ -114,20 +114,20 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
// Create new pending block, with updated stateRootHash
val pendBlockAndState = blockGenerator.generateBlock(
blockchain.getBestBlock().get,
Seq(signedTransaction.tx),
Seq(signedTransaction),
Address(testAddress),
blockGenerator.emptyX,
None
)

// Try to simulate transaction, on world with updated stateRootHash, but not updated storages
assertThrows[MPTException] {
stxLedger.simulateTransaction(signedTransaction, pendBlockAndState.pendingBlock.block.header, None)
stxLedger.simulateTransaction(signedTransactionWithAddress, pendBlockAndState.pendingBlock.block.header, None)
}

// Try to simulate transaction, on world with all changes stored in caches
val simulationResult = stxLedger.simulateTransaction(
signedTransaction,
signedTransactionWithAddress,
pendBlockAndState.pendingBlock.block.header,
Some(pendBlockAndState.worldState)
)
Expand All @@ -141,7 +141,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
blockGenerator
.generateBlock(
bestBlock.get,
Seq(signedTransaction.tx, duplicatedSignedTransaction.tx),
Seq(signedTransaction, duplicatedSignedTransaction),
Address(testAddress),
blockGenerator.emptyX,
None
Expand All @@ -167,20 +167,19 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
) shouldBe Right(BlockHeaderValid)

blockExecution.executeAndValidateBlock(fullBlock) shouldBe a[Right[_, Seq[Receipt]]]
fullBlock.body.transactionList shouldBe Seq(signedTransaction.tx)
fullBlock.body.transactionList shouldBe Seq(signedTransaction)
fullBlock.header.extraData shouldBe headerExtraData
}

it should "filter out transactions exceeding block gas limit and include correct transactions" in new TestSetup {
val txWitGasTooBigGasLimit = SignedTransaction
.sign(
transaction.copy(gasLimit = BigInt(2).pow(100000), nonce = signedTransaction.tx.tx.nonce + 1),
transaction.copy(gasLimit = BigInt(2).pow(100000), nonce = signedTransaction.tx.nonce + 1),
keyPair,
Some(0x3d.toByte)
)
.tx

val transactions = Seq(txWitGasTooBigGasLimit, signedTransaction.tx, duplicatedSignedTransaction.tx)
val transactions = Seq(txWitGasTooBigGasLimit, signedTransaction, duplicatedSignedTransaction)
val pendingBlock =
blockGenerator
.generateBlock(bestBlock.get, transactions, Address(testAddress), blockGenerator.emptyX, None)
Expand All @@ -205,7 +204,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
blockchainReader.getBlockHeaderByHash
) shouldBe Right(BlockHeaderValid)
blockExecution.executeAndValidateBlock(fullBlock) shouldBe a[Right[_, Seq[Receipt]]]
fullBlock.body.transactionList shouldBe Seq(signedTransaction.tx)
fullBlock.body.transactionList shouldBe Seq(signedTransaction)
fullBlock.header.extraData shouldBe headerExtraData
}

Expand Down Expand Up @@ -260,9 +259,8 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
blockValidation
)

val generalTx = SignedTransaction.sign(transaction, keyPair, None).tx
val specificTx =
SignedTransaction.sign(transaction.copy(nonce = transaction.nonce + 1), keyPair, Some(0x3d.toByte)).tx
val generalTx = SignedTransaction.sign(transaction, keyPair, None)
val specificTx = SignedTransaction.sign(transaction.copy(nonce = transaction.nonce + 1), keyPair, Some(0x3d.toByte))

val pendingBlock =
blockGenerator
Expand Down Expand Up @@ -351,7 +349,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
value = 0,
payload = ByteString.empty
)
val generalTx = SignedTransaction.sign(transaction1, keyPair, None).tx
val generalTx = SignedTransaction.sign(transaction1, keyPair, None)

val generatedBlock =
blockGenerator
Expand All @@ -362,13 +360,13 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
}

it should "generate block after eip155 and allow both chain specific and general transactions" in new TestSetup {
val generalTx = SignedTransaction.sign(transaction.copy(nonce = transaction.nonce + 1), keyPair, None).tx
val generalTx = SignedTransaction.sign(transaction.copy(nonce = transaction.nonce + 1), keyPair, None)

val pendingBlock =
blockGenerator
.generateBlock(
bestBlock.get,
Seq(generalTx, signedTransaction.tx),
Seq(generalTx, signedTransaction),
Address(testAddress),
blockGenerator.emptyX,
None
Expand All @@ -393,19 +391,19 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
BlockHeaderValid
)
blockExecution.executeAndValidateBlock(fullBlock) shouldBe a[Right[_, Seq[Receipt]]]
fullBlock.body.transactionList shouldBe Seq(signedTransaction.tx, generalTx)
fullBlock.body.transactionList shouldBe Seq(signedTransaction, generalTx)
fullBlock.header.extraData shouldBe headerExtraData
}

it should "include consecutive transactions from single sender" in new TestSetup {
val nextTransaction =
SignedTransaction.sign(transaction.copy(nonce = signedTransaction.tx.tx.nonce + 1), keyPair, Some(0x3d.toByte)).tx
SignedTransaction.sign(transaction.copy(nonce = signedTransaction.tx.nonce + 1), keyPair, Some(0x3d.toByte))

val pendingBlock =
blockGenerator
.generateBlock(
bestBlock.get,
Seq(nextTransaction, signedTransaction.tx),
Seq(nextTransaction, signedTransaction),
Address(testAddress),
blockGenerator.emptyX,
None
Expand All @@ -430,13 +428,13 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
BlockHeaderValid
)
blockExecution.executeAndValidateBlock(fullBlock) shouldBe a[Right[_, Seq[Receipt]]]
fullBlock.body.transactionList shouldBe Seq(signedTransaction.tx, nextTransaction)
fullBlock.body.transactionList shouldBe Seq(signedTransaction, nextTransaction)
fullBlock.header.extraData shouldBe headerExtraData
}

it should "filter out failing transaction from the middle of tx list" in new TestSetup {
val nextTransaction =
SignedTransaction.sign(transaction.copy(nonce = signedTransaction.tx.tx.nonce + 1), keyPair, Some(0x3d.toByte)).tx
SignedTransaction.sign(transaction.copy(nonce = signedTransaction.tx.nonce + 1), keyPair, Some(0x3d.toByte))

val privateKeyWithNoEthere =
BigInt(1, Hex.decode("584a31be275195585603ddd05a53d16fae9deafba67213b6060cec9f16e44cae"))
Expand All @@ -450,13 +448,13 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
payload = ByteString.empty
)
val signedFailingTransaction =
SignedTransaction.sign(failingTransaction, keyPairFromPrvKey(privateKeyWithNoEthere), Some(0x3d.toByte)).tx
SignedTransaction.sign(failingTransaction, keyPairFromPrvKey(privateKeyWithNoEthere), Some(0x3d.toByte))

val pendingBlock =
blockGenerator
.generateBlock(
bestBlock.get,
Seq(nextTransaction, signedFailingTransaction, signedTransaction.tx),
Seq(nextTransaction, signedFailingTransaction, signedTransaction),
Address(testAddress),
blockGenerator.emptyX,
None
Expand All @@ -480,20 +478,19 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
BlockHeaderValid
)
blockExecution.executeAndValidateBlock(fullBlock) shouldBe a[Right[_, Seq[Receipt]]]
fullBlock.body.transactionList shouldBe Seq(signedTransaction.tx, nextTransaction)
fullBlock.body.transactionList shouldBe Seq(signedTransaction, nextTransaction)
fullBlock.header.extraData shouldBe headerExtraData
}

it should "include transaction with higher gas price if nonce is the same" in new TestSetup {
val txWitSameNonceButLowerGasPrice = SignedTransaction
.sign(transaction.copy(gasPrice = signedTransaction.tx.tx.gasPrice - 1), keyPair, Some(0x3d.toByte))
.tx
.sign(transaction.copy(gasPrice = signedTransaction.tx.gasPrice - 1), keyPair, Some(0x3d.toByte))

val pendingBlock =
blockGenerator
.generateBlock(
bestBlock.get,
Seq(txWitSameNonceButLowerGasPrice, signedTransaction.tx),
Seq(txWitSameNonceButLowerGasPrice, signedTransaction),
Address(testAddress),
blockGenerator.emptyX,
None
Expand All @@ -517,7 +514,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
BlockHeaderValid
)
blockExecution.executeAndValidateBlock(fullBlock) shouldBe a[Right[_, Seq[Receipt]]]
fullBlock.body.transactionList shouldBe Seq(signedTransaction.tx)
fullBlock.body.transactionList shouldBe Seq(signedTransaction)
fullBlock.header.extraData shouldBe headerExtraData
}

Expand Down Expand Up @@ -668,11 +665,14 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
val treasuryAccount: Address = Address(0xeeeeee)
val maliciousAccount: Address = Address(0x123)

lazy val signedTransaction: SignedTransactionWithSender =
lazy val signedTransaction: SignedTransaction =
SignedTransaction.sign(transaction, keyPair, Some(0x3d.toByte))
lazy val duplicatedSignedTransaction: SignedTransactionWithSender =
lazy val duplicatedSignedTransaction: SignedTransaction =
SignedTransaction.sign(transaction.copy(gasLimit = 2), keyPair, Some(0x3d.toByte))

lazy val signedTransactionWithAddress: SignedTransactionWithSender =
SignedTransactionWithSender(signedTransaction, Address(keyPair))

val baseBlockchainConfig: BlockchainConfig = BlockchainConfig(
forkBlockNumbers = ForkBlockNumbers(
frontierBlockNumber = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class StdSignedLegacyTransactionValidatorSpec extends AnyFlatSpec with Matchers
val keyPair = crypto.generateKeyPair(new SecureRandom)
val stx = SignedTransaction.sign(txBeforeHomestead, keyPair, Some(0x03.toByte))
signedTransactionValidator.validate(
stx.tx,
stx,
senderAccount = senderAccountAfterHomestead,
blockHeader = blockHeaderAfterHomestead,
upfrontGasCost = upfrontGasCost,
Expand All @@ -254,7 +254,7 @@ class StdSignedLegacyTransactionValidatorSpec extends AnyFlatSpec with Matchers
val keyPair = crypto.generateKeyPair(new SecureRandom)
val stx = SignedTransaction.sign(txAfterHomestead, keyPair, Some(0x03.toByte))
signedTransactionValidator.validate(
stx.tx,
stx,
senderAccount = senderAccountAfterHomestead,
blockHeader = blockHeaderAfterHomestead.copy(number = blockchainConfig.forkBlockNumbers.eip155BlockNumber),
upfrontGasCost = upfrontGasCost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class SignedLegacyTransactionSpec
.kec256(key.getPublic.asInstanceOf[ECPublicKeyParameters].getQ.getEncoded(false).tail)
.drop(FirstByteOfAddress)
)
val result = SignedTransaction.sign(tx, key, Some(chainId))
val signedTransaction = SignedTransaction.sign(tx, key, Some(chainId))
val result = SignedTransactionWithSender(signedTransaction, Address(key))

allowedPointSigns should contain(result.tx.signature.v)
address shouldEqual result.senderAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class FilterManagerSpec
payload = ByteString()
)

val stx = SignedTransaction.sign(tx, keyPair, None)
val stx = SignedTransactionWithSender(SignedTransaction.sign(tx, keyPair, None), Address(keyPair))
val pendingTxs = Seq(
stx
)
Expand Down Expand Up @@ -442,7 +442,7 @@ class FilterManagerSpec
payload = ByteString()
)

val stx = SignedTransaction.sign(tx, keyPair, None)
val stx = SignedTransactionWithSender(SignedTransaction.sign(tx, keyPair, None), Address(keyPair))
val pendingTxs = Seq(stx)

(keyStore.listAccounts _).expects().returning(Right(List(stx.senderAddress)))
Expand Down
27 changes: 18 additions & 9 deletions src/test/scala/io/iohk/ethereum/ledger/BlockExecutionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
val block1BodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(invalidStx))
val block1 = Block(validBlockHeader, block1BodyWithTxs)
val block2BodyWithTxs: BlockBody =
validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin.tx))
validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin))
val block2 = Block(
validBlockHeader.copy(parentHash = validBlockHeader.hash, number = validBlockHeader.number + 1),
block2BodyWithTxs
Expand Down Expand Up @@ -84,7 +84,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
"two blocks with txs (that last one has invalid tx)" in new BlockchainSetup {
val invalidStx = SignedTransaction(validTx, ECDSASignature(1, 2, 3.toByte))
val block1BodyWithTxs: BlockBody =
validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin.tx))
validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin))
val block1 = Block(validBlockHeader, block1BodyWithTxs)
val block2BodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(invalidStx))
val block2 = Block(
Expand Down Expand Up @@ -208,7 +208,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper

"block with one tx (that produces OutOfGas)" in new BlockchainSetup {

val blockBodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin.tx))
val blockBodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin))
val block = Block(validBlockHeader, blockBodyWithTxs)

val mockVm = new MockVM(c =>
Expand Down Expand Up @@ -243,7 +243,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.tx
val transaction: LegacyTransaction = validStxSignedByOrigin.tx
// Check valid world
val minerPaymentForTxs = UInt256(transaction.gasLimit * transaction.gasPrice)
val changes = Seq(
Expand Down Expand Up @@ -280,7 +280,10 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper

forAll(table) { (gasLimit, logs, addressesToDelete, txValidAccordingToValidators) =>
val tx = validTx.copy(gasLimit = gasLimit)
val stx = SignedTransaction.sign(tx, originKeyPair, Some(blockchainConfig.chainId))
val stx = SignedTransactionWithSender(
SignedTransaction.sign(tx, originKeyPair, Some(blockchainConfig.chainId)),
Address(originKeyPair)
)

val blockHeader: BlockHeader = validBlockHeader.copy(gasLimit = gasLimit)
val blockBodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(stx.tx))
Expand Down Expand Up @@ -346,7 +349,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
"last one wasn't executed correctly" in new BlockExecutionTestSetup {
val invalidStx = SignedTransaction(validTx, ECDSASignature(1, 2, 3.toByte))
val blockBodyWithTxs: BlockBody =
validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin.tx, invalidStx))
validBlockBodyWithNoTxs.copy(transactionList = Seq(validStxSignedByOrigin, invalidStx))
val block = Block(validBlockHeader, blockBodyWithTxs)

val txsExecResult: Either[BlockExecutionError, BlockResult] =
Expand All @@ -358,7 +361,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
"first one wasn't executed correctly" in new BlockExecutionTestSetup {
val invalidStx = SignedTransaction(validTx, ECDSASignature(1, 2, 3.toByte))
val blockBodyWithTxs: BlockBody =
validBlockBodyWithNoTxs.copy(transactionList = Seq(invalidStx, validStxSignedByOrigin.tx))
validBlockBodyWithNoTxs.copy(transactionList = Seq(invalidStx, validStxSignedByOrigin))
val block = Block(validBlockHeader, blockBodyWithTxs)

val txsExecResult: Either[BlockExecutionError, BlockResult] =
Expand Down Expand Up @@ -559,8 +562,14 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
gasLimit = defaultGasLimit * 2,
nonce = validTx.nonce + (if (origin1Address == origin2Address) 1 else 0)
)
val stx1 = SignedTransaction.sign(tx1, keyPair(origin1Address), Some(blockchainConfig.chainId))
val stx2 = SignedTransaction.sign(tx2, keyPair(origin2Address), Some(blockchainConfig.chainId))
val keyPair1 = keyPair(origin1Address)
val keyPair2 = keyPair(origin2Address)

val st1 = SignedTransaction.sign(tx1, keyPair1, Some(blockchainConfig.chainId))
val st2 = SignedTransaction.sign(tx2, keyPair2, Some(blockchainConfig.chainId))

val stx1 = SignedTransactionWithSender(st1, Address(keyPair1))
val stx2 = SignedTransactionWithSender(st2, Address(keyPair2))

val validBlockBodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(stx1.tx, stx2.tx))
val block = Block(validBlockHeader, validBlockBodyWithTxs)
Expand Down
Loading

0 comments on commit e7d3aaf

Please sign in to comment.