Class: Ethereum::Tester::State

Inherits:
Object
  • Object
show all
Defined in:
lib/ethereum/tester/state.rb

Constant Summary collapse

TMP_DIR_PREFIX =
'eth-tester-'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: nil, privkeys: Fixture.keys, start_alloc: nil, gas_price: Fixture.gas_price, gas_limit: Fixture.gas_limit) ⇒ State

Returns a new instance of State.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ethereum/tester/state.rb', line 13

def initialize(env: nil, privkeys: Fixture.keys, start_alloc: nil, gas_price: Fixture.gas_price, gas_limit: Fixture.gas_limit)
  if env
    @db = env.db
    @env = env
  else
    @db = DB::EphemDB.new
    @env = Env.new @db
  end

  @temp_data_dir = Dir.mktmpdir TMP_DIR_PREFIX

  @privkeys = privkeys
  @accounts = @privkeys.map {|k| PrivateKey.new(k).to_address }
  @gas_price = gas_price
  @gas_limit = gas_limit

  start_alloc ||= get_start_alloc()
  @block = Block.genesis @env, start_alloc: start_alloc
  @block.timestamp = 1410973349
  @block.coinbase = @accounts[0]
  @block.gas_limit = 10**9

  @blocks = [@block]
  @last_tx = nil

  ObjectSpace.define_finalizer(self) {|id| FileUtils.rm_rf(@temp_data_dir) }
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/ethereum/tester/state.rb', line 11

def block
  @block
end

#blocksObject (readonly)

Returns the value of attribute blocks.



11
12
13
# File 'lib/ethereum/tester/state.rb', line 11

def blocks
  @blocks
end

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/ethereum/tester/state.rb', line 11

def env
  @env
end

Instance Method Details

#_send_tx(sender, to, value, evmdata: '', funid: nil, abi: nil, profiling: 0) ⇒ Object

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ethereum/tester/state.rb', line 100

def _send_tx(sender, to, value, evmdata: '', funid: nil, abi: nil, profiling: 0)
  if funid || abi
    raise ArgumentError, "Send with funid+abi is deprecated. Please use the abi_contract mechanism."
  end

  t1, g1 = Time.now, @block.gas_used
  sendnonce = @block.get_nonce PrivateKey.new(sender).to_address
  tx = Transaction.new(sendnonce, @gas_price, @gas_limit, to, value, evmdata)
  @last_tx = tx
  tx.sign(sender)

  recorder = profiling > 1 ? LogRecorder.new : nil

  success, output = @block.apply_transaction(tx)
  raise TransactionFailed if success.false?
  out = {output: output}

  if profiling > 0
    zero_bytes = tx.data.count Constant::BYTE_ZERO
    none_zero_bytes = tx.data.size - zero_bytes
    intrinsic_gas_used = Opcodes::GTXCOST +
      Opcodes::GTXDATAZERO * zero_bytes +
      Opcodes::GTXDATANONZERO * none_zero_bytes
    t2, g2 = Time.now, @block.gas_used
    out[:time] = t2 - t1
    out[:gas] = g2 - g1 - intrinsic_gas_used
  end

  if profiling > 1
    # TODO: collect all traced ops use LogRecorder
  end

  out
end

#abi_contract(code, sender: , endowment: 0, language: :serpent, libraries: nil, path: nil, constructor_parameters: nil, log_listener: nil, listen: true, **kwargs) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ethereum/tester/state.rb', line 59

def abi_contract(code, sender: @privkeys[0], endowment: 0, language: :serpent,
                 libraries: nil, path: nil, constructor_parameters: nil,
                 log_listener: nil, listen: true, **kwargs)
  code = Language.format_spaces code
  compiler = Language.get language

  contract_interface = compiler.mk_full_signature code, path: path, **kwargs
  translator = ABI::ContractTranslator.new contract_interface

  encoded_parameters = constructor_parameters ?
    translator.encode_constructor_arguments(constructor_parameters) :
    nil

  address = contract(code, sender: sender, endowment: endowment, language: language,
                     libraries: libraries, path: path, constructor_call: encoded_parameters,
                     **kwargs)

  ABIContract.new(self, translator, address, listen: listen, log_listener: log_listener, default_key: sender)
end

#call(*args, **kwargs) ⇒ Object

Raises:



92
93
94
# File 'lib/ethereum/tester/state.rb', line 92

def call(*args, **kwargs)
  raise DeprecatedError, "Call deprecated. Please use the abi_contract mechanism or message(sender, to, value, data) directly, using the ABI module to generate data if needed."
end

#contract(code, sender: , endowment: 0, language: :serpent, libraries: nil, path: nil, constructor_call: nil, **kwargs) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ethereum/tester/state.rb', line 45

def contract(code, sender: @privkeys[0], endowment: 0, language: :serpent,
             libraries: nil, path: nil, constructor_call: nil, **kwargs)
  code = Language.format_spaces code
  compiler = Language.get language

  bytecode = compiler.compile code, path: path, libraries: libraries, **kwargs
  bytecode += constructor_call if constructor_call

  address = evm bytecode, sender: sender, endowment: endowment
  raise AssertError, "Contract code empty" if @block.get_code(address).empty?

  address
end

#evm(bytecode, sender: , endowment: 0, gas: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ethereum/tester/state.rb', line 79

def evm(bytecode, sender: @privkeys[0], endowment: 0, gas: nil)
  sendnonce = @block.get_nonce PrivateKey.new(sender).to_address

  tx = Transaction.contract sendnonce, @gas_price, @gas_limit, endowment, bytecode
  tx.sign sender
  tx.startgas = gas if gas

  success, output = @block.apply_transaction tx
  raise ContractCreationFailed if success.false?

  output
end

#headObject



41
42
43
# File 'lib/ethereum/tester/state.rb', line 41

def head
  @blocks.last
end

#mine(n = 1, coinbase: ) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ethereum/tester/state.rb', line 178

def mine(n=1, coinbase: @accounts[0])
  n.times do |i|
    @block.finalize
    @block.commit_state

    @db.put @block.full_hash, RLP.encode(@block)

    t = @block.timestamp + 6 + rand(12)
    x = Block.build_from_parent @block, coinbase, timestamp: t
    @block = x

    @blocks.push @block
  end
end

#mkspv(sender, to, value, data: [], funid: nil, abi: nil) ⇒ Object

Raises:

  • (NotImplemented)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ethereum/tester/state.rb', line 140

def mkspv(sender, to, value, data: [], funid: nil, abi: nil)
  raise NotImplemented

  serpent = Language.get :serpent
  raise "ruby-serpent not installed" unless serpent

  sendnonce = @block.get_nonce PrivateKey.new(sender).to_address
  evmdata = funid ? serpent.encode_abi(funid, *abi) : serpent.encode_datalist(*data)

  tx = Transaction.new(sendnonce, @gas_price, @gas_limit, to, value, evmdata)
  @last_tx = tx
  tx.sign(sender)

  SPV.make_transaction_proof(@block, tx)
end

#profile(*args, **kwargs) ⇒ Object



135
136
137
138
# File 'lib/ethereum/tester/state.rb', line 135

def profile(*args, **kwargs)
  kwargs[:profiling] = true
  _send_tx(*args, **kwargs)
end

#revert(data) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/ethereum/tester/state.rb', line 197

def revert(data)
  @block = RLP.decode data, sedes: Block, env: @env

  @block.make_mutable!
  @block._cached_rlp = nil

  @block.header.make_mutable!
  @block.header._cached_rlp = nil
end

#send_tx(*args, **kwargs) ⇒ Object



96
97
98
# File 'lib/ethereum/tester/state.rb', line 96

def send_tx(*args, **kwargs)
  _send_tx(*args, **kwargs)[:output]
end

#snapshotObject



193
194
195
# File 'lib/ethereum/tester/state.rb', line 193

def snapshot
  RLP.encode @block
end

#trace(sender, to, value, data = []) ⇒ Object



172
173
174
175
176
# File 'lib/ethereum/tester/state.rb', line 172

def trace(sender, to, value, data=[])
  recorder = LogRecorder.new
  send_tx sender, to, value, data
  recorder.pop_records # TODO: implement recorder
end

#verifyspv(sender, to, value, data: [], funid: nil, abi: nil, proof: []) ⇒ Object

Raises:

  • (NotImplemented)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ethereum/tester/state.rb', line 156

def verifyspv(sender, to, value, data: [], funid: nil, abi: nil, proof: [])
  raise NotImplemented

  serpent = Language.get(:serpent)
  raise "ruby-serpent not installed" unless serpent

  sendnonce = @block.get_nonce PrivateKey.new(sender).to_address
  evmdata = funid ? serpent.encode_abi(funid, *abi) : serpent.encode_datalist(*data)

  tx = Transaction.new(sendnonce, @gas_price, @gas_limit, to, value, evmdata)
  @last_tx = tx
  tx.sign(sender)

  SPV.verify_transaction_proof(@block, tx, proof)
end