Skip to content

Commit

Permalink
v7 block update
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jan 4, 2020
1 parent a108447 commit c4c2822
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 45 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/org/pivxj/core/AbstractBlockChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sha256Hash> filteredTxHashList, @Nullable Map<Sha256Hash, Transaction> filteredTxn)
Expand All @@ -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;
}

Expand Down
60 changes: 23 additions & 37 deletions core/src/main/java/org/pivxj/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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){
Expand All @@ -336,34 +326,26 @@ 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);
return;
}
// 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 {
Expand Down Expand Up @@ -484,7 +466,7 @@ private Sha256Hash calculateHash() {
writeHeader(bos);
return Sha256Hash.wrap(
Utils.reverseBytes(
isZerocoin()?
isUsingSha256Hash()?
Sha256Hash.hashTwice(bos.toByteArray())
:
Hash9.digest(bos.toByteArray())
Expand Down Expand Up @@ -521,34 +503,38 @@ 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;
}
}

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;
}

/**
Expand Down Expand Up @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/org/pivxj/core/CoinDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/pivxj/core/NetworkParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -480,6 +481,10 @@ public long getZerocoinStartedHeight() {
return zerocoinStartedHeight;
}

public long getZerocoinEndHeight() {
return zerocoinEndHeight;
}

/**
* Construct and return a custom serializer.
*/
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/org/pivxj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1543,7 +1550,7 @@ protected void processInv(InventoryMessage inv) {
//masternodepings

//if(blockChain.getBestChainHeight() > (this.getBestHeight() - 100))
{
/*{
//if(context.masternodeSync.isSynced()) {
it = masternodePings.iterator();
Expand Down Expand Up @@ -1571,6 +1578,7 @@ protected void processInv(InventoryMessage inv) {
//}
}
}
it = sporks.iterator();
while (it.hasNext()) {
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/pivxj/crypto/MnemonicCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public static byte[] toSeed(List<String> 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;
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/pivxj/params/MainNetParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/pivxj/params/TestNet3Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/pivxj/wallet/DeterministicSeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public DeterministicSeed(byte[] seed, List<String> 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) {
Expand Down

0 comments on commit c4c2822

Please sign in to comment.