From 6cb30e0143761f3ee958f772f576d8d7c7c54261 Mon Sep 17 00:00:00 2001 From: Vyatcheslav Suharnikov Date: Sat, 2 Nov 2024 09:55:11 +0400 Subject: [PATCH] Logging test names, test updates --- .../com/wavesplatform/api/NodeHttpApi.scala | 14 +++++++ .../test/scala/units/BaseItTestSuite.scala | 1 + .../test/scala/units/BridgeE2CTestSuite.scala | 39 ++++++++++++------- .../test/scala/units/OneNodeTestSuite.scala | 5 +++ .../test/scala/units/ReportingTestName.scala | 19 +++++++++ .../test/scala/units/TwoNodesTestSuite.scala | 5 +++ .../test/scala/units/el/ElBridgeClient.scala | 4 +- src/main/scala/units/Bridge.scala | 3 ++ .../scala/units/E2CTransfersTestSuite.scala | 13 ++++--- 9 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 consensus-client-it/src/test/scala/units/ReportingTestName.scala diff --git a/consensus-client-it/src/test/scala/com/wavesplatform/api/NodeHttpApi.scala b/consensus-client-it/src/test/scala/com/wavesplatform/api/NodeHttpApi.scala index d51d7e69..b626ab97 100644 --- a/consensus-client-it/src/test/scala/com/wavesplatform/api/NodeHttpApi.scala +++ b/consensus-client-it/src/test/scala/com/wavesplatform/api/NodeHttpApi.scala @@ -7,6 +7,7 @@ import com.wavesplatform.api.LoggingBackend.{LoggingOptions, LoggingOptionsTag} import com.wavesplatform.api.NodeHttpApi.* import com.wavesplatform.api.http.ApiMarshallers.TransactionJsonWrites import com.wavesplatform.api.http.TransactionsApiRoute.ApplicationStatus +import com.wavesplatform.api.http.`X-Api-Key` import com.wavesplatform.state.DataEntry.Format import com.wavesplatform.state.{DataEntry, EmptyDataEntry, TransactionId} import com.wavesplatform.transaction.Asset.IssuedAsset @@ -26,6 +27,17 @@ class NodeHttpApi(apiUri: Uri, backend: SttpBackend[Identity, ?]) extends HasRet protected override implicit val patienceConfig: PatienceConfig = PatienceConfig(timeout = averageBlockDelay, interval = 1.second) + def print(message: String): Unit = + basicRequest + .post(uri"$apiUri/debug/print") + .body(Json.obj("message" -> message)) + .header(`X-Api-Key`.name, ApiKeyValue) + .response(asJsonEither[ErrorResponse, BroadcastResponse]) + .send(backend) + .body + .left + .foreach { e => throw new RuntimeException(e) } + def waitForHeight(atLeast: Int): Int = { val loggingOptions: LoggingOptions = LoggingOptions() log.debug(s"${loggingOptions.prefix} waitForHeight($atLeast)") @@ -175,6 +187,8 @@ class NodeHttpApi(apiUri: Uri, backend: SttpBackend[Identity, ?]) extends HasRet } object NodeHttpApi { + val ApiKeyValue = "testapi" + case class HeightResponse(height: Int) object HeightResponse { implicit val heightResponseFormat: OFormat[HeightResponse] = Json.format[HeightResponse] diff --git a/consensus-client-it/src/test/scala/units/BaseItTestSuite.scala b/consensus-client-it/src/test/scala/units/BaseItTestSuite.scala index 76986468..6bf2062d 100644 --- a/consensus-client-it/src/test/scala/units/BaseItTestSuite.scala +++ b/consensus-client-it/src/test/scala/units/BaseItTestSuite.scala @@ -32,6 +32,7 @@ trait BaseItTestSuite with CustomMatchers with EitherValues with OptionValues + with ReportingTestName with HasRetry with HasConsensusLayerDappTxHelpers { override val currentHitSource: ByteStr = ByteStr.empty diff --git a/consensus-client-it/src/test/scala/units/BridgeE2CTestSuite.scala b/consensus-client-it/src/test/scala/units/BridgeE2CTestSuite.scala index 13d0c936..0d8f63af 100644 --- a/consensus-client-it/src/test/scala/units/BridgeE2CTestSuite.scala +++ b/consensus-client-it/src/test/scala/units/BridgeE2CTestSuite.scala @@ -5,6 +5,7 @@ import com.wavesplatform.api.http.ApiError.ScriptExecutionError import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.utils.EthEncoding import org.web3j.protocol.core.DefaultBlockParameterName +import org.web3j.protocol.core.methods.response.TransactionReceipt import org.web3j.protocol.exceptions.TransactionException import org.web3j.utils.Convert import units.el.ElBridgeClient @@ -15,14 +16,25 @@ class BridgeE2CTestSuite extends TwoNodesTestSuite { protected val userAmount = 1 protected val wavesAmount = UnitsConvert.toWavesAmount(userAmount) + protected def sendNative(amount: BigInt = UnitsConvert.toWei(userAmount)): TransactionReceipt = + ec1.elBridge.sendNative(elSender, clRecipient.toAddress, amount) + "L2-264 Amount should % 10 Gwei" in { - try ec1.elBridge.sendNativeAndWait(elSender, clRecipient.toAddress, BigInt(1)) + try sendNative(1) catch { case e: TransactionException => val encodedRevertReason = e.getTransactionReceipt.get().getRevertReason val revertReason = ElBridgeClient.decodeRevertReason(encodedRevertReason) revertReason shouldBe "Sent value 1 must be greater or equal to 10000000000" } + + try sendNative(Convert.toWei("11", Convert.Unit.GWEI).longValueExact()) + catch { + case e: TransactionException => + val encodedRevertReason = e.getTransactionReceipt.get().getRevertReason + val revertReason = ElBridgeClient.decodeRevertReason(encodedRevertReason) + revertReason shouldBe "Sent value 11000000000 must be a multiple of 10000000000" + } } "L2-325 Sent tokens burned" in { @@ -30,7 +42,7 @@ class BridgeE2CTestSuite extends TwoNodesTestSuite { val burnedTokensBefore = BigInt(burnedTokens) val transferAmount = Convert.toWei("10", Convert.Unit.GWEI).longValueExact() - ec1.elBridge.sendNativeAndWait(elSender, clRecipient.toAddress, transferAmount) + sendNative(transferAmount) val burnedTokensAfter = BigInt(burnedTokens) burnedTokensAfter shouldBe (transferAmount + burnedTokensBefore) @@ -40,10 +52,10 @@ class BridgeE2CTestSuite extends TwoNodesTestSuite { log.info("Broadcast Bridge.sendNative transaction") def bridgeBalance = ec1.web3j.ethGetBalance(ec1.elBridge.address.hex, DefaultBlockParameterName.LATEST).send().getBalance val bridgeBalanceBefore = bridgeBalance - val sendTxnReceipt = ec1.elBridge.sendNativeAndWait(elSender, clRecipient.toAddress, UnitsConvert.toWei(userAmount)) + val sendTxnReceipt = sendNative() - val bridgeBalanceAfter = bridgeBalance withClue("1. The balance of Bridge contract wasn't changed: ") { + val bridgeBalanceAfter = bridgeBalance bridgeBalanceAfter shouldBe bridgeBalanceBefore } @@ -91,17 +103,18 @@ class BridgeE2CTestSuite extends TwoNodesTestSuite { if (currFinalizedHeight < blockConfirmationHeight) fail("Not yet finalized") } - def receiverBalance: Long = waves1.api.balance(clRecipient.toAddress, waves1.chainContract.token) - val receiverBalanceBefore = receiverBalance + withClue("3. Tokens received: ") { + log.info( + s"Broadcast withdraw transaction: transferIndexInBlock=$sendTxnLogIndex, amount=$wavesAmount, " + + s"merkleProof={${transferProofs.map(EthEncoding.toHexString).mkString(",")}}" + ) - log.info( - s"Broadcast withdraw transaction: transferIndexInBlock=$sendTxnLogIndex, amount=$wavesAmount, " + - s"merkleProof={${transferProofs.map(EthEncoding.toHexString).mkString(",")}}" - ) - waves1.api.broadcastAndWait(withdraw()) + def receiverBalance: Long = waves1.api.balance(clRecipient.toAddress, waves1.chainContract.token) + val receiverBalanceBefore = receiverBalance - val balanceAfter = receiverBalance - withClue("3. Tokens received: ") { + waves1.api.broadcastAndWait(withdraw()) + + val balanceAfter = receiverBalance balanceAfter shouldBe (receiverBalanceBefore + wavesAmount) } } diff --git a/consensus-client-it/src/test/scala/units/OneNodeTestSuite.scala b/consensus-client-it/src/test/scala/units/OneNodeTestSuite.scala index 3627ff63..334ba7cc 100644 --- a/consensus-client-it/src/test/scala/units/OneNodeTestSuite.scala +++ b/consensus-client-it/src/test/scala/units/OneNodeTestSuite.scala @@ -65,4 +65,9 @@ trait OneNodeTestSuite extends BaseItTestSuite { log.info(s"Wait for #$epoch1Number epoch") waves1.api.waitForHeight(epoch1Number) } + + override protected def print(text: String): Unit = { + super.print(text) + waves1.api.print(text) + } } diff --git a/consensus-client-it/src/test/scala/units/ReportingTestName.scala b/consensus-client-it/src/test/scala/units/ReportingTestName.scala new file mode 100644 index 00000000..012530a5 --- /dev/null +++ b/consensus-client-it/src/test/scala/units/ReportingTestName.scala @@ -0,0 +1,19 @@ +package units + +import org.scalatest.{Args, Status, SuiteMixin} + +trait ReportingTestName extends SuiteMixin { + self: BaseItTestSuite => + + abstract override protected def runTest(testName: String, args: Args): Status = { + printWrapped(s"Test '$testName' started") + val r = super.runTest(testName, args) + printWrapped(s"Test '$testName' ${if (r.succeeds()) "SUCCEEDED" else "FAILED"}") + r + } + + private def printWrapped(text: String): Unit = print(s"---------- $text ----------") + + protected def print(text: String): Unit = + log.debug(text) +} diff --git a/consensus-client-it/src/test/scala/units/TwoNodesTestSuite.scala b/consensus-client-it/src/test/scala/units/TwoNodesTestSuite.scala index d9f2e39e..9606b1c5 100644 --- a/consensus-client-it/src/test/scala/units/TwoNodesTestSuite.scala +++ b/consensus-client-it/src/test/scala/units/TwoNodesTestSuite.scala @@ -113,4 +113,9 @@ trait TwoNodesTestSuite extends BaseItTestSuite { waves1.api.waitForHeight(epoch1Number) } + override protected def print(text: String): Unit = { + super.print(text) + waves1.api.print(text) + waves2.api.print(text) + } } diff --git a/consensus-client-it/src/test/scala/units/el/ElBridgeClient.scala b/consensus-client-it/src/test/scala/units/el/ElBridgeClient.scala index 7bfe0e26..ac084d42 100644 --- a/consensus-client-it/src/test/scala/units/el/ElBridgeClient.scala +++ b/consensus-client-it/src/test/scala/units/el/ElBridgeClient.scala @@ -15,13 +15,13 @@ import units.eth.EthAddress import java.util.Collections class ElBridgeClient(web3j: Web3j, val address: EthAddress) extends HasRetry with ScorexLogging { - def sendNativeAndWait( + def sendNative( sender: Credentials, recipient: Address, amountInEther: BigInt ): TransactionReceipt = { val senderAddress = sender.getAddress - log.debug(s"sendNativeAndWait($senderAddress->$recipient: $amountInEther Ether)") + log.debug(s"sendNative($senderAddress->$recipient: $amountInEther Ether)") val bridgeContract = BridgeContract.load(address.hex, web3j, sender, new DefaultGasProvider) bridgeContract.send_sendNative(recipient.publicKeyHash, amountInEther.bigInteger).send() } diff --git a/src/main/scala/units/Bridge.scala b/src/main/scala/units/Bridge.scala index 5760258c..2b395a53 100644 --- a/src/main/scala/units/Bridge.scala +++ b/src/main/scala/units/Bridge.scala @@ -27,6 +27,9 @@ object Bridge { val ElSentNativeEventTopic = org.web3j.abi.EventEncoder.encode(ElSentNativeEventDef) + /** @param amount + * In waves units, see bridge.sol + */ case class ElSentNativeEvent(wavesAddress: Address, amount: Long) object ElSentNativeEvent { diff --git a/src/test/scala/units/E2CTransfersTestSuite.scala b/src/test/scala/units/E2CTransfersTestSuite.scala index 6881ccf5..30be7445 100644 --- a/src/test/scala/units/E2CTransfersTestSuite.scala +++ b/src/test/scala/units/E2CTransfersTestSuite.scala @@ -102,15 +102,16 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { } } - "Deny withdrawals with a non-positive amount" in forAll( + "L2-265 Deny withdrawals with invalid amount" in forAll( Table( "index", + Long.MinValue, 0L, - Long.MinValue + transfer.amount - 1 ) ) { amount => withExtensionDomain() { d => - step(s"Start new epoch with ecBlock") + step("Start new epoch with ecBlock") d.advanceNewBlocks(reliable.address) val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) d.ecClients.addKnown(ecBlock) @@ -143,7 +144,7 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { } } - "Can't get transferred tokens twice" in { + "L2-273 Can't get transferred tokens twice" in { val settings = defaultSettings.copy( additionalBalances = List(AddrWithBalance(transferReceiver.toAddress, defaultFees.chainContract.withdrawFee * 2)) ) @@ -184,7 +185,7 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { withExtensionDomain(settings) { d => step(s"Start new epoch with ecBlock1") d.advanceNewBlocks(reliable.address) - val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(List(transferEvent)) + val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(List(transferEvent)) def tryWithdraw(): Either[Throwable, BlockId] = d.appendMicroBlockE(d.chainContract.withdraw(transferReceiver, ecBlock1, transferProofs, 0, transfer.amount)) @@ -209,7 +210,7 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { withExtensionDomain(settings) { d => step(s"Start new epoch with ecBlock1") d.advanceNewBlocks(reliable.address) - val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(List(transferEvent.copy(data = "d3ad884fa04292"))) + val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(List(transferEvent.copy(data = "d3ad884fa04292"))) d.ecClients.willForge(ecBlock1) d.ecClients.willForge(d.createEcBlockBuilder("0-0", reliable).build())