mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-13 10:45:51 +00:00
Block: Add convenience methods for BIP conformance (BIP34, BIP66, BIP65, BIP101). Also list BIPs in Block.toString().
This commit is contained in:
parent
fe736d2554
commit
63a5e3b410
@ -86,6 +86,8 @@ public class Block extends Message {
|
||||
public static final long BLOCK_VERSION_BIP66 = 3;
|
||||
/** Block version introduced in BIP 65: OP_CHECKLOCKTIMEVERIFY */
|
||||
public static final long BLOCK_VERSION_BIP65 = 4;
|
||||
/** Block version bitmask for BIP101: Increase maximum blocksize */
|
||||
public static final long BLOCK_VERSION_MASK_BIP101 = 0x20000007;
|
||||
|
||||
// Fields defined as part of the protocol format.
|
||||
private long version;
|
||||
@ -474,9 +476,14 @@ public class Block extends Message {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder s = new StringBuilder("v");
|
||||
s.append(version);
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(" block: \n");
|
||||
s.append(" version: ").append(version);
|
||||
String bips = Joiner.on(", ").skipNulls().join(isBIP34() ? "BIP34" : null, isBIP66() ? "BIP66" : null,
|
||||
isBIP65() ? "BIP65" : null, isBIP101() ? "BIP101" : null);
|
||||
if (!bips.isEmpty())
|
||||
s.append(" (").append(bips).append(')');
|
||||
s.append('\n');
|
||||
s.append(" previous block: ").append(getPrevBlockHash()).append("\n");
|
||||
s.append(" merkle root: ").append(getMerkleRoot()).append("\n");
|
||||
s.append(" time: [").append(time).append("] ").append(Utils.dateTimeFormat(time * 1000)).append("\n");
|
||||
@ -998,4 +1005,36 @@ public class Block extends Message {
|
||||
public boolean hasTransactions() {
|
||||
return !this.transactions.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this block conforms to
|
||||
* <a href="https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki">BIP34: Height in Coinbase</a>.
|
||||
*/
|
||||
public boolean isBIP34() {
|
||||
return version >= BLOCK_VERSION_BIP34;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this block conforms to
|
||||
* <a href="https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki">BIP66: Strict DER signatures</a>.
|
||||
*/
|
||||
public boolean isBIP66() {
|
||||
return version >= BLOCK_VERSION_BIP66;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this block conforms to
|
||||
* <a href="https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki">BIP65: OP_CHECKLOCKTIMEVERIFY</a>.
|
||||
*/
|
||||
public boolean isBIP65() {
|
||||
return version >= BLOCK_VERSION_BIP65;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this block conforms to
|
||||
* <a href="https://github.com/bitcoin/bips/blob/master/bip-0101.mediawiki">BIP101: Increase maximum block size</a>.
|
||||
*/
|
||||
public boolean isBIP101() {
|
||||
return (version & BLOCK_VERSION_MASK_BIP101) == BLOCK_VERSION_MASK_BIP101;
|
||||
}
|
||||
}
|
||||
|
@ -486,7 +486,7 @@ public abstract class NetworkParameters {
|
||||
final VersionTally tally, final Integer height) {
|
||||
final EnumSet<Block.VerifyFlag> flags = EnumSet.noneOf(Block.VerifyFlag.class);
|
||||
|
||||
if (block.getVersion() >= Block.BLOCK_VERSION_BIP34) {
|
||||
if (block.isBIP34()) {
|
||||
final Integer count = tally.getCountAtOrAbove(Block.BLOCK_VERSION_BIP34);
|
||||
if (null != count && count >= getMajorityEnforceBlockUpgrade()) {
|
||||
flags.add(Block.VerifyFlag.HEIGHT_IN_COINBASE);
|
||||
|
@ -241,4 +241,54 @@ public class BlockTest {
|
||||
assertEquals(BALANCE_AFTER_BLOCK, wallet.getBalance(BalanceType.ESTIMATED));
|
||||
assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.AVAILABLE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isBIPs() throws Exception {
|
||||
final MainNetParams mainnet = MainNetParams.get();
|
||||
final Block genesis = mainnet.getGenesisBlock();
|
||||
assertFalse(genesis.isBIP34());
|
||||
assertFalse(genesis.isBIP66());
|
||||
assertFalse(genesis.isBIP65());
|
||||
assertFalse(genesis.isBIP101());
|
||||
|
||||
// 227835/00000000000001aa077d7aa84c532a4d69bdbff519609d1da0835261b7a74eb6: last version 1 block
|
||||
final Block block227835 = mainnet.getDefaultSerializer()
|
||||
.makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block227835.dat")));
|
||||
assertFalse(block227835.isBIP34());
|
||||
assertFalse(block227835.isBIP66());
|
||||
assertFalse(block227835.isBIP65());
|
||||
assertFalse(block227835.isBIP101());
|
||||
|
||||
// 227836/00000000000000d0dfd4c9d588d325dce4f32c1b31b7c0064cba7025a9b9adcc: version 2 block
|
||||
final Block block227836 = mainnet.getDefaultSerializer()
|
||||
.makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block227836.dat")));
|
||||
assertTrue(block227836.isBIP34());
|
||||
assertFalse(block227836.isBIP66());
|
||||
assertFalse(block227836.isBIP65());
|
||||
assertFalse(block227836.isBIP101());
|
||||
|
||||
// 363703/0000000000000000011b2a4cb91b63886ffe0d2263fd17ac5a9b902a219e0a14: version 3 block
|
||||
final Block block363703 = mainnet.getDefaultSerializer()
|
||||
.makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block363703.dat")));
|
||||
assertTrue(block363703.isBIP34());
|
||||
assertTrue(block363703.isBIP66());
|
||||
assertFalse(block363703.isBIP65());
|
||||
assertFalse(block363703.isBIP101());
|
||||
|
||||
// 383616/00000000000000000aab6a2b34e979b09ca185584bd1aecf204f24d150ff55e9: version 4 block
|
||||
final Block block383616 = mainnet.getDefaultSerializer()
|
||||
.makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block383616.dat")));
|
||||
assertTrue(block383616.isBIP34());
|
||||
assertTrue(block383616.isBIP66());
|
||||
assertTrue(block383616.isBIP65());
|
||||
assertFalse(block383616.isBIP101());
|
||||
|
||||
// 370661/00000000000000001416a613602d73bbe5c79170fd8f39d509896b829cf9021e
|
||||
final Block block370661 = mainnet.getDefaultSerializer()
|
||||
.makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block370661.dat")));
|
||||
assertTrue(block370661.isBIP34());
|
||||
assertTrue(block370661.isBIP66());
|
||||
assertTrue(block370661.isBIP65());
|
||||
assertTrue(block370661.isBIP101());
|
||||
}
|
||||
}
|
||||
|
BIN
core/src/test/resources/org/bitcoinj/core/block227835.dat
Normal file
BIN
core/src/test/resources/org/bitcoinj/core/block227835.dat
Normal file
Binary file not shown.
BIN
core/src/test/resources/org/bitcoinj/core/block227836.dat
Normal file
BIN
core/src/test/resources/org/bitcoinj/core/block227836.dat
Normal file
Binary file not shown.
BIN
core/src/test/resources/org/bitcoinj/core/block363703.dat
Normal file
BIN
core/src/test/resources/org/bitcoinj/core/block363703.dat
Normal file
Binary file not shown.
BIN
core/src/test/resources/org/bitcoinj/core/block370661.dat
Normal file
BIN
core/src/test/resources/org/bitcoinj/core/block370661.dat
Normal file
Binary file not shown.
BIN
core/src/test/resources/org/bitcoinj/core/block383616.dat
Normal file
BIN
core/src/test/resources/org/bitcoinj/core/block383616.dat
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user