// Check that it starts downloading the block chain correctly on request. @Test public void startBlockChainDownload() throws Exception { Block b1 = createFakeBlock(blockStore).block; blockChain.add(b1); Block b2 = makeSolvedTestBlock(b1); blockChain.add(b2); connect(); fail.set(true); peer.addEventListener( new AbstractPeerEventListener() { @Override public void onChainDownloadStarted(Peer p, int blocksLeft) { if (p == peer && blocksLeft == 108) fail.set(false); } }, Threading.SAME_THREAD); peer.startBlockChainDownload(); List<Sha256Hash> expectedLocator = new ArrayList<Sha256Hash>(); expectedLocator.add(b2.getHash()); expectedLocator.add(b1.getHash()); expectedLocator.add(unitTestParams.getGenesisBlock().getHash()); GetBlocksMessage message = (GetBlocksMessage) outbound(writeTarget); assertEquals(message.getLocator(), expectedLocator); assertEquals(Sha256Hash.ZERO_HASH, message.getStopHash()); }
/** * flattenBlock Returns this Brick as a list of ChordBlocks * * @return an ArrayList describing the Brick's contents */ @Override public ArrayList<ChordBlock> flattenBlock() { ArrayList<ChordBlock> chordList = new ArrayList<ChordBlock>(); ArrayList<Block> currentList = this.getSubBlocks(); // Occasionally getting null pointer exception here, // Due to, e.g. Minor On bricks having 0 duration. // I hope this fix is ok. if (currentList == null) { // ErrorLog.log(ErrorLog.SEVERE, "Internal Error: null block list in brick " + toString()); return chordList; } Iterator<Block> iter = currentList.iterator(); // Iterate through subblocks. If a block is a chord, just add it. If it // is a brick, recursively flatten. while (iter.hasNext()) { Block currentBlock = iter.next(); if (currentBlock != null) { chordList.addAll(currentBlock.flattenBlock()); } } if (chordList.size() > 0) { chordList.get(chordList.size() - 1).setSectionEnd(endValue); } return chordList; }
public boolean canSpawn() { if (this.world.random.nextInt(3) == 0) { return false; } else { if (this.world.b(this.boundingBox) && this.world.getCubes(this, this.boundingBox).isEmpty() && !this.world.containsLiquid(this.boundingBox)) { int i = MathHelper.floor(this.locX); int j = MathHelper.floor(this.boundingBox.b); int k = MathHelper.floor(this.locZ); if (j < 63) { return false; } Block block = this.world.getType(i, j - 1, k); if (block == Blocks.GRASS || block.getMaterial() == Material.LEAVES) { return true; } } return false; } }
private static Block createGenesis(NetworkParameters n) { Block genesisBlock = new Block(n); Transaction t = new Transaction(n); try { // A script containing the difficulty bits and the following message: // // "Mar-17-2014 Harvard Scientists: First Direct Evidence of Cosmic Inflation" byte[] bytes = Hex.decode( "04ffff001d0104494d61722d31372d32303134204861727661726420536369656e74697374733a204669727374204469726563742045766964656e6365206f6620436f736d696320496e666c6174696f6e"); t.addInput(new TransactionInput(n, t, bytes)); ByteArrayOutputStream scriptPubKeyBytes = new ByteArrayOutputStream(); Script.writeBytes( scriptPubKeyBytes, Hex.decode( "04c6c6a01450f2b8bbb176b98f593623b04a5f5761a7d0adae4d21f77c366e01e147217a9d8864a871d95d182e01d2b003168f0fa7851278a86ac0aa682919f9a6")); scriptPubKeyBytes.write(ScriptOpCodes.OP_CHECKSIG); t.addOutput( new TransactionOutput(n, t, Utils.toNanoCoins(1, 0), scriptPubKeyBytes.toByteArray())); } catch (Exception e) { // Cannot happen. throw new RuntimeException(e); } genesisBlock.addTransaction(t); return genesisBlock; }
private void registerExpandableIndents( @NotNull Block block, @NotNull AbstractBlockWrapper wrapper) { if (block.getIndent() instanceof ExpandableIndent) { ExpandableIndent indent = (ExpandableIndent) block.getIndent(); myBlocksToForceChildrenIndent.putValue(indent, wrapper); } }
private void play() { if (isReload) reloadGame(); if (isEnd || isPause) return; addFeed(); doesEatFeed(); addRegime(); doesEatRegime(); crossBorder(); head = blocks.peekFirst(); Block newHead = new Block(head.getX() + dx[inputDirection], head.getY() + dy[inputDirection]); Rectangle headRect = new Rectangle(head.getX(), head.getY(), 10, 10); if (isEnd(newHead)) { isEnd = true; // Image image = new Image(this.getClass().getResourceAsStream("/gameOver.png")); // ImageView imageView = new ImageView(image); // group.getChildren().add(imageView); // scene.setOnKeyPressed(null); saveRecord(); showRecord(); return; } else { blocks.push(newHead); rectangles.push(headRect); blocks.pollLast(); updateGui(); } }
/* This method simulates train movement. Distance updates and is stored. If it surpasses length of the block, train proceeds. */ public Block moveTrain(double moved) { double newDist = moved + distanceTraveled; // System.out.println("In " + this.section + " " + this.blockNumber + " moved: " + newDist + // "Length:"+ this.blockLength); Block currentBlock = this; if (newDist >= blockLength) { Block temp = currentBlock; blockOccupied = false; distanceTraveled = 0; newDist = newDist - blockLength; // ((Block) this.getNext()).placeTrain(trainID, newDist); <----------- traversal move currentBlock = this.traverseTrain(trainID); if (temp == currentBlock) // THIS IS THE JOB OF THE WAYSIDE... DO NOT ACTUALLY IMPLEMENT { currentBlock.toggleSwitch(); currentBlock = currentBlock.traverseTrain(trainID); System.out.println("toggle"); } currentBlock = currentBlock.placeTrain(trainID, newDist); // trainID = 0; } else { distanceTraveled = newDist; } // System.out.print("Train is currently in block: " + currentBlock.getSection() + " " + // currentBlock.getBlockNumber()); return currentBlock; }
@Test public void testCoinbaseHeightTestnet() throws Exception { // Testnet block 21066 (hash 0000000004053156021d8e42459d284220a7f6e087bf78f30179c3703ca4eefa) // contains a coinbase transaction whose height is two bytes, which is // shorter than we see in most other cases. Block block = TestNet3Params.get() .getDefaultSerializer() .makeBlock( ByteStreams.toByteArray(getClass().getResourceAsStream("block_testnet21066.dat"))); // Check block. assertEquals( "0000000004053156021d8e42459d284220a7f6e087bf78f30179c3703ca4eefa", block.getHashAsString()); block.verify(21066, EnumSet.of(Block.VerifyFlag.HEIGHT_IN_COINBASE)); // Testnet block 32768 (hash 000000007590ba495b58338a5806c2b6f10af921a70dbd814e0da3c6957c0c03) // contains a coinbase transaction whose height is three bytes, but could // fit in two bytes. This test primarily ensures script encoding checks // are applied correctly. block = TestNet3Params.get() .getDefaultSerializer() .makeBlock( ByteStreams.toByteArray(getClass().getResourceAsStream("block_testnet32768.dat"))); // Check block. assertEquals( "000000007590ba495b58338a5806c2b6f10af921a70dbd814e0da3c6957c0c03", block.getHashAsString()); block.verify(32768, EnumSet.of(Block.VerifyFlag.HEIGHT_IN_COINBASE)); }
@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); }
/** * Reads block from stream Iff at end of stream returns an zero-lenght block * * @param bitlength * @return * @throws IOException */ public Block read(int bitlength) { int bytelength = (int) Math.ceil((double) bitlength / 8); Block block = new Block(0); Byte buffer = 0; for (int i = 0; i < bytelength; i++) { try { buffer = this.inputstream.read(); } catch (IOException e) { if (block.getLength() > 0) { return block; } else { return new Block(0); } } if (i * 8 + 8 > bitlength) { return block.joinBlock(new Block(8).setValue(8, buffer).trim(bytelength * 8 - bitlength)); } else { block = block.joinBlock(new Block(8).setValue(8, buffer)); } } return block; }
@Test public void testHeaderParse() throws Exception { Block block = params.getDefaultSerializer().makeBlock(blockBytes); Block header = block.cloneAsHeader(); Block reparsed = params.getDefaultSerializer().makeBlock(header.bitcoinSerialize()); assertEquals(reparsed, header); }
public void testEquals() { Block b = new Block(123, 100, 200, 122); // random block Block c = new Block(123, 100, 200, 122); Block d = new Block(123, 100, 200, 123); assertTrue(b.equals(c)); assertFalse(b.equals(d)); }
public void testPosition() { Block b = new Block(123, 122, 200, 182); // random block assertTrue(b.top() == 123); assertTrue(b.left() == 122); assertTrue(b.right() == 182); assertTrue(b.bottom() == 200); }
public void onBlockRightClicked(Player player, Block blockClicked, Item item) { SensorBlock s = GetSensorAt(new Location(blockClicked.getX(), blockClicked.getY(), blockClicked.getZ())); if (s != null) { s.OnRightClickSign(); } }
public void debugTo(PrintStream out) { for (Block block : Lists.reverse(blocks)) { if (block.id != 0) { out.println("B" + block.id + ":"); } else { out.println("B" + block.id + " (Exit) :"); } int i = 0; for (Tree tree : block.elements()) { out.println(" " + i + ": " + syntaxNodeToDebugString(tree)); i++; } if (block.terminator != null) { out.println(" T: " + syntaxNodeToDebugString(block.terminator)); } if (!block.successors.isEmpty()) { out.print(" Successors:"); for (Block successor : block.successors) { out.print(" B" + successor.id); } out.println(); } } out.println(); }
private void buildBlock(List<Building> buildingList) { Block block = new Block(); block.add(buildingList.get(0)); if (buildingList.size() == 1) { blockList.add(block); return; } int maxRight = buildingList.get(0).getRight(); for (int i = 1; i < buildingList.size(); i++) { if (buildingList.get(i).getLeft() <= maxRight) { if (buildingList.get(i).getRight() > maxRight) { maxRight = buildingList.get(i).getRight(); } block.add(buildingList.get(i)); // the last building if (i == buildingList.size() - 1) { blockList.add(block); } } if (buildingList.get(i).getLeft() > maxRight) { blockList.add(block); buildBlock(buildingList.subList(i, buildingList.size())); break; } } }
/** * @param blocks * @param currentLine * @param inputLines * @return la prochaine ligne a traitee */ private static int getNextBlock(List<Block> blocks, int currentLine, List<String> inputLines) { int curLineIdx = currentLine; Block newBlock = new Block(); while (true) { // Plus d'entrée if (curLineIdx >= inputLines.size()) { if (newBlock.containsSomething()) { addNextBlock(blocks, newBlock); return curLineIdx; } else { return -1; } } String curLine = inputLines.get(curLineIdx); log.debug("Processing line {} of {}. [{}]", curLineIdx, inputLines.size(), curLine); ++curLineIdx; Matcher m = CUT_PASTE_BOUNDARY.matcher(curLine); // Bloc est fini if (m.matches()) { addNextBlock(blocks, newBlock); return curLineIdx; } newBlock.addLine(curLine); } }
/** * Changes all Blocks of type <code>NONE</code> to <code>PARAGRAPH</code> if this Block is a List * and any of the ListItems contains a paragraph. */ public void expandListParagraphs() { if (this.type != BlockType.ORDERED_LIST && this.type != BlockType.UNORDERED_LIST) { return; } Block outer = this.blocks, inner; boolean hasParagraph = false; while (outer != null && !hasParagraph) { if (outer.type == BlockType.LIST_ITEM) { inner = outer.blocks; while (inner != null && !hasParagraph) { if (inner.type == BlockType.PARAGRAPH) { hasParagraph = true; } inner = inner.next; } } outer = outer.next; } if (hasParagraph) { outer = this.blocks; while (outer != null) { if (outer.type == BlockType.LIST_ITEM) { inner = outer.blocks; while (inner != null) { if (inner.type == BlockType.NONE) { inner.type = BlockType.PARAGRAPH; } inner = inner.next; } } outer = outer.next; } } }
private void extendMainChain(Block block) { if (Arrays.equals(block.getBlockPrev(), this.lastBlock.getBlockHash())) { block.setMain(true); this.addBlock(block); this.lastBlock = block; } }
/** * Arranges the blocks without any constraints. This puts all blocks into a single row. * * @param container the container. * @param g2 the graphics device. * @return The size after the arrangement. */ protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) { double x = 0.0; double width = 0.0; double maxHeight = 0.0; List blocks = container.getBlocks(); int blockCount = blocks.size(); if (blockCount > 0) { Size2D[] sizes = new Size2D[blocks.size()]; for (int i = 0; i < blocks.size(); i++) { Block block = (Block) blocks.get(i); sizes[i] = block.arrange(g2, RectangleConstraint.NONE); width = width + sizes[i].getWidth(); maxHeight = Math.max(sizes[i].height, maxHeight); block.setBounds(new Rectangle2D.Double(x, 0.0, sizes[i].width, sizes[i].height)); x = x + sizes[i].width + this.horizontalGap; } if (blockCount > 1) { width = width + this.horizontalGap * (blockCount - 1); } if (this.verticalAlignment != VerticalAlignment.TOP) { for (int i = 0; i < blocks.size(); i++) { // Block b = (Block) blocks.get(i); if (this.verticalAlignment == VerticalAlignment.CENTER) { // TODO: shift block down by half } else if (this.verticalAlignment == VerticalAlignment.BOTTOM) { // TODO: shift block down to bottom } } } } return new Size2D(width, maxHeight); }
/** * Arranges the blocks without any constraints. This puts all blocks into a single row. * * @param container the container. * @param g2 the graphics device. * @return The size after the arrangement. */ protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) { List blocks = container.getBlocks(); Block b = (Block) blocks.get(0); Size2D s = b.arrange(g2, RectangleConstraint.NONE); b.setBounds(new Rectangle2D.Double(0.0, 0.0, s.width, s.height)); return new Size2D(s.width, s.height); }
private static Block createGenesis(NetworkParameters n) { Block genesisBlock = new Block(n); Transaction t = new Transaction(n); try { // A script containing the difficulty bits and the following message: // // "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" byte[] bytes = Utils.HEX.decode( "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73"); t.addInput(new TransactionInput(n, t, bytes)); ByteArrayOutputStream scriptPubKeyBytes = new ByteArrayOutputStream(); Script.writeBytes( scriptPubKeyBytes, Utils.HEX.decode( "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")); scriptPubKeyBytes.write(ScriptOpCodes.OP_CHECKSIG); t.addOutput(new TransactionOutput(n, t, FIFTY_COINS, scriptPubKeyBytes.toByteArray())); } catch (Exception e) { // Cannot happen. throw new RuntimeException(e); } genesisBlock.addTransaction(t); return genesisBlock; }
public void uncheckedTryHarvestBlock(int par1, int par2, int par3) { if (par1 == this.partiallyDestroyedBlockX && par2 == this.partiallyDestroyedBlockY && par3 == this.partiallyDestroyedBlockZ) { int var4 = this.curblockDamage - this.initialDamage; int var5 = this.theWorld.getBlockId(par1, par2, par3); if (var5 != 0) { Block var6 = Block.blocksList[var5]; float var7 = var6.getPlayerRelativeBlockHardness( this.thisPlayerMP, this.thisPlayerMP.worldObj, par1, par2, par3) * (float) (var4 + 1); if (var7 >= 0.7F) { this.isDestroyingBlock = false; this.theWorld.destroyBlockInWorldPartially( this.thisPlayerMP.entityId, par1, par2, par3, -1); this.tryHarvestBlock(par1, par2, par3); } else if (!this.receivedFinishDiggingPacket) { this.isDestroyingBlock = false; this.receivedFinishDiggingPacket = true; this.posX = par1; this.posY = par2; this.posZ = par3; this.field_73093_n = this.initialDamage; } } } }
@Test public void testAssignReturn() { mainBlock.addSimpleAssign("it", 5); mainBlock.addReturnVar("it"); int result = runMain(); assertEquals(5, result); }
/** * Brick / 1 Copy constructor for a brick. Makes a deep copy. * * @param brick, a Brick */ public Brick(Brick brick) { super(brick.name, brick.getKey()); variant = brick.getVariant(); subBlocks = new ArrayList<Block>(); // Loop through all the subblocks, making copies of each ListIterator blockIter = brick.getSubBlocks().listIterator(); while (blockIter.hasNext()) { Block block = (Block) blockIter.next(); if (block.isOverlap()) { if (block.isBrick()) { Brick overlapBrick = new Brick((Brick) block); subBlocks.addAll(overlapBrick.flattenBlock()); } } else if (block instanceof Brick) { Brick nextBrick = new Brick((Brick) block); String subName = nextBrick.getName(); if (subName.contains(LAUNCHER_KEYWORD)) { String newName = subName.replaceAll(" \\(Launcher\\)", ""); newName = newName.replaceAll(LAUNCHER_KEYWORD, APPROACH_KEYWORD); nextBrick.setName(newName); } subBlocks.add(nextBrick); } else { subBlocks.add(new ChordBlock((ChordBlock) block)); } } type = brick.getType(); this.updateDuration(); mode = brick.getMode(); endValue = getSectionEnd(); }
private LogLimits verifyAndGetLogLimits(Block html) { long start = -4096; long end = Long.MAX_VALUE; boolean isValid = true; String startStr = $("start"); if (startStr != null && !startStr.isEmpty()) { try { start = Long.parseLong(startStr); } catch (NumberFormatException e) { isValid = false; html.h1()._("Invalid log start value: " + startStr)._(); } } String endStr = $("end"); if (endStr != null && !endStr.isEmpty()) { try { end = Long.parseLong(endStr); } catch (NumberFormatException e) { isValid = false; html.h1()._("Invalid log end value: " + endStr)._(); } } if (!isValid) { return null; } LogLimits limits = new LogLimits(); limits.start = start; limits.end = end; return limits; }
@SuppressWarnings("unchecked") public List getBlockBBsInBB(World world, AxisAlignedBB axisalignedbb) { UnsafeList unsafeList = new UnsafeList(); int minX = MathHelper.floor(axisalignedbb.a); int maxX = MathHelper.floor(axisalignedbb.d + 1.0D); int minY = MathHelper.floor(axisalignedbb.b); int maxY = MathHelper.floor(axisalignedbb.e + 1.0D); int minZ = MathHelper.floor(axisalignedbb.c); int maxZ = MathHelper.floor(axisalignedbb.f + 1.0D); for (int x = minX; x < maxX; x++) { for (int z = minZ; z < maxZ; z++) { if (world.isChunkLoaded(x >> 4, z >> 4)) { for (int y = minY - 1; y < maxY; y++) { Block block = CraftMagicNumbers.getBlock(world.getBlockAt(x, y, z)); if (block != null && block.getBlockData().getMaterial().isSolid()) { block.a( block.getBlockData(), ((CraftWorld) world).getHandle(), new BlockPosition(x, y, z), axisalignedbb, unsafeList, null); } } } } } return unsafeList; }
private Block createBranch(Tree terminator, Block trueBranch, Block falseBranch) { Block result = createBlock(); result.terminator = terminator; result.successors.add(trueBranch); result.successors.add(falseBranch); return result; }
public boolean isColliding(int x, int y) { for (Block b : blocks) // if the block is a down block. if (b.getX() < x && x - b.getX() < b.getWidth() && b.getY() < y) return true; return false; }
/** * @param x * @param y * @param plane * @return */ public boolean isChunkUpdated(int x, int y, int plane) { if (blocks[plane][x][y] == null) { return false; } Block block = blocks[plane][x][y]; return block.isUpdated(); }