diff --git a/core/src/main/java/org/pivxj/core/AbstractBlockChain.java b/core/src/main/java/org/pivxj/core/AbstractBlockChain.java index 4f97e5dfe..a90ea984b 100644 --- a/core/src/main/java/org/pivxj/core/AbstractBlockChain.java +++ b/core/src/main/java/org/pivxj/core/AbstractBlockChain.java @@ -421,8 +421,8 @@ public boolean add(FilteredBlock block) throws VerificationException, PrunedExce * @throws BlockStoreException if the block store had an underlying error or newBlock does not exist in the block store at all. * @return The full set of all changes made to the set of open transaction outputs. */ - protected abstract TransactionOutputChanges connectTransactions(StoredBlock newBlock) throws VerificationException, BlockStoreException, PrunedException; - + protected abstract TransactionOutputChanges connectTransactions(StoredBlock newBlock) throws VerificationException, BlockStoreException, PrunedException; + // filteredTxHashList contains all transactions, filteredTxn just a subset private boolean add(Block block, boolean tryConnecting, @Nullable List filteredTxHashList, @Nullable Map filteredTxn) @@ -436,7 +436,7 @@ private boolean add(Block block, boolean tryConnecting, return true; } if (tryConnecting && orphanBlocks.containsKey(block.getHash())) { - log.info("orphanBlock.."); + log.info("Received an already got orphan."); return false; } diff --git a/core/src/main/java/org/pivxj/core/Block.java b/core/src/main/java/org/pivxj/core/Block.java index 0abdb6d82..d265f46f6 100644 --- a/core/src/main/java/org/pivxj/core/Block.java +++ b/core/src/main/java/org/pivxj/core/Block.java @@ -71,6 +71,8 @@ public enum VerifyFlag { */ public static final long ZEROCOIN_BLOCK_VERSION = 4; + public static final long ZEROCOIN_BLOCK_VERSION_END = 7; + public static final boolean ACTIVATE_ZEROCOIN = true; /** @@ -284,40 +286,28 @@ protected void parse() throws ProtocolException { // header cursor = offset; version = readUint32(); - //System.out.println("parse version: "+version); prevBlockHash = readHash(); - //System.out.println("parse prevBlockHash: "+prevBlockHash); merkleRoot = readHash(); - //System.out.println("parse merkleRoot: "+merkleRoot); time = readUint32(); - //System.out.println("parse time: "+time); difficultyTarget = readUint32(); - //System.out.println("parse difficultyTarget: "+difficultyTarget); nonce = readUint32(); - //System.out.println("parse nonce: "+nonce); - int headerSize = getHeaderSize(); if (isZerocoin() && length >= ZEROCOIN_HEADER_SIZE) { // accumulator zeroCoinAccumulator = readHash(true); - //System.out.println("parse zeroCoinAccumulator: "+zeroCoinAccumulator); - //System.out.println("offset: "+offset); - //System.out.println("cursor: "+cursor); - //System.out.println("payload size: "+payload.length); - //System.out.println("Hash payload "+Arrays.toString(copy)); - // hash = Sha256Hash.wrapReversed(Sha256Hash.hashTwice(copy)); - // System.out.println("protocol hash parsed: "+hash.toString()); hash = Sha256Hash.wrapReversed(Sha256Hash.hashTwice(payload, offset, cursor - offset)); - //System.out.println("protocol test hash parsed 2: "+hash.toString()); - - //hash = Sha256Hash.wrapReversed(Sha256Hash.hashTwice(payload, offset, cursor - offset)); } else { - hash = Sha256Hash.wrapReversed(Hash9.digest(payload, offset, cursor - offset)); - //System.out.println("hash parsed: "+hash.toString()); + if (version >= ZEROCOIN_BLOCK_VERSION_END) { + // No zc accumulator from here + hash = Sha256Hash.wrapReversed(Sha256Hash.hashTwice(payload, offset, cursor - offset)); + } else { + hash = Sha256Hash.wrapReversed(Hash9.digest(payload, offset, cursor - offset)); + } } headerBytesValid = serializer.isParseRetainMode(); // transactions + int headerSize = getHeaderSize(); parseTransactions(offset + headerSize); length = cursor - offset; }catch (Exception e){ @@ -336,7 +326,6 @@ public int getOptimalEncodingMessageSize() { // default for testing void writeHeader(OutputStream stream) throws IOException { int headerSize = getHeaderSize(); - //System.out.println("writeHeader header size: "+headerSize); // try for cached write first if (headerBytesValid && payload != null && payload.length >= offset + headerSize) { stream.write(payload, offset, headerSize); @@ -344,26 +333,19 @@ void writeHeader(OutputStream stream) throws IOException { } // fall back to manual write Utils.uint32ToByteStreamLE(version, stream); - //System.out.println("writeHeader version: "+version); stream.write(prevBlockHash.getReversedBytes()); - //System.out.println("writeHeader prevBlockHash: "+prevBlockHash); stream.write(getMerkleRoot().getReversedBytes()); - //System.out.println("writeHeader merkle root: "+getMerkleRoot()); Utils.uint32ToByteStreamLE(time, stream); - //System.out.println("writeHeader time: "+time); Utils.uint32ToByteStreamLE(difficultyTarget, stream); - //System.out.println("writeHeader difficultyTarget: "+difficultyTarget); Utils.uint32ToByteStreamLE(nonce, stream); - //System.out.println("writeHeader nonce: "+nonce); - if (isZerocoin()){ - byte[] accumulator = (zeroCoinAccumulator!=null)?zeroCoinAccumulator.getReversedBytes():new byte[32]; + if (isZerocoin()) { + byte[] accumulator = (zeroCoinAccumulator != null) ? zeroCoinAccumulator.getReversedBytes() : new byte[32]; stream.write(accumulator); - //System.out.println("writeHeader zeroCoinAccumulator: "+nonce); } } public int getHeaderSize(){ - return isZerocoin()?ZEROCOIN_HEADER_SIZE:HEADER_SIZE; + return isZerocoin() ? ZEROCOIN_HEADER_SIZE : HEADER_SIZE; } private void writeTransactions(OutputStream stream) throws IOException { @@ -484,7 +466,7 @@ private Sha256Hash calculateHash() { writeHeader(bos); return Sha256Hash.wrap( Utils.reverseBytes( - isZerocoin()? + isUsingSha256Hash()? Sha256Hash.hashTwice(bos.toByteArray()) : Hash9.digest(bos.toByteArray()) @@ -521,26 +503,30 @@ public Sha256Hash getHash() { return hash; } + public boolean isUsingSha256Hash() { + return version >= ZEROCOIN_BLOCK_VERSION; + } + public boolean isZerocoin() { if (!ACTIVATE_ZEROCOIN) { return false; }else { - return version >= ZEROCOIN_BLOCK_VERSION; + return version >= ZEROCOIN_BLOCK_VERSION && version < ZEROCOIN_BLOCK_VERSION_END; } } - public static boolean isZerocoinHeight(NetworkParameters networkParameters,long height) { + public static boolean isZerocoinHeight(NetworkParameters networkParameters, long height) { if (!ACTIVATE_ZEROCOIN) { return false; }else - return height>=networkParameters.getZerocoinStartedHeight(); + return height >= networkParameters.getZerocoinStartedHeight() && height < networkParameters.getZerocoinEndHeight(); } public static int getHeaderSizeByVersion(long version){ if (!ACTIVATE_ZEROCOIN) { return Block.HEADER_SIZE; }else { - return version >= Block.ZEROCOIN_BLOCK_VERSION ? ZEROCOIN_HEADER_SIZE : HEADER_SIZE; + return (version >= Block.ZEROCOIN_BLOCK_VERSION && version < Block.ZEROCOIN_BLOCK_VERSION_END) ? ZEROCOIN_HEADER_SIZE : HEADER_SIZE; } } @@ -548,7 +534,7 @@ public static int getHeaderSize(NetworkParameters params,long height){ if (!ACTIVATE_ZEROCOIN) { return Block.HEADER_SIZE; }else - return Block.isZerocoinHeight(params,height)?Block.ZEROCOIN_HEADER_SIZE:Block.HEADER_SIZE; + return Block.isZerocoinHeight(params, height) ? Block.ZEROCOIN_HEADER_SIZE : Block.HEADER_SIZE; } /** @@ -611,7 +597,7 @@ public String toString() { s.append(" difficulty target (nBits): ").append(difficultyTarget).append("\n"); s.append(" nonce: ").append(nonce).append("\n"); if (isZerocoin()){ - s.append(" accumulator: ").append((zeroCoinAccumulator!=null)?zeroCoinAccumulator.toString():0).append("\n");; + s.append(" accumulator: ").append((zeroCoinAccumulator != null) ? zeroCoinAccumulator.toString(): 0).append("\n"); } if (transactions != null && transactions.size() > 0) { s.append(" with ").append(transactions.size()).append(" transaction(s):\n"); diff --git a/core/src/main/java/org/pivxj/core/CoinDefinition.java b/core/src/main/java/org/pivxj/core/CoinDefinition.java index f037e4558..7c84f4cb5 100644 --- a/core/src/main/java/org/pivxj/core/CoinDefinition.java +++ b/core/src/main/java/org/pivxj/core/CoinDefinition.java @@ -77,8 +77,8 @@ public static final int getTargetTimespan(int height, boolean testNet) { // // PIVX 3.2.0 // - public static final int PROTOCOL_VERSION = 70917; //version.h PROTOCOL_VERSION - public static final int MIN_PROTOCOL_VERSION = 70917; //version.h MIN_PROTO_VERSION + public static final int PROTOCOL_VERSION = 70918; //version.h PROTOCOL_VERSION + public static final int MIN_PROTOCOL_VERSION = 70918; //version.h MIN_PROTO_VERSION public static final int BLOCK_CURRENTVERSION = 2; //CBlock::CURRENT_VERSION public static final int MAX_BLOCK_SIZE = 1 * 1000 * 1000; @@ -93,6 +93,10 @@ public static final int getTargetTimespan(int height, boolean testNet) { public static final long TESTNET_ZEROCOIN_STARTING_BLOCK_HEIGHT = 201564; public static final long MAINNET_ZEROCOIN_STARTING_BLOCK_HEIGHT = 863787; + + public static final long TESTNET_ZEROCOIN_END_BLOCK_HEIGHT = 1347000; + public static final long MAINNET_ZEROCOIN_END_BLOCK_HEIGHT = 2153200; + //high fee required for zerocoin mints public static final Coin MIN_ZEROCOIN_MINT_FEE = Coin.CENT; //the maximum amount of confirmations until accumulated in 19 diff --git a/core/src/main/java/org/pivxj/core/NetworkParameters.java b/core/src/main/java/org/pivxj/core/NetworkParameters.java index 944e8bff0..819632624 100644 --- a/core/src/main/java/org/pivxj/core/NetworkParameters.java +++ b/core/src/main/java/org/pivxj/core/NetworkParameters.java @@ -86,6 +86,7 @@ public abstract class NetworkParameters { protected int bip32HeaderPriv; protected long zerocoinStartedHeight; + protected long zerocoinEndHeight; /** Used to check majorities for block version upgrade */ protected int majorityEnforceBlockUpgrade; @@ -480,6 +481,10 @@ public long getZerocoinStartedHeight() { return zerocoinStartedHeight; } + public long getZerocoinEndHeight() { + return zerocoinEndHeight; + } + /** * Construct and return a custom serializer. */ diff --git a/core/src/main/java/org/pivxj/core/Peer.java b/core/src/main/java/org/pivxj/core/Peer.java index 9be1a4c17..48bf83c14 100644 --- a/core/src/main/java/org/pivxj/core/Peer.java +++ b/core/src/main/java/org/pivxj/core/Peer.java @@ -536,6 +536,13 @@ public void processMessage(Message m) throws Exception { // No further communication is possible until version handshake is complete. if (!(m instanceof VersionMessage || m instanceof VersionAck || (versionHandshakeFuture.isDone() && !versionHandshakeFuture.isCancelled()))) { + + if (m instanceof GetSporksMessage) { + // sporks received, do not take them into account. This is an invalid functioning of the core node that will be fixed in the future. + log.info("Received getspork message before the handshake completed, skipping it"); + return; + } + String reason = " " + ((m instanceof RejectMessage) ? ((RejectMessage) m).getReasonString() : ""); throw new ProtocolException( "Received " + m.getClass().getSimpleName() + " before version handshake is complete."+ reason); @@ -1543,7 +1550,7 @@ protected void processInv(InventoryMessage inv) { //masternodepings //if(blockChain.getBestChainHeight() > (this.getBestHeight() - 100)) - { + /*{ //if(context.masternodeSync.isSynced()) { it = masternodePings.iterator(); @@ -1571,6 +1578,7 @@ protected void processInv(InventoryMessage inv) { //} } } + it = sporks.iterator(); while (it.hasNext()) { @@ -1581,6 +1589,7 @@ protected void processInv(InventoryMessage inv) { getdata.addItem(item); //} } + */ // If we are requesting filteredblocks we have to send a ping after the getdata so that we have a clear // end to the final FilteredBlock's transactions (in the form of a pong) sent to us diff --git a/core/src/main/java/org/pivxj/crypto/MnemonicCode.java b/core/src/main/java/org/pivxj/crypto/MnemonicCode.java index 05ad5d067..88f4cc1d8 100644 --- a/core/src/main/java/org/pivxj/crypto/MnemonicCode.java +++ b/core/src/main/java/org/pivxj/crypto/MnemonicCode.java @@ -134,7 +134,6 @@ public static byte[] toSeed(List words, String passphrase) { byte[] seed = PBKDF2SHA512.derive(pass, salt, PBKDF2_ROUNDS, 64); watch.stop(); log.info("PBKDF2 took {}", watch); - //System.out.println("MnemonicCode class: "+ Arrays.toString(words.toArray())+", passphrase: "+passphrase+ ", \ntoSeed: "+HEX.encode(seed)); return seed; } diff --git a/core/src/main/java/org/pivxj/params/MainNetParams.java b/core/src/main/java/org/pivxj/params/MainNetParams.java index d708772c0..728f65b2d 100644 --- a/core/src/main/java/org/pivxj/params/MainNetParams.java +++ b/core/src/main/java/org/pivxj/params/MainNetParams.java @@ -61,6 +61,7 @@ public MainNetParams() { CoinDefinition.initCheckpoints(checkpoints); zerocoinStartedHeight = CoinDefinition.MAINNET_ZEROCOIN_STARTING_BLOCK_HEIGHT; + zerocoinEndHeight = CoinDefinition.MAINNET_ZEROCOIN_END_BLOCK_HEIGHT; dnsSeeds = CoinDefinition.dnsSeeds; diff --git a/core/src/main/java/org/pivxj/params/TestNet3Params.java b/core/src/main/java/org/pivxj/params/TestNet3Params.java index d03ef7ed1..6bede378c 100644 --- a/core/src/main/java/org/pivxj/params/TestNet3Params.java +++ b/core/src/main/java/org/pivxj/params/TestNet3Params.java @@ -65,6 +65,7 @@ public TestNet3Params() { //alertSigningKey = HEX.decode(CoinDefinition.TESTNET_SATOSHI_KEY); zerocoinStartedHeight = CoinDefinition.TESTNET_ZEROCOIN_STARTING_BLOCK_HEIGHT; + zerocoinEndHeight = CoinDefinition.TESTNET_ZEROCOIN_END_BLOCK_HEIGHT; dnsSeeds = CoinDefinition.testnetDnsSeeds; diff --git a/core/src/main/java/org/pivxj/wallet/DeterministicSeed.java b/core/src/main/java/org/pivxj/wallet/DeterministicSeed.java index 85cfd8dfb..af8141979 100644 --- a/core/src/main/java/org/pivxj/wallet/DeterministicSeed.java +++ b/core/src/main/java/org/pivxj/wallet/DeterministicSeed.java @@ -60,7 +60,6 @@ public DeterministicSeed(byte[] seed, List mnemonic, long creationTimeSe this.mnemonicCode = checkNotNull(mnemonic); this.encryptedMnemonicCode = null; this.creationTimeSeconds = creationTimeSeconds; - //System.out.println("seed: "+ toHexString()+"creationTime: "+creationTimeSeconds+" , mnemonic code: "+ Arrays.toString(mnemonicCode.toArray())); } public DeterministicSeed(EncryptedData encryptedMnemonic, @Nullable EncryptedData encryptedSeed, long creationTimeSeconds) {