Skip to content

Commit

Permalink
Logging test names, test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuharnikov committed Nov 2, 2024
1 parent 1294c0c commit 6cb30e0
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)")
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait BaseItTestSuite
with CustomMatchers
with EitherValues
with OptionValues
with ReportingTestName
with HasRetry
with HasConsensusLayerDappTxHelpers {
override val currentHitSource: ByteStr = ByteStr.empty
Expand Down
39 changes: 26 additions & 13 deletions consensus-client-it/src/test/scala/units/BridgeE2CTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,22 +16,33 @@ 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 {
def burnedTokens = ec1.web3j.ethGetBalance(ElBridgeClient.BurnAddress.hex, DefaultBlockParameterName.LATEST).send().getBalance
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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
19 changes: 19 additions & 0 deletions consensus-client-it/src/test/scala/units/ReportingTestName.scala
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/units/Bridge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions src/test/scala/units/E2CTransfersTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
)
Expand Down Expand Up @@ -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))

Expand All @@ -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())

Expand Down

0 comments on commit 6cb30e0

Please sign in to comment.