Blockchain

 
 
A blockchain is a distributed database managed by a peer-to-peer network. The distributed database is also called a ledger which is a continuously growing chain of blocks. Each block contains a number of records and each block is linked to a previous block.

It is extremely difficult to change a record in any given block without the alteration of all subsequent blocks and the collusion of the network.

The first implementation of a blockchain was Bitcoin in 2009 invented by an unknown programmer, or a group of programmers, under the name Satoshi Nakamoto.

After the Bitcoin success many blockchain implementations followed such as Litecoin and Peercoin.

Other blockchain implementations introduces new kind of functionalities such as Ethereum and Dash.







How to use the Solidity compiler solc



Information
Solidity is a contract-oriented, high-level language whose syntax is similar to that of JavaScript and it is designed to target the Ethereum Virtual Machine (EVM).

This tutorial shows how to use the Solidity compiler also known as solc.

Solidity compiler, see: http://solidity.readthedocs.io/en/develop/using-the-compiler.html
Information about Solidity, see: https://solidity.readthedocs.io/en/latest/index.html
Solidity code, see: https://github.com/ethereum/solidity

Operating system used
macOS 10.12 Sierra

Software prerequisites
Solidity compiler solc

Procedure
  1. Here below several examples are given how to use the Solidity compiler solc.
    The files used in the examples can be downloaded here:


  2. Compile a single file:
    Type: solc --bin SimpleStorage.sol

    This command shows the following:

    Warning: This is a pre-release compiler version, please do not use it in production.

    ======= SimpleStorage.sol:SimpleStorage ======= Binary: 6060604052341561000f576000.......e27a0029

    Note:
    The filename "SimpleStorage" and file extension ".sol" does not matter at all.
    Instead of SimpleStorage.sol you could name the file "test.txt".

  3. If you want to deploy your contract, activate the optimizer while compiling.
    Note: The binary code is now shorter.
    Type: solc --optimize --bin SimpleStorage.sol

    This command shows the following:

    Warning: This is a pre-release compiler version, please do not use it in production.

    ======= SimpleStorage.sol:SimpleStorage ======= Binary: 6060604052341561000f576000.......e27a0029

  4. Output the binary source in a separate file, specify the output folder
    Type: solc --optimize -o outputDirectory --bin SimpleStorage.sol

    This command creates the file ./outputDirectory/SimpleStorage.bin

    Note:
    solc takes the contract name as the filename.

  5. Output the binary source and ABI in separate files.
    Type: solc --optimize -o outputDirectory --abi --bin SimpleStorage.sol

    This command creates the files:
    ./outputDirectory/SimpleStorage.bin
    ./outputDirectory/SimpleStorage.abi

    The abi file contains the following:

    [{"constant":false, .... ,"payable":false,"type":"function"}]

    Note:
    To overwrite existing files in the outputDirectory, type:
    Type: solc --optimize --overwrite -o outputDirectory --abi --bin SimpleStorage.sol

  6. The GreeterCombined.sol file contains two contracts "Mortal" and "Greeter".
    Output the binary source and ABI in separate files.
    Type: solc --optimize -o outputDirectory --abi --bin GreeterCombined.sol

    This command creates the files:
    ./outputDirectory/Mortal.bin
    ./outputDirectory/Mortal.abi
    ./outputDirectory/Greeter.bin
    ./outputDirectory/Greeter.abi

  7. The GreeterSimpleWithLibrary.sol file contains one contract "GreeterSimple" and one library "GreeterLib".
    Output the binary source and ABI in separate files.
    Type: solc --optimize -o outputDirectory --abi --bin GreeterSimpleWithLibrary.sol

    This command creates the files:
    ./outputDirectory/GreeterSimple.bin
    ./outputDirectory/GreeterSimple.abi
    ./outputDirectory/GreeterLib.bin
    ./outputDirectory/GreeterLib.abi

  8. Compile two contracts each in separate file "Greeter.sol" and "Mortal.sol".
    Output the binary source and ABI in separate files.
    Type: solc --optimize -o outputDirectory --abi --bin Greeter.sol Mortal.sol

    This command creates the files:
    ./outputDirectory/Mortal.bin
    ./outputDirectory/Mortal.abi
    ./outputDirectory/Greeter.bin
    ./outputDirectory/Greeter.abi

    Note:
    The order in which you specify the .sol files in the command "solc ... Greeter.sol Mortal.sol" does not matter.
    But you must use the import statement in the Greeter.sol file.
    You can use:
    import "Mortal.sol";
    or
    import "./Mortal.sol"; (Note: The online solidity editor Remix uses ./)

  9. Compile the library "GreeterLib.sol".
    Output the binary source and ABI in separate files.
    Type: solc --optimize -o outputDirectory --abi --bin GreeterLib.sol

    This command creates the files:
    ./outputDirectory/GreeterLib.bin
    ./outputDirectory/GreeterLib.abi

    Note:
    No need to specify the pragma statement.

  10. Compile the contract and library each in separate file "GreeterSimple.sol" and "GreeterLib.sol".
    Output the binary source and ABI in separate files.
    Type: solc --optimize -o outputDirectory --abi --bin GreeterSimple.sol GreeterLib.sol

    This command creates the files:
    ./outputDirectory/GreeterLib.bin
    ./outputDirectory/GreeterLib.abi
    ./outputDirectory/GreeterSimple.bin
    ./outputDirectory/GreeterSimple.abi

    Note:
    The order in which you specify the .sol files in the command "solc ... GreeterSimple.sol GreeterLib.sol" does not matter.
    But you must use the import statement in the GreeterSimple.sol file.
    You can use:
    import "GreeterLib.sol";
    or
    import "./GreeterLib.sol"; (Note: The online solidity editor Remix uses ./)

  11. Compile a single file with the bytecode optimizer enabled and the estimated number of contract runs for optimizer tuning is set to 0:
    Type: solc --optimize --optimize-runs 0 --bin SimpleStorage.sol

    This command shows the following:

    Warning: This is a pre-release compiler version, please do not use it in production.

    ======= SimpleStorage.sol:SimpleStorage ======= Binary: 6060604052341561000f576000.......e27a0029

    Note:
    If you do not specify --optimize-runs it defaults to --optimize-runs 200