How can i access vault in SmartContract? - corda

How can I access vault in Smart Contract?
I want to do below business validation in Smart Contract
- New Data and attachment which I have entered, already exists in vault or not

You cannot access the vault, or any other source of outside information, from within the contract. This is because contract execution must be deterministic. If a contract's view of the validity of a ledger update depended on the current contents of your vault, disagreements could arise between different nodes (or even within the same node at different points in time) on whether a given ledger update was valid. This would destroy the integrity of the ledger - there would be no consensus on which updates were valid.
In your case, it might be best to impose the additional constraints you want to impose within the flow. For example, within the flow you could check the contents of the proposed transaction against the contents of the vault, and sign or not sign the transaction accordingly.
It's important to keep in mind - just because a transaction is contractually valid, does not mean you have to sign it!

Related

How Do I Customise The Corda Hospital?

As you can see from this other question a flow is being sent to the hospital when a unique db constraint is being violated
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation:
This is clearly never going to be able to be resolved so I want it to fail instead and not go to the hospital.
It is currently going to the hospital due to Cordas built-in rules.
Is it possible to modify these rules to prevent this exception from being sent to the hospital?
Unfortunately, according to the official documentation; this type of errors will go to the flow hospital:
Database constraint violation (ConstraintViolationException): This scenario may occur due to natural contention between racing flows as Corda delegates handling using the database’s optimistic concurrency control. If this exception occurs, the flow will retry. After retrying a number of times, the errored flow is kept in for observation.
So you have 2 things that you can do:
Go to the database and modify the existing record that's colliding with the record the flow is trying to add.
Go to your node's terminal and kill the flow.

How exactly is immutability handled in Corda

Since the data is stored in RDBMS it could be accessed and changed later at any point by any party.
And any reference to the changed data could be a problem.
Does the notaries storage (which can't be queried) or the Merkle trees play the role?
Immutability is handled in Corda through the use of digital signatures.
You are correct in saying that Corda transactions are backed by RDBS, however, you wouldn't be able to change 'data' without invalidating the signatures that have been applied to them.
The Notary doesn't act as secondary storage, the Notary (or rather Notaries as they always operate in clusters) is responsible for preventing double spends.
A simple example might be, I create a transaction where I send you 3 tokens, we both sign this transaction to make it valid (according to some contract rules we've defined). If you were to then change the 'data' backing this transaction to instead reflect 4 tokens, you would invalidate the signatures.
You could recreate your own signature, but you wouldn't be able to recreate mine. If you now tried to spend this state reflecting 4 tokens, it would be rejected by the new receiving party.

Self / automatic / no signing state

Would it be possible to have a state that doesn't require signing by a counterparty?
My usecase is that there should be pieces of data that everyone has access to, but only the person owning the data can decide the correctness and should thus be the only party signing the state. (Calculations will be done in the contract and checks will be done before creating the state)
Not sure sure if my question is clear, so please let me know if I should elaborate some more?
Yes.
A transaction is required to be signed by every Party listed on every Command in the transaction.
So if your transaction's Commands only list the owner of the data as a Party, the owner of the data will be the only required signer.

Do we really need a notary that validates?

At the risk of sounding naive, I ask myself "Is a validating notary necessary?", given all the issues with it - transaction and dependencies leak, exposure of state model, to name a few.
The answer I hear has something to do with the potential attack where a dishonest node tries to steal someone else's asset. For example, in a legitimate transaction, partyA sold partyB some asset S subject to a Move contract. Immediately afterwards, partyA creates a self-signed transaction that transfers S back to himself subject to a dummy contract in a bogus flow that does not even run the ledger transaction verify(). But when he calls FinaltyFlow to satisfy the simple notary to commit the transaction on the ledger, it will fail the verifyContracts() because S appoints to the Move contract which should say owner partyB must sign the bogus transaction.
So that does not convince me of the need for a validating notary.
Apparently, I must have missed something. Can someone enlighten me?
Thanks.
\Sean
As you say, the advantage of the validating notary is that it prevents a malicious party from "wedging" a state - that is, creating an invalid transaction that consumes an unconsumed state that they are aware of. Although the transaction is invalid, the non-validating notary would not be aware of this, and would mark the state as spent.
You're correct that this invalid transaction would fail the FinalityFlow's call to verifyContracts(). However, we cannot force a malicious node to use FinalityFlow to send something to the notary. If the malicious node was sufficiently motivated, they could build the invalid transaction hash by hand and send that to the notary directly, for example.
However, note that in the non-validating notary case, there are still multiple layers of protection against wedging:
The malicious party has to be aware of the state(s) they want to wedge. Since information in Corda is only distributed on a need-to-know basis, the node would only be aware of a small subset of unconsumed states
If the state is wedged by accident, the node can show the invalid transaction to the notary. Upon seeing that the transaction is invalid, the notary will mark the re-mark the state as unconsumed
As Corda is a permissioned network, the notary can keep a record of the legal identity of everyone who submits a transaction. If a malicious node is submitting invalid transactions to consume states, their identity will be known, and the dispute can be resolved outside of the platform, based on the agreements governing participation in the network
On the other hand, SGX addresses the data leak issue associated with validating notaries. If the validation of the transaction occurs within an SGX enclave, the validating notary gains no information about the contents of the transaction they are validating.
Ultimately, the choice between validating and non-validating notaries comes down to the threat model associated with a given deployment. If you are using Corda in a setting where you are sure the participants won't deliberately change their node's code or act maliciously, then a validating notary is not needed.
But if you assume somebody WILL try to cheat and would be willing to write their own code to do so, then a validating notary provides an extra layer of protection.
So Corda provides choices:
Choose to reveal more to a notary cluster if you trust the participants relatively less...
Choose to reveal less to a notary cluster if you consider the risk of revealing too much to the notaries to be the bigger problem
(and use SGX if you're paranoid about everybody!)

Consensus and Notaries

I've been trying to understand the concepts of Consensus and Notaries, there are some questions:
do all nodes/participants require to do the verification consensus in order to assure the transaction inputs, outputs are valid? Does it somehow resemble verifying the Bitcoin input transactions?
uniqueness consensus is a must when involving unique successor of a state (aka preventing double spending) and by using uniqueness consensus, notary service is involved?
(this one confused me) in notary services, containing verifying and non-verifying notaries, does the verifying notary do the same way as the verifying consensus to validate all transactions (going back to the very beginning of each state to check the signatures & correctness), that’s why it is called verifying notary service?
Do both notary services will store the map locally for checking consumed state of the proposed transactions? If yes, how can I see the details of the map?
Thanks and cheers,
No. Only the participants involved in a given transaction, plus the transaction's notary, verify a given transaction. This is core to Corda's privacy-first approach, where transaction information is only distributed on a need-to-know basis.
Yes, each transaction is assigned to a specific notary service (there may be several on the network), and the notary ensures that there are no double-spends.
That's correct. The non-verifying notary simply checks that the transaction's inputs haven't already been spent. The verifying notary also "walks the chain" to ensure that the transaction is valid. You have a choice between using a non-verifying and a verifying notary for each transaction, depending on how much information you wish to disclose.
Both types of notaries store the information about which states have been spent locally. Only the notary itself can see the details of this map, by checking the NODE_NOTARY_COMMIT_LOG in its H2 database.

Resources