13

I've read a few other questions/answers on smart contract consensus by nodes in the network, but I still don't understand the steps. Here's how I picture it:

  1. Two parties might agree upon a smart contract, sign with their private keys
  2. Nodes validate that those parties are who they say they are
  3. Smart contract is appended to the blockchain

Let's say that the contract was a simple payout from Adam to Bob depending on the day of the week (Monday = 1 bubblecoin, Tuesday = 2 bubblecoins, etc.)

Part of the code might look something like this:

If Monday, send 1 from Adam to Bob, else 
If Tuesday, send 2 from Adam to Bob, else 
If Wednesday, send 3 from Adam to Bob....

My question is, if we ran the code on Wednesday, what do those nodes do? Do they check to make sure the code within the contract is the same as what every other node sees? Or do they actually run the code, get the output (here it would be 3 from Adam to Bob), and compare that OUTPUT with others in the system? If they compare output, what if the output is different between nodes? I'm hoping that providing an example contract will help me visualize what is actually being agreed upon by the nodes.

3 Answers 3

5

what is actually being agreed upon by the nodes

Many details, but at a high level 2 main ones are:

  • the state of the blockchain, including the exact contract code
  • the behavior and results of that code when it is executed (technical term is the state transition function)

More granular details include where the code is on the blockchain, who put it on the blockchain, how much "coins" an account has, etc.

Do they check to make sure the code within the contract is the same as what every other node sees?

An Ethereum node follows the rules defined by the Ethereum protocol. If something doesn't follow the rules, the node effectively ignores it. A node gets data from other nodes, but processes the data according to the protocol. A node is more concerned about following the Ethereum protocol, rather than if other nodes are following the protocol. But it is important for a node to have connections to other nodes that are following the protocol, otherwise the node doesn't have the data to join and be part of the Ethereum network.

...what if the output is different between nodes?

Nodes that follow the Ethereum protocol should always be in agreement because the protocol is deterministic and carefully defined. Basically, if one node is following the protocol, and another node produces a different output, only the first node is part of the network: the latter node is ignored. When a definition is missing in the Ethereum protocol, nodes can disagree, and to resolve the disagreement, the protocol is updated, and nodes are upgraded to obtain the new rules and form consensus again.

2

To run a smart-contract you need to interact with it, its not self-executable

Interaction can only be possible by sending it a transaction (sending ethers to it). So if you send ETH on Wednesday it checked to see the condition and do as you specified on the code.

compare that OUTPUT with others in the system?

No, it will not compare the output to the other nodes, because after every 11 seconds of a transaction miners verifies it by mining.

If they compare output, what if the output is different between nodes? It cannot be different. If you run the same code in different machines the output must have been the same, its promised by design.

0

TL;DR

Date is always based on the time and date when the block was mined.


Longer answer.

Ethereum smart contracts use a deterministic code.

Solidity is one of the deterministic languages that makes you able to generate Ethereum EVM deterministic code.

Deterministic code means that if you call a function using some parameters, you will always get the same result.

Parameters are defined by the transaction used to call that contract function.

Deterministic code thus can't provide expressions that wouldn't give the same result each time you execute it with the same parameters. Deterministic code will not contain things like random functions.

The example in your question is If Monday, send 1 from Adam to Bob, else this can be better translated to If it was monday at the moment the transaction was included in the mined block, send 1 from Adam to Bob, else. This is fully deterministic and every time you will replay the function it will give the same result as the date the block was mined never changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.