Designing sruby (Secure Ruby) - Use ivars (@balance) or Storage class (storage.balance) for (Blockchain) State?


(Gerald Bauer) #1

Hello,

   If anyone follows along about the new ruby variant / flavor called sruby
   - Small, Smart, Secure, Safe, Solid & Sound (S6) Ruby.

  Here's a language design choice: How to track the contract's
(blockchain) state?

  a) Let's (re)use ruby's ivars (object instance variables) e.g.:

  def initialize
    @owner = msg.sender
    @counter = 0
    @bets = Mapping.of( Integer => Bet )
  end

  or b) let's use a more "explicit" Storage class e.g.:

  def initialize
    storage[:owner] = msg.sender
    storage[:counter] = 0
    storage[:bets] = Mapping.of( Integer => Bet )
  end

  or c) something completly different.

What's your take? a) b) or c).
Help design the future of ruby on the blockchain world computer :-).

  Cheers. Prost.

PS: New to (Secure) Ruby Programming?

Free recommended (online) papers & books and contracts include:

- The "Red Paper" about sruby [1] - Small, Smart, Secure, Safe, Solid
& Sound (S6) Ruby - The Ruby Programming Language for Contract /
Transaction Scripts on the Blockchain World Computer - Yes, It's Just
Ruby
- Programming Crypto Blockchain Contracts Step-by-Step Book / Guide
[2]. Let's Start with Ponzi & Pyramid Schemes. Run Your Own Lotteries,
Gambling Casinos and more on the Blockchain World Computer...
- Safe Data Structures (Array, Hash, Struct) [3] - Say goodbye to null
/ nil (and maybe) and the Billion-Dollar mistake. Say hello to zero
and the Billon-Dollar fix.
- Ruby Sample Contracts for the Universum Blockchain/World Computer Runtime [4]

[1] https://github.com/s6ruby/redpaper
[2] https://github.com/s6ruby/programming-cryptocontracts
[3] https://github.com/s6ruby/safestruct
[4] https://github.com/s6ruby/universum-contracts


(Gerald Bauer) #2

Hello,
   FYI: I've decided to have it both ways - and follow the ruby (and
perl) tradition of There's more than one way to do it :-).

    How? The code gets "auto-patched" with a regex [1] on load and
changes the "short" form to the long from using a storage class.

     Happy coding with ruby (and sruby). Cheers. Prost.

[1] code = code.gsub( /(@{1,})([a-z][a-zA-Z0-9_]*)/ ) do |_|
        if $1.size == 1
            puts "auto-patching contract code >#{$1}#{$2}< to

storage[ :#{$2} ]<"

            "storage[ :#{$2} ]"
        else
           ## assume class variable - skip - keep as is
           "#{$1}#{$2}"
       end
    end