Class: Zold::Patch
- Inherits:
-
Object
- Object
- Zold::Patch
- Defined in:
- lib/zold/patch.rb
Overview
A patch
Instance Method Summary collapse
-
#initialize(wallets, log: Log::Quiet.new) ⇒ Patch
constructor
A new instance of Patch.
- #join(wallet, baseline = true) ⇒ Object
-
#save(file, overwrite: false) ⇒ Object
Returns TRUE if the file was actually modified.
- #to_s ⇒ Object
Constructor Details
#initialize(wallets, log: Log::Quiet.new) ⇒ Patch
Returns a new instance of Patch.
34 35 36 37 38 39 40 |
# File 'lib/zold/patch.rb', line 34 def initialize(wallets, log: Log::Quiet.new) raise 'Wallets can\'t be nil' if wallets.nil? raise 'Wallets must be of type Wallets' unless wallets.is_a?(Wallets) @wallets = wallets @txns = [] @log = log end |
Instance Method Details
#join(wallet, baseline = true) ⇒ Object
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 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 |
# File 'lib/zold/patch.rb', line 47 def join(wallet, baseline = true) if @id.nil? @id = wallet.id @key = wallet.key if baseline @txns = wallet.txns @log.debug("The baseline: #{@txns.count} transactions, the balance is #{wallet.balance}") else @log.debug("The baseline of #{@txns.count} transactions ignored") end @network = wallet.network end if wallet.network != @network raise "The wallet is from a different network '#{wallet.network}', ours is '#{@network}'" end raise 'Public key mismatch' if wallet.key != @key raise "Wallet ID mismatch: #{@id} != #{wallet.id}" if wallet.id != @id wallet.txns.each do |txn| next if @txns.find { |t| t == txn } if txn.amount.negative? dup = @txns.find { |t| t.id == txn.id && t.amount.negative? } if dup @log.error("An attempt to overwrite #{dup.to_text} with this: #{txn.to_text}") next end balance = @txns.map(&:amount).map(&:to_i).inject(&:+).to_i if balance < txn.amount.to_i * -1 && !wallet.root? @log.error("Transaction ##{txn.id} attempts to make the balance of \ #{wallet.id}/#{Amount.new(coins: balance).to_zld}/#{@txns.size} negative: #{txn.to_text}") next end unless Signature.new.valid?(@key, wallet.id, txn) @log.error("Invalid RSA signature at transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}") next end else if !txn.sign.nil? && !txn.sign.empty? @log.error("RSA signature is redundant at ##{txn.id} of #{wallet.id}: #{txn.to_text}") next end payer = @wallets.find(txn.bnf) unless payer.exists? @log.error("Paying wallet #{wallet.id} is absent at ##{txn.id}: #{txn.to_text}") next end unless payer.has?(txn.id, wallet.id) @log.error("Paying wallet #{txn.bnf} doesn't have transaction ##{txn.id} \ among #{payer.txns.count} transactions: #{txn.to_text}") next end end @txns << txn @log.debug("Merged on top, balance is #{@txns.map(&:amount).inject(&:+)}: #{txn.to_text}") end end |
#save(file, overwrite: false) ⇒ Object
Returns TRUE if the file was actually modified
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/zold/patch.rb', line 104 def save(file, overwrite: false) raise 'You have to join at least one wallet in' if @id.nil? before = '' before = AtomicFile.new(file).read if File.exist?(file) wallet = Wallet.new(file) wallet.init(@id, @key, overwrite: overwrite, network: @network) @txns.each { |t| wallet.add(t) } after = AtomicFile.new(file).read before != after end |
#to_s ⇒ Object
42 43 44 45 |
# File 'lib/zold/patch.rb', line 42 def to_s return 'empty patch' if @txns.empty? "#{@txns.count} txns" end |