lib/zold/patch.rb in zold-0.22.6 vs lib/zold/patch.rb in zold-0.22.7

- old
+ new

@@ -42,26 +42,28 @@ def to_s return 'nothing' if @txns.empty? "#{@txns.count} txns" end + # 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. + 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 + # 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. - # - # If +baseline+ is set to TRUE the provided wallet is considered to be - # the baseline and all transactions will be blindly trusted. - def join(wallet, baseline: true, legacy: false) + def join(wallet) if @id.nil? @id = wallet.id @key = wallet.key - if baseline - @txns = wallet.txns - @log.debug("The baseline of #{wallet.id} is #{wallet.balance}/#{@txns.count}t") - else - @log.debug("The baseline of #{@txns.count} transactions ignored") - end @network = wallet.network end unless wallet.network == @network @log.error("The wallet is from a different network '#{wallet.network}', ours is '#{@network}'") return @@ -73,11 +75,10 @@ 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 existing transaction \"#{dup.to_text}\" \ @@ -113,18 +114,17 @@ 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 + 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 else - @log.error("Paying wallet #{txn.bnf} file is absent and it's a \"shallow\" MERGE: #{txn.to_text}") - next + @log.debug("Paying wallet #{txn.bnf} file is absent but it's a \"shallow\" MERGE: #{txn.to_text}") 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 end end @txns << txn @log.debug("Merged on top, balance is #{@txns.map(&:amount).inject(&:+)}: #{txn.to_text}") end