Search code examples
transactionsblockchainethereumsmartcontracts

Where is the data of a transaction saved?


To introduce my question I would like to give an example. Suppose a customer wants to join a gym. A transaction is sent and contains:

  • customer id
  • name and surname
  • start date
  • end date

The smart contract receives the transaction and creates a membership at the gym. Where is this data saved? After some time I want to verify that the subscription has not expired and I send a checkRequest. Can I specify only the id and then the smart contract through the id search for the subscription? I mean, can the smart contract access the subscription knowing the id? Can the smart contract take the subscription that matches the sent id and do the check?

I cannot understand the difference between the data saved on the blockchain permanently (when is this data saved?) and the data saved in the smart contract (does the smart contract have storage?) Is there a difference? I know the question is trivial but I can't understand.


Solution

  • The data transmitted as part of a transaction (method and parameters) is stored in the blockchain. The results of transaction processing by the contract are stored in the local storage of each node. The contract has access to its data at any time.

    An example of a contract is below:

    • the AddCustomer method is called transactionally and adds a user
    • the GetCustomerById method is called "locally" and shows user data by his identifier
    pragma solidity 0.5.0;
    
    contract Gym
    {
        address  Owner   ;
    
        struct Customer
        {
           bytes32  name ;
           bytes32  surname ;
           bytes32  start_date ;
           bytes32  end_date ;
        }
    
        mapping (bytes32 => Customer) Customers ;
    
    
       constructor() public
       {
          Owner = tx.origin ;
    
       }
    
       function AddCustomer(bytes32 id_, bytes32 name_, bytes32 surname_, bytes32 start_date_, bytes32 end_date_) public
       {
              Customers[id_]=Customer({       name: name_, 
                                           surname: surname_,
                                        start_date: start_date_,
                                          end_date: end_date_
                                       }) ;
       }
    
        function GetCustomerById(bytes32 id_) public view returns (bytes32, bytes32, bytes32, bytes32 retVal)
        {
           return(Customers[id_].name, Customers[id_].surname, Customers[id_].start_date, Customers[id_].end_date) ;
        }
    
    }