Class: Teth::Minitest

Inherits:
Minitest::Test
  • Object
show all
Extended by:
Configurable
Includes:
Ethereum
Defined in:
lib/teth/minitest.rb

Constant Summary collapse

@@privkeys =

Fixtures

.times.map do |i|
  Utils.keccak256 rand(Constant::TT256).to_s
end

Instance Method Summary collapse

Methods included from Configurable

option

Instance Method Details

#addressObject



118
119
120
# File 'lib/teth/minitest.rb', line 118

def address
  addresses[0]
end

#addressesObject



130
131
132
# File 'lib/teth/minitest.rb', line 130

def addresses
  @addresses ||= privkeys.map {|k| PrivateKey.new(k).to_address }
end

#contractObject



74
75
76
# File 'lib/teth/minitest.rb', line 74

def contract
  @contract
end

#find_contract_sourceObject



78
79
80
81
82
# File 'lib/teth/minitest.rb', line 78

def find_contract_source
  name = self.class.name[0...-4]
  dir = find_contracts_directory
  find_source(dir, name, 'sol') || find_source(dir, name, 'se')
end

#find_contracts_directoryObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/teth/minitest.rb', line 84

def find_contracts_directory
  last = nil
  cur = ENV['PWD']
  while cur != last
    path = File.join cur, self.class.contract_dir_name
    return path if File.directory?(path)
    last = cur
    cur = File.dirname cur
  end
  nil
end

#find_source(dir, name, ext) ⇒ Object



96
97
98
99
100
# File 'lib/teth/minitest.rb', line 96

def find_source(dir, name, ext)
  name = "#{name}.#{ext}"
  list = Dir.glob File.join(dir, "**/*.#{ext}")
  list.find {|fn| File.basename(fn) =~ /\A#{name}\z/i }
end

#headObject



152
153
154
# File 'lib/teth/minitest.rb', line 152

def head
  state.head
end

#heuristic_prettify(bytes) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/teth/minitest.rb', line 59

def heuristic_prettify(bytes)
  dry_bytes = bytes.gsub(/\A(\x00)+/, '')
  dry_bytes = dry_bytes.gsub(/(\x00)+\z/, '')
  if (bytes.size - dry_bytes.size) > 3
    # there's many ZERO bytes in the head or tail of bytes, it must be padded
    if dry_bytes.size == 20 # address
      Utils.encode_hex(dry_bytes)
    else
      dry_bytes
    end
  else
    Utils.encode_hex(bytes)
  end
end

#privkeyObject



110
111
112
# File 'lib/teth/minitest.rb', line 110

def privkey
  privkeys[0]
end

#privkeysObject



122
123
124
# File 'lib/teth/minitest.rb', line 122

def privkeys
  @privkeys ||= @@privkeys
end

#pubkeyObject



114
115
116
# File 'lib/teth/minitest.rb', line 114

def pubkey
  pubkeys[0]
end

#pubkeysObject



126
127
128
# File 'lib/teth/minitest.rb', line 126

def pubkeys
  @pubkeys ||= privkeys.map {|k| PrivateKey.new(k).to_pubkey }
end

#setupObject



16
17
18
19
20
# File 'lib/teth/minitest.rb', line 16

def setup
  path = find_contract_source
  raise "Cannot find corresponding contract source" unless path
  setup_contract(path)
end

#setup_contract(path) ⇒ Object



28
29
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
# File 'lib/teth/minitest.rb', line 28

def setup_contract(path)
  case path
  when /\.sol\z/
    type = :solidity
  when /\.se\z/
    type = :serpent
  else
    raise "Unknown contract source type: #{path}"
  end

  code = File.read path
  log_listener = ->(log) do
    if log.instance_of?(Log) # unrecognized event
      if self.class.print_logs
        topics = log.topics.map {|t| heuristic_prettify Utils.int_to_big_endian(t) }
        data = heuristic_prettify(log.data)
        puts "[Log] #{Utils.encode_hex(log.address)} >>> topics=#{topics} data=#{data.inspect}"
      end
    else # user defined event
      if self.class.print_logs && self.class.print_events
        from = log.delete '_from'
        name = log.delete '_event_type'
        s = log.keys.map {|k| "#{k}=#{log[k]}" }.join(' ')
        puts "[Event] #{from} #{name} >>> #{s}"
      end
    end
  end
  @contract = state.abi_contract code,
    language: type, sender: privkey, log_listener: log_listener
end

#stateObject



148
149
150
# File 'lib/teth/minitest.rb', line 148

def state
  @state ||= Tester::State.new privkeys: privkeys
end

#teardownObject



22
23
24
25
26
# File 'lib/teth/minitest.rb', line 22

def teardown
  @state = nil
  @account = nil
  @contract = nil
end