...by Daniel Szego
quote
"On a long enough timeline we will all become Satoshi Nakamoto.."
Daniel Szego

Wednesday, April 4, 2018

Solidity Tips and Tricks - debug mode with blocknumber


As we have seen in our previous blog, designing smart contract structure with debug features might be a pretty good idea. One possibility is to realize it with an explicit switch that can be manually switched on or off, however it might happen automatically as well, like based on the actual blocknumber. The following code demonstrates the idea: 

contract TestDebugMode2 
 {    
   uint public targetDebugNumber = 1111111111; 
   
   modifier isInDebug() {       
       require(block.number < targetDebugNumber);
        _;
  }

  function onlyInDebug() isInDebug {

  }
} 

Solidity Tips and Tricks - debug mode with simple switch


Well, designing a smart contract is pretty tricky, basically the contract is immutable so basically it is not upgradable, even if you use some direct patterns for update. So what you can basically do is to build in some debug functionalities, like getting extra debug information or updating parts of the system in a debug mode... The most simple way how you can control such a debug mode is to have a bool switch called debugMode that can be set true only once at the go live causing that the debug functionalities will not be accessible anymore. An example can be seen on the following code:

contract TestDebugMode {
    
 bool public debugMode = true ; 
    
   function goLive(){
      debugMode = false;
   }

   modifier isInDebug(){
      require(debugMode == true);        
      _;
   }

   function onlyInDebug() isInDebug {

   }
}

Certainly, a real smart code might be somewhat more difficult, as it has to be guaranteed that only an owner or an administrator can deactivate the debug mode by going live. 

Consortium blockchain solutions and ICO-s

There seem to be nowadays a couple a blockchain solutions that target consortium or private business blockchain solutions and despite planning or doing an ICO or other form of token sales. One of the best such example is Ripple, which offers bank to bank financial services despite that a lot of its XRP tokens can be freely traded in the major exchanges. 

Such a constellation seems to be a little bit paradox for the first sight. The problem is that institutional investors still do not really buy tokens, instead most of the tokens are sold for everyday people or to bitcoin whales. Considering a B2C decentralized service this can provide a functioning solution, as most of the tokens are probably bought by people who are actually willing to use the service as well as soon it will be ready. This makes the business adaptation faster and accelerates the network effect as well. However with a B2B service it is very difficult to design a usage token in a way that it can be bought by average people as investment but the system will be working in a consortium scenario possibly between enterprises. Certainly it would be an exception if the token could represent a pure investment possibility, however it would require a huge regulation overhead.  

Tuesday, April 3, 2018

blockchain governance and the two levels of smart contract


Blockchain governance is a complicated topic, especially if we consider not only the consortium but the public solutions as well. Mismanaged governance can cause forking and other undesired effects. It would be a great idea if governance activities of a blockchain could be actually supported by smart contracts, implying two categories of smart contracts in the system: 

1. system smart contracts, realizing basic functionalities and services related either to maintain the consensus and the system or providing general services to other smart contracts. 
2. user based or custom smart contracts, providing general user defined functionalities using both the consensus algorithms of the blockchain and the functionalities of the system smart contracts. 

The key concept of such a system smart contract layer is to provide a further integration link between the infrastructure nodes, the services of such a nodes and the system smart contracts, or putting elements of the genesis file into the smart contract. 

On the problems and possible improvements of solidity



The biggest problem of solidity is its inconsistent semantics and unexpected side effects of certain operations.

Examples are type conversion, arithmetic operators, fallback function, different ways of calling other contracts or libraries, variable visibility and access ...

And of course a key point is to understand what it means that your code will be executed on the blockchain.

In this sense, the solution won't be to have a non-Turing complete limited language. The solution will be to have a very strict and logical language in which all side effects are easily foreseen and the language semantics makes more sense in a distributed ledger context. 

Solidity Tips and Tricks - type aliases


There are a couple of aliases or similar types in solidity that you should be paid attention.

- uint is alias to uint256.

- byte is alias to bytes1.

- sha3 is alias to keccak256.

- now is alias to block.timestamp.

- bytes is the same byte[] but packed tightly.

- string is almost the same as bytes but doesn't allow length or index access.

- address is equivalent to uint160

Solidity Tips and Tricks - var as type declaration


Although there is a possibility to use var at a variable declaration, meaning that the variable is initialized by the first declaration automatically, you should have extreme caution using this pattern. As an example, using for instance var i = 0; initializes the variable automatically as uint8 implying that the following loop is an infinite one causing an out of gas exception in solidity.  

contract TestContract {
  function infiniteLoop() {
    for (var i = 0; i < 1000; i++) {
    ...
    }
  }
}

Solidity Tips and Tricks - converting types


In solidity converting between types is not always very straightforward, as an example there is no typecast from address to string, there is no typecast bytes to string, and from bytes to address. If you need such a conversion, you have to implement it. The following example shows a possible implementation for such a conversions:

contract TypeCasting {
    
  function toString(address addr) returns (string) {
    bytes memory b = new bytes(20);
    for (uint i = 0; i < 20; i++) {
      b[i] = byte(uint8(uint(addr) / (2**(8*(19 - i)))));
    }
    return string(b);
  }
  
  function bytes32ToString(bytes32 x) constant returns (string) {
    bytes memory bytesString = new bytes(32);
    uint charCount = 0;
    for (uint j = 0; j < 32; j++) {
      byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
      if (char != 0) {
        bytesString[charCount] = char;
        charCount++;
      }
    }
    bytes memory bytesStringTrimmed = new bytes(charCount);
    for (j = 0; j < charCount; j++) {
      bytesStringTrimmed[j] = bytesString[j];
    }
    return string(bytesStringTrimmed);
  }
  
  function bytesToAddress(bytes _address) public returns (address) {
  uint160 m = 0;
  uint160 b = 0;

  for (uint8 i = 0; i < 20; i++) {
    m *= 256;
    b = uint160(_address[i]);
    m += (b);
  }
 }
}

Solidity Tips and Tricks - empty address


If you want to check if your address is null or equals to the 0 address, you have to use the address(0) keyword, like in the following example:

contract TestContract {

  function isAddressEmpty(address addr) returns (bool) {
    return (addr == address(0));
  }

} 

Solidity Tips and Tricks - comparing strings


In solidity you can not really compare two strings with each other. You can surely compare them as byte arrays practically comparing the characters one by one, however a more elegant way is to compute a hash of the two string and compare simply the two hashes. Due to the cryptographic properties of the hash functions, the chance to have the same hash of two different strings is astronomical small.  An example can be seen bellow:

contract Compare {
    
    function compare (string a, string b) returns (bool){
        return sha3(a) == sha3(b);
    }
}