Upgrading some Corda3 source code to run on v4 - kotlin

First of all, I've only started learning corda 3 months ago so I've got some learning to do.
I've inherited some code that runs fine under Corda v3.3 but the customers want it to run on v4. I'm trying to follow the instructions on the main website. I've got an initiating flow which calls a subflow, which in turn calls a transponder flow.
The initiating flow:
#InitiatingFlow(version = 2)
#StartableByRPC
class TransferFlow(private val issuerName: String = "",
private val seller: String = "",
private val amount: BigDecimal = BigDecimal("0"),
private val buyer: String = "",
private val custodianNameOfBuyer: String = "",
private val notaryName: String = "") : FlowLogic<SignedTransaction>() {
#Suspendable
override fun call(): SignedTransaction {
subFlow(UpdateStatusOfTransferFlow(
sessions,
tokenTransferAgreement.linearId,
"Removed Tokens From Seller"))
}
}
class UpdateStatusOfTransferFlow(
private val sessions: Set<FlowSession>,
private val tokenTransferAgreementID: UniqueIdentifier,
private val newStatus: String) : FlowLogic<SignedTransaction>() {
#Suspendable
override fun call(): SignedTransaction {
sessions.size
val idQueryCriteria = QueryCriteria.LinearStateQueryCriteria(linearId = listOf(tokenTransferAgreementID))
val states = serviceHub.vaultService.queryBy<TokenTransferAgreement>(idQueryCriteria).states
if (states.size != 1) throw FlowException("Can not find a unique state for $tokenTransferAgreementID")
val inputStateAndRef = states.single()
val inputState = inputStateAndRef.state.data
val notary = inputStateAndRef.state.notary
val outputState = inputState.withNewStatus(newStatus)
val cmd = Command(TokenContract.Commands.UpdateStatusOfTransfer(),
inputState.participants.map { it.owningKey })
val txBuilder = TransactionBuilder(notary = notary)
txBuilder.addCommand(cmd)
txBuilder.addInputState(inputStateAndRef)
txBuilder.addOutputState(outputState, TokenContract.ID)
txBuilder.verify(serviceHub)
val ptx = serviceHub.signInitialTransaction(txBuilder)
val sessions2 = (inputState.participants.toSet() - ourIdentity).map { initiateFlow(it) }
return subFlow(CollectSignaturesFlow(ptx, sessions2))
}
}
And the responder:
#InitiatedBy(TransferFlowResponder::class)
class UpdateStatusOfTransferFlowResponder(private val session: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
val tokenTransferAgreements = mutableListOf<TokenTransferAgreement>()
var isBuyer = true
var notary = CordaUtility.getNotary(serviceHub) ?: throw FlowException("An notary is expected!")
val signedTransactionFlow = subFlow(object : SignTransactionFlow(session) {
override fun checkTransaction(stx: SignedTransaction) = requireThat {
"There must be one output!" using (stx.tx.outputStates.size == 1)
val tokenTransferAgreement = stx.tx.outputStates.first() as TokenTransferAgreement
tokenTransferAgreements.add(tokenTransferAgreement)
notary = stx.notary ?: throw FlowException("An notary is expected!")
if (ourIdentity == tokenTransferAgreement.issuer) {
//checks go here
}
})
}
}
I believe I am supposed to add a call to ReceiveFinality flow at some point, however it only takes 1 session as an argument, not a list as I have here. Should I make multiple calls, one for each session? I am also not sure if the calls should go in the transponder or the UpdateStatusOfTransferFlow class.
Help here would be appreciated.

The FinalityFlow is mainly responsible for ensuring transactions are notarized, distributed accordingly and persisted to local vaults.
In previous versions of Corda, all nodes would by default accept incoming requests for finality.
From V4 onwards, you're required to write a ReceiveFinalityFlow to write your own processing logic before finality.
The way finality currently runs in Corda is the initiating node, as an intermediate step during finality, distributes notarised transaction to all other participants. Each of the participating nodes it sends to will only expect to receive a session from this node.
So where you might submit multiple sessions to the initiating FinalityFlow to include all the participants, the responding nodes will only ever receive just the one session from the initiator.
In the future, we may look at having the Notary distribute the notarized transaction to all participants, but even then, the ReceiveFinalityFlow would still only expect one session, this time from the Notary.

Related

Calling subFlow inside a main flow to update the same state in corda

Here in the main flow which is updating the ContainerState, and I just tried to call a subFlow of another UserDefined flow which is also updating attribute of same state.
(I know, it can be done in main flow itself without calling another flow, but this is just a sample created for asking here)
So Before creating builder of main flow, I am calling the subFlow and update it with some parameter. Both are referring to ContainerState of same id.
I am getting
[WARN] 13:07:03,441 [Mock network] interceptors.DumpHistoryOnErrorInterceptor. - Flow [fb6a42fd-b9bf-45bb-ab98-2f2510235f91] error {fiber-id=10000008, flow-id=fb6a42fd-b9bf-45bb-ab98-2f2510235f91, invocation_id=38c29575-637a-459b-b8e9-81a960e93c7c, invocation_timestamp=2022-12-07T13:07:03.064Z, origin=O=Mock Company 1, L=London, C=GB, session_id=38c29575-637a-459b-b8e9-81a960e93c7c, session_timestamp=2022-12-07T13:07:03.064Z, thread-id=406, tx_id=EB1EA4EEA934C9C874461A6DDE35892057EBC472F405AF95241784CEDE3D6C92}
net.corda.core.flows.UnexpectedFlowEndException: Counter-flow errored
at Received unexpected counter-flow exception from peer O=Mock Company 1, L=London, C=GB.() ~[?:?]
Doesn't subflow suspends the main flow and the finalityFlow in subflow will complete all the required steps there?
UpdateFlow:
#InitiatingFlow
#StartableByRPC
class UpdateContainerStateFlow(private val containerId: UUID,
val status: String, val comments: String) : FlowLogic<SignedTransaction>() {
override val progressTracker = ProgressTracker()
#Suspendable
override fun call(): SignedTransaction {
val queryCriteria = QueryCriteria.LinearStateQueryCriteria(linearId = listOf(UniqueIdentifier(id = containerId)))
val currentContainer = serviceHub.vaultService.queryBy<ContainerState>(queryCriteria).states.single()
val notary = serviceHub.networkMapCache.getNotary( CordaX500Name.parse("O=Notary,L=London,C=GB"))
val updateContainer = currentContainer.state.data.copy(
status = status,
commentsFromStakeHolders = comments
)
val abc = subFlow(UpdateContainerStateFlow2(
containerId = containerId,
status = "subflowOne",
comments = "test"
))
val builder = TransactionBuilder(notary)
.addCommand(ContainerContract.Commands.Update(), listOf(updateContainer.logisticCompany.owningKey, updateContainer.partnerCompany.owningKey))
.addInputState(currentContainer)
.addOutputState(updateContainer)
// Step 4. Verify and sign it with our KeyPair.
builder.verify(serviceHub)
val ptx = serviceHub.signInitialTransaction(builder)
val sessions = (updateContainer.participants - ourIdentity).map { initiateFlow(it) }.toSet()
val stx = subFlow(CollectSignaturesFlow(ptx, sessions))
// Step 7. Assuming no exceptions, we can now finalise the transaction
return subFlow(FinalityFlow(stx, sessions))
}
}
#InitiatedBy(UpdateContainerStateFlow::class)
class UpdateContainerStateFlowResponder(val counterpartySession: FlowSession) : FlowLogic<SignedTransaction>() {
#Suspendable
override fun call(): SignedTransaction {
val signTransactionFlow = object : SignTransactionFlow(counterpartySession) {
override fun checkTransaction(stx: SignedTransaction) = requireThat {
//Addition checks
}
}
val txId = subFlow(signTransactionFlow).id
return subFlow(ReceiveFinalityFlow(counterpartySession, expectedTxId = txId))
}
}
UpdateContainerStateFlow2() is nothing but the same.
I think, there is something wrong in my understanding. Please help me to correct it. Thanks.
I got the issue, it was because I was using the same currentContainer
val currentContainer = serviceHub.vaultService.queryBy<ContainerState>(queryCriteria).states.single()
in the transaction builder after the subflow is executed. Therefore Corda will throw error that the input state used is already consumed.

Kotlin Coroutines - cannot return object from room db

I'm not super sure what I'm doing here so go easy on me:
I'm making a wordle clone and the word that is to be guessed is stored as a string in a pre-populated room database which I am trying to retrieve to my ViewModel and currently getting:
"StandaloneCoroutine{Active}#933049a"
instead of the actual data.
I have tried using LiveData which only returned null which as far as I'm aware is because it was not observed.
Switched to coroutines which seemed to make more sense if my UI doesn't need the data anyway.
I ended up with this so far:
DAO:
#Dao
interface WordListDao {
#Query("SELECT word FROM wordlist WHERE used = 0 ORDER BY id DESC LIMIT 1")
suspend fun readWord(): String
// tried multiple versions here only string can be converted from Job
// #Query("SELECT * FROM wordlist WHERE used = 0 ORDER BY id DESC LIMIT 1")
// fun readWord(): LiveData<WordList>
// #Query("SELECT word FROM wordlist WHERE used = 0 ORDER BY id DESC LIMIT 1")
// fun readWord(): WordList
}
repository:
class WordRepository(private val wordListDao: WordListDao) {
//val readWordData: String = wordListDao.readWord()
suspend fun readWord(): String {
return wordListDao.readWord()
}
}
model:
#Entity(tableName = "wordlist")
data class WordList(
#PrimaryKey(autoGenerate = true)
val id: Int,
val word: String,
var used: Boolean
)
VM:
class HomeViewModel(application: Application) : ViewModel() {
private val repository: WordRepository
private var word: String
init {
val wordDb = WordListDatabase.getDatabase(application)
val wordDao = wordDb.wordlistDao()
repository = WordRepository(wordDao)
word = viewModelScope.launch {
repository.readWord()
}.toString()
Log.d("TAG", ": $word") // does nothing?
}
println(word) // StandaloneCoroutine{Active}#933049a
}
This is the only way that I have managed to not get the result of:
Cannot access database on the main thread
There is a better way to do this, I just can't figure it out.
You can access the return value of repository.readWord() only inside the launch block.
viewModelScope.launch {
val word = repository.readWord()
Log.d("TAG", ": $word") // Here you will get the correct word
}
If you need to update you UI when this word is fetched from database, you need to use an observable data holder like a LiveData or StateFlow.
class HomeViewModel(application: Application) : ViewModel() {
private val repository: WordRepository
private val _wordFlow = MutableStateFlow("") // A mutable version for use inside ViewModel
val wordFlow = _word.asStateFlow() // An immutable version for outsiders to read this state
init {
val wordDb = WordListDatabase.getDatabase(application)
val wordDao = wordDb.wordlistDao()
repository = WordRepository(wordDao)
viewModelScope.launch {
_wordFlow.value = repository.readWord()
}
}
}
You can collect this Flow in your UI layer,
someCoroutineScope {
viewModel.wordFlow.collect { word ->
// Update UI using this word
}
}
Edit: Since you don't need the word immediately, you can just save the word in a simple global variable for future use, easy.
class HomeViewModel(application: Application) : ViewModel() {
private lateinit var repository: WordRepository
private lateinit var word: String
init {
val wordDb = WordListDatabase.getDatabase(application)
val wordDao = wordDb.wordlistDao()
repository = WordRepository(wordDao)
viewModelScope.launch {
word = repository.readWord()
}
// word is not available here, but you also don't need it here
}
// This is the function which is called when user types a word and presses enter
fun submitGuess(userGuess: String) {
// You can access the `word` here and compare it with `userGuess`
}
}
The database operation will only take a few milliseconds to complete so you can be sure that by the time you actually need that original word, it will have been fetched and stored in the word variable.
(Now that I'm at a computer I can write a bit more.)
The problems with your current code:
You cannot safely read from the database on the main thread synchronously. That's why the suspend keyword would be used in your DAO/repository. Which means, there is no way you can have a non-nullable word property in your ViewModel class that is initialized in an init block.
Coroutines are asychronous. When you call launch, it is queuing up the coroutine to start its work, but the launch function returns a Job, not the result of the coroutine, and your code beneath the launch call continues on the same thread. The code inside the launch call is sent off to the coroutines system to be run and suspend calls will in most cases, as in this case, be switching to background threads back and forth. So when you call toString() on the Job, you are just getting a String representation of the coroutine Job itself, not the result of its work.
Since the coroutine does its work asynchronously, when you try to log the result underneath the launch block, you are logging it before the coroutine has even had a chance to fetch the value yet. So even if you had assigned the result of the coroutine to some String variable, it would still be null by the time you are logging it.
For your database word to be usable outside a coroutine, you need to put it in something like a LiveData or SharedFlow so that other places in code can subscribe to it and do something with the value when it arrives.
SharedFlow is a pretty big topic to learn, so I'll just use LiveData for the below samples.
One way to create a LiveData using your suspend function to retrieve the word is to use the liveData builder function, which returns a LiveData that uses a coroutine under the hood to get the value to publish via the LiveData:
class HomeViewModel(application: Application) : ViewModel() {
private val repository: WordRepository = WordListDatabase.getDatabase(application)
.wordDb.wordlistDao()
.let(::WordRepository)
private val word: LiveData<String> = liveData {
repository.readWord()
}
val someLiveDataForUi: LiveData<Something> = Transformations.map(word) { word ->
// Do something with word and return result. The UI code can
// observe this live data to get the result when it becomes ready.
}
}
To do this in a way that is more similar to your code (just to help with understanding, since this is less concise), you can create a MutableLiveData and publish to the LiveData from your coroutine.
class HomeViewModel(application: Application) : ViewModel() {
private val repository: WordRepository
private val word = MutableLiveData<String>()
init {
val wordDb = WordListDatabase.getDatabase(application)
val wordDao = wordDb.wordlistDao()
repository = WordRepository(wordDao)
viewModelScope.launch {
word.value = repository.readWord()
}
}
val someLiveDataForUi: LiveData<Something> = Transformations.map(word) { word ->
// Do something with word and return result. The UI code can
// observe this live data to get the result when it becomes ready.
}
}
If you're not ready to dive into coroutines yet, you can define your DAO to return a LiveData instead of suspending. It will start reading the item from the database and publish it through the live data once it's ready.
#Dao
interface WordListDao {
#Query("SELECT word FROM wordlist WHERE used = 0 ORDER BY id DESC LIMIT 1")
fun readWord(): LiveData<String>
}
class HomeViewModel(application: Application) : ViewModel() {
private val repository: WordRepository = WordListDatabase.getDatabase(application)
.wordDb.wordlistDao()
.let(::WordRepository)
private val word: LiveData<String> = repository.readWord()
//...
}
The return value is as expected, because launch does always return a Job object representing the background process.
I do not know how you want to use the String for, but all operations which should be done after receiving the String must be moved inside the Coroutine or in a function which is called from the Coroutine.
viewModelScope.launch {
val word = repository.readWord()
// do stuff with word
// switch to MainThread if needed
launch(Dispatchers.Main){}
}

Axonframework event scheduler keeps rerunning my event infinitely

So I want to create a simple food order service, but this service require some information from the other service so I use saga pattern. Here's how it should work if I order a food, first it will attempt to create order but if there's any error it will retry for 3 times and publish either success or failed event.
Here's the sample code.
#Saga
class OrderCreationSaga {
#Transient
#Autowired
private lateinit var commandGateway: CommandGateway
#Transient
#Autowired
private lateinit var eventScheduler: EventScheduler
#Transient
#Autowired
private lateinit var eventBus: EventBus
#Transient
#Autowired
private lateinit var scheduleToken: ScheduleToken
private lateinit var orderId: String
private var retryCounter = 1
#StartSaga
#SagaEventHandler(associationProperty = "orderId")
fun on(event: OrderCreationAttempted) {
this.orderId = event.orderId
eventBus.publish(GenericEventMessage(event.toOrderCreationRequested()))
}
#SagaEventHandler(associationProperty = "orderId")
fun on(event: OrderCreationRequested) {
try {
// send data to another service
orderCreationService.createOrder(event).block()
eventBus.publish(GenericEventMessage(
OrderCreationSuccess(
orderId = event.orderId
))
)
} catch (error: Throwable) {
// catching request error, retry for 3 times
if (this.retryCounter == 3) {
eventBus.publish(GenericEventMessage(
OrderCreationFailed(
orderId = this.orderId,
)
))
} else {
eventBus.publish(GenericEventMessage(
OrderCreationRetry(
orderId = event.orderId,
)
))
this.retryCounter++
}
}
}
#EndSaga
#SagaEventHandler(associationProperty = "orderId")
fun on(event: OrderCreationSuccess) {
// do the success job
}
#EndSaga
#SagaEventHandler(associationProperty = "orderId")
fun on(event: OrderCreationFailed) {
// do the failed job
}
#SagaEventHandler(associationProperty = "orderId")
fun on(event: OrderCreationRetry) {
val duration = Duration.ofSeconds(30)
val scheduleEvent = OrderCreationRequested(orderId = event.orderId)
scheduleToken = eventScheduler.schedule(duration, scheduleEvent)
}
}
But the weird thing happens so after it published a success event it will publish a OrderCreationRequested event again for some reason (I know this because I've checked the event log inside axonserver). This keeps looping infinitely, is this because my code or some configuration or could be something else?
So the problem was I forgot to set my username and password for my MongoDB and then someone just trying to delete all of my data including tracking token for axon-server. So because of the tracking token has been delete axon-server start creating the new one with 0 value which makes all the event start rerunning again and again. I solve this problem by just add the username and password for my MongoDB.

CUBA Platform push messages from backend to UI

i was wondering if it is possible to send messages from the backend (for example a running task that receives information from an external system) to the UI. In my case it needs to be a specific session (no broadcast) and only on a specific screen
plan B would be polling the backend frequently but i was hoping to get something more "realtime"
I was trying to work something out like this, but i keep getting a NotSerializableException.
#Push
class StorageAccess : Screen(), MessageListener {
#Inject
private lateinit var stationWSService: StationWebSocketService
#Inject
private lateinit var notifications: Notifications
#Subscribe
private fun onInit(event: InitEvent) {
}
#Subscribe("stationPicker")
private fun onStationPickerValueChange(event: HasValue.ValueChangeEvent<StorageUnit>) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
val prevValue = event.prevValue
if (prevValue != null) {
stationWSService.remove(current.userSession.id)
}
val value = event.value ?: return
stationWSService.listen(current.userSession.id, value, this)
}
override fun messageReceived(message: String) {
val current = AppUI.getCurrent()
current.access {
notifications.create().withCaption(message).show()
}
}
#Subscribe
private fun onAfterDetach(event: AfterDetachEvent) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
stationWSService.remove(current.userSession.id)
}
}
-- The callback interface
interface MessageListener : Serializable {
fun messageReceived(message: String);
}
-- The listen method of my backend service
private val listeners: MutableMap<String, MutableMap<UUID, MessageListener>> = HashMap()
override fun listen(id: UUID, storageUnit: StorageUnit, callback: MessageListener) {
val unitStationIP: String = storageUnit.unitStationIP ?: return
if (!listeners.containsKey(unitStationIP))
listeners[unitStationIP] = HashMap()
listeners[unitStationIP]?.set(id, callback)
}
The Exception i get is NotSerializableException: com.haulmont.cuba.web.sys.WebNotifications which happens during adding the listener to the backend: stationWSService.listen(current.userSession.id, value, this)
as far as i understand this is the place where the UI sends the information to the backend - and with it the entire status of the class StorageAccess, including all its members.
is there an elegant solution to this?
regards
There is an add-on that solves exactly this problem: https://github.com/cuba-platform/global-events-addon

Kotlin - How do i set a sharedpreference code so that i open the closed app, it opens the last Activity, where i left it

I made two activities in my application, I want the app to be opened where I left off. In other words, not the default activity but the activity where I was when I last exited the app.
You could set a SplashActivity, where your app start, that will start other activities.
In this SplashActivity, you can set a var lastActivity, that will be a code to keep in which activity you were last time.
You get it with SharedPreference, and then go to the activity.
i.e :
String lastActivity = SharedPreference.getString(...) // I don't really remember the syntax
if (lastActivity == "HelloWorldActivity")
startActivity(HelloWorldActivity.getStartIntent(context))
else if (lastActivity == "GoodByeActivity")
startActivity(GoodByeActivity.getStartIntent(context))
Then, do NOT forget to edit your SharedPreference value EACH TIME you change activity.
I don't know if this is a good practice, but feel free to test this and give your think.
EDIT
First, you need to understand how is Shared Preference File. I think it looks like this :
"app_name"="Your app name"
"last_activity"="Your last activity"
"user_age"="23"
This could be the first activity you launch :
class SplashActivity : AppCompatActivity() {
var lastActivity = ""
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate()
/*
Here, we will get the SharedPreferencesFile.
Then, we get the value linked to the key TAG_LAST_ACTIVITY (set in companion object)
*/
val sharedPref = this.getSharedPreferences(getString(R.string.shared_preference_file_name), 0)
lastActivity = sharedPref.getString(TAG_LAST_ACTIVITY, "")
var activityToStart : AppCompatActivity? = null
if (lastActivity.isBlank())
activityToStart = YourActivityToStartAtFirstLaunch.getStartIntent(this)
else if (lastActivity.equals(TAG_ACTIVITY_ONE))
activityToStart = ActivityOne.getStartIntent(this)
else if (lastActivity.equals(TAG_ACTIVITY_TWO))
activityToStart = ActivityTwo.getStartIntent(this)
else if
... // Use as many as else if you need, but think about the "when" condition, it is better !
startActivity(activityToStart)
}
companion object {
private const val TAG_LAST_ACTIVITY = "last_activity"
private const val TAG_ACTIVITY_ONE = "activity_one"
private const val TAG_ACTIVITY_TWO = "activity_two"
}
}
And this could be your ActivityOne, for example :
class ActivityOne : AppCompatActivity() {
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate()
/*
Here, we will modify the variable LAST_ACTIVITY in the shared preferences file by setting it to "activity_one".
So, if the user quit this app now, you will know at next launch in which activity he stopped.
I think it is a better practice to set this in the onPause() or onStopped() method. Think about it ! ;)
*/
val sharedPrefEditor = this.getSharedPreferences(getString(R.string.shared_preference_file_name, 0)).edit()
sharedPrefEditor.putString(TAG_LAST_ACTIVITY, TAG_ACTIVITY_ONE)
sharedPrefEditor.apply()
}
companion object {
fun getStartIntent(context : Context) : Intent = Intent(context, ActivityOne()::class.java)
private const val TAG_ACTIVITY_ONE = "activity_one"
private const val TAG_LAST_ACTIVITY = "last_activity"
}
}
Do not forget to put your shared preference file name in your values/strings.xml file :
<string name="shared_preference_file_name">com.example.yourappname.sharedpref"</string>