Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Tx#calculate_minimum_fee

  • Loading branch information...
commit 15298d0c561b5370f10c82f4fbd22bfc68369820 1 parent 8019e77
@lian lian authored
View
12 lib/bitcoin.rb
@@ -346,6 +346,18 @@ def self.network= name
@network = name.to_sym
end
+ CENT = 1_000_000
+ COIN = 100_000_000
+ MAX_MONEY = 21_000_000 * COIN
+ MAX_BLOCK_SIZE = 1_000_000
+ MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE/2
+ MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50
+ MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100
+
+ MIN_FEE_MODE = [ :block, :relay, :send ]
+ MIN_TX_FEE = 50_000
+ MIN_RELAY_TX_FEE = 10_000
+
NETWORKS = {
:bitcoin => {
:magic_head => "\xF9\xBE\xB4\xD9",
View
32 lib/bitcoin/protocol/tx.rb
@@ -208,7 +208,37 @@ def self.from_json_file(path); from_json( Bitcoin::Protocol.read_binary_file(pat
def validator(store, block = nil)
@validator ||= Bitcoin::Validation::Tx.new(self, store, block)
end
- end
+ def minimum_relay_fee; calculate_minimum_fee(1_000, true, :relay); end
+ def minimum_block_fee; calculate_minimum_fee(1_000, true, :block); end
+
+ def calculate_minimum_fee(block_size=1, allow_free=true, mode=:block)
+ base_fee = (mode == :relay) ? Bitcoin::MIN_RELAY_TX_FEE : Bitcoin::MIN_TX_FEE
+ tx_size = to_payload.bytesize
+ new_block_size = block_size + tx_size
+ min_fee = (1 + tx_size / 1_000) * base_fee
+
+ if allow_free
+ if block_size == 1
+ min_fee = 0 if tx_size < 10_000
+ else
+ min_fee = 0 if new_block_size < 27_000
+ end
+ end
+
+ if min_fee < base_fee
+ outputs.each{|output| (min_fee = base_fee; break) if output.value < Bitcoin::CENT }
+ end
+
+ if block_size != 1 && new_block_size >= (Bitcoin::MAX_BLOCK_SIZE_GEN/2)
+ #return Bitcoin::MAX_MONEY if new_block_size >= Bitcoin::MAX_BLOCK_SIZE_GEN
+ min_fee *= Bitcoin::MAX_BLOCK_SIZE_GEN / (Bitcoin::MAX_BLOCK_SIZE_GEN - new_block_size)
+ end
+
+ min_fee = Bitcoin::MAX_MONEY unless min_fee.between?(0, Bitcoin::MAX_MONEY)
+ min_fee
+ end
+
+ end
end
end
View
8 spec/bitcoin/protocol/tx_spec.rb
@@ -200,6 +200,14 @@
#File.open("rawtx-#{prev_tx.hash}.json",'wb'){|f| f.print prev_tx.to_json }
end
+ it '#calculate_minimum_fee' do
+ tx = Tx.new( fixtures_file('rawtx-b5d4e8883533f99e5903ea2cf001a133a322fa6b1370b18a16c57c946a40823d.bin') )
+ tx.minimum_relay_fee.should == 0
+ tx.minimum_block_fee.should == 0
+ tx = Tx.from_json(fixtures_file('bc179baab547b7d7c1d5d8d6f8b0cc6318eaa4b0dd0a093ad6ac7f5a1cb6b3ba.json'))
+ tx.minimum_relay_fee.should == 10_000
+ tx.minimum_block_fee.should == 50_000
+ end
describe "Tx - BIP Scripts" do
Please sign in to comment.
Something went wrong with that request. Please try again.