Class: Ethereum::Account

Inherits:
Object show all
Includes:
RLP::Sedes::Serializable
Defined in:
lib/ethereum/account.rb

Overview

An Ethereum account.

  • ‘@nonce`: the account’s nonce (the number of transactions sent by the account)

  • ‘@balance`: the account’s balance in wei

  • ‘@storage`: the root of the account’s storage trie

  • ‘@code_hash`: the SHA3 hash of the code associated with the account

  • ‘@db`: the database in which the account’s code is stored

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Account

Returns a new instance of Account.

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
# File 'lib/ethereum/account.rb', line 44

def initialize(*args)
  @db = args.pop if args.size == 5 # (nonce, balance, storage, code_hash, db)
  @db = args.last.delete(:db) if args.last.is_a?(Hash)
  raise ArgumentError, "No database object given" unless @db.is_a?(DB::BaseDB)

  super(*args)
end

Class Method Details

.build_blank(db, initial_nonce = 0) ⇒ Account

Create a blank account.

The returned account will have zero nonce and balance, a blank storage trie and empty code.

Parameters:

  • db (BaseDB)

    the db in which the account will store its code

Returns:

  • (Account)

    the created blank account



36
37
38
39
40
41
# File 'lib/ethereum/account.rb', line 36

def build_blank(db, initial_nonce=0)
  code_hash = Utils.keccak256 Constant::BYTE_EMPTY
  db.put code_hash, Constant::BYTE_EMPTY

  new initial_nonce, 0, Trie::BLANK_ROOT, code_hash, db
end

Instance Method Details

#codeObject

The EVM code of the account.

This property will be read from or written to the db at each access, with ‘code_hash` used as key.



58
59
60
# File 'lib/ethereum/account.rb', line 58

def code
  @db.get code_hash
end

#code=(value) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/ethereum/account.rb', line 62

def code=(value)
  self.code_hash = Utils.keccak256 value

  # Technically a db storage leak, but doesn't really matter; the only
  # thing that fails to get garbage collected is when code disappears due
  # to a suicide.
  @db.inc_refcount(code_hash, value)
end