universum 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70d14cee1fae619bb93bdf56c7a81b79987f16bb
4
- data.tar.gz: 8ed7b6fde7aabaeb6430ebbc61c1863e50241064
3
+ metadata.gz: 2a83f038e0a43351f492fc32ea68042559a82782
4
+ data.tar.gz: 604e2ed189008bc319d88b2cbb0d1cafbe8bffc1
5
5
  SHA512:
6
- metadata.gz: 6a06807b4ff6cd4a8b4d84dc688e8a9af995a08135aaca0fcf8b1f1d0c249cf0530560ca901ebcf9c189e4b11131037cd1862369e956a5117ac5a5ef9216287c
7
- data.tar.gz: e3e45a313da7ee2c0e0094f61c5fc082e015b8a5d0dfcc3e291decbd430d48746fa10f0ad4db4f6fca171413fd6cb8a3af3aff5949e328ffddafe879fa731995
6
+ metadata.gz: 7e27c2c0b46fd2e82bbfa1994656cdc174499efcb23b4f0cbb8304ef497c421ca4c32ab1b778003f0e24470de430ab9126e01dbf0d95c8b9d265b9208a2c4ad1
7
+ data.tar.gz: c0c4e8c82b5c4cf32a9d6414b7246a66f0bd675820cdb9c27b6376bfcaba37c028086d43955e61b468f865db0c563eee93cac86f87e7b70f691936be983cc9f1
data/Manifest.txt CHANGED
@@ -18,9 +18,11 @@ lib/universum/units_money.rb
18
18
  lib/universum/units_time.rb
19
19
  lib/universum/universum.rb
20
20
  lib/universum/version.rb
21
+ test/contracts/ballot.rb
21
22
  test/contracts/greeter.rb
22
23
  test/contracts/mytoken.rb
23
24
  test/helper.rb
25
+ test/test_ballot.rb
24
26
  test/test_enum.rb
25
27
  test/test_event.rb
26
28
  test/test_greeter.rb
data/README.md CHANGED
@@ -82,8 +82,6 @@ And let's run the greeter with Universum (Uni):
82
82
  ``` ruby
83
83
  require 'universum'
84
84
 
85
- Account['0x1111'] ## setup a test account
86
-
87
85
  ## create contract (english version)
88
86
  tx = Uni.send_transaction( from: '0x1111', data: ['./greeter', 'Hello World!'] )
89
87
  greeter = tx.receipt.contract
@@ -149,7 +147,7 @@ def initialize( initial_supply )
149
147
  end
150
148
 
151
149
  def transfer( to, value )
152
- assert @balance_of[ msg.sender ] >= value
150
+ assert @balance_of[ msg.sender ] >= value
153
151
  assert @balance_of[ to ] + value >= @balance_of[ to ]
154
152
 
155
153
  @balance_of[ msg.sender ] -= value
@@ -171,7 +169,10 @@ See the [`/universum-contracts`](https://github.com/s6ruby/universum-contracts)
171
169
 
172
170
  ## More Documentation / Articles / Contracts
173
171
 
174
- [Programming Crypto Blockchain Contracts Step-by-Step Book / Guide](https://github.com/s6ruby/programming-cryptocontracts) - Let's Start with Ponzi & Pyramid Schemes. Run Your Own Lotteries, Gambling Casinos and more on the Blockchain World Computer...
172
+ [Programming Crypto Blockchain Contracts Step-by-Step Book / Guide](https://github.com/s6ruby/programming-cryptocontracts) - Let's start with ponzi & pyramid schemes. Run your own lotteries, gambling casinos and more on the blockchain world computer...
173
+
174
+ [Safe Data Structures (Array, Hash, Struct)](https://github.com/s6ruby/safestruct) - Say goodbye to null / nil (and maybe) and the Billion-Dollar mistake. Say hello to zero and the Billon-Dollar fix.
175
+
175
176
 
176
177
 
177
178
 
@@ -186,6 +187,8 @@ $ gem install universum
186
187
 
187
188
  ## License
188
189
 
190
+ ![](https://publicdomainworks.github.io/buttons/zero88x31.png)
191
+
189
192
  The `universum` scripts are dedicated to the public domain.
190
193
  Use it as you please with no restrictions whatsoever.
191
194
 
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ Hoe.spec 'universum' do
18
18
  self.history_file = 'CHANGELOG.md'
19
19
 
20
20
  self.extra_deps = [
21
- ['safestruct'],
21
+ ['safestruct', '>=1.0.0'],
22
22
  ]
23
23
 
24
24
  self.licenses = ['Public Domain']
data/lib/universum.rb CHANGED
@@ -12,6 +12,8 @@ require 'uri'
12
12
  ## 3rd party gems
13
13
  require 'safestruct' # SafeArray, SafeHash, SafeStruct, etc.
14
14
 
15
+ ## note: make Mapping an alias for Hash (lets you use Mapping.of() for Hash.of)
16
+ Mapping = Hash
15
17
 
16
18
 
17
19
  ## our own code
@@ -3,6 +3,7 @@
3
3
 
4
4
  class Contract
5
5
 
6
+ include Safe # adds support for SafeStruct (Struct), SafeArray (Array), SafeHash (Mapping)
6
7
  #########
7
8
  # load "class-less" contract
8
9
  # e.g. SathoshiDice = Contract.load( './sathoshi_dice' )
@@ -52,6 +52,15 @@ class Enum
52
52
  ###################
53
53
  ## meta-programming "macro" - build class (on the fly)
54
54
  def self.build_class( *keys )
55
+
56
+ ## check if all keys are symbols and follow the ruby id(entifier) naming rules
57
+ keys.each do |key|
58
+ if key.is_a?( Symbol ) && key =~ /\A[a-z][a-zA-Z0-9_]*\z/
59
+ else
60
+ raise ArgumentError.new( "[Enum] arguments to Enum.new must be all symbols following the ruby id naming rules; >#{key}< failed" )
61
+ end
62
+ end
63
+
55
64
  klass = Class.new( Enum )
56
65
 
57
66
  ## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
@@ -24,7 +24,7 @@ class Integer
24
24
  #
25
25
  # wei 1 wei | 1
26
26
  # kwei (babbage) 1e3 wei | 1_000
27
- # mwei (lovelace | ada) 1e6 wei | 1_000_000
27
+ # mwei (lovelace) 1e6 wei | 1_000_000
28
28
  # gwei (shannon) 1e9 wei | 1_000_000_000
29
29
  # microether (szabo) 1e12 wei | 1_000_000_000_000
30
30
  # milliether (finney) 1e15 wei | 1_000_000_000_000_000
@@ -32,7 +32,7 @@ class Integer
32
32
  #
33
33
  # Names in Honor:
34
34
  # wei => Wei Dai
35
- # lovelace | ada => Ada Lovelace (1815-1852)
35
+ # lovelace => Ada Lovelace (1815-1852)
36
36
  # babbage => Charles Babbage (1791-1871)
37
37
  # shannon => Claude Shannon (1916-2001)
38
38
  # szabo => Nick Szabo
@@ -50,7 +50,6 @@ class Integer
50
50
  ## alias - use alias or alias_method - why? why not?
51
51
  def babbage() kwei; end
52
52
  def lovelace() mwei; end
53
- def ada() mwei; end ## todo/check: in use, really? keep just lovelave- why? why not?
54
53
  def shannon() gwei; end
55
54
  def szabo() microether; end
56
55
  def finney() milliether; end
@@ -8,7 +8,7 @@ class Universum
8
8
 
9
9
  MAJOR = 0
10
10
  MINOR = 3
11
- PATCH = 0
11
+ PATCH = 1
12
12
  VERSION = [MAJOR,MINOR,PATCH].join('.')
13
13
 
14
14
  def self.version
@@ -0,0 +1,66 @@
1
+ #########################
2
+ # Ballot Contract
3
+
4
+ Voter = Struct.new( weight: 0,
5
+ voted: false,
6
+ vote: 0,
7
+ delegate: Address(0))
8
+
9
+ Proposal = Struct.new( vote_count: 0 )
10
+
11
+ ## Create a new ballot with $(num_proposals) different proposals.
12
+ def initialize( num_proposals )
13
+ @chairperson = msg.sender
14
+ @voters = Mapping.of( Address => Voter )
15
+ @proposals = Array.of( Proposal, num_proposals )
16
+
17
+ @voters[@chairperson].weight = 1
18
+ end
19
+
20
+ ## Give $(to_voter) the right to vote on this ballot.
21
+ ## May only be called by $(chairperson).
22
+ def give_right_to_vote( to_voter )
23
+ assert msg.sender == @chairperson && @voters[to_voter].voted? == false
24
+ @voters[to_voter].weight = 1
25
+ end
26
+
27
+ ## Delegate your vote to the voter $(to).
28
+ def delegate( to )
29
+ sender = @voters[msg.sender] # assigns reference
30
+ assert sender.voted? == false
31
+
32
+ while @voters[to].delegate != Address(0) && @voters[to].delegate != msg.sender do
33
+ to = @voters[to].delegate
34
+ end
35
+ assert to != msg.sender
36
+
37
+ sender.voted = true
38
+ sender.delegate = to
39
+ delegate_to = @voters[to]
40
+ if delegate_to.voted
41
+ @proposals[delegate_to.vote].vote_count += sender.weight
42
+ else
43
+ delegate_to.weight += sender.weight
44
+ end
45
+ end
46
+
47
+ ## Give a single vote to proposal $(to_proposal).
48
+ def vote( to_proposal )
49
+ sender = @voters[msg.sender]
50
+ assert sender.voted? == false && to_proposal < @proposals.length
51
+ sender.voted = true
52
+ sender.vote = to_proposal
53
+ @proposals[to_proposal].vote_count += sender.weight
54
+ end
55
+
56
+ def winning_proposal
57
+ winning_vote_count = 0
58
+ winning_proposal = 0
59
+ @proposals.each_with_index do |proposal,i|
60
+ if proposal.vote_count > winning_vote_count
61
+ winning_vote_count = proposal.vote_count
62
+ winning_proposal = i
63
+ end
64
+ end
65
+ winning_proposal
66
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_ballot.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+
12
+ class TestBallot < MiniTest::Test
13
+
14
+ Ballot = Contract.load( "#{Universum.root}/test/contracts/ballot" )
15
+
16
+ def test_uni
17
+ Account['0x1111'] ### todo/fix: auto-add account in from: if not present or missing!!!!
18
+ Account['0xaaaa']
19
+ Account['0xbbbb']
20
+ Account['0xcccc']
21
+ Account['0xdddd']
22
+
23
+ tx = Uni.send_transaction( from: '0x1111', data: [Ballot, 2] )
24
+ pp tx
25
+ pp tx.receipt
26
+
27
+ ballot = tx.receipt.contract
28
+ pp ballot
29
+
30
+ Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xaaaa'] )
31
+ Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xbbbb'] )
32
+ Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xcccc'] )
33
+ Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xdddd'] )
34
+ pp ballot
35
+
36
+ Uni.send_transaction( from: '0xaaaa', to: ballot, data: [:vote, 1] )
37
+ Uni.send_transaction( from: '0xbbbb', to: ballot, data: [:delegate, '0xaaaa'] )
38
+ Uni.send_transaction( from: '0xcccc', to: ballot, data: [:vote, 0] )
39
+ Uni.send_transaction( from: '0xdddd', to: ballot, data: [:delegate, '0xbbbb'] )
40
+ pp ballot
41
+
42
+ ## todo/fix: get return value from sent_transaction
43
+ assert_equal 1, ballot.winning_proposal
44
+ end
45
+
46
+ end # class TestBallot
@@ -47,7 +47,6 @@ def test_money
47
47
 
48
48
  assert_equal 1_000_000, 1.mwei
49
49
  assert_equal 1_000_000, 1.lovelace
50
- assert_equal 1_000_000, 1.ada
51
50
  assert_equal 1_000_000*2, 1.mwei*2
52
51
  assert_equal value+1_000_000, value+1.mwei
53
52
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: universum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-14 00:00:00.000000000 Z
11
+ date: 2019-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: safestruct
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rdoc
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -85,9 +85,11 @@ files:
85
85
  - lib/universum/units_time.rb
86
86
  - lib/universum/universum.rb
87
87
  - lib/universum/version.rb
88
+ - test/contracts/ballot.rb
88
89
  - test/contracts/greeter.rb
89
90
  - test/contracts/mytoken.rb
90
91
  - test/helper.rb
92
+ - test/test_ballot.rb
91
93
  - test/test_enum.rb
92
94
  - test/test_event.rb
93
95
  - test/test_greeter.rb