Sunday, March 27, 2016

Lost in the Ethereum blockchain

There is difficult to find what was happened with your money in Ethereum, in my example I've definitely have some bug in my contract :

contract regaMember is poolMember {
    uint public score;
    function regaMember(uint _score, address _owner) {
        score = _score;
        owner = _owner;
    }
    function invest(uint _amount) public {
        if(this.balance < _amount) throw;
        uint amount_1 = _amount * 61 / 100;
        uint amount_2 = _amount * 9 / 100; 
        uint amount_3 = _amount * 15 / 100;
        address[] memory path = new address[](3);
        getPath(path);
        posting(an._11_cash, an._01_reserve, amount_1);
        posting(an._11_cash, an._02_contribution, amount_2);
        posting(an._11_cash, an._03_commission, amount_3);
        posting(an._11_cash, an._04_super_pool, amount_3);
        path[0].send(2*amount_3);
        path[1].send(amount_1+amount_2);
    }
}

But how to find where is my money after I've called "invest" method? There is only one successful transaction in blockchain that has moved funds from my primary account to the regaMember contract but I did not manage to find where are they after all. The problem with getPath() function that did not return path[] as expected but now I am lost in blockchain...

Wednesday, March 23, 2016

JAVA -> Ethereum deployment and transaction cost

There is a table with simple model to calculate deployment and transaction cost if we will migrate all our current scoring & underwriting system to Ethereum smart contracts. The cost of gas is not high but we also understand that such smart contract system will not work because it will be very slow and will not be able to process all our current transaction volume in reasonable time.

Tuesday, March 22, 2016

Forever successful

Back to Ethereum transaction processing. There are two different views on a successful transaction - one is your application view and another is Ethereum view. Unfortunately for us these views are absolutely different. The failure in your application when your decided to rollback current transaction in the view of Ethereum is great success :



So, if you have decided to rollback you will lose GasLimit * GasPrice and what is most important it's not possible to understand from transaction history it was grand failure or great success...

Monday, March 21, 2016

Lost in the gas

Back to transaction topic. It's possible make a transaction atomic using check / rollback mechanism. It's not very convenient and still provide a lot freedom for mistakes and at the end of the day with funds that did not manage to arrive at destination account. But another question what happened with gas when we called rollback?

if(!regaMain.send(_amount))
throw;

We lost it! The whole gas limit! So, if a client was trying to send money to our smart contract and smart contract for any reason call rollback he/her will ended with less money on account. Not very nice marketing feature of the service...

Here is an example, when the second transaction in the block is failed to execute and all transaction was reversed minus 5,000,000 * 20 gWei - very good motivation do not call throw;





Sunday, March 20, 2016

Gas is first

Working on our REGA Risk-Sharing project we are facing several issues but the first one is the transaction gas. In Ethereum everyone who send message changing the state must pay for this action in units calling gas. Let’s assume you have a prospective client who downloaded your mobile app or web page and you would like this client to start interaction with Ethereum. But here we’ve got a problem - the client does not have any Ether to send a ‘non constant’ messages. If the very first message is the ‘buy message’ then it’s not a problem (or almost, see below) but in our case before we decided can we have this potential client as our pool member we must score him and calculate a tariff. So, for this purpose we must provide a the client with small amount in Wei to make any changing states call possible.  But if client has decides to exit app and did not spend provided funds? Can we have them back - small amount multiple by large number of clients and we will finish spending our money for nothing. Solution is to control client account - not very nice solution for the client then. We decided that it’s better for client to have two accounts - one with his own money and another one with money provided by service for initial interaction with REGA. The last one we can control having its private key.
Now we moved to another issue. If you client is ready to buy your service and send the money to your smart contract, can we calculate the exact amount before the call? Unfortunately no. We can estimate the maximum price of transaction and include it in the tariff but as result the client will finish with a balance with a little bit more Wei then expected. Another solution to calculate the tariff with minimum gas price and then send the funds back to client account covering additional gas cost if any. More simple solution is send value from client personal account and pay gas from our gas account but it looks like it’s not possible.
But the most impotent question question is can I prove to my client that this amount in his transaction is spent for gas. It looks like the answer is no. We can't find in transaction history the gas spendings for particular transaction but only for the block. Can you imagine a financial service that can't say  how much you spend on fee and commission?




Saturday, March 19, 2016

Back to the past?

Back to the past

It was more then 20 years ago when we have bought a new core banking system called BankMaster. It was old Cobol software but with some modern words added by Kindle marketing people.  They promised the client-server architecture but in fact BankMaster was loading the Cobol code from the server and then run it on users personal computers. It was ok but what we’ve discovered later when have paid all the money and migrated the current general ledger to the BankMaster one was shocking - BankMaster did non support transactions. In the BM database by the end of the day we always have had several single side postings - debit or credit of one account without corresponding credit or debit of another one. Due to this nice feature our support team were spending all nights to clean up the mess before it was became possible to run a close of day procedure.

It was the glorious past and we almost forgot about it but now when we are doing project using Ethereum it looks like the past is not going to leave us alone. Ethereum is cryptocurrency (Ether) and virtual machine (EVM) for running smart contracts - the small programs linked to cryptocurrency accounts. There are two types of account in Ethereum - a personal accounts and a smart contract control accounts, and we can transfer values in Ether between them using secured messages. The messages are changing state of the Ethereum World and recored in transactions, transactions make blocks and blocks linked to each other using cryptographic hash function create blockchain. Nice and simple but not in reality. Each transaction in the Ethereum blockchain must be originated by a message from personal account but can trigger smart contract execution at EVM that can be resulted in other messages invocation targeting personal or contract accounts. The first question - can we see all messages transferring Ether in transaction history (blockchain)? The simple answer - no, we can’t. We can see only the first message that has originated transaction with value in Ether that the sender has send to destination account. The more complicated answer - yes, we can do it with the help of some smart guys who know how to record these internal messages during the EVM execution and show them to public. So, we can easily see first message as transaction in transaction history but it’s difficult to search and see all other messages originated by this first one. The second question - if some internal message inside our transaction is failed to execute does it mean that the whole transaction must be reversed? The simple answer and more obvious one - yes, it must be this way because  it’s nature of a transaction. But here we are facing the BankMaster legacy - Ethereum allows a single side posting in complex transactions. It’s possible to debit one account sending money to an another one but never receive value on the destination account due to some problem in the intermediate smart contracts. Let’s have example. Suppose we have personal account P1 that sending Ξ 1.000 to account C1 - contract account and in the same message ask to send the same amount to account C2. Unfortunately smart contract of the C2 has some bug and did not managed to accept the payment and as the result we hang our funds at C1 account but in blockchain everything looks like the transfer is completed and funds are transferred from P1 to C2.


There is the Solidity code for the two contracts. The C1 account has regaMember contract and in our example we are using send2Pool message, sending Ξ 1.000 to the pool account C2 with regaPool contract:

contract regaMember  {
bytes32 public firstname;
   bytes32 public lastname;
   bool public authorized;
  rega public regaAuth;
regaPool public regaMain;
function ask4Authorization() public constant;
function grandAuthorization(address _rega) public;
function send2Pool(uint _amount) {
address myAddress = this;
    if(!authorized || regaMain == address(0) || 
myAddress.balance < _amount)
    throw;
    regaMain.send(_amount);
}
}
contract regaPool {
struct poolMember {
address member;
uint balance;
}
rega public creator;
bytes32 public name;
poolMember[] public poolMembers;
function() { poolMembers.push(poolMember({member:msg.sender,
balance:msg.value}));
}
}

In my personal account in the test Ethereum node (P1) I have about  Ξ 58.000 and in my call to regaMember account I’ve set the gasPrice to 20 gWei and gas limit to 5,000,000. There is screenshot for the latest block # 3211 and block  # 3202 with our transaction. It’s number 2 in the block transaction list (transactions). The transaction is below with. It transfers  Ξ 1.000 from my personal account to C1 account and it’s also calling send2Pool method that should send the Ξ 1.000 to C2 (pool account). Now we can look at the balances. The first one is balance of my personal account (P1), the second one is C1 (regaMember) account and the last one is regaPool C2 account.

Welcome to the past, guys! Now I need good tool to search Ethereum blockchain for single side postings or there is some hidden switch to make my node transaction friendly?