Using Path constraints
The classes for Paths
and PathConstraints
are all provided in the ContractUtlis.kt
file, this means that the verifyPathConstraints() function is actually very simple to write:
Use
ContractUtlis
to extract the Path from the transaction being verified.Create
allowedPaths
which uses a when statement to map the input status to the PathConstraints that should be applied for that status.You now have a single
requireThat
statement that passes thetxPath
and theallowedPaths
to theverifyPath()
helper function inContractUtils
that returns true if the path is allowed and false if it is not.
ContractUtils.kt:
fun <T: StatusState> verifyPathConstraints(tx: LedgerTransaction, primaryStateClass: Class<T>){
val commandValue = tx.commands.requireSingleCommand<AgreementContract.Commands>().value // get the command
val txPath = getPath(tx, primaryStateClass, commandValue) // call the getPath() utility function to get the Path of the transaction
val inputStatus = requireSingleInputStatus(tx, primaryStateClass) // get the Primary state status
val allowedPaths: List<PathConstraint<T>> = when (inputStatus){ // populate the when clause mapping: statuses -> allowed constraints
null -> listOf(
PathConstraint(Commands.Propose(), PROPOSED, MultiplicityConstraint(0))
)
PROPOSED -> listOf(
PathConstraint(Commands.Reject(), REJECTED),
PathConstraint(Commands.Agree(), AGREED)
)
REJECTED -> listOf(
PathConstraint(Commands.Repropose(), PROPOSED)
)
AGREED -> listOf(
PathConstraint(Commands.Complete(), null, outputMultiplicityConstraint = MultiplicityConstraint(0))
)
else -> listOf()
}
requireThat {
"txPath must be allowed by PathConstraints for inputStatus $inputStatus." using verifyPath(txPath, allowedPaths) // call the utility function to check the paths
}
}
Because a lot of the heavy lifting has been moved to the ContractUtils.kt
, this pattern can be replicated for other Smart Contracts by substituting in the specific statuses and the PathConstraints
.
Was this page helpful?
Thanks for your feedback!
Chat with us
Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.
Propose documentation improvements directly
Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.
We're sorry this page wasn't helpful. Let us know how we can make it better!
Chat with us
Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.
Create an issue
Create a new GitHub issue in this repository - submit technical feedback, draw attention to a potential documentation bug, or share ideas for improvement and general feedback.
Propose documentation improvements directly
Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.