Class: Zold::Patch
- Inherits:
-
Object
- Object
- Zold::Patch
- Defined in:
- lib/zold/patch.rb
Overview
A patch
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(wallets, log: Log::NULL) ⇒ Patch
constructor
A new instance of Patch.
-
#join(wallet) ⇒ Object
Joins a new wallet on top of existing patch.
-
#legacy(wallet, hours: 24) ⇒ Object
Add legacy transactions first, since they are negative and can’t be deleted ever.
-
#save(file, overwrite: false) ⇒ Object
Returns TRUE if the file was actually modified.
- #to_s ⇒ Object
Constructor Details
Instance Method Details
#empty? ⇒ Boolean
129 130 131 |
# File 'lib/zold/patch.rb', line 129 def empty? @id.nil? end |
#join(wallet) ⇒ Object
Joins a new wallet on top of existing patch. An attempt is made to copy as many transactions from the newcoming wallet to the existing set of transactions, avoiding mistakes and duplicates.
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 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 |
# File 'lib/zold/patch.rb', line 61 def join(wallet) if @id.nil? @id = wallet.id @key = wallet.key @network = wallet.network end unless wallet.network == @network @log.error("The wallet is from a different network '#{wallet.network}', ours is '#{@network}'") return end unless wallet.key == @key @log.error('Public key mismatch') return end unless wallet.id == @id @log.error("Wallet ID mismatch, ours is #{@id}, theirs is #{wallet.id}") return end 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 existing transaction \"#{dup.to_text}\" \ with a new one \"#{txn.to_text}\" from #{wallet.mnemo}") next end balance = @txns.map(&:amount).map(&:to_i).inject(&:+).to_i if balance < txn.amount.to_i * -1 && !wallet.root? @log.error("The transaction ##{txn.id} attempts to make the balance of \ #{wallet.id}/#{Amount.new(zents: balance).to_zld}/#{@txns.size} negative: \"#{txn.to_text}\"") next end unless Signature.new(@network).valid?(@key, wallet.id, txn) @log.error("Invalid RSA signature at the transaction ##{txn.id} of #{wallet.id}: \"#{txn.to_text}\"") next end else dup = @txns.find { |t| t.id == txn.id && t.bnf == txn.bnf && t.amount.positive? } if dup @log.error("Overwriting \"#{dup.to_text}\" with \"#{txn.to_text}\" from #{wallet.mnemo} (same ID/BNF)") next end if !txn.sign.nil? && !txn.sign.empty? @log.error("RSA signature is redundant at ##{txn.id} of #{wallet.id}: \"#{txn.to_text}\"") next end unless wallet.prefix?(txn.prefix) @log.error("Payment prefix '#{txn.prefix}' doesn't match with the key of #{wallet.id}: \"#{txn.to_text}\"") next end if !@wallets.acq(txn.bnf, &:exists?) && yield(txn) unless @wallets.acq(txn.bnf, &:exists?) @log.error("Paying wallet #{txn.bnf} file is absent even after PULL: \"#{txn.to_text}\"") next end unless @wallets.acq(txn.bnf) { |p| p.includes_negative?(txn.id, wallet.id) } @log.error("The beneficiary #{@wallets.acq(txn.bnf, &:mnemo)} of #{@id} \ doesn't have this transaction: \"#{txn.to_text}\"") next end end end @txns << txn @log.debug("Merged on top, balance is #{@txns.map(&:amount).inject(&:+)}: #{txn.to_text}") end end |
#legacy(wallet, hours: 24) ⇒ Object
Add legacy transactions first, since they are negative and can’t be deleted ever. This method is called by merge.rb in order to add legacy negative transactions to the patch before everything else. They are not supposed to be disputed, ever.
51 52 53 54 55 56 |
# File 'lib/zold/patch.rb', line 51 def legacy(wallet, hours: 24) raise 'You can\'t add legacy to a non-empty patch' unless @id.nil? wallet.txns.each do |txn| @txns << txn if txn.amount.negative? && txn.date < Time.now - hours * 60 * 60 end end |
#save(file, overwrite: false) ⇒ Object
Returns TRUE if the file was actually modified
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/zold/patch.rb', line 134 def save(file, overwrite: false) raise 'You have to join at least one wallet in' if empty? before = '' before = OpenSSL::Digest::SHA256.file(file).hexdigest if File.exist?(file) wallet = Wallet.new(file) wallet.init(@id, @key, overwrite: overwrite, network: @network) File.open(file, 'a') do |f| @txns.each do |txn| f.print "#{txn}\n" end end wallet.refurbish after = OpenSSL::Digest::SHA256.file(file).hexdigest before != after end |
#to_s ⇒ Object
42 43 44 45 |
# File 'lib/zold/patch.rb', line 42 def to_s return 'nothing' if @txns.empty? "#{@txns.count} txns" end |