use floating point arithmetic to compute the message size with overhead and use the size overhead in the computation of bid_per_byte

This commit is contained in:
Ben Teitelbaum 2019-02-27 17:05:24 -08:00 committed by nitram
parent c94451c680
commit 9753a3cd9f
2 changed files with 7 additions and 5 deletions

View file

@ -139,11 +139,9 @@ post '/order' do
order.message_size = message_size
order.message_digest = sha256.to_s
message_size_with_overhead = message_size + FRAMING_OVERHEAD_PER_FRAGMENT * (message_size / FRAGMENT_SIZE).ceil
if bid.to_f / message_size_with_overhead.to_f < MIN_PER_BYTE_BID
bid_too_small_error(message_size_with_overhead * MIN_PER_BYTE_BID)
if bid.to_f / order.message_size_with_overhead < MIN_PER_BYTE_BID
bid_too_small_error(order.message_size_with_overhead * MIN_PER_BYTE_BID)
end
invoice = new_invoice(order, bid)

View file

@ -68,7 +68,7 @@ class Order < ActiveRecord::Base
def adjust_bids
self.bid = paid_invoices_total
self.bid_per_byte = (self.bid.to_f / self.message_size.to_f).round(2)
self.bid_per_byte = (self.bid.to_f / self.message_size_with_overhead).round(2)
self.unpaid_bid = unpaid_invoices_total
end
@ -80,6 +80,10 @@ class Order < ActiveRecord::Base
self.adjust_bids
self.bid_per_byte >= MIN_PER_BYTE_BID
end
def message_size_with_overhead
self.message_size.to_f + FRAMING_OVERHEAD_PER_FRAGMENT * (self.message_size.to_f / FRAGMENT_SIZE).ceil
end
def paid_invoices_total
self.invoices.where(status: :paid).pluck(:amount).reduce(:+) || 0