Add new function CalcMinClaimTrieFee to calulate the fee required for claimtrie transactions

This commit is contained in:
Kay Kurokawa 2018-02-18 22:43:34 -05:00 committed by Umpei Kay Kurokawa
parent 6f0b24d62d
commit 8817a7df51
4 changed files with 83 additions and 6 deletions

View file

@ -63,6 +63,7 @@ BITCOIN_TESTS =\
test/claimtrie_tests.cpp \ test/claimtrie_tests.cpp \
test/claimtriecache_tests.cpp \ test/claimtriecache_tests.cpp \
test/claimtriebranching_tests.cpp \ test/claimtriebranching_tests.cpp \
test/nameclaim_tests.cpp \
test/netbase_tests.cpp \ test/netbase_tests.cpp \
test/pmt_tests.cpp \ test/pmt_tests.cpp \
test/policyestimator_tests.cpp \ test/policyestimator_tests.cpp \

View file

@ -1,7 +1,9 @@
#include <boost/foreach.hpp>
#include "nameclaim.h" #include "nameclaim.h"
#include "hash.h" #include "hash.h"
#include "util.h" #include "util.h"
std::vector<unsigned char> uint32_t_to_vch(uint32_t n) std::vector<unsigned char> uint32_t_to_vch(uint32_t n)
{ {
std::vector<unsigned char> vchN; std::vector<unsigned char> vchN;
@ -173,3 +175,27 @@ size_t ClaimNameSize(const CScript& scriptIn)
return vvchParams[0].size(); return vvchParams[0].size();
} }
} }
CAmount CalcMinClaimTrieFee(const CTransaction& tx, const CAmount &minFeePerNameClaimChar)
{
if (minFeePerNameClaimChar == 0)
{
return 0;
}
CAmount min_fee = 0;
BOOST_FOREACH(const CTxOut& txout, tx.vout)
{
int op;
std::vector<std::vector<unsigned char> > vvchParams;
if (DecodeClaimScript(txout.scriptPubKey, op, vvchParams))
{
if (op == OP_CLAIM_NAME)
{
int claim_name_size = vvchParams[0].size();
min_fee += claim_name_size*minFeePerNameClaimChar;
}
}
}
return min_fee;
}

View file

@ -1,11 +1,22 @@
#ifndef BITCOIN_NAMECLAIM_H #ifndef BITCOIN_NAMECLAIM_H
#define BITCOIN_NAMECLAIM_H #define BITCOIN_NAMECLAIM_H
#include "amount.h"
#include "script/script.h" #include "script/script.h"
#include "primitives/transaction.h"
#include "uint256.h" #include "uint256.h"
#include <vector> #include <vector>
// This is the minimum claim fee per character in the name of an OP_CLAIM_NAME command that must
// be attached to transactions for it to be accepted into the memory pool.
// Rationale: current implementation of the claim trie uses more memory for longer name claims
// due to the fact that each chracater is assigned a trie node regardless of whether it contains
// any claims or not. In the future, we can switch to a radix tree implementation where
// empty nodes do not take up any memory and the minimum fee can be priced on a per claim
// basis.
#define MIN_FEE_PER_NAMECLAIM_CHAR 200000
// This is the max claim script size in bytes, not including the script pubkey part of the script. // This is the max claim script size in bytes, not including the script pubkey part of the script.
// Scripts exceeding this size are rejected in CheckTransaction in main.cpp // Scripts exceeding this size are rejected in CheckTransaction in main.cpp
#define MAX_CLAIM_SCRIPT_SIZE 8192 #define MAX_CLAIM_SCRIPT_SIZE 8192
@ -28,6 +39,7 @@ uint32_t vch_to_uint32_t(std::vector<unsigned char>& vchN);
size_t ClaimScriptSize(const CScript& scriptIn); size_t ClaimScriptSize(const CScript& scriptIn);
// get size of the name in a claim script, returns 0 if scriptin is not a claimetrie transaction // get size of the name in a claim script, returns 0 if scriptin is not a claimetrie transaction
size_t ClaimNameSize(const CScript& scriptIn); size_t ClaimNameSize(const CScript& scriptIn);
// calculate the minimum fee (mempool rule) required for transaction
CAmount CalcMinClaimTrieFee(const CTransaction& tx, const CAmount &minFeePerNameClaimChar);
#endif // BITCOIN_NAMECLAIM_H #endif // BITCOIN_NAMECLAIM_H

View file

@ -0,0 +1,38 @@
#include "nameclaim.h"
#include "primitives/transaction.h"
#include <boost/test/unit_test.hpp>
#include "test/test_bitcoin.h"
//BOOST_FIXTURE_TEST_SUITE(nameclaim_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(calc_min_claimtrie_fee)
{
CMutableTransaction tx;
tx.vout.resize(1);
tx.vout[0].scriptPubKey = ClaimNameScript("A","test");
BOOST_CHECK_EQUAL(CalcMinClaimTrieFee(tx, MIN_FEE_PER_NAMECLAIM_CHAR), MIN_FEE_PER_NAMECLAIM_CHAR);
// check that fee is adjusted based on name length
CMutableTransaction tx2;
tx2.vout.resize(1);
tx2.vout[0].scriptPubKey = ClaimNameScript("ABCDE","test");
BOOST_CHECK_EQUAL(CalcMinClaimTrieFee(tx2,MIN_FEE_PER_NAMECLAIM_CHAR), MIN_FEE_PER_NAMECLAIM_CHAR*5);
// check that multiple OP_CLAIM_NAME outputs are counted
CMutableTransaction tx3;
tx3.vout.resize(2);
tx3.vout[0].scriptPubKey = ClaimNameScript("A","test");
tx3.vout[1].scriptPubKey = ClaimNameScript("AB","test");
BOOST_CHECK_EQUAL(CalcMinClaimTrieFee(tx3,MIN_FEE_PER_NAMECLAIM_CHAR), MIN_FEE_PER_NAMECLAIM_CHAR*3);
// if tx has no claim minimum fee is 0
CMutableTransaction tx4;
tx4.vout.resize(1);
BOOST_CHECK_EQUAL(CalcMinClaimTrieFee(tx4,MIN_FEE_PER_NAMECLAIM_CHAR), 0);
}
//BOOST_AUTO_TEST_SUITE_END()