简体   繁体   中英

How do transactions take place in a blockchain?

I'm very new to blockchain technology. As a part of project, I am trying to develop a blockchain application for e-voting. In many projects which I've seen on github the solidity it was as shown below

pragma solidity ^0.4.11;
// We have to specify what version of compiler this code will compile with

contract Voting {
  /* mapping field below is equivalent to an associative array or hash.
  The key of the mapping is candidate name stored as type bytes32 and value is
  an unsigned integer to store the vote count
  */
  
  mapping (bytes32 => uint8) public votesReceived;
  
  /* Solidity doesn't let you pass in an array of strings in the constructor (yet).
  We will use an array of bytes32 instead to store the list of candidates
  */
  
  bytes32[] public candidateList;



  /* This is the constructor which will be called once when you
  deploy the contract to the blockchain. When we deploy the contract,
  we will pass an array of candidates who will be contesting in the election
  */
  function Voting(bytes32[] candidateNames) {
    candidateList = candidateNames;
  }

  // This function returns the total votes a candidate has received so far
  function totalVotesFor(bytes32 candidate) returns (uint8) {
    if (validCandidate(candidate) == false) throw;
    return votesReceived[candidate];
  }

  // This function increments the vote count for the specified candidate. This
  // is equivalent to casting a vote
  function voteForCandidate(bytes32 candidate) {
    if (validCandidate(candidate) == false) throw;
    votesReceived[candidate] += 1;
  }

  function validCandidate(bytes32 candidate) returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
      }
    }
    return false;
  }
}

So what data from this is created as a block in the blockchain? And how exactly does this code create transactions in the blockchain?

It's the other way around.

  1. Transaction is created by a client application (in a form on JSON object) and signed by the sender's private key
  2. sent to a node
  3. and broadcasted to the network by the node, where it waits in the mempool (list of transactions not yet mined) to be mined.
  4. A miner includes it in a block
  5. If the transaction recipient is this smart contract, miner of the transaction executes it to be able to calculate its state changes. Also, after it's included in a block, all nodes validate and project the state changes on their side.

So to sum it up: This code doesn't create transactions. But it's executed when a transaction to a contract that contains this code is mined.


Example:

Your code is deployed on address 0x123 . A sender sends a transaction to your contract 0x123 , with data field (of the transaction) stating that they want to execute the function voteForCandidate() with bytes32 candidate having value 0x01 .

When the transaction gets mined, the miner executes the contract in their EVM instance and calculates the state changes, that result in incrementing storage value of votesReceived[0x01] . Then broadcasts this information (altogether with all other transactions that they mined) to the network so that every node knows that votesReceived[0x01] on address 0x123 has been changed in this transaction.

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