Class: Universum

Inherits:
Object
  • Object
show all
Defined in:
lib/universum/version.rb,
lib/universum/universum.rb

Overview

Uni short for Universum

Constant Summary collapse

MAJOR =
0
MINOR =
5
PATCH =
1
VERSION =
[MAJOR,MINOR,PATCH].join('.')

Class Method Summary collapse

Class Method Details

.accountsObject



104
# File 'lib/universum/universum.rb', line 104

def self.accounts()  Account.all; end


18
19
20
# File 'lib/universum/version.rb', line 18

def self.banner
  "universum/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end

.blockObject



127
128
129
# File 'lib/universum/universum.rb', line 127

def self.block
  @@block ||= Block.new
end

.block=(value) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/universum/universum.rb', line 131

def self.block=( value )
  if value.is_a? Hash
    kwargs = value
    @@block = Block.new( kwargs )
  else   ## assume Block class/type
    @@block = value
  end
end

.blockhash(number) ⇒ Object



122
123
124
125
# File 'lib/universum/universum.rb', line 122

def self.blockhash( number )
  ## for now return "dummy" blockhash
  sha256( "blockhash #{number}" )
end

.contractsObject



105
# File 'lib/universum/universum.rb', line 105

def self.contracts() Contract.all; end

.handlersObject

use listeners/observers/subscribers/… - why? why not?



150
151
152
# File 'lib/universum/universum.rb', line 150

def self.handlers    ## use listeners/observers/subscribers/... - why? why not?
  @@handlers ||= []
end

.log(event) ⇒ Object



154
155
156
# File 'lib/universum/universum.rb', line 154

def self.log( event )
  handlers.each { |h| h.call( event ) }
end

.msgObject



108
109
110
# File 'lib/universum/universum.rb', line 108

def self.msg
  @@msg ||= Msg.new
end

.msg=(value) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/universum/universum.rb', line 112

def self.msg=( value )
  if value.is_a? Hash
    kwargs = value
    @@msg = Msg.new( kwargs )
  else   ## assume Msg class/type
    @@msg = value
  end
end

.rootObject



22
23
24
# File 'lib/universum/version.rb', line 22

def self.root
  "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
end

.send_transaction(from:, to: '0x0000', value: 0, data: []) ⇒ Object

convenience helpers



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/universum/universum.rb', line 30

def self.send_transaction( from:, to: '0x0000', value: 0, data: [] )
  counter = @@counter ||= 0    ## total tx counter for debugging (start with 0)
  @@counter += 1

  ## todo/fix:
  ##   allow/add auto-create account for convenience (and easy testing)!!!

  ## note: always lookup (use) account for now
  if from.is_a?( Account )
     = from
  else
     = Account.at( from )
  end

  ## setup contract msg context
  self.msg = { sender: .address.hex, value: value }


  ## allow shortcut for Class (without ctor arguments) - no need to wrap in array
  data = [data]   if data.is_a? Class

  tx = Transaction.new( from: , to: to, value: value, data: data )


  ## special case - contract creation transaction
  if to == '0x0000'
    klass = data[0]       ## contract class - todo/fix: check if data[] is a contract class!!!
    args  = data[1..-1]   ## arguments

    puts "** tx ##{counter} (block ##{block.number}): #{tx.log_str}"
    contract = klass.create( *args )   ## note: balance and this (and msg.send/transfer) NOT available/possible !!!!

    if value > 0
      ## move value to msg (todo/fix: restore if exception)
      ._sub( value )  # (sub)tract / debit from the sender (account)
      contract._add( value )        # add / credit to the recipient
    end

    puts " new #{contract.class.name} contract adddress: #{contract.address.hex.inspect}"
    ## add new contract to (lookup) directory
    Contract.store( contract.address.hex, contract )

    ## issue (mined) transaction receipt
    receipt = Receipt.new( tx: tx,
                           block: block,
                           contract: contract )
  else
     if value > 0
       ## move value to msg (todo/fix: restore if exception)
       ._sub( value )  # (sub)tract / debit from the sender (account)
       to._add( value )       # add / credit to the recipient
     end

     self.this = to    ## assumes for now that to is always a contract (and NOT an account)!!!

     data = [:receive]   if data.empty?  ## assume receive (default method) for now if data empty (no method specified)

     m    = data[0]       ## method name / signature
     args = data[1..-1]   ## arguments

     puts "** tx ##{counter} (block ##{block.number}): #{tx.log_str}"

     to.__send__( m, *args )    ## note: use __send__ to avoid clash with send( value ) for sending payments!!!

     ## issue (mined) transaction receipt
     receipt = Receipt.new( tx: tx,
                            block: block )
  end

  Receipt.store( receipt )
  tx   # return transaction
end

.thisObject

returns current contract



140
141
142
# File 'lib/universum/universum.rb', line 140

def self.this    ## returns current contract
  @@this
end

.this=(value) ⇒ Object



144
145
146
147
# File 'lib/universum/universum.rb', line 144

def self.this=(value)
  ## todo/fix: check that value is a contract
  @@this = value
end

.versionObject



14
15
16
# File 'lib/universum/version.rb', line 14

def self.version
  VERSION
end