Bitcoin Forum
May 07, 2024, 01:17:10 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Poll
Question: What's your thoughts on the topic?
Wut? - 0 (0%)
Interesting... - 0 (0%)
There is no need in it - 0 (0%)
OP's english sucks - 0 (0%)
Total Voters: 0

Pages: [1]
  Print  
Author Topic: Testable smart contracts, some thoughts  (Read 149 times)
Slava79 (OP)
Member
**
Offline Offline

Activity: 182
Merit: 17

¯\_(ツ)_/¯


View Profile
April 20, 2018, 08:33:58 AM
Last edit: April 20, 2018, 01:07:31 PM by Slava79
 #1

I've been reading recently about the smart contract languages and approaches in different platforms. My interest was in to what degree the approaches of functional programming (which I'm a fan of) are applied, especially pure functions https://en.wikipedia.org/wiki/Pure_function. From my opinion writing contracts methods  without mutable state would considerably improve code safety and testability and (if applied to cryptocurrency) reduced the amount of these infamous disasters with lost or frozen funds etc.

Here is the short list (too short actually, I should admit) of the current state of art in smart contracts what was able to find, used in production and Turing complete languages.

1. Ethereum
      Solidity. Statically typed language, mutable state. There are constant and pure modifiers to mark functions as not changing any state, but this is used as additional restriction, not applicable to the whole contract code, if you need to change state, you only option is  to do it inside functions body.

Code:
function SayHello() public constant returns(string says) {
    return greeting;
}

function returnTrue() public pure returns(bool response) {
    return true;
}

2. Neo
      C#, VB.Net, F#, Java, Kotlin, Python, C, C++, Golang, JavaScript. The list of supported languages is impressive. Still, looking in the code examples, the constructions used are all in imperative style, expecting to operate with mutable state:
Code:
Storage.Put(Storage.CurrentContext, domain, to);

3. Lisk, EOS, Stratis, Ardor, others? It sounds weird, but when I dig deeper, it turned out all of these platforms are either not supporting Turing complete smart contracts or are still in development. So that there are only 2 real platforms with possibility of writing complex smart contracts - Ethereum and NEO. Probably I'm missing something?


But back to functional approach and what I started with. I've been tinkering with the idea for some time and came up with the following code design prototype (not a real thing yet). The example is in JavaScript as it has better adoption, I plan to rewrite example in PureScript or Elm.

Code:
/**
 * Move balance contract
 *
 * Simplified example of how a cryptocurrency logic could be implemented as a smart contract
 * with functional approach
 *
 *
 * @param {Object}      ctx        Execution context
 * @param {BigInteger}  amount     Amount to move
 *
 * @return {Object}  Array of properties to set after function execution
 */

function contract(ctx, amount) {

    var callerBalance = getProperty(ctx.caller, 'balance', zero);
    var calleeBalance = getProperty(ctx.callee, 'balance', zero);

    var newCallerBalance = callerBalance.subtract(amount);
    var newCalleeBalance = calleeBalance.add(amount);

    return [
        { target: ctx.caller, balance: newCallerBalance },
        { target: ctx.callee, balance: newCalleeBalance },
    ];
}

In the above example (it's almost a pseudocode for now) I tried to demonstrate how could smart contracts be implemented to change state on blockchain and still not having mutable operators, in the sense that all the inner logic and state changes inside the function would not matter. Instead, only the return values are used to associate key/value store with new state changes.

What you think, does it sound any interesting?



Building a JavaScript Smart Contracts Engine
Github | Site | Chat
1715044630
Hero Member
*
Offline Offline

Posts: 1715044630

View Profile Personal Message (Offline)

Ignore
1715044630
Reply with quote  #2

1715044630
Report to moderator
1715044630
Hero Member
*
Offline Offline

Posts: 1715044630

View Profile Personal Message (Offline)

Ignore
1715044630
Reply with quote  #2

1715044630
Report to moderator
1715044630
Hero Member
*
Offline Offline

Posts: 1715044630

View Profile Personal Message (Offline)

Ignore
1715044630
Reply with quote  #2

1715044630
Report to moderator
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715044630
Hero Member
*
Offline Offline

Posts: 1715044630

View Profile Personal Message (Offline)

Ignore
1715044630
Reply with quote  #2

1715044630
Report to moderator
1715044630
Hero Member
*
Offline Offline

Posts: 1715044630

View Profile Personal Message (Offline)

Ignore
1715044630
Reply with quote  #2

1715044630
Report to moderator
1715044630
Hero Member
*
Offline Offline

Posts: 1715044630

View Profile Personal Message (Offline)

Ignore
1715044630
Reply with quote  #2

1715044630
Report to moderator
Happydd
Sr. Member
****
Offline Offline

Activity: 630
Merit: 250



View Profile
April 21, 2018, 12:02:42 PM
 #2

Right! Smart contracts can be verified. What do you know about smart contracts? It's called "Smart contract". This is an algorithm that generates a number of conditions, and when these conditions are met, the corresponding terms will be executed without human intervention. The smart chip is used by the Ethereum in the blockchain system, which allows blockchain to track all deals and transactions.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!