Module: Reth::JSONRPC::Helper

Included in:
Handler, LogFilter
Defined in:
lib/reth/jsonrpc/helper.rb

Instance Method Summary collapse

Instance Method Details

#bytes_to_hex(bytes) ⇒ Object



18
19
20
# File 'lib/reth/jsonrpc/helper.rb', line 18

def bytes_to_hex(bytes)
  "0x#{Utils.encode_hex(bytes)}"
end

#chainObject



6
7
8
# File 'lib/reth/jsonrpc/helper.rb', line 6

def chain
  @node.services.chain
end

#decode_block_tag(tag) ⇒ Object



63
64
65
66
67
# File 'lib/reth/jsonrpc/helper.rb', line 63

def decode_block_tag(tag)
  return tag if tag.nil?
  return tag if %w(latest earliest pending).include?(tag)
  return hex_to_int(tag)
end

#discoveryObject



14
15
16
# File 'lib/reth/jsonrpc/helper.rb', line 14

def discovery
  @node.services.discovery
end

#encode_block(block, include_transactions = false, pending = false, is_header = false) ⇒ Object

Raises:

  • (ArgumentError)


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
102
# File 'lib/reth/jsonrpc/helper.rb', line 69

def encode_block(block, include_transactions=false, pending=false, is_header=false)
  raise ArgumentError, "cannot include transactions for header" if include_transactions && is_header

  h = {
    number: pending ? nil : int_to_hex(block.number),
    hash: pending ? nil : bytes_to_hex(block.full_hash),
    parentHash: bytes_to_hex(block.prevhash),
    nonce: pending ? nil : bytes_to_hex(block.nonce),
    sha3Uncles: bytes_to_hex(block.uncles_hash),
    logsBloom: pending ? nil : bytes_to_hex(Utils.int_to_big_endian(block.bloom)),
    transactionsRoot: bytes_to_hex(block.tx_list_root),
    stateRoot: bytes_to_hex(block.state_root),
    miner: pending ? nil : bytes_to_hex(block.coinbase),
    difficulty: int_to_hex(block.difficulty),
    extraData: bytes_to_hex(block.extra_data),
    gasLimit: int_to_hex(block.gas_limit),
    gasUsed: int_to_hex(block.gas_used),
    timestamp: int_to_hex(block.timestamp)
  }

  unless is_header
    h[:totalDifficulty] = int_to_hex(block.chain_difficulty)
    h[:size] = int_to_hex(RLP.encode(block).size)
    h[:uncles] = block.uncles.map {|u| bytes_to_hex(u.full_hash) }

    if include_transactions
      h[:transactions] = block.get_transactions.each_with_index.map {|tx, i| encode_tx(tx, block, i, pending) }
    else
      h[:transactions] = block.get_transactions.map {|tx| bytes_to_hex(tx.full_hash) }
    end
  end

  h
end

#encode_loglist(logs) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/reth/jsonrpc/helper.rb', line 120

def encode_loglist(logs)
  logs.map do |l|
    {
      logIndex: l[:pending] ? nil : int_to_hex(l[:log_idx]),
      transactionIndex: l[:pending] ? nil : int_to_hex(l[:tx_idx]),
      transactionHash: l[:pending] ? nil : bytes_to_hex(l[:txhash]),
      blockHash: l[:pending] ? nil : bytes_to_hex(l[:block].full_hash),
      blockNumber: l[:pending] ? nil : int_to_hex(l[:block].number),
      address: bytes_to_hex(l[:log].address),
      data: bytes_to_hex(l[:log].data),
      topics: l[:log].topics.map {|t| bytes_to_hex Utils.zpad_int(t) },
      type: l[:pending] ? 'pending' : 'mined'
    }
  end
end

#encode_tx(transaction, block, i, pending) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/reth/jsonrpc/helper.rb', line 104

def encode_tx(transaction, block, i, pending)
  {
    hash: bytes_to_hex(transaction.full_hash),
    nonce: int_to_hex(transaction.nonce),
    blockHash: bytes_to_hex(block.full_hash),
    blockNumber: pending ? nil : int_to_hex(block.number),
    transactionIndex: int_to_hex(i),
    from: bytes_to_hex(transaction.sender),
    to: bytes_to_hex(transaction.to),
    value: int_to_hex(transaction.value),
    gasPrice: int_to_hex(transaction.gasprice),
    gas: int_to_hex(transaction.startgas),
    input: bytes_to_hex(transaction.data)
  }
end

#get_block(id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reth/jsonrpc/helper.rb', line 44

def get_block(id)
  case id
  when 'latest'
    chain.chain.head
  when 'earliest'
    chain.chain.genesis
  when 'pending'
    chain.chain.head_candidate
  when Integer
    hash = chain.chain.index.get_block_by_number id
    chain.chain.get hash
  when String
    id = hex_to_bytes(id) if id[0,2] == '0x'
    chain.chain.get id
  else
    raise "unknown block id: #{id}"
  end
end

#get_compilersObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/reth/jsonrpc/helper.rb', line 136

def get_compilers
  return @compilers if @compilers

  @compilers = {}

  if serpent = Ethereum::Tester::Language.all[:serpent]
    @compilers[:serpent] = [serpent, :compile]
    @compilers[:lll] = [serpent, :compile_lll]
  end

  if solidity = Ethereum::Tester::Language.all[:solidity]
    @compilers[:solidity] = [solidity, :compile_rich]
  end

  @compilers
end

#get_ivar_value(ivar) ⇒ Object



39
40
41
42
# File 'lib/reth/jsonrpc/helper.rb', line 39

def get_ivar_value(ivar)
  raise "operation failed: #{ivar.reason}" if ivar.rejected?
  ivar.value
end

#hex_to_bytes(hex) ⇒ Object



22
23
24
25
# File 'lib/reth/jsonrpc/helper.rb', line 22

def hex_to_bytes(hex)
  hex = hex[2..-1] if hex[0,2] == '0x'
  Utils.decode_hex hex
end

#hex_to_int(hex) ⇒ Object



33
34
35
36
37
# File 'lib/reth/jsonrpc/helper.rb', line 33

def hex_to_int(hex)
  hex = hex[2..-1] if hex[0,2] == '0x'
  hex = '0' + hex if hex.size % 2 == 1 # padding left to make size even
  Utils.big_endian_to_int hex_to_bytes(hex)
end

#int_to_hex(n) ⇒ Object



27
28
29
30
31
# File 'lib/reth/jsonrpc/helper.rb', line 27

def int_to_hex(n)
  hex = Utils.encode_hex Utils.int_to_big_endian(n)
  hex = hex.gsub(/\A0+/, '')
  "0x#{hex.empty? ? '0' : hex}"
end

#peermanagerObject



10
11
12
# File 'lib/reth/jsonrpc/helper.rb', line 10

def peermanager
  @node.services.peermanager
end