public void solve() { block.solve(); } public void addTransaction(Transaction tx) { block.addTransaction(tx); }
public UnitTestParams() { super(); id = ID_UNITTESTNET; packetMagic = 0x0b110907; addressHeader = 111; p2shHeader = 196; acceptableAddressCodes = new int[] { addressHeader, p2shHeader }; maxTarget = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); genesisBlock.setTime(System.currentTimeMillis() / 1000); genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); genesisBlock.solve(); port = 18333; interval = 10; dumpedPrivateKeyHeader = 239; targetTimespan = 200000000; // 6 years. Just a very big number. spendableCoinbaseDepth = 5; subsidyDecreaseBlockCount = 100; dnsSeeds = null; addrSeeds = null; bip32HeaderPub = 0x043587CF; bip32HeaderPriv = 0x04358394; majorityEnforceBlockUpgrade = 3; majorityRejectBlockOutdated = 4; majorityWindow = 7; }
public UnitTestParams() { super(); id = ID_UNITTESTNET; packetMagic = 0xf4e5f3f4L; // must be same as testnet3 addressHeader = 111; p2shHeader = 196; acceptableAddressCodes = new int[] { addressHeader, p2shHeader }; maxTarget = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); genesisBlock.setTime(System.currentTimeMillis() / 1000); genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); genesisBlock.solve(); port = 18333; interval = 10; dumpedPrivateKeyHeader = 239; targetTimespan = 200000000; // 6 years. Just a very big number. spendableCoinbaseDepth = 5; subsidyDecreaseBlockCount = 100; dnsSeeds = null; addrSeeds = null; bip32HeaderPub = 0x043587CF; bip32HeaderPriv = 0x04358394; majorityEnforceBlockUpgrade = 3; majorityRejectBlockOutdated = 4; majorityWindow = 7; // support for legacy tests - dont test BCH 2017-11-13 hardfork by default cashHardForkActivationTime = (System.currentTimeMillis()/1000)+24*60*60; daaHeight = 1000000; }
public UnitTestParams() { super(); id = ID_UNITTESTNET; packetMagic = 0x0b110907; addressHeader = CoinDefinition.testnetAddressHeader; p2shHeader = CoinDefinition.testnetp2shHeader; acceptableAddressCodes = new int[] { addressHeader, p2shHeader }; maxTarget = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); genesisBlock.setTime(System.currentTimeMillis() / 1000); genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); genesisBlock.solve(); port = CoinDefinition.TestPort; interval = 10; dumpedPrivateKeyHeader = 239; targetTimespan = 200000000; // 6 years. Just a very big number. spendableCoinbaseDepth = 5; subsidyDecreaseBlockCount = 100; dnsSeeds = null; addrSeeds = null; bip32HeaderPub = 0x043587CF; bip32HeaderPriv = 0x04358394; majorityEnforceBlockUpgrade = 3; majorityRejectBlockOutdated = 4; majorityWindow = 7; DIP0001BlockHeight = 100000; // not active strSporkAddress = "yjPtiKh2uwk3bDutTEA2q9mCtXyiZRWn55"; }
public UnitTestParams() { super(); id = ID_UNITTESTNET; packetMagic = 0x0b110907; addressHeader = 111; p2shHeader = 196; p2wpkhHeader = 3; p2wshHeader = 40; acceptableAddressCodes = new int[] { addressHeader, p2shHeader, p2wpkhHeader, p2wshHeader }; maxTarget = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); genesisBlock.setTime(System.currentTimeMillis() / 1000); genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); genesisBlock.solve(); port = 18333; interval = 10; dumpedPrivateKeyHeader = 239; targetTimespan = 200000000; // 6 years. Just a very big number. spendableCoinbaseDepth = 5; subsidyDecreaseBlockCount = 100; dnsSeeds = null; addrSeeds = null; bip32HeaderPub = 0x043587CF; bip32HeaderPriv = 0x04358394; majorityEnforceBlockUpgrade = 3; majorityRejectBlockOutdated = 4; majorityWindow = 7; }
public static Block makeSolvedTestBlock(Block prev, Address to, Transaction... transactions) throws BlockStoreException { Block b = prev.createNextBlock(to); // Coinbase tx already exists. for (Transaction tx : transactions) { b.addTransaction(tx); } b.solve(); return b; } }
public static Block makeSolvedTestBlock(BlockStore blockStore, Address coinsTo) throws BlockStoreException { Block b = blockStore.getChainHead().getHeader().createNextBlock(coinsTo); b.solve(); return b; }
public static Block makeSolvedTestBlock(Block prev, Transaction... transactions) throws BlockStoreException { Address to = new ECKey().toAddress(prev.getParams()); Block b = prev.createNextBlock(to); // Coinbase tx already exists. for (Transaction tx : transactions) { b.addTransaction(tx); } b.solve(); return b; }
private NewBlock createNextBlock(Block baseBlock, int nextBlockHeight, @Nullable TransactionOutPointWithValue prevOut, Coin additionalCoinbaseValue) throws ScriptException { Integer height = blockToHeightMap.get(baseBlock.getHash()); if (height != null) checkState(height == nextBlockHeight - 1); Coin coinbaseValue = FIFTY_COINS.shiftRight(nextBlockHeight / params.getSubsidyDecreaseBlockCount()) .add((prevOut != null ? prevOut.value.subtract(SATOSHI) : ZERO)) .add(additionalCoinbaseValue == null ? ZERO : additionalCoinbaseValue); Block block = baseBlock.createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, coinbaseOutKeyPubKey, coinbaseValue, nextBlockHeight); Transaction t = new Transaction(params); if (prevOut != null) { // Entirely invalid scriptPubKey to ensure we aren't pre-verifying too much t.addOutput(new TransactionOutput(params, t, ZERO, new byte[] {(byte)(new Random().nextInt() & 0xff), uniquenessCounter++})); // Spendable output t.addOutput(new TransactionOutput(params, t, SATOSHI, new byte[] {OP_1})); addOnlyInputToTransaction(t, prevOut); block.addTransaction(t); block.solve(); } return new NewBlock(block, prevOut == null ? null : new TransactionOutPointWithValue(t, 1)); } private NewBlock createNextBlock(NewBlock baseBlock, int nextBlockHeight, @Nullable TransactionOutPointWithValue prevOut,
@Test public void testProofOfWork() throws Exception { // This params accepts any difficulty target. NetworkParameters params = UnitTestParams.get(); Block block = params.getDefaultSerializer().makeBlock(blockBytes); block.setNonce(12346); try { block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); fail(); } catch (VerificationException e) { // Expected. } // Blocks contain their own difficulty target. The BlockChain verification mechanism is what stops real blocks // from containing artificially weak difficulties. block.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); // Now it should pass. block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); // Break the nonce again at the lower difficulty level so we can try solving for it. block.setNonce(1); try { block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); fail(); } catch (VerificationException e) { // Expected to fail as the nonce is no longer correct. } // Should find an acceptable nonce. block.solve(); block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); assertEquals(block.getNonce(), 2); }
/** Emulates receiving a valid block */ public static BlockPair createFakeBlock(BlockStore blockStore, StoredBlock previousStoredBlock, long version, long timeSeconds, int height, Transaction... transactions) { try { Block previousBlock = previousStoredBlock.getHeader(); Address to = new ECKey().toAddress(previousBlock.getParams()); Block b = previousBlock.createNextBlock(to, version, timeSeconds, height); // Coinbase tx was already added. for (Transaction tx : transactions) { tx.getConfidence().setSource(TransactionConfidence.Source.NETWORK); b.addTransaction(tx); } b.solve(); BlockPair pair = new BlockPair(); pair.block = b; pair.storedBlock = previousStoredBlock.build(b); blockStore.put(pair.storedBlock); blockStore.setChainHead(pair.storedBlock); return pair; } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } catch (BlockStoreException e) { throw new RuntimeException(e); // Cannot happen. } }
@Test public void testForking5() throws Exception { // Test the standard case in which a block containing identical transactions appears on a side chain. Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b1); final Transaction t = b1.transactions.get(1); assertEquals(FIFTY_COINS, wallet.getBalance()); // genesis -> b1 // -> b2 Block b2 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); Transaction b2coinbase = b2.transactions.get(0); b2.transactions.clear(); b2.addTransaction(b2coinbase); b2.addTransaction(t); b2.solve(); chain.add(roundtrip(b2)); assertEquals(FIFTY_COINS, wallet.getBalance()); assertTrue(wallet.isConsistent()); assertEquals(2, wallet.getTransaction(t.getHash()).getAppearsInHashes().size()); // -> b2 -> b3 Block b3 = b2.createNextBlock(someOtherGuy); chain.add(b3); assertEquals(FIFTY_COINS, wallet.getBalance()); }
@Test public void testForking6() throws Exception { // Test the case in which a side chain block contains a tx, and then it appears in the main chain too. Block b1 = PARAMS.getGenesisBlock().createNextBlock(someOtherGuy); chain.add(b1); // genesis -> b1 // -> b2 Block b2 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b2); assertEquals(Coin.ZERO, wallet.getBalance()); // genesis -> b1 -> b3 // -> b2 Block b3 = b1.createNextBlock(someOtherGuy); b3.addTransaction(b2.transactions.get(1)); b3.solve(); chain.add(roundtrip(b3)); assertEquals(FIFTY_COINS, wallet.getBalance()); }
@Test public void intraBlockDependencies() throws Exception { // Covers issue 166 in which transactions that depend on each other inside a block were not always being // considered relevant. Address somebodyElse = new ECKey().toAddress(PARAMS); Block b1 = PARAMS.getGenesisBlock().createNextBlock(somebodyElse); ECKey key = wallet.freshReceiveKey(); Address addr = key.toAddress(PARAMS); // Create a tx that gives us some coins, and another that spends it to someone else in the same block. Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, addr); Transaction t2 = new Transaction(PARAMS); t2.addInput(t1.getOutputs().get(0)); t2.addOutput(valueOf(2, 0), somebodyElse); b1.addTransaction(t1); b1.addTransaction(t2); b1.solve(); chain.add(b1); assertEquals(Coin.ZERO, wallet.getBalance()); }
t.addSignedInput(spendableOutput, new Script(spendableOutputScriptPubKey), outKey); rollingBlock.addTransaction(t); rollingBlock.solve(); chain.add(rollingBlock); totalAmount = totalAmount.add(amount);
@Test public void difficultyTransitions() throws Exception { // Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an // artificially shortened period. Block prev = PARAMS.getGenesisBlock(); Utils.setMockClock(System.currentTimeMillis()/1000); for (int height = 0; height < PARAMS.getInterval() - 1; height++) { Block newBlock = prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), height); assertTrue(chain.add(newBlock)); prev = newBlock; // The fake chain should seem to be "fast" for the purposes of difficulty calculations. Utils.rollMockClock(2); } // Now add another block that has no difficulty adjustment, it should be rejected. try { chain.add(prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), PARAMS.getInterval())); fail(); } catch (VerificationException e) { } // Create a new block with the right difficulty target given our blistering speed relative to the huge amount // of time it's supposed to take (set in the unit test network parameters). Block b = prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), PARAMS.getInterval() + 1); b.setDifficultyTarget(0x201fFFFFL); b.solve(); assertTrue(chain.add(b)); // Successfully traversed a difficulty transition period. }
rollingBlock.solve(); chain.setRunScripts(false); try {
Block b2 = b1.createNextBlock(someOtherGuy); b2.addTransaction(spend); b2.solve(); chain.add(roundtrip(b2));
Block b3 = b1.createNextBlock(someOtherGuy); b3.addTransaction(spend); b3.solve(); chain.add(roundtrip(b3));