Skip to content

Commit 4a567b1

Browse files
authored
Merge pull request #74 from knarayan/main
code to query simple CorDapp using linearId
2 parents b1bb533 + 4d494a7 commit 4a567b1

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

docs/docs/external/getting-started.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ The code for this lies in the `core/drivers/fabric-driver` folder.
216216

217217
#### Configuring
218218

219-
In the `core/drivers/fabric-driver` folder, copy `.env.template` to `.env` and update `CONNECTION_PROFILE` to point to the connection profile of the fabric network (e.g. `<PATH_TO_NETWORK_SETUPS_REPO>/fabric/shared/network1/peerOrganizations/org1.network1.com/connection-org1.json`)
219+
In the `core/drivers/fabric-driver` folder, copy `.env.template` to `.env` and update `CONNECTION_PROFILE` to point to the connection profile of the fabric network (e.g. `<PATH-TO-NETWORK-SETUPS>/fabric/shared/network1/peerOrganizations/org1.network1.com/connection-org1.json`)
220220

221221
Configure `fabric-driver` for `network1` as follows:
222222
- Navigate to the `core/drivers/fabric-driver` folder.
@@ -373,11 +373,15 @@ Once you have the networks, the relays, and the drivers, running, and the ledger
373373

374374
## Corda to Fabric
375375

376-
To test the scenario where `Corda_Network` requests the value of the state (key) `Arcturus` from `network1`, do the following:
376+
To test the scenario where `Corda_Network` requests the value of the state (key) `a` from `network1`, do the following:
377377
- Navigate to the `samples/corda-simple-application` folder.
378378
- Run the following:
379379
```bash
380-
./clients/build/install/clients/bin/clients request-state localhost:9081 localhost:9080/network1/mychannel:simplestate:Read:Arcturus
380+
./clients/build/install/clients/bin/clients request-state localhost:9081 localhost:9080/network1/mychannel:simplestate:Read:a
381+
```
382+
- Query the value of the requested state (key) `a` in `Corda_Network` using the following (replace LinearId with the CorDapp simplestate linearId value obtained in the previous command):
383+
```bash
384+
./clients/build/install/clients/bin/clients get-state-using-linear-id LinearId
381385
```
382386

383387
To test the scenario where `Corda_Network` requests the value of the state (key) `Arcturus` from `network2`, do the following:
@@ -396,6 +400,10 @@ To test the scenario where `network1` requests the value of the state (key) `H`
396400
```bash
397401
fabric-cli interop --local-network=network1 --sign=true --requesting-org=Org1MSP localhost:9081/Corda_Network/localhost:10006#com.cordaSimpleApplication.flow.GetStateByKey:H --debug=true
398402
```
403+
- Query the value of the requested state (key) `H` in `network1` using the following (replace the Args with the Args value obtained in the previous command):
404+
```bash
405+
fabric-cli interop --local-network=network1 chaincode invoke mychannel simplestate read '["Args"]'
406+
```
399407

400408
To test the scenario where `network2` requests the value of the state (key) `H` from `Corda_Network`, do the following:
401409
- Navigate to the `tests/network-setups/fabric/dev/scripts/fabric-cli` folder.
@@ -414,6 +422,10 @@ To test the scenario where `network1` requests the value of the state (key) `Arc
414422
```bash
415423
fabric-cli interop --local-network=network1 --requesting-org=Org1MSP localhost:9083/network2/mychannel:simplestate:Read:Arcturus
416424
```
425+
- Query the value of the requested state (key) `Arcturus` in `network1` using the following (replace the Args with the Args value obtained in the previous command):
426+
```bash
427+
fabric-cli interop --local-network=network1 chaincode invoke mychannel simplestate read '["Args"]'
428+
```
417429

418430
To test the scenario where `network2` requests the value of the state (key) `a` from `network1`, do the following:
419431
- Navigate to the `tests/network-setups/fabric/dev/scripts/fabric-cli` folder.
@@ -431,7 +443,7 @@ Bring down the various components as follows:
431443
* Navigate to the `tests/network-setups/shared/corda` folder.
432444
* Run the following:
433445
```bash
434-
make clean-local
446+
make clean
435447
```
436448
- To bring down one or both of the Fabric networks:
437449
* Navigate to the `tests/network-setups/fabric/dev` folder.

samples/corda-simple-application/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ Use the command line client to manage state in the network:
5454
./clients/build/install/clients/bin/clients create-state H 1
5555
```
5656

57-
- Query a `SimpleStates` by key
57+
- Query a `SimpleState` by key
5858

5959
```
6060
./clients/build/install/clients/bin/clients get-state H
6161
```
6262

63+
- Query a `SimpleState` by linearId
64+
65+
```
66+
./clients/build/install/clients/bin/clients get-state-using-linear-id LinearId
67+
```
68+
6369
- Query all `SimpleStates`
6470

6571
```

samples/corda-simple-application/clients/src/main/kotlin/com/cordaSimpleApplication/client/CordaClient.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fun main(args: Array<String>) = App()
5050
UpdateStateCommand(),
5151
DeleteStateCommand(),
5252
GetStateCommand(),
53+
GetStateUsingLinearIdCommand(),
5354
GetStatesCommand(),
5455
RequestStateCommand(),
5556
CreateVerificationPolicyCommand(),

samples/corda-simple-application/clients/src/main/kotlin/com/cordaSimpleApplication/client/InteropRequests.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package com.cordaSimpleApplication.client
99
import arrow.core.Either
1010
import arrow.core.Left
1111
import arrow.core.Right
12+
import com.cordaSimpleApplication.flow.CreateState
13+
import com.cordaSimpleApplication.state.SimpleState
1214
import com.cordaInteropApp.flows.CreateExternalRequest
1315
import com.cordaInteropApp.flows.WriteExternalStateInitiator
1416
import com.cordaInteropApp.flows.GetExternalStateByLinearId
@@ -174,7 +176,16 @@ fun writeExternalStateToVault(
174176
.returnValue.get()
175177
}.fold({
176178
it.map { linearId ->
177-
println("Verification was successful and state was stored with linearId $linearId.\n")
179+
println("Verification was successful and external-state was stored with linearId $linearId.\n")
180+
181+
val (valueByteArray, externalStateProof) = proxy.startFlow(::GetExternalStateByLinearId, linearId.toString()).returnValue.get()
182+
val value = valueByteArray.toString(Charsets.UTF_8)
183+
val key = address.split(":").last()
184+
val createdState = proxy.startFlow(::CreateState, key, value)
185+
.returnValue.get().tx.outputStates.first() as SimpleState
186+
println("Created simplestate: ${createdState}")
187+
println("LinearId ${createdState.linearId} can be used to fetch the requested state from the CorDapp simplestate vault")
188+
178189
linearId.toString()
179190
}
180191
}, {

samples/corda-simple-application/clients/src/main/kotlin/com/cordaSimpleApplication/client/SimpleStateManager.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,35 @@ class GetStateCommand : CliktCommand(help = "Gets state by key. Requires a key")
121121
val proxy = rpc.proxy
122122
val states = proxy.startFlow(::GetStateByKey, key)
123123
.returnValue.get()
124-
println(states)
124+
println(states.toString(Charsets.UTF_8))
125+
} catch (e: Exception) {
126+
println(e.toString())
127+
} finally {
128+
rpc.close()
129+
}
130+
}
131+
}
132+
133+
/**
134+
* The CLI command used to trigger a GetStateByLinearId flow.
135+
*
136+
* @property linearId The linearId for the [SimpleState] to be retrieved.
137+
*/
138+
class GetStateUsingLinearIdCommand : CliktCommand(help = "Gets state by linearId. Requires a linearId") {
139+
val linearId: String by argument()
140+
val config by requireObject<Map<String, String>>()
141+
override fun run() {
142+
println("Get state with linearId $linearId")
143+
val rpc = NodeRPCConnection(
144+
host = config["CORDA_HOST"]!!,
145+
username = "clientUser1",
146+
password = "test",
147+
rpcPort = config["CORDA_PORT"]!!.toInt())
148+
try {
149+
val proxy = rpc.proxy
150+
val state = proxy.startFlow(::GetStateByLinearId, linearId)
151+
.returnValue.get()
152+
println(state)
125153
} catch (e: Exception) {
126154
println(e.toString())
127155
} finally {

samples/corda-simple-application/workflows-kotlin/src/main/kotlin/com/example/flow/SimpleFlow.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.cordaSimpleApplication.contract.SimpleContract
1111
import com.cordaSimpleApplication.state.SimpleState
1212
import javassist.NotFoundException
1313
import net.corda.core.contracts.Command
14+
import net.corda.core.contracts.UniqueIdentifier
1415
import net.corda.core.flows.*
1516
import net.corda.core.node.services.queryBy
1617
import net.corda.core.transactions.SignedTransaction
@@ -254,6 +255,30 @@ class GetStateByKey(val key: String) : FlowLogic<ByteArray>() {
254255
}
255256
}
256257

258+
/**
259+
* The GetStateByLinearId flow is used to retrieve a [SimpleState] from the vault based on its linearId.
260+
*
261+
* @property linearId the linearId for the [SimpleState] to be retrieved.
262+
*/
263+
@StartableByRPC
264+
class GetStateByLinearId(val linearId: String) : FlowLogic<String>() {
265+
@Suspendable
266+
267+
/**
268+
* The call() method captures the logic to find a [SimpleState] in the vault based on its linearId.
269+
*
270+
* @return returns the [SimpleState].
271+
*/
272+
override fun call(): String {
273+
val states = serviceHub.vaultService.queryBy<SimpleState>().states
274+
.filter { it.state.data.linearId == UniqueIdentifier.Companion.fromString(linearId) }
275+
.map { it.state.data }
276+
val state = states.first()
277+
println("Retrieved state with linearId $linearId: $state\n")
278+
return state.toString()
279+
}
280+
}
281+
257282
/**
258283
* The GetStates flow is used to retrieve all [SimpleState]s from the vault.
259284
*/

0 commit comments

Comments
 (0)