When a contract is triggered, all nodes execute the code; where exactly does consensus come in? Are they agreeing upon the OUTPUT or just the code itself?
What I expect should be happening from my reading of the docs (I have not personally traced through the code or the consensus process yet):
- Each mining node selects a list of pending transactions from the transaction pool and executes these transaction (including the insertion and execution of smart contract code).
- The results from the above executions will change the balances of accounts and the data stored with smart contract code (including the insertion of new contract code into the blockchain).
- The transactions and updated data are packaged into a block and the transaction hash + block hash + nonce is iteratively solved to calculate a hash. If this computed hash is less than a target number (determined by the current difficulty level), the mining node has solved the block and broadcasts this winning block results across the P2P network.
- Each node receiving this broadcasted block executes the transactions and smart contract code in the order specified by the winning block, and records the resulting changes to account balances and data stored with smart contract code.
- Each node then computes the transaction hash + block hash + nonce and verifies that the computed hash us less than the target number (determined by the current difficulty level).
- If the computed hash is less than the target, each node accepts the winning block as valid and adds this to it's copy of the blockchain.
In summary, I expect that all nodes execute the code (and transactions) and verify the block "numbers" are valid before accepting the block as valid in the consensus process.
EDIT 12/05/2016 - As stated by
@Nick Johnson, the state tree is compared as this provides a checksum of the data in the current and all previous blocks.
Q: Are they agreeing upon the OUTPUT or just the code itself?
A: They are agreeing on the output based on the execution of each transaction (including running the code).
The consensus is on the contract's values for certain block after each node executed the contract independently.
You are welcome to browse various contract and check the in consensus state: