List of usage examples for java.util Collections lastIndexOfSubList
public static int lastIndexOfSubList(List<?> source, List<?> target)
From source file:Main.java
public static void main(String args[]) { // create two array list objects List<String> arrlistsrc = new ArrayList<String>(); List<String> arrlisttarget = new ArrayList<String>(); // populate two lists arrlistsrc.add("A"); arrlistsrc.add("from java2s.com"); arrlistsrc.add("C"); arrlistsrc.add("D"); arrlistsrc.add("E"); arrlisttarget.add("C"); arrlisttarget.add("D"); arrlisttarget.add("E"); // check starting position of the last occurrence int index = Collections.lastIndexOfSubList(arrlistsrc, arrlisttarget); System.out.println("Starting position is: " + index); }
From source file:Utilities.java
public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six one".split(" ")); System.out.println(list);/* w w w . j av a2s .c o m*/ System.out.println("max: " + Collections.max(list)); System.out.println("min: " + Collections.min(list)); AlphabeticComparator comp = new AlphabeticComparator(); System.out.println("max w/ comparator: " + Collections.max(list, comp)); System.out.println("min w/ comparator: " + Collections.min(list, comp)); List sublist = Arrays.asList("Four five six".split(" ")); System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist)); System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist)); Collections.replaceAll(list, "one", "Yo"); System.out.println("replaceAll: " + list); Collections.reverse(list); System.out.println("reverse: " + list); Collections.rotate(list, 3); System.out.println("rotate: " + list); List source = Arrays.asList("in the matrix".split(" ")); Collections.copy(list, source); System.out.println("copy: " + list); Collections.swap(list, 0, list.size() - 1); System.out.println("swap: " + list); Collections.fill(list, "pop"); System.out.println("fill: " + list); List dups = Collections.nCopies(3, "snap"); System.out.println("dups: " + dups); // Getting an old-style Enumeration: Enumeration e = Collections.enumeration(dups); Vector v = new Vector(); while (e.hasMoreElements()) v.addElement(e.nextElement()); // Converting an old-style Vector // to a List via an Enumeration: ArrayList arrayList = Collections.list(v.elements()); System.out.println("arrayList: " + arrayList); }
From source file:Main.java
public static int lastIndexOfSubList(final List<?> list0, final List<?> list1) { return Collections.lastIndexOfSubList(list0, list1); }
From source file:co.rsk.rpc.Web3RskImpl.java
public void mnr_submitBitcoinBlock(String bitcoinBlockHex) { if (logger.isDebugEnabled()) logger.debug("mnr_submitBitcoinBlock(): " + bitcoinBlockHex.length()); co.rsk.bitcoinj.core.NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get(); new co.rsk.bitcoinj.core.Context(params); byte[] bitcoinBlockByteArray = Hex.decode(bitcoinBlockHex); co.rsk.bitcoinj.core.BtcBlock bitcoinBlock = params.getDefaultSerializer().makeBlock(bitcoinBlockByteArray); co.rsk.bitcoinj.core.BtcTransaction coinbase = bitcoinBlock.getTransactions().get(0); byte[] coinbaseAsByteArray = coinbase.bitcoinSerialize(); List<Byte> coinbaseAsByteList = java.util.Arrays.asList(ArrayUtils.toObject(coinbaseAsByteArray)); List<Byte> rskTagAsByteList = java.util.Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG)); int rskTagPosition = Collections.lastIndexOfSubList(coinbaseAsByteList, rskTagAsByteList); byte[] blockHashForMergedMiningArray = new byte[SHA3Helper.Size.S256.getValue() / 8]; System.arraycopy(coinbaseAsByteArray, rskTagPosition + RskMiningConstants.RSK_TAG.length, blockHashForMergedMiningArray, 0, blockHashForMergedMiningArray.length); String blockHashForMergedMining = TypeConverter.toJsonHex(blockHashForMergedMiningArray); minerServer.submitBitcoinBlock(blockHashForMergedMining, bitcoinBlock); }
From source file:co.rsk.rpc.modules.mnr.MnrModuleImpl.java
private String extractBlockHashForMergedMining(BtcTransaction coinbase) { byte[] coinbaseAsByteArray = coinbase.bitcoinSerialize(); List<Byte> coinbaseAsByteList = Arrays.asList(ArrayUtils.toObject(coinbaseAsByteArray)); List<Byte> rskTagAsByteList = Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG)); int rskTagPosition = Collections.lastIndexOfSubList(coinbaseAsByteList, rskTagAsByteList); byte[] blockHashForMergedMiningArray = new byte[Keccak256Helper.Size.S256.getValue() / 8]; System.arraycopy(coinbaseAsByteArray, rskTagPosition + RskMiningConstants.RSK_TAG.length, blockHashForMergedMiningArray, 0, blockHashForMergedMiningArray.length); return TypeConverter.toJsonHex(blockHashForMergedMiningArray); }
From source file:co.rsk.validators.ProofOfWorkRule.java
@Override public boolean isValid(BlockHeader header) { // TODO: refactor this an move it to another class. Change the Global ProofOfWorkRule to AuthenticationRule. // TODO: Make ProofOfWorkRule one of the classes that inherits from AuthenticationRule. if (isFallbackMiningPossibleAndBlockSigned(header)) { boolean isValidFallbackSignature = validFallbackBlockSignature(constants, header, header.getBitcoinMergedMiningHeader()); if (!isValidFallbackSignature) { logger.warn("Fallback signature failed. Header {}", header.getShortHash()); }/*w w w . j a v a 2 s . co m*/ return isValidFallbackSignature; } co.rsk.bitcoinj.core.NetworkParameters bitcoinNetworkParameters = bridgeConstants.getBtcParams(); MerkleProofValidator mpValidator; try { if (blockchainConfig.getConfigForBlock(header.getNumber()).isRskip92()) { mpValidator = new Rskip92MerkleProofValidator(header.getBitcoinMergedMiningMerkleProof()); } else { mpValidator = new GenesisMerkleProofValidator(bitcoinNetworkParameters, header.getBitcoinMergedMiningMerkleProof()); } } catch (RuntimeException ex) { logger.warn("Merkle proof can't be validated. Header {}", header.getShortHash(), ex); return false; } byte[] bitcoinMergedMiningCoinbaseTransactionCompressed = header .getBitcoinMergedMiningCoinbaseTransaction(); if (bitcoinMergedMiningCoinbaseTransactionCompressed == null) { logger.warn("Compressed coinbase transaction does not exist. Header {}", header.getShortHash()); return false; } if (header.getBitcoinMergedMiningHeader() == null) { logger.warn("Bitcoin merged mining header does not exist. Header {}", header.getShortHash()); return false; } BtcBlock bitcoinMergedMiningBlock = bitcoinNetworkParameters.getDefaultSerializer() .makeBlock(header.getBitcoinMergedMiningHeader()); BigInteger target = DifficultyUtils.difficultyToTarget(header.getDifficulty()); BigInteger bitcoinMergedMiningBlockHashBI = bitcoinMergedMiningBlock.getHash().toBigInteger(); if (bitcoinMergedMiningBlockHashBI.compareTo(target) > 0) { logger.warn("Hash {} is higher than target {}", bitcoinMergedMiningBlockHashBI.toString(16), target.toString(16)); return false; } byte[] bitcoinMergedMiningCoinbaseTransactionMidstate = new byte[RskMiningConstants.MIDSTATE_SIZE]; System.arraycopy(bitcoinMergedMiningCoinbaseTransactionCompressed, 0, bitcoinMergedMiningCoinbaseTransactionMidstate, 8, RskMiningConstants.MIDSTATE_SIZE_TRIMMED); byte[] bitcoinMergedMiningCoinbaseTransactionTail = new byte[bitcoinMergedMiningCoinbaseTransactionCompressed.length - RskMiningConstants.MIDSTATE_SIZE_TRIMMED]; System.arraycopy(bitcoinMergedMiningCoinbaseTransactionCompressed, RskMiningConstants.MIDSTATE_SIZE_TRIMMED, bitcoinMergedMiningCoinbaseTransactionTail, 0, bitcoinMergedMiningCoinbaseTransactionTail.length); byte[] expectedCoinbaseMessageBytes = org.bouncycastle.util.Arrays.concatenate(RskMiningConstants.RSK_TAG, header.getHashForMergedMining()); List<Byte> bitcoinMergedMiningCoinbaseTransactionTailAsList = Arrays .asList(ArrayUtils.toObject(bitcoinMergedMiningCoinbaseTransactionTail)); List<Byte> expectedCoinbaseMessageBytesAsList = Arrays .asList(ArrayUtils.toObject(expectedCoinbaseMessageBytes)); int rskTagPosition = Collections.lastIndexOfSubList(bitcoinMergedMiningCoinbaseTransactionTailAsList, expectedCoinbaseMessageBytesAsList); if (rskTagPosition == -1) { logger.warn( "bitcoin coinbase transaction tail message does not contain expected RSKBLOCK:RskBlockHeaderHash. Expected: {} . Actual: {} .", Arrays.toString(expectedCoinbaseMessageBytes), Arrays.toString(bitcoinMergedMiningCoinbaseTransactionTail)); return false; } /* * We check that the there is no other block before the rsk tag, to avoid a possible malleability attack: * If we have a mid state with 10 blocks, and the rsk tag, we can also have * another mid state with 9 blocks, 64bytes + the rsk tag, giving us two blocks with different hashes but the same spv proof. * */ if (rskTagPosition >= 64) { logger.warn("bitcoin coinbase transaction tag position is bigger than expected 64. Actual: {}.", Integer.toString(rskTagPosition)); return false; } List<Byte> rskTagAsList = Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG)); int lastTag = Collections.lastIndexOfSubList(bitcoinMergedMiningCoinbaseTransactionTailAsList, rskTagAsList); if (rskTagPosition != lastTag) { logger.warn("The valid RSK tag is not the last RSK tag. Tail: {}.", Arrays.toString(bitcoinMergedMiningCoinbaseTransactionTail)); return false; } int remainingByteCount = bitcoinMergedMiningCoinbaseTransactionTail.length - rskTagPosition - RskMiningConstants.RSK_TAG.length - RskMiningConstants.BLOCK_HEADER_HASH_SIZE; if (remainingByteCount > RskMiningConstants.MAX_BYTES_AFTER_MERGED_MINING_HASH) { logger.warn("More than 128 bytes after RSK tag"); return false; } // TODO test long byteCount = Pack.bigEndianToLong(bitcoinMergedMiningCoinbaseTransactionMidstate, 8); long coinbaseLength = bitcoinMergedMiningCoinbaseTransactionTail.length + byteCount; if (coinbaseLength <= 64) { logger.warn("Coinbase transaction must always be greater than 64-bytes long. But it was: {}", coinbaseLength); return false; } SHA256Digest digest = new SHA256Digest(bitcoinMergedMiningCoinbaseTransactionMidstate); digest.update(bitcoinMergedMiningCoinbaseTransactionTail, 0, bitcoinMergedMiningCoinbaseTransactionTail.length); byte[] bitcoinMergedMiningCoinbaseTransactionOneRoundOfHash = new byte[32]; digest.doFinal(bitcoinMergedMiningCoinbaseTransactionOneRoundOfHash, 0); Sha256Hash bitcoinMergedMiningCoinbaseTransactionHash = Sha256Hash .wrapReversed(Sha256Hash.hash(bitcoinMergedMiningCoinbaseTransactionOneRoundOfHash)); if (!mpValidator.isValid(bitcoinMergedMiningBlock.getMerkleRoot(), bitcoinMergedMiningCoinbaseTransactionHash)) { logger.warn("bitcoin merkle branch doesn't match coinbase and state root"); return false; } return true; }
From source file:co.rsk.mine.MinerServerImpl.java
private byte[] compressCoinbase(byte[] bitcoinMergedMiningCoinbaseTransactionSerialized) { int rskTagPosition = Collections.lastIndexOfSubList( java.util.Arrays.asList(ArrayUtils.toObject(bitcoinMergedMiningCoinbaseTransactionSerialized)), java.util.Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG))); int remainingByteCount = bitcoinMergedMiningCoinbaseTransactionSerialized.length - rskTagPosition - RskMiningConstants.RSK_TAG.length - RskMiningConstants.BLOCK_HEADER_HASH_SIZE; if (remainingByteCount > RskMiningConstants.MAX_BYTES_AFTER_MERGED_MINING_HASH) { throw new IllegalArgumentException("More than 128 bytes after ROOOTSTOCK tag"); }//from w w w. j a v a2s .c o m int sha256Blocks = rskTagPosition / 64; int bytesToHash = sha256Blocks * 64; SHA256Digest digest = new SHA256Digest(); digest.update(bitcoinMergedMiningCoinbaseTransactionSerialized, 0, bytesToHash); byte[] hashedContent = digest.getEncodedState(); byte[] trimmedHashedContent = new byte[RskMiningConstants.MIDSTATE_SIZE_TRIMMED]; System.arraycopy(hashedContent, 8, trimmedHashedContent, 0, RskMiningConstants.MIDSTATE_SIZE_TRIMMED); byte[] unHashedContent = new byte[bitcoinMergedMiningCoinbaseTransactionSerialized.length - bytesToHash]; System.arraycopy(bitcoinMergedMiningCoinbaseTransactionSerialized, bytesToHash, unHashedContent, 0, unHashedContent.length); return Arrays.concatenate(trimmedHashedContent, unHashedContent); }
From source file:org.ethereum.validator.ProofOfWorkRule.java
@Override public boolean isValid(Block block) { BlockHeader header = block.getHeader(); co.rsk.bitcoinj.core.NetworkParameters bitcoinNetworkParameters = SystemProperties.CONFIG .getBlockchainConfig().getCommonConstants().getBridgeConstants().getBtcParams(); byte[] bitcoinMergedMiningCoinbaseTransactionCompressed = header .getBitcoinMergedMiningCoinbaseTransaction(); co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = bitcoinNetworkParameters.getDefaultSerializer() .makeBlock(header.getBitcoinMergedMiningHeader()); co.rsk.bitcoinj.core.PartialMerkleTree bitcoinMergedMiningMerkleBranch = new co.rsk.bitcoinj.core.PartialMerkleTree( bitcoinNetworkParameters, header.getBitcoinMergedMiningMerkleProof(), 0); BigInteger target = DifficultyUtils.difficultyToTarget(header.getDifficultyBI()); BigInteger bitcoinMergedMiningBlockHashBI = bitcoinMergedMiningBlock.getHash().toBigInteger(); if (bitcoinMergedMiningBlockHashBI.compareTo(target) > 0) { logger.error("Hash {} is higher than target {}", bitcoinMergedMiningBlockHashBI.toString(16), target.toString(16));//from w w w. j a v a 2s . co m return false; } byte[] bitcoinMergedMiningCoinbaseTransactionMidstate = new byte[RskMiningConstants.MIDSTATE_SIZE]; System.arraycopy(bitcoinMergedMiningCoinbaseTransactionCompressed, 0, bitcoinMergedMiningCoinbaseTransactionMidstate, 8, RskMiningConstants.MIDSTATE_SIZE_TRIMMED); byte[] bitcoinMergedMiningCoinbaseTransactionTail = new byte[bitcoinMergedMiningCoinbaseTransactionCompressed.length - RskMiningConstants.MIDSTATE_SIZE_TRIMMED]; System.arraycopy(bitcoinMergedMiningCoinbaseTransactionCompressed, RskMiningConstants.MIDSTATE_SIZE_TRIMMED, bitcoinMergedMiningCoinbaseTransactionTail, 0, bitcoinMergedMiningCoinbaseTransactionTail.length); byte[] expectedCoinbaseMessageBytes = org.spongycastle.util.Arrays.concatenate(RskMiningConstants.RSK_TAG, header.getHashForMergedMining()); List<Byte> bitcoinMergedMiningCoinbaseTransactionTailAsList = java.util.Arrays .asList(ArrayUtils.toObject(bitcoinMergedMiningCoinbaseTransactionTail)); List<Byte> expectedCoinbaseMessageBytesAsList = java.util.Arrays .asList(ArrayUtils.toObject(expectedCoinbaseMessageBytes)); int rskTagPosition = Collections.lastIndexOfSubList(bitcoinMergedMiningCoinbaseTransactionTailAsList, expectedCoinbaseMessageBytesAsList); if (rskTagPosition == -1) { logger.error( "bitcoin coinbase transaction tail message does not contain expected RSKBLOCK:RskBlockHeaderHash. Expected: {} . Actual: {} .", Arrays.toString(expectedCoinbaseMessageBytes), Arrays.toString(bitcoinMergedMiningCoinbaseTransactionTail)); return false; } /* * We check that the there is no other block before the rsk tag, to avoid a possible malleability attack: * If we have a mid state with 10 blocks, and the rsk tag, we can also have * another mid state with 9 blocks, 64bytes + the rsk tag, giving us two blocks with different hashes but the same spv proof. * */ if (rskTagPosition >= 64) { logger.error("bitcoin coinbase transaction tag position is bigger than expected 64. Actual: {}.", Integer.toString(rskTagPosition)); return false; } List<Byte> rskTagAsList = java.util.Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG)); if (rskTagPosition != Collections.lastIndexOfSubList(bitcoinMergedMiningCoinbaseTransactionTailAsList, rskTagAsList)) { logger.error("The valid RSK tag is not the last RSK tag. Tail: {}.", Arrays.toString(bitcoinMergedMiningCoinbaseTransactionTail)); return false; } int remainingByteCount = bitcoinMergedMiningCoinbaseTransactionTail.length - rskTagPosition - RskMiningConstants.RSK_TAG.length - RskMiningConstants.BLOCK_HEADER_HASH_SIZE; if (remainingByteCount > RskMiningConstants.MAX_BYTES_AFTER_MERGED_MINING_HASH) { logger.error("More than 128 bytes after ROOOTSTOCK tag"); return false; } SHA256Digest digest = new SHA256Digest(bitcoinMergedMiningCoinbaseTransactionMidstate); digest.update(bitcoinMergedMiningCoinbaseTransactionTail, 0, bitcoinMergedMiningCoinbaseTransactionTail.length); byte[] bitcoinMergedMiningCoinbaseTransactionOneRoundOfHash = new byte[32]; digest.doFinal(bitcoinMergedMiningCoinbaseTransactionOneRoundOfHash, 0); co.rsk.bitcoinj.core.Sha256Hash bitcoinMergedMiningCoinbaseTransactionHash = co.rsk.bitcoinj.core.Sha256Hash .wrapReversed( co.rsk.bitcoinj.core.Sha256Hash.hash(bitcoinMergedMiningCoinbaseTransactionOneRoundOfHash)); List<co.rsk.bitcoinj.core.Sha256Hash> txHashesInTheMerkleBranch = new ArrayList<>(); co.rsk.bitcoinj.core.Sha256Hash merkleRoot = bitcoinMergedMiningMerkleBranch .getTxnHashAndMerkleRoot(txHashesInTheMerkleBranch); if (!merkleRoot.equals(bitcoinMergedMiningBlock.getMerkleRoot())) { logger.error("bitcoin merkle root of bitcoin block does not match the merkle root of merkle branch"); return false; } if (!txHashesInTheMerkleBranch.contains(bitcoinMergedMiningCoinbaseTransactionHash)) { logger.error("bitcoin coinbase transaction {} not included in merkle branch", bitcoinMergedMiningCoinbaseTransactionHash); return false; } return true; }