Skip to content

Instantly share code, notes, and snippets.

@kayvank
Last active August 26, 2018 03:17
Show Gist options
  • Save kayvank/813cc78068bf0edb14e18d825b26e1ba to your computer and use it in GitHub Desktop.
Save kayvank/813cc78068bf0edb14e18d825b26e1ba to your computer and use it in GitHub Desktop.
Proxy service to deploy & propose a Rholang contract
package coop.rchain.service
import coop.rchain.domain.{Err, ErrorCode, DeployAndProposeResponse}
import coop.rchain.repo.RholangProxy
import coop.rchain.utils.Globals._
import scala.util.{Failure, Success, Try}
object RholangContractProxy {
val (host, port) =
(appCfg.getString("grpc.host"), appCfg.getInt("grpc.ports.external"))
val grpc = RholangProxy(host, port)
def apply(): RholangContractProxy = new RholangContractProxy(grpc)
def apply(grpc: RholangProxy): RholangContractProxy =
new RholangContractProxy(grpc)
}
class RholangContractProxy(grpc: RholangProxy) {
val immersionConstract: String => Either[Err, String] = fileName => {
lazy val uri = scala.io.Source.fromURI(getClass.getResource(fileName).toURI)
Try(
uri.getLines.reduce(_ + _ + "\n")
) match {
case Success(s) =>
uri.close
Right(s)
case Failure(e) =>
uri.close
Left(Err(ErrorCode.contractFile, fileName, None))
}
}
def deployAndPropose(
fileName: String): Either[Err, DeployAndProposeResponse] =
for {
r <- immersionConstract(fileName)
e <- grpc.deployAndPropse(r)
} yield e
}
package coop.rchain.repo
import coop.rchain.casper.protocol._
import coop.rchain.domain.{Err, ErrorCode}
import com.google.protobuf.empty._
import coop.rchain.models.Channel.ChannelInstance.Quote
import coop.rchain.models.{Channel, Par}
import coop.rchain.models.Expr.ExprInstance.GString
import io.grpc.ManagedChannelBuilder
import coop.rchain.domain._
object RholangProxy {
def apply(host: String, port: Int): RholangProxy =
new RholangProxy(host, port)
}
class RholangProxy(host: String, port: Int) {
private lazy val channel =
ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build
private lazy val deployService = DeployServiceGrpc.blockingStub(channel)
def deployContract(contract: String) = {
val resp = deployService.doDeploy(
DeployData()
.withTerm(contract)
.withTimestamp(System.currentTimeMillis())
.withPhloLimit(0)
.withPhloPrice(0)
.withNonce(0)
.withFrom("0x1")
)
if (resp.success)
Right(resp.message)
else Left(Err(ErrorCode.grpcDeploy, resp.message, Some(contract)))
}
def showBlocks =
deployService.showBlocks(Empty()).toList
def proposeBlock = {
val response: DeployServiceResponse = deployService.createBlock(Empty())
response.success match {
case true => Right(response.message)
case false => Left(Err(ErrorCode.grpcPropose, response.message, None))
}
}
def deployAndPropse(contract: String) = {
for {
d <- deployContract(contract)
p <- proposeBlock
} yield DeployAndProposeResponse(d, p)
}
def dataAtName(uName: String) = {
import coop.rchain.models.rholang.implicits._
val par: Par = GString(uName)
val ch: Channel = Channel(Quote(par))
val rep = deployService.listenForDataAtName(ch)
rep
}
}
package coop.rchain.repo
import com.typesafe.scalalogging.Logger
import coop.rchain.casper.protocol.ListeningNameDataResponse
import coop.rchain.domain.{DeployAndProposeResponse, Err}
import coop.rchain.models.{Channel, Par}
import coop.rchain.models.Channel.ChannelInstance.Quote
import coop.rchain.models.Expr.ExprInstance.GString
import org.specs2._
import coop.rchain.utils.Globals._
import coop.rchain.service.RholangContractProxy
class RholangProxySpec extends Specification { def is =s2"""
Rnode Specification
deploy and propose a contract file $e1
show names at that the proposed contract name $e2
"""
val log=Logger[RholangProxySpec]
val host = appCfg.getString("grpc.host")
val grpc = RholangProxy("localhost", 40401)
val contractProxy = RholangContractProxy(grpc)
def e1 = {
val computed:Either[Err, DeployAndProposeResponse] = contractProxy.deployAndPropose("/rho/userContract.rho")
log.info(s"Deployed & proposed block: ${computed.toOption.get}")
computed.isRight === true
}
def e2 = {
import coop.rchain.casper.util.rholang._
val uName= InterpreterUtil.mkTerm("""@["Immersion",newUserId"]""")
val computed= grpc.dataAtName("@Immersion")
log.info(s"dataAtName= ${computed}")
computed.toOption.isDefined === true
}
def e3 = {
val computed= grpc.showBlocks
log.debug(s"show-blocks = ${computed}")
computed.toOption.isDefined === true
}
def e4_deploy = {
val grpc = RholangProxy("localhost", 40401)
val contract =
"""@["Immersion", "newUserId"]!("userid-123456789")"""
val deploy_result = grpc.deployContract(contract)
val proposeResult = grpc.proposeBlock
val computed : ListeningNameDataResponse= grpc.dataAtName(
"""@["Immersion", "newUserId"]""" )
computed.blockResults.foreach(x => (log.debug(s"--- blockInfo==> ${x}")))
computed.status.isEmpty === false
}
def e5 = {
val grpcDeploy = RholangProxy("localhost", 40401)
val computed : ListeningNameDataResponse= grpcDeploy.dataAtName(
"""@["Immersion", "newUserId"]""" )
log.debug(s"listening on name= ${computed}")
computed.status.isEmpty === false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment