Skip to content

Commit

Permalink
L2-380 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuharnikov committed Oct 31, 2024
1 parent bee0913 commit 3588126
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ class NodeHttpApi(apiUri: Uri, backend: SttpBackend[Identity, ?]) extends HasRet
}
}

def assetQuantity(asset: IssuedAsset)(implicit loggingOptions: LoggingOptions = LoggingOptions()): Long = {
log.debug(s"${loggingOptions.prefix} assetQuantity($asset)")
basicRequest
.get(uri"$apiUri/assets/details/$asset?full=false")
.response(asJson[AssetDetailsResponse])
.tag(LoggingOptionsTag, loggingOptions)
.send(backend)
.body match {
case Left(HttpError(body, statusCode)) => failRetry(s"Server returned error $body with status ${statusCode.code}")
case Left(DeserializationException(body, error)) => failRetry(s"failed to parse response $body: $error")
case Right(r) => r.quantity
}
}

def waitForConnectedPeers(atLeast: Int): Unit = {
implicit val loggingOptions: LoggingOptions = LoggingOptions(logRequestBody = false)
log.debug(s"${loggingOptions.prefix} waitForConnectedPeers($atLeast)")
Expand Down Expand Up @@ -181,6 +195,11 @@ object NodeHttpApi {
implicit val assetBalanceResponseFormat: OFormat[AssetBalanceResponse] = Json.format[AssetBalanceResponse]
}

case class AssetDetailsResponse(quantity: Long)
object AssetDetailsResponse {
implicit val assetDetailsResponseFormat: OFormat[AssetDetailsResponse] = Json.format[AssetDetailsResponse]
}

case class ConnectedPeersResponse(peers: List[JsObject])
object ConnectedPeersResponse {
implicit val connectedPeersResponseFormat: OFormat[ConnectedPeersResponse] = Json.format[ConnectedPeersResponse]
Expand Down
59 changes: 59 additions & 0 deletions consensus-client-it/src/test/scala/units/BridgeC2ETestSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package units

import com.wavesplatform.transaction.TxHelpers
import units.eth.EthAddress

class BridgeC2ETestSuite extends OneNodeTestSuite {
protected val clSender = clRichAccount1
protected val elReceiver = elRichAccount1
protected val userAmount = 1
protected val wavesAmount = UnitsConvert.toWavesAmount(userAmount)

"L2-380 Checking balances in CL->EL transfers" in {
def clAssetQuantity: Long = waves1.api.assetQuantity(waves1.chainContract.token)
def chainContractBalance: Long = waves1.api.balance(chainContractAddress, waves1.chainContract.token)

val clAssetQuantityBefore = clAssetQuantity
val chainContractBalanceBefore = chainContractBalance

waves1.api.broadcastAndWait(
chainContract.transfer(
sender = clSender,
destElAddress = EthAddress.unsafeFrom(elReceiver.getAddress),
asset = waves1.chainContract.token,
amount = wavesAmount
)
)

val chainContractBalanceAfter = chainContractBalance
withClue("1. Chain contract balance unchanged: ") {
chainContractBalanceAfter shouldBe chainContractBalanceBefore
}

val clAssetQuantityAfter = clAssetQuantity
withClue("1. Tokens burned: ") {
clAssetQuantityAfter shouldBe (clAssetQuantityBefore - wavesAmount)
}
}

override def beforeAll(): Unit = {
super.beforeAll()

log.info("Prepare: issue tokens on chain contract and transfer to a user")
waves1.api.broadcastAndWait(
TxHelpers.reissue(
asset = waves1.chainContract.token,
sender = chainContractAccount,
amount = wavesAmount
)
)
waves1.api.broadcastAndWait(
TxHelpers.transfer(
from = chainContractAccount,
to = clSender.toAddress,
amount = wavesAmount,
asset = waves1.chainContract.token
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ package units
import com.wavesplatform.account.KeyPair
import com.wavesplatform.api.http.ApiError.ScriptExecutionError
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.settings.Constants
import com.wavesplatform.utils.EthEncoding
import org.web3j.protocol.core.DefaultBlockParameterName
import org.web3j.protocol.core.methods.request.EthFilter
import org.web3j.protocol.core.methods.response.EthLog
import org.web3j.utils.Convert
import units.client.engine.model.GetLogsResponseEntry
import units.eth.EthAddress

import scala.jdk.CollectionConverters.CollectionHasAsScala

class BridgeTestSuite extends TwoNodesTestSuite {
class BridgeE2CTestSuite extends TwoNodesTestSuite {
"L2-379 Checking balances in EL->CL transfers" in {
val elSender = elRichAccount1
val clRecipient = clRichAccount1
val userAmount = 1
val wavesAmount = UnitsConvert.toWavesAmount(userAmount)

log.info("Broadcast Bridge.sendNative transaction")
val ethAmount = Convert.toWei(userAmount.toString, Convert.Unit.ETHER).toBigIntegerExact

def bridgeBalance = ec1.web3j.ethGetBalance(ec1.elBridge.address.hex, DefaultBlockParameterName.LATEST).send().getBalance
val bridgeBalanceBefore = bridgeBalance
val sendTxnReceipt = ec1.elBridge.sendNativeAndWait(elSender, clRecipient.toAddress, ethAmount)
val sendTxnReceipt = ec1.elBridge.sendNativeAndWait(elSender, clRecipient.toAddress, UnitsConvert.toEthAmount(userAmount))

val bridgeBalanceAfter = bridgeBalance
withClue("1. The balance of Bridge contract wasn't changed: ") {
Expand Down Expand Up @@ -58,7 +55,6 @@ class BridgeTestSuite extends TwoNodesTestSuite {

val sendTxnLogIndex = rawLogsInBlock.indexWhere(_.getTransactionHash == sendTxnReceipt.getTransactionHash)
val transferProofs = Bridge.mkTransferProofs(transferEvents, sendTxnLogIndex).reverse
val wavesAmount = userAmount * Constants.UnitsInWave

log.info(s"Wait block $blockHash on contract")
val blockConfirmationHeight = retry {
Expand Down Expand Up @@ -105,6 +101,4 @@ class BridgeTestSuite extends TwoNodesTestSuite {
balanceAfter shouldBe (receiverBalanceBefore + wavesAmount)
}
}

"L2-380 Checking balances in CL->EL transfers" in {}
}
11 changes: 11 additions & 0 deletions consensus-client-it/src/test/scala/units/UnitsConvert.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package units

import com.wavesplatform.settings.Constants
import org.web3j.utils.Convert

import java.math.BigInteger

object UnitsConvert {
def toWavesAmount(userAmount: BigDecimal): Long = (userAmount * Constants.UnitsInWave).toLongExact
def toEthAmount(userAmount: BigDecimal): BigInteger = Convert.toWei(userAmount.bigDecimal, Convert.Unit.ETHER).toBigIntegerExact
}

0 comments on commit 3588126

Please sign in to comment.