Class: HiveSQL::Transaction

Inherits:
SqlBase
  • Object
show all
Defined in:
lib/hive_sql/models/transaction.rb

Instance Method Summary collapse

Methods inherited from SqlBase

#tx

Instance Method Details

#opObject

So you have a Transaction#tx_id and you want to know what the operation was that lead to it. Well, that’s tricky because all of the ops are in their own tables. This method will (slowly) try to find the appropriate table.



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
# File 'lib/hive_sql/models/transaction.rb', line 34

def op
  retries = 0
  puts type
  # Here, we map the type to class name, if supported.  Most of them can be
  # mapped automatically, e.g. "vote" => "Vote" but some types share tables
  # with one another.  We also use timestamps to narrow the search
  # parameters, for all the good it does.  We use the expiration minus the
  # maximum TaPoS window.
  op_type = case type
  when 'account_create_with_delegation', 'create_claimed_account' then 'AccountCreate'
  when 'comment_options' then 'CommentOption'
  when 'custom_json' then 'Custom'
  when 'delegate_vesting_shares' then 'DelegateVestingShare'
  when 'feed_publish' then 'Feed'
  when 'limit_order_create', 'limit_order_create2' then 'LimitOrder'
  when 'Pow2' then 'Pow'
  when 'set_withdraw_vesting_route' then 'WithdrawVestingRoute'
  when 'transfer_from_savings', 'transfer_to_vesting' then 'Transfer'
  when 'withdraw_vesting' then 'Withdraw'
  when *%w(
    cancel_transfer_from_savings change_recovery_account claim_account
    decline_voting_rights limit_order_cancel recover_account
    request_account_recovery witness_set_properties
  ) then raise "Unsupported: #{type}"
  else; type.split('_').collect(&:capitalize).join
  end
  
  tapos_window_start = expiration - 28800.seconds
  ops = Tx.const_get(op_type).where(tx_id: self).
    where("timestamp BETWEEN ? AND ?", tapos_window_start, expiration)
  
  loop do
    retries += 1
    op = ops.first
    
    return op if !!op
    break if retries > 10
    
    sleep 3
  end
  
  raise "Unable to find #{type} for tx_id: #{tx_id}"
end