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

Thursday, April 12, 2018

On public blockchains and open innovation


There are several business model in the blockchain space at the moment, like public blockchains that are pretty much controversially at the moment like from Ethereum and of course the Hyperledger that is carried out in a classical business way installing nodes at different consortium members and doing project management in a classical way. The major advantage is however for a public blockchain solution that it maximally supports open innovation. If you are a developer or a couple of developers, you can easily deploy your smart contracts or DApp to the open blockchain and realize revenue on that. Classical consortium blockchains are playground of the big companies, having classical sales channels, requirement engineering, project management and organisational structures. Such a construct usually kills every kind of a innovation.

If project Hyperledger or Microsoft Azure Blockchain wants to keep up with the exponential changing blockchain world they need to create at least semi public blockchains with internal tokenomics, at which even if the nodes are hosted by different consortium members, every developer has the potential to create and host his own DApp solution and has the potential to earn money with that. 

Hackatons do not support open innovation.
The possibility of creating DApp-s by developers and realizing cash-flow on that without worrying on infrastructure or any big enterprise structure does support open innovation.   

public - semi public and consortium blockchain platforms


In usual blockchain frameworks there are two major ways of defining the general visibility:
- public blockchain: the nodes can be run by anybody, even by hackers. The transactions are visible to everybody as well and practically the whole world can participate. 
- consortium blockchain: the nodes are operated by certain consortium members, and these members have access to the transactions and the whole blockchain content as well. 

There might be a third solution between public and consortium scenarios, let we call it a semi public blockchain. In this version the peers are operated by a consortium with known members, but the network itself fully public meaning that everybody can participate on the transaction side and everybody can deploy and operate smart contracts or decentralized applications on top of the network without requiring a permission from the consortium of the infrastructure side. Such scenario is proposed for example by the Hedera Hashgraph. 

Wednesday, April 11, 2018

Notes on stable cryptocurrency


Designing a stable cryptocurrency is a pretty difficult thing, even if it is possible at all. One thing that might work if the cryptocurrency is "backed" by a digital asset for that we can estimate both the supply and the demand side. Such a digital asset might be for example storage or computation. Both for storage and for computation we can estimate the supply side, which is like the Moore's rule and probably we can estimate the demand as well based on industry trends or historical data. The only thing that we have to do is to design a cryptocurrecny with a certain monetary policy in a way that the monetary policy mimics somehow the price of the backed asset. Certainly, there might be a possibility for unexpected changes in supply or demand, like for instance the appearance of a brand new technology, so the possibility should be given for manually monetary policy adjusting.   

Saturday, April 7, 2018

Permissioned versus public or private transaction in blockchains

It is important to note that if we regard transaction privacy of a blockchain system, there might be two different categories to consider:
- transaction privacy: if a transaction visibility is public or private
- transaction permission: here we usually consider something on a higher abstraction level than simple transaction. As an example, something as cryptoasset. In this sense it makes sense to imagine systems where different transactions can be associated with different permissions. 

Certainly, there is an additional dimension that can be considered: it is the publicity of the P2P nodes of the system. 

The different combinations are shown on the following diagram:


1. Systems like Bitcoin or Ethereum are public and non-permissioned. Certainly, there is always a minimum default permission even at non-permissioned systems: only the owner of the private key can spend or transfer the asset.
2. We would expect from a voting system that is private and non-permissioned, meaning that everyone can participate but the voting itself is not visible directly on the blockchain. Cryptocurrencies like Dash or ZCash are developed in the similar direction as well. 
3. A Land title system would have the properties of being permissioned and public.   
4. Last but not least a typical banking application would have both the characteristics of being permissioned and private. Blockchain frameworks as Hyperledger Fabric are able to realize such an applications. 

Wednesday, April 4, 2018

Comparing Ethereum with Hashgraph

Based on the framework of the previous blog and dimensions, we can compare Ethereum with the new emerging technology called Hashgraph. The characteristics of Ethereum can be seen on the following picture:


And similarly the characteristics of Hashgraph can be seen on the following picture:



Comparing the two frameworks, we can see that there are differences in two areas. First of all Hashgraph is at the moment rather an extended consensus mechanism and not a full scale distributed ledger technology. That means that some elements are simply not defined exactly by the framework and are pretty much open for further implementation. Such things are related to the exact transaction realization.The computational part of the consensus can be pretty easily extended in java, however how exactly transactions are implemented is a point that provides the possibility to several implementations. In this sense properties like crypto-economics transaction semantics or transaction privacy are parameters that provide several possibilities in the future. The sure thing is that contrary to Ethereum Hashgraph best fit in consortium scenarios, the structure of the transaction storage is a non-blockchain one and both performance and fault tolerance are more than excellent.

Certainly, this article covers the original concept of Hashgraph produced by Swirlds, in the public version of the technology produced by the Hedera consortium it is planned to have more structure and services regarding on transaction, cryptocurrecnies and storage.

Solidity Tips and Tricks - difference between .call.value(), .send() and .transfer()


It is important to understand the difference of semantics and security implications of the different ways of sending ether to a contract:
- .call.value(ether) - sends ether to a contract by giving all the possible gas for the execution having a strong risk for reentrancy attacks. If it is succeeded true is returned if not false. 
- .send(ether) - sends ether to a contract by giving only 2300 gas for the execution making possible to do only logging and event and preventing reentrancy attacks. Similarly to the previous case if the call succeed it returns true, otherwise it returns false. 
- .transfer(ether) is tecnically the same as require(send(ether)), so if the ether sending not succeeding and error will be thrown and the transaction is reverted.  


Solidity security patterns - reentrancy


Reentrancy can happen if a smart contract calls a second one in a direct way or indirectly by transferring it ether. Typically at the code execution before the given statement terminates it called once again by another contract. Consider the following code fragments:

contract ReentrancyError {
  ...
  function withdraw(uint _amount) public{
    if(balances[msg.sender] >= _amount) {
      if(msg.sender.call.value(_amount)()) {
        balances[msg.sender] -= _amount;
      }
    }
  }
}

contract Attacker {

  function attack() public {
      target.withdraw(donated);
  }
  
  function() public payable{
      if (counter < 10) {
        counter++;
        target.withdraw(donated);
      }
  }
}

The withdraw function of the ReentrancyError contract is meant to be called by externally owned accounts, however a tricky hacker might force to call the function from a hacked contract. If so by calling the withdraw function at the msg.sender.call.value(_amount) by transferring ether to the Attacker contract the fallback function will be called, that calls the withdraw function back again. As balance of the sender is still not modified at this stage, the attacker will succeed to transfer the amount of ether once again starting a new cycle (up to 10).

Possible solutions in this example are:
- reducing the balance before the ether transfer
- using mutex
- using require statement instead of if

Solidity security patterns - difference between call delegate call and library call


It might be a little bit tricky to distinguish between the different kind of calls in solidity. Basically library call is realized on a low-level by delegate call and it is different from a standard call. In a standard call the context of the called contract is considered as active. In a library or delegate call the calling context, meaning variables, storage, visibility remain active. In other words in a library call only the function code of the called contract will be loaded, every other parameter remain the same. The situation can be best seen by the following contract examples: 

contract CalledContract {
    event showAddressEvent(address _from);
    function test() payable public {
        showAddressEvent(this);
    }
}

contract CallingContract {
    function callDifferentContracts(address _contractAddress) public {
        require(_contractAddress.call(bytes4(keccak256("test()"))));
        require(_contractAddress.delegatecall(bytes4(keccak256("test()"))));
        testLib.calledSomeLibFun();
    }
}

library testLib {
    event showAddressEvent(address _from);
    function calledSomeLibFun() public {
        showAddressEvent(this);
    }
} 

By calling the callDifferentContracts function with the address of the CalledContract, the code first reveals the address of CalledContract as it is expected by a standard call. After that it shows the address of the CallingContract both at the delegatecall and at the library call examples. 

Solidity security patterns - delegatecall and callcode


The problem is both with delegatecall and callcode that they dynamically load the code of a smart contract and call its function. Hence they do this loading in a way that the context (storage, memory etc) of the calling contract is preserved. It means that from the called contract any kind of variables (even privates !) of the calling contract can be changed. So it must be particularly paid attention that only one specific smart contract and only one specific function is to be called by callcode or delegatecall.

contract TestCallCode {

  function callDelegateFunction(address _a, string _functionName)  {
        _a.delegatecall(bytes4(keccak256(_functionName)));
 }
}

In the previous example if an attacker can call the callDelegateFunction and influence the address, he can redirect the call to any smart contract of a choice and with the functionName any function can be called. Even if there is no direct match between the _a address and _functionName the system can be sometimes tricked like with implementing malicious code into the fallback function. 

Solidity Tips and Tricks - triggering timed event


In solidity or in Ethereum, you can't trigger an event internally from the environment. What you can do is to have an external trusted source that triggers for you something with the help of an explicit transaction. This raises however the problem of centralization and trust, although there is the possibility to structure decentralized oracle network for similar problems, they still remain pretty much centralized. 

Sometimes however the requirement is more simple, one should not really trigger actively an event explicitly, it has only to be made sure that a given functionality works differently before and after a given point of time. In such cases the block number or the block timestamp can be used. The following situation shows an example pattern for that:

contract TestDebugMode 
{
 uint public eventBlockNumber = 1111111111; 
 uint public eventTimestamp = 1111111111;

  function timeBasedLogic()  
    {
     if (eventBlockNumber > block.number) {
           // do logic  
     }
     if (eventTimestamp > block.timestamp) {     
          // do logic 
     }    
 }
}


It is usually proposed to use the block number because in certain situations block timestamp can be gamed by the miners.