3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 10:45:51 +00:00

Implement special priority tx size calculation.

This commit is contained in:
Mike Hearn 2015-07-09 15:05:36 +02:00
parent 621975556c
commit da20b02906
2 changed files with 40 additions and 17 deletions

View File

@ -583,6 +583,23 @@ public class Transaction extends ChildMessage implements Serializable {
return optimalEncodingMessageSize;
}
/**
* The priority (coin age) calculation doesn't use the regular message size, but rather one adjusted downwards
* for the number of inputs. The goal is to incentivise cleaning up the UTXO set with free transactions, if one
* can do so.
*/
public int getMessageSizeForPriorityCalc() {
int size = getMessageSize();
for (TransactionInput input : inputs) {
// 41: min size of an input
// 110: enough to cover a compressed pubkey p2sh redemption (somewhat arbitrary).
int benefit = 41 + Math.min(110, input.getScriptSig().getProgram().length);
if (size > benefit)
size -= benefit;
}
return size;
}
/**
* A coinbase transaction is one that creates a new coin. They are the first transaction in each block and their
* value is determined by a formula that all implementations of Bitcoin share. In 2011 the value of a coinbase

View File

@ -1,24 +1,16 @@
package org.bitcoinj.core;
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
import org.bitcoinj.params.UnitTestParams;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptBuilder;
import org.bitcoinj.testing.FakeTxBuilder;
import org.junit.Before;
import org.junit.Test;
import org.easymock.EasyMock;
import org.bitcoinj.core.TransactionConfidence.*;
import org.bitcoinj.params.*;
import org.bitcoinj.script.*;
import org.bitcoinj.testing.*;
import org.easymock.*;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.replay;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
/**
* Just check the Transaction.verify() method. Most methods that have complicated logic in Transaction are tested
@ -296,4 +288,18 @@ public class TransactionTest {
tx.addSignedInput(fakeTx.getOutput(0).getOutPointFor(), script, key);
}
@Test
public void testPrioSizeCalc() throws Exception {
Transaction tx1 = FakeTxBuilder.createFakeTx(PARAMS, Coin.COIN, ADDRESS);
int size1 = tx1.getMessageSize();
int size2 = tx1.getMessageSizeForPriorityCalc();
assertEquals(113, size1 - size2);
tx1.getInput(0).setScriptSig(new Script(new byte[109]));
assertEquals(78, tx1.getMessageSizeForPriorityCalc());
tx1.getInput(0).setScriptSig(new Script(new byte[110]));
assertEquals(78, tx1.getMessageSizeForPriorityCalc());
tx1.getInput(0).setScriptSig(new Script(new byte[111]));
assertEquals(79, tx1.getMessageSizeForPriorityCalc());
}
}