Intro to smart contracts

decentralized apps tools ethereum smart contracts dapps

What are smart contracts?

Smart contracts are reusable code templates or some logic written which gets executed on Ethereum Virtual Machine. We can think of Smartcontract as analgous to AWS Lambda/Google Cloud Function.

As in the current cloud systems like AWS/Google Cloud we pay for request(atleast for lambdas). In ethereum network we need to pay some fees to get our transactions executed. This fees is called Gas in blockchain world.

Lets take a simple example

pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public constant returns (uint) {
        return storedData;
    }
}

In the above example the first line tells which version of solidity we need to use for compiling. contract is a keyword something like Class in other programming languages. In this contract we have a single State variable called storedData which stores the value of the data assigned.

Solidity Variables

In solidity there are two types of storages

memory storage class is to tell that this information is local and need not be stored on the blockchain(example tmp variables). storage this modifier however tells that we need to store the associated value on blockchain(This type tends to write to blockchains which will need gas to complete the transaction)

Solidity Functions

Functions are the executable units of code within a contract.

    function set(uint x) public {
        storedData = x;
    }

As you can see here there are different visibility for a function.

Note

pragma solidity^0.4.12;

contract Test {
    function test(uint[20] a) public returns (uint){
         return a[10]*2;
    }
    function test2(uint[20] a) external returns (uint){
         return a[10]*2;
    }
}

Calling each function, we can see that the public function uses more gas(496), while the external function uses only 261.

The difference is because in public functions, Solidity immediately copies array arguments to memory, while external functions can read directly from calldata. Memory allocation is expensive, whereas reading from calldata is cheap.

The reason that public functions need to write all of the arguments to memory is that public functions may be called internally, which is actually an entirely different process than external calls. Internal calls are executed via jumps in the code, and array arguments are passed internally by pointers to memory. Thus, when the compiler generates the code for an internal function, that function expects its arguments to be located in memory.

For external functions, the compiler doesn’t need to allow internal calls, and so it allows arguments to be read directly from calldata, saving the copying step.

As for best practices, you should use external if you expect that the function will only ever be called externally, and use public if you need to call the function internally. It almost never makes sense to use the this.f() pattern, as this requires a real CALL to be executed, which is expensive. Also, passing arrays via this method would be far more expensive than passing them internally.

You will essentially see performance benefits with external any time you are only calling a function externally, and passing in large arrays.

Comments

comments powered by Disqus