您好,我正在尝试学习Corda的一些概念。我的系统上本地运行了一个Cordapp。我想与它集成前端。我该怎么办?
答案 0 :(得分:1)
在Corda中,您通过RPC与节点进行交互。您的前端服务器需要:
这是在example中创建与Spring中的节点的RPC连接:
private const val CORDA_USER_NAME = "config.rpc.username"
private const val CORDA_USER_PASSWORD = "config.rpc.password"
private const val CORDA_NODE_HOST = "config.rpc.host"
private const val CORDA_RPC_PORT = "config.rpc.port"
/**
* Wraps a node RPC proxy.
*
* The RPC proxy is configured based on the properties in `application.properties`.
*
* @param host The host of the node we are connecting to.
* @param rpcPort The RPC port of the node we are connecting to.
* @param username The username for logging into the RPC client.
* @param password The password for logging into the RPC client.
* @property proxy The RPC proxy.
*/
@Component
open class NodeRPCConnection(
@Value("\${$CORDA_NODE_HOST}") private val host: String,
@Value("\${$CORDA_USER_NAME}") private val username: String,
@Value("\${$CORDA_USER_PASSWORD}") private val password: String,
@Value("\${$CORDA_RPC_PORT}") private val rpcPort: Int): AutoCloseable {
lateinit var rpcConnection: CordaRPCConnection
private set
lateinit var proxy: CordaRPCOps
private set
@PostConstruct
fun initialiseNodeRPCConnection() {
val rpcAddress = NetworkHostAndPort(host, rpcPort)
val rpcClient = CordaRPCClient(rpcAddress)
val rpcConnection = rpcClient.start(username, password)
proxy = rpcConnection.proxy
}
@PreDestroy
override fun close() {
rpcConnection.notifyServerAndClose()
}
}
这是在Spring控制器中使用RPC连接的example:
/**
* A CorDapp-agnostic controller that exposes standard endpoints.
*/
@RestController
@RequestMapping("/") // The paths for GET and POST requests are relative to this base path.
class StandardController(
private val rpc: NodeRPCConnection) {
companion object {
private val logger = LoggerFactory.getLogger(RestController::class.java)
}
private val proxy = rpc.proxy
@GetMapping(value = "/status", produces = arrayOf("text/plain"))
private fun status() = "200"
@GetMapping(value = "/servertime", produces = arrayOf("text/plain"))
private fun serverTime() = LocalDateTime.ofInstant(proxy.currentNodeTime(), ZoneId.of("UTC")).toString()
@GetMapping(value = "/addresses", produces = arrayOf("text/plain"))
private fun addresses() = proxy.nodeInfo().addresses.toString()
@GetMapping(value = "/identities", produces = arrayOf("text/plain"))
private fun identities() = proxy.nodeInfo().legalIdentities.toString()
@GetMapping(value = "/platformversion", produces = arrayOf("text/plain"))
private fun platformVersion() = proxy.nodeInfo().platformVersion.toString()
@GetMapping(value = "/peers", produces = arrayOf("text/plain"))
private fun peers() = proxy.networkMapSnapshot().flatMap { it.legalIdentities }.toString()
@GetMapping(value = "/notaries", produces = arrayOf("text/plain"))
private fun notaries() = proxy.notaryIdentities().toString()
@GetMapping(value = "/flows", produces = arrayOf("text/plain"))
private fun flows() = proxy.registeredFlows().toString()
@GetMapping(value = "/states", produces = arrayOf("text/plain"))
private fun states() = proxy.vaultQueryBy<ContractState>().states.toString()
}
您会注意到,您需要使用Java类来创建与节点的连接。这意味着将需要使用JVM语言编写Web服务器。另外,您可以使用Graal之类的东西以非JVM语言编写服务器,并将其编译为JVM字节码。有一个示例here。