In Corda, TransactionResolutionException when verifying a transaction - corda

In a Corda flow, I am receiving and verifying a transaction as follows:
#InitiatedBy(Initiator::class)
class Responder(val counterpartySession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
val stx = counterpartySession.receive<SignedTransaction>().unwrap { it }
stx.verify(serviceHub, false)
}
}
However, this is throwing the following exception:
net.corda.core.contracts.TransactionResolutionException: Transaction
resolution failure for
3D90346DD7F7397479312EF4DD5A4741F4CA31C2070BC4F8A0588974B1CD1523
What is the cause of this TransactionResolutionException, and how can I fix it?

When a node verifies a transaction, it doesn't just verify the transaction itself. It also verifies the entire transaction chain. A TransactionResolutionException indicates that one or more of the transactions in the transaction chain is missing and cannot be verified.
To avoid this, you should send transactions using SendTransactionFlow, and receive transactions using ReceiveTransactionFlow. This flow pair does two things:
It sends the transaction in question
It allows the receiving node to download from the counterparty all the transactions they are missing in the chain of the transaction they are receiving

In the node you are verifying you don't have the TransactionState that the passed input references too.
It is not correct to pass transaction for signing using Send or SendAndReceive.
It is better to use flows SendTransactionFlow and ReceiveTransactionFlow which will download the states those inputs reference.
If you can't send a SignedTransaction as above flows require (because for example you are sending a FilteredTransaction), you can send it with FlowSession.send() method, but the owner of the input responds with SendStateAndRefFlow flow, and the requester can receive it ReceiveStateAndRefFlow to add the inputs on the original transaction.
This will allow to have the referenced states of the inputs in all nodes, allowing you to verify the transaction.

Related

R3- Corda transaction without notary signature

I am new to Corda and I want to know the role of the notary in detail. As per the document it says, for example a transaction happens between Party A and Party B , notary will sign the transaction and the role of the notary is to prevent double-spending. Can we have a transaction without notary signature. In TransactionBuilder class its saying var notary: Party? Notary used for the transaction. If null, this indicates the transaction DOES NOT have a notary. As per the syntax we can set it as null and if we put it as null whether the transaction will be valid and how the corda prevent double-spending. Please correct me if I misunderstood the concept.
The notary signature is only required in the following cases:
If your transaction has inputs: To testify that those inputs were not consumed, preventing double spends.
If your transaction has reference states: To testify that those reference states were not consumed, preventing the use of "out-dated" reference data.
If your transaction has a time-window: To testify that the transaction was finalized within the specified time-window.
You can see here the function needsNotarySignature() inside FinalityFlow which proves the above points.
I agree with Adel's answer on the notary signature requirement for the cases mentioned by him. To add to that, a transaction is needed to be associated with a notary, it should not be null.
One case where one might think that a notary is not required is an issuance of a state. Actually, the signature of notary is not required, however, the transaction still needs a notary since the state needs to be tagged to a notary to prevent any double-spend when the state is consumed.
In case no notary is attached to it the state can be sent to different notaries and that could lead to double spend.
You could validate this in the TransactionBuilder's addOutput method:
fun addOutputState(
state: ContractState,
contract: ContractClassName = requireNotNullContractClassName(state),
constraint: AttachmentConstraint = AutomaticPlaceholderConstraint
): TransactionBuilder {
checkNotNull(notary) { "Need to specify a notary for the state, or set a default one on TransactionBuilder initialisation" }
addOutputState(state, contract, notary!!, constraint = constraint)
return this
}

How verify function is working in Cordapp

I have developed one sample cordapp where I am instantiating the state object inside the InitiatorFlow.
BookingState outputState = new BookingState(customerName, customerAge,checkInDate,checkOutDate,roomType,roomRate,bookingAmount,getOurIdentity(),lemonTree);
Will this statement automatically call the verify method which presents inside the Contract class?
or Do I need to call the verify method inside the Flow class?
Do TransactionBuilder need verify method?
Will the below code will collect the signature from counterparty? I am asking because in maximum cases I have seen this code to collect the counterparty signature. (SignedTransaction stx = subFlow(new CollectSignaturesFlow(ptx, session));)
private final ProgressTracker.Step GATHERING_SIGNATURE = new ProgressTracker.Step("Gathering the counterparty's signature"){
#Override
public ProgressTracker childProgressTracker() {
return CollectSignaturesFlow.Companion.tracker();
}
}
Answering your question in the same order:
No, the initiator of the flow must explicitly call the verify() function before signing it locally.
Same as above.
The code that you shared does not collect signatures; it only defines that step in the progress tracker so it prints it out on your node's shell when you run the flow. However calling CollectSignaturesFlow will send the transaction to all provided counter-parties FlowSession, and receive back their signatures when they call SignTransactionFlow in the responder flow. See an example of calling CollecSignaturesFlow here.
Looking at your code:
If you call CollectSignaturesFlow in initiator, you must call SignTransactionFlow in responder; otherwise your initiator flow will hang, because it has a receive (which is called inside CollectSignaturesFlow), and that receive will wait until a responder flow will send something, and in that case the responder must call SignTransactionFlow which internally has a send to send the signed transaction by the counter-party. See IOU example here.
The reason why your flow still verifies the transaction and throws an error when you supply a faulty one is because you call FinalityFlow inside your initiator which implicitly verifies the transaction before notarizing it (see here).
You might ask, why do I need to call transactionBuilder.verify() if the notary already does it? Well, why sign the transaction and send it to other parties without verifying it first? You're consuming resources and sending messages between nodes (to send and receive signed transactions); instead, first check whether the transaction is valid or not, then sign it locally and send it to other peers. Btw, counter-parties in responder flow also run the verify() method implicitly when they receive the transaction through calling SignTransactionFlow (see here).
Below is a sample code snippet to answer your questions.
// 1. This will only create BookingState instance
BookingState outputState = new BookingState(customerName, customerAge,checkInDate,checkOutDate,roomType,roomRate,bookingAmount,getOurIdentity(),lemonTree);
TransactionBuilder transactionBuilder = new TransactionBuilder(notary);
transactionBuilder.addOutputState(outputState);
transactionBuilder.addCommand(new BookingContract.Commands.Issue() ,
ImmutableList.of(issuer.getOwningKey(),owner.getOwningKey()));
//2. You should call the verify method from transactionbuilder class which will //run the verify method of contract class.
transactionBuilder.verify(getServiceHub());
//3. Below code is used to collect signatures from the counterparty
FlowSession session = initiateFlow(owner);
SignedTransaction fullySignedTransaction = subFlow(new CollectSignaturesFlow(signedTransaction, singletonList(session)));

java.lang.IllegalArgumentException: Flow sessions were not provided for the following transaction participants:

I have a Use case where I have to send complaint details (Complaint is a ledger ie IOUState.class)to two operators ( say JIO and Airtel) from Sender operator (TTL)
State class constructor has complaint details and three Operators (Party objects), One sender and two receivers.
The first part of the user case is executing fine and transaction/complaints details are getting logged in Vault state/Linear state.
The second part of the transaction involves one of the receiver operator sending complaint resolution response to the sender operator and should not be sending compliant response to third operator. ie Say complaint is related to Airtel then Airtel will respond to TTL and JIO needs to be out of loop.
I have written one state class and two flow classes.
But when I execute the second flow (Airtel to TTL) it is throwing java.lang.IllegalArgumentException: Flow sessions were not provided for the following transaction participants: [O=PartyJIO, L=MUMBAI, C=IN]
I have created the transaction with Command requiring only two Signers , TTL and Airtel but dont know why still getting the error
//Flow class in Airtel Node, TAP is TTL Node
List requiredSigners = Arrays.asList(getOurIdentity().getOwningKey(), TAP.getOwningKey());
Command command = new Command<>(new IOUContract.Create(), requiredSigners);
txBuilder = new TransactionBuilder(notary)
.addInputState(IOUState1.get(0))
.addOutputState(outputState, IOUContract.ID)
.addCommand(command);
FlowSession otherPartySession1 = initiateFlow(TAP); //TAP is TTL Party Object
//Otherpartysession1 is getting executed at Airtel node
SignedTransaction fullySignedTx = subFlow(new CollectSignaturesFlow(
signedTx, Arrays.asList(otherPartySession1), CollectSignaturesFlow.tracker()));
java.lang.IllegalArgumentException: Flow sessions were not provided for the following transaction participants:
SignedTransaction fullySignedTx = subFlow(new CollectSignaturesFlow(
signedTx, Arrays.asList(otherPartySession1), CollectSignaturesFlow.tracker()));
java.lang.IllegalArgumentException: Flow sessions were not provided for the following transaction participants:
(Developer Relations # R3 here)
In Corda 4, you are required to pass FinalityFlow a list of sessions that includes all of the transaction's participants, so that the transaction can be distributed accordingly.
Just because someone is in this list of participants, it does not make them a required signer. The required signers are determined by the public keys listed on the transaction's commands.
Anthony Keenan also looked at this and, on this page found out
The PartyB that is resolved from the transaction has an owning key that is different to the owning key in serviceHub.myInfo.legalIdentities so it thinks it's an 'external participant' and expects a flow session passing in.
So.. it may be the case you have recreated your keys somehow.

Corda Walking the Chain in finalityFlow

In Corda, FinalityFlow:
Verifies transaction on initiator node
Notarizes transaction
Persists signedTransaction to vault of initiator
Distributes transaction to the participants
As per consensus, verification involves walking the chain.
I looked in FinalityFlow code. Where exactly does the walking-the-chain thing happen?
Do the notary and the participants also walk the chain? If yes, they check the signatures on each transaction in the chain, but where exactly in the code does it happen?
As per my understanding, SendTransactionFlow sends the transaction to the other parties on the participants lists. The other party also requests for attachments and transaction dependencies. Where actually does the walking-the-chain thing happen?
I need to understand walking the chain from a coding perspective.
In FinalityFlow, the caller uses the following line to send the notarised transaction to the participants of all the states:
subFlow(SendTransactionFlow(session, notarised))
If we look at AbstractNode.installCoreFlows, we see that the node installs a default handler for FinalityFlow called FinalityHandler. FinalityHandler responds to the call to SendTransactionFlow in FinalityFlow by calling ReceiveTransactionFlow.
Inside ReceiveTransactionFlow, we can see that the node resolves the transaction's dependencies, verifies the transaction and checks its signatures:
val stx = otherSideSession.receive<SignedTransaction>().unwrap {
subFlow(ResolveTransactionsFlow(it, otherSideSession))
it.verify(serviceHub, checkSufficientSignatures)
it
}
As part of resolving the transaction's dependencies in ResolveTransactionsFlow, the node verifies each one and checks its signatures (by default, verify checks the signatures on the transaction):
result.forEach {
it.verify(serviceHub)
serviceHub.recordTransactions(StatesToRecord.NONE, listOf(it))
}
The notary will only walk the chain in this way if they are a validating notary.

Corda: User interaction for verifying the transaction request received from the initiator node

We have a use case which requires the following steps:
(1) Initiator triggers the transaction flow through UI
(2) The flow is initiated, signed by the initiator and sent to recipient for his verification and signatures (in Corda)
(3) The initiator's flow should get suspended until the recipient validates the transaction by verifying the contract code and submits "verified" again through the UI
(4) This should restart the initiator's flow and the remaining process should be followed as expected in Corda
It was mentioned a few weeks back that user interaction is not yet supported in Corda; is this feature still not present? In the future, we may even want to add the state's attributes through a UI since it gives us the flexibility to propose a transaction we want rather than have it hard-coded. Any idea if this could be possible in future releases?
See the Negotiation Cordapp sample for an example of how this would work in practice here.
Suspending a flow for human interaction isn't currently implemented (as of Corda V3.0).
Instead, you'd implement this by adding a status flag to your state:
class FooState(
override val participants: List<Party>,
val accepted: Boolean) : ContractState
You'd have three commands:
interface Commands : CommandData {
class Propose : Commands
class Reject: Commands
class Accept: Commands
}
And two flows:
A proposal flow: In this flow, the initiator creates and signs a Propose transaction to issue the state onto the ledger with a Propose command and the accepted flag set to false
An accept flow: In this flow, the recipient either:
Creates a Reject transaction that consumes the proposed state and outputs nothing. The state has been removed from the ledger and is effectively rejected
Creates an Accept transaction that updates the proposed state so that accepted is true. The state has now been accepted, and this fact is registered on the ledger
You'd give the accept flow a parameter which determines whether or not to accept the proposal. This parameter would be provided by the user when the flow is kicked off either via an API or directly over RPC.

Resources