简体   繁体   中英

Understanding Blockchain smart contract

I understood some basics of blockchain and Bitcoin by studying over the internet and asking questions to stake overflow. Blockchain: It's a distributed database where records are kept and shared to all connected nodes. Each block of transactions has a hash of its previous block and hence it's immune to tempering. Blocks are added through some consensus mechanisms. Once a block is added to the chain, it cannot be deleted.

Bitcoin: Bitcoin is a cryptocurrency implemented over blockchain technology. It is also the first successful implementation of blockchain technology. The flow of Bitcoin transaction is like below-

Any node connected to the Bitcoin blockchain network can generate transactions that are propagated to all connected nodes. As every node has all previous records, every node first validates the transactions comparing with the sender's and receivers' transaction history.

  1. At a certain point in time, the nodes try to make a block with all pending transactions by using the PoW consensus mechanism.
  2. Here the difficulty of the consensus mechanism is to find the hash of the to block having a number of leading zeros.
  3. If a node gets that targeted number of leading zero in the hash, that node's block will be added to the blockchain. Then other nodes will validate this block.
  4. And it will be finally added to the Bitcoin blockchain database. Then the similar work will continue to add next blocks.

I'm unable to understand how Smart contract works on the blockchain. I know in smart contract each transaction is a kind of computer script/program (eg, if some condition, then execute something). But how the computer program is getting executed? how transactions are getting validated? and how consensus achieved? I'm not getting to understand all these questions.

It would be great if anyone can explain smart contract the way I understood Bitcoin blockchain as explained above.

You should do more research into Ethereum the first blockchain that has smart contracts.

Think of these contracts as conditions or a service level agreements. Let's say that you and I are conducting business and if I don't uphold my end of the bargain the funds go back to you. Otherwise, the transaction is processed.

This is possible through the Ethereum Virtual Machine (EVM). This is another reason Ethereum is expensive, because each block on the chain has some cade that is executed in a virtual environment.

A good project for you is to build your own block chain and create a smart contract using Solidity. Solidity is the programming language on Ethereum that makes smart contracts possible.

Don't use Bitcoin as a reference for figuring out what smart contracts are and how they are included in the blockchain. That is not the purpose of Bitcoin so several pieces will be missing to really understand the integration of smart contracts.

Sorry for not directly following your questions, I just believe that a more detailed explanation is required to really understand how smart contracts are included / executed.

As you are asking about smart contracts let's take the practical example of Ethereum to explain how they are included in the blockchain and how to interact with them. ( The main idea is similar for other smart contract blockchains )

Accounts:

Let's start by differentiating two types of Ethereum account:

  • Externally Owned Accounts (EOA)
  • Contract Accounts (CA)

Externally Owned Accounts:

An EOA is what you already know in Bitcoin, a user that holds a private key and a public address. An EOA is autonomous / proactive (You can decide to do something with your account whenever you want).

An EOA can send a public transaction as it can sign it with its private key.

Contract Accounts:

A CA does not have private key but only a public address as you must be able to uniquely identify the smart contract on the blockchain. A CA is reactive only (A CA account actions will always be the result of an EOA action).

A CA account cannot send a public transaction like an EOA would as it cannot sight it with a public key since it doesn't have one.

In Ethereum there is this notion of account state.

Basically a mapping from addresses (EOA and CA) to a specific state.

The state of a specific account contains information's such as the balance and the nonce (number of transactions / contracts that this account created) and for CA this is expanded with code hash and storage hash.

Transactions

A transaction can be:

  • smart contract creation
  • a value/data transfer from account A to account B.

Smart contract creation:

This transaction has no specific receiving address. It includes data containing the smart contract code in the 'data' field. The blockchain state will be updated to account for the new smart contract as long as this transaction is included in a block and consensus is reached.

An EOA address will be deterministically given to your smart contract (see this thread for an explanation on how it is done) and a new mapping is added in the account state for your smart contract containing the contract's code. You can now send transfer transactions to your smart contract address.

Value/Data transfer from account A to account B.

Now let's say you want to call the method "setName" of your smart contract. You or anyone can send a transaction from your account address (A) to the smart contract address (B) with the relevant information in the 'data' field (which method to call, parameters, etc..)

The execution itself will take place in the Ethereum Virtual Machine (EVM) where your contracts code will be interpreted and any change to the account states will be recorded.

With all of that in mind I suppose the answer to your questions will be clearer:

But how the computer program is getting executed?

It is executed inside the EVM on nodes. This execution will find it's origin in an EOA transaction calling the smart contract. The result of the execution is the changes to the account states.

how transactions are getting validated?

Smart contracts cannot issue transactions as they are referenced as CA accounts. The original EOA transaction creating or calling the smart contract is validated as any other transaction and submitted to the normal consensus scheme of the blockchain.

The confusion might come from the fact that a smart contract can send funds to another address (EOA or CA), but they don't do so in the form of a public transaction.

This is possible because smart contracts are deterministic, therefore if you have the blockchain you have all the smart contract creation and smart contract calls in the form of transactions in the blockchain, and executing a smart contract in the EVM can change the account state. so if you send funds from (account A - EOA) to (account B - CA) that forwards them to (account C - EOA or CA) inside the EVM during its execution, it is recorded and replayable as everything is deterministic.

So if a smart contracts does something illegal ( spending more than its balance ) the original transaction that triggered it is invalid.

how consensus achieved?

There is no explicit consensus for smart contracts actions, it only directly concern blocks and transactions.

While the whole answer is quite Ethereum specific, I hope it gives you a better idea of the integration of smart contracts in a blockchain system.

Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. You should not think only about Bitcoin or Ethereum. NEAR is a great option too. I was searching for someone who can help me with my investment discovery and 4IRE is one of the opportunities. They have a lot of valuable information that can help you with your research like Smart Contract and Smart Contract Audit , especially when you know that they are pro on it. I`m sure it could be useful for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM