lib/zold/patch.rb in zold-0.11.21 vs lib/zold/patch.rb in zold-0.11.22

- old
+ new

@@ -16,10 +16,11 @@ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +require_relative 'log' require_relative 'wallet' require_relative 'signature' require_relative 'atomic_file' # Patch. @@ -28,20 +29,25 @@ # Copyright:: Copyright (c) 2018 Yegor Bugayenko # License:: MIT module Zold # A patch class Patch + def initialize(log: Log::Quiet.new) + @log = log + end + def to_s return 'empty' if @id.nil? "#{@txns.count} txns" end def join(wallet) if @id.nil? @id = wallet.id @key = wallet.key @txns = wallet.txns + @log.debug("The baseline: #{@txns.count} transactions, the balance is #{wallet.balance}") @network = wallet.network end if wallet.network != @network raise "The wallet is from a different network '#{wallet.network}', ours is '#{@network}'" end @@ -49,20 +55,31 @@ raise "Wallet ID mismatch: #{@id} != #{wallet.id}" if wallet.id != @id negative = @txns.select { |t| t.amount.negative? } max = negative.empty? ? 0 : negative.max_by(&:id).id wallet.txns.each do |txn| next if @txns.find { |t| t == txn } - next if - txn.amount.negative? && !@txns.empty? && - (txn.id <= max || - @txns.find { |t| t.id == txn.id } || - @txns.map(&:amount).inject(&:+) < txn.amount) - if !txn.amount.negative? && !txn.sign.empty? - raise "RSA signature is redundant at ##{txn.id} of #{wallet.id}: #{txn.to_text}" + if txn.amount.negative? + if txn.id <= max + @log.debug("Transaction ID is less than max #{max}: #{txn.to_text}") + next + end + if @txns.find { |t| t.id == txn.id } + @log.debug("Transaction ##{txn.id} already exists: #{txn.to_text}") + next + end + if !@txns.empty? && @txns.map(&:amount).inject(&:+) < txn.amount + @log.debug("Transaction ##{txn.id} attempts to make the balance negative: #{txn.to_text}") + next + end + unless Signature.new.valid?(@key, wallet.id, txn) + @log.debug("Invalid RSA signature at transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}") + next + end + elsif !txn.sign.nil? && !txn.sign.empty? + @log.debug("RSA signature is redundant at ##{txn.id} of #{wallet.id}: #{txn.to_text}") + next end - if txn.amount.negative? && !Signature.new.valid?(@key, wallet.id, txn) - raise "Invalid RSA signature at transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}" - end + @log.debug("Merged on top: #{txn.to_text}") @txns << txn end end # Returns TRUE if the file was actually modified