lib/zold/patch.rb in zold-0.19.2 vs lib/zold/patch.rb in zold-0.20.0

- old
+ new

@@ -41,11 +41,11 @@ def to_s return 'nothing' if @txns.empty? "#{@txns.count} txns" end - def join(wallet, baseline = true) + def join(wallet, baseline: true, legacy: false) if @id.nil? @id = wallet.id @key = wallet.key if baseline @txns = wallet.txns @@ -66,25 +66,27 @@ 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 legacy && (txn.amount.positive? || txn.date > Time.now - 24 * 60 * 60) 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 \"#{txn.to_text}\" from #{wallet.mnemo}") + @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("Transaction ##{txn.id} attempts to make the balance of \ -#{wallet.id}/#{Amount.new(zents: balance).to_zld}/#{@txns.size} negative: #{txn.to_text}") + @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 transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}") + @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 @@ -98,11 +100,19 @@ 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 unless @wallets.acq(txn.bnf, &:exists?) - @log.error("Paying wallet file is absent: #{txn.to_text}") - next + if block_given? + yield(txn.bnf) + unless @wallets.acq(txn.bnf, &:exists?) + @log.error("Paying wallet #{txn.bnf} file is absent even after PULL: \"#{txn.to_text}\"") + next + end + else + @log.error("Paying wallet #{txn.bnf} file is absent and it's a \"shallow\" MERGE: #{txn.to_text}") + next + end 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