pexels photo 1198828 Learn about Solidity: A Language for Writing Smart Contracts on the Blockchain

Learn about Solidity: A Language for Writing Smart Contracts on the Blockchain

Read Time:8 Minute

Table of Contents

• What is Solidity:

⁃ What are Smart Contracts

• Basics of a Block-chain Transaction

⁃ Blocks

⁃ The Ethereum Virtual Machine (EVM)

⁃ Gas

⁃ Storage, Memory and the Stack

⁃ ​Message Calls

⁃ Delegatecall/Callcode and Libraries

⁃ Logs

⁃ Create​

⁃ Deactivate and Self-destruct​

⁃ Precompiled Contracts​

image 1649328718404 compressed Learn about Solidity: A Language for Writing Smart Contracts on the Blockchain

What is Solidity:

  • ​Solidity is an object-oriented, high-level language for implementing smart contracts.
  • Smart contracts are programs which govern the behaviour of accounts within the Ethereum state.
  • Solidity is a curly-bracket language designed to target the Ethereum Virtual Machine (EVM).
  • It is influenced by C++, Python and JavaScript.
  • Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features.
  • With Solidity you can create contracts for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets.
  • When deploying contracts, you should use the latest released version of Solidity.
  • Except for exceptional cases, only the latest version receives security fixes.
  • Breaking changes as well as new features are introduced regularly.
  • The current 0.y.z version number indicates the fast pace of change.

What are Smart Contracts

  • Smart contracts are stored as code on the blockchain, and can be called by other contracts and by transactions.  
  • When called, they run on the blockchain’s virtual machine, in a completely isolated and secure environment. 
  • This gives them the ability to interact with other contracts, make decisions, and store data on the blockchain. 
  • They can be used to create decentralized applications, which are applications that run on the blockchain and are not controlled by any single entity. 
  • Decentralized applications have many advantages over traditional applications. 
  • They are more secure, because they are not centrally controlled and therefore cannot be shut down or censored. 
  • They are also more efficient, because they do not require a middleman to facilitate transactions. And because they are running on the blockchain, they can take advantage of the blockchain’s inherent security and immutability.
pexels alesia kozik 6781269 1649328819538 compressed Learn about Solidity: A Language for Writing Smart Contracts on the Blockchain

Basics of a Block-chain Transaction

  • Blockchains are a concept that can be understood by programmers
  • Most of the complications with blockchains are there to provide a certain set of features and promises
  • You do not have to worry about the underlying technology to use a blockchain
  • A blockchain is a globally shared, transactional database.
  • Everyone can read entries in the database just by participating in the network.
  • If you want to change something in the database, you have to create a so-called transaction which has to be accepted by all others.
  • The word transaction implies that the change you want to make is either not done at all or completely applied.
  • Furthermore, while your transaction is being applied to the database, no other transaction can alter it.
  • A transaction is always cryptographically signed by the sender (creator).
  • This makes it straightforward to guard access to specific modifications of the database.

Blocks

  • ​A double-spend attack is when two transactions exist that both want to empty an account.
  • One of the transactions will be valid, typically the one that is accepted first.
  • The problem is that “first” is not an objective term in a peer-to-peer network.
  • A globally accepted order of the transactions will be selected for you, solving the conflict.
  • The transactions will be bundled into what is called a “block” and then they will be executed and distributed among all participating nodes.
  • If two transactions contradict each other, the one that ends up being second will be rejected and not become part of the block.
  • Blocks are added to the chain in rather regular intervals.
  • It may happen that blocks are reverted from time to time, but only at the “tip” of the chain.
  • The more blocks are added on top of a particular block, the less likely this block will be reverted.

The Ethereum Virtual Machine (EVM)

  • The Ethereum Virtual Machine is the runtime environment for smart contracts in Ethereum.
  • The EVM is sandboxed and isolated, which means that code running inside the EVM has no access to network, filesystem or other processes.
  • Smart contracts even have limited access to other smart contracts.

    The Ethereum Virtual Machine (EVM) is a Turing-complete virtual machine that allows anyone to run arbitrary blockchain applications on the Ethereum network.

    The EVM makes it possible for developers to create smart contracts and decentralized applications (DApps) on the Ethereum blockchain. The EVM executes code exactly as programmed without any possibility of fraud or third-party interference.

    The EVM is a key part of Ethereum’s appeal to developers. By providing a simple, powerful, and tamper-proof platform for running code, the EVM enables developers to create complex decentralized applications with ease.

    In addition to the EVM, Ethereum also provides a number of other tools and services that make it an attractive platform for developers. These include:
  • The Ethereum Virtual Machine (EVM) is a Turing-complete virtual machine that allows anyone to run arbitrary blockchain applications on the Ethereum network.
  • The EVM makes it possible for developers to create smart contracts and decentralized applications (DApps) on the Ethereum blockchain. The EVM executes code exactly as programmed without any possibility of fraud or third-party interference.
  • The EVM is a key part of Ethereum’s appeal to developers. By providing a simple, powerful, and tamper-proof platform for running code, the EVM enables developers to create complex decentralized applications with ease.

In addition to the EVM, Ethereum also provides a number of other tools and services that make it an attractive platform for developers. These include:

  • ​A decentralized storage platform called IPFS
  • A decentralized application platform called Swarm
  • A decentralized messaging platform called Whisper
  • A decentralized payment platform called Shh


Ethereum is a platform that is constantly evolving. New features and services are being added all the time, making it an exciting and dynamic environment for developers.

Accounts

  • ​Ethereum has two types of accounts: external and contract.
  • The address of an external account is determined from the public key.
  • The address of a contract account is determined at the time the contract is created.
  • Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage.
  • Every account has a balance in Ether which can be modified by sending transactions that include Ether.

Gas

  • ​Each transaction is charged with a certain amount of gas
  • The purpose of gas is to limit the amount of work that is needed to execute the transaction and to pay for this execution at the same time
  • The gas price is a value set by the creator of the transaction
  • The creator of the transaction has to pay gas_price * gas up front from the sending account
  • If some gas is left after the execution, it is refunded to the creator in the same way
  • If the gas is used up at any point, an out-of-gas exception is triggered, which reverts all modifications made to the state in the current call frame

Storage, Memory and the Stack

  • ​The Ethereum Virtual Machine has three areas where it can store data- storage, memory and the stack.
  • Each account has a data area called storage, which is persistent between function calls and transactions.
  • Storage is a key-value store that maps 256-bit words to 256-bit words.
  • The second data area is called memory, of which a contract obtains a freshly cleared instance for each message call.
  • Memory is linear and can be addressed at byte level, but reads are limited to a width of 256 bits, while writes can be either 8 bits or 256 bits wide.
  • Memory is expanded by a word (256-bit), when accessing (either reading or writing) a previously untouched memory word (i.e. any offset within a word).
  • The EVM is not a register machine but a stack machine, so all computations are performed on a data area called the stack.
  • It has a maximum size of 1024 elements and contains words of 256 bits.
  • Access to the stack is limited to the top end in the following way: It is possible to copy one of the topmost 16 elements to the top of the stack or swap the topmost element with one of the 16 elements below it.


​Message Calls

  • Calls are limited to a depth of 1024
  • For more complex operations, loops should be preferred over recursive calls
  • Only 63/64th of the gas can be forwarded in a message call

Delegatecall/Callcode and Libraries

  • The delegatecall message call is identical to a regular message call, except that the code at the target address is executed in the context of the calling contract.
  • This means that a contract can dynamically load code from a different address at runtime.
  • Storage, current address and balance still refer to the calling contract, only the code is taken from the called address.
  • This makes it possible to implement the “library” feature in Solidity: Reusable library code that can be applied to a contract’s storage, e.g. in order to implement a complex data structure.

Logs

  • ​Logs are used by Solidity to implement events.
  • Contracts cannot access log data after it has been created.
  • Log data is stored in bloom filters.
  • Network peers that do not download the whole blockchain (so-called “light clients”) can still find these logs.

Create​

  • Contracts can create other contracts
  • The only difference between these create calls and normal message calls is that the payload data is executed and the result stored as code
  • The caller / creator receives the address of the new contract on the stack

Deactivate and Self-destruct​

  • The only way to remove code from the blockchain is by performing the selfdestruct operation on a contract at that address.
  • The remaining Ether stored at that address is sent to a designated target when the contract is removed.
  • If someone sends Ether to a removed contract, the Ether is lost forever.
  • Disabling contracts by changing internal state is preferable to deactivation.
  • Doing so makes functions revert and thus the contract unusable.

Precompiled Contracts​

  • The address range between 1 and 8 contains “precompiled contracts.”
  • The behavior of these contracts is not defined by EVM code stored at that address.
  • The gas consumption of these contracts is not defined by EVM code stored at that address.
  • Different EVM-compatible chains might use a different set of precompiled contracts.
  • It is possible that new precompiled contracts are added to the Ethereum main chain in the future.

Leave a Reply

%d bloggers like this: