Skip to content

Commit

Permalink
Merge pull request #230 from dashpay/feature-core-20-phase-1
Browse files Browse the repository at this point in the history
feat: support core 20
  • Loading branch information
HashEngineering authored Sep 26, 2023
2 parents 7773c1f + 01c4e3c commit 1f64d0e
Show file tree
Hide file tree
Showing 62 changed files with 1,746 additions and 577 deletions.
6 changes: 3 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ apply plugin: 'maven-publish'
apply plugin: 'jacoco'
apply plugin: 'signing'

version = '19.0.6'
version = '20.0.0-SNAPSHOT'
archivesBaseName = 'dashj-core'
eclipse.project.name = 'dashj-core'

dependencies {
compile 'org.bouncycastle:bcprov-jdk15to18:1.68'
implementation 'com.google.guava:guava:28.2-android'
implementation 'com.google.guava:guava:30.0-jre'
compile 'com.google.protobuf:protobuf-javalite:3.14.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.8'
implementation 'org.slf4j:slf4j-api:1.7.30'
Expand All @@ -29,7 +29,7 @@ dependencies {
implementation 'de.sfuhrm:saphir-hash-core:3.0.10'
implementation 'org.dashj:dashj-bls:1.0.0'
implementation 'com.lambdaworks:scrypt:1.4.0'
implementation 'org.json:json:20210307'
implementation 'org.json:json:20220320'

}

Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public BitcoinSerializer(NetworkParameters params, boolean parseRetain) {
this.parseRetain = parseRetain;
}

public BitcoinSerializer(NetworkParameters params, boolean parseRetain, int protocolVersion) {
this(params, parseRetain);
setProtocolVersion(protocolVersion);
}

/**
* Writes message to to the output stream.
*/
Expand Down
164 changes: 164 additions & 0 deletions core/src/main/java/org/bitcoinj/core/BlockQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package org.bitcoinj.core;

import javax.annotation.Nonnull;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class BlockQueue implements Collection<StoredBlock> {
private final LinkedHashMap<Sha256Hash, StoredBlock> map;
private final ArrayDeque<StoredBlock> queue;

public BlockQueue() {
queue = new ArrayDeque<>(16);
map = new LinkedHashMap<>();
}

public void clear() {
queue.clear();
map.clear();
}

@Override
public Spliterator<StoredBlock> spliterator() {
return queue.spliterator();
}

@Override
public Stream<StoredBlock> stream() {
return queue.stream();
}

@Override
public Stream<StoredBlock> parallelStream() {
return queue.parallelStream();
}

public StoredBlock pop() {
StoredBlock thisBlock = queue.removeFirst();
map.remove(thisBlock.getHeader().getHash());
return thisBlock;
}

public StoredBlock peek() {
return queue.getFirst();
}
public boolean contains(StoredBlock block) {
return map.containsKey(block.getHeader().getHash());
}

public boolean isEmpty() {
return queue.isEmpty();
}

@Override
public boolean contains(Object o) {
if (o instanceof StoredBlock)
return contains((StoredBlock) o);
return false;
}

public int size() {
return queue.size();
}

static class BlockQueueIterator implements java.util.Iterator<StoredBlock> {

Iterator<StoredBlock> iterator;
BlockQueue blockQueue;
StoredBlock currentBlock;
public BlockQueueIterator(BlockQueue blockQueue) {
iterator = blockQueue.queue.iterator();
this.blockQueue = blockQueue;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public StoredBlock next() {
currentBlock = iterator.next();
return currentBlock;
}

@Override
public void remove() {
if (currentBlock != null) {
iterator.remove();
blockQueue.map.remove(currentBlock.getHeader().getHash());
currentBlock = null;
}
}

@Override
public void forEachRemaining(Consumer<? super StoredBlock> consumer) {
iterator.forEachRemaining(consumer);
}
}

@Override @Nonnull
public Iterator<StoredBlock> iterator() {
return new BlockQueueIterator(this);
}

@Override
public void forEach(Consumer<? super StoredBlock> consumer) {
queue.forEach(consumer);
}

@Override @Nonnull
public Object[] toArray() {
return queue.toArray();
}

@Override @Nonnull
public <T> T[] toArray(T[] ts) {
return queue.toArray(ts);
}

@Override
public boolean add(StoredBlock block) {
boolean result = queue.add(block);
result = result && map.put(block.getHeader().getHash(), block) == null;
return result;
}

// the following methods are not currently implemented
@Override
public boolean remove(Object o) {
return false;
}

@Override
public boolean containsAll(@Nonnull Collection<?> collection) {
return false;
}

@Override
public boolean addAll(@Nonnull Collection<? extends StoredBlock> collection) {
queue.addAll(collection);
collection.forEach(block -> map.put(block.getHeader().getHash(), block));
return false;
}

@Override
public boolean removeAll(@Nonnull Collection<?> collection) {
return false;
}

@Override
public boolean removeIf(Predicate<? super StoredBlock> predicate) {
return Collection.super.removeIf(predicate);
}

@Override
public boolean retainAll(@Nonnull Collection<?> collection) {
return false;
}
}
13 changes: 10 additions & 3 deletions core/src/main/java/org/bitcoinj/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void initDash(boolean liteMode, boolean allowInstantX, @Nullable EnumSet<

public void setMasternodeListManager(SimplifiedMasternodeListManager masternodeListManager) {
this.masternodeListManager = masternodeListManager;
masternodeListManager.setBlockChain(blockChain, headerChain, peerGroup, quorumManager, quorumSnapshotManager);
masternodeListManager.setBlockChain(blockChain, headerChain, peerGroup, quorumManager, quorumSnapshotManager, chainLockHandler);
}

public void closeDash() {
Expand Down Expand Up @@ -405,10 +405,17 @@ public void setPeerGroupAndBlockChain(PeerGroup peerGroup, AbstractBlockChain bl
if (initializedObjects) {
sporkManager.setBlockChain(blockChain, peerGroup);
masternodeSync.setBlockChain(blockChain, netFullfilledRequestManager);
masternodeListManager.setBlockChain(blockChain, peerGroup != null ? peerGroup.headerChain : null, peerGroup, quorumManager, quorumSnapshotManager);
masternodeListManager.setBlockChain(
blockChain,
peerGroup != null ? peerGroup.headerChain : null,
peerGroup,
quorumManager,
quorumSnapshotManager,
chainLockHandler
);
instantSendManager.setBlockChain(blockChain, peerGroup);
signingManager.setBlockChain(blockChain, headerChain);
chainLockHandler.setBlockChain(blockChain);
chainLockHandler.setBlockChain(blockChain, headerChain);
blockChain.setChainLocksHandler(chainLockHandler);
quorumManager.setBlockChain(blockChain);
updatedChainHead(blockChain.getChainHead());
Expand Down
35 changes: 30 additions & 5 deletions core/src/main/java/org/bitcoinj/core/NetworkParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ public abstract class NetworkParameters {

/** Used to check for v19 upgrade */
protected int v19BlockHeight = Integer.MAX_VALUE;
/** Used to check for v20 upgrade */
protected int v20BlockHeight = Integer.MAX_VALUE;

protected boolean isDIP24Only = false;

/**
* See getId(). This may be null for old deserialized wallets. In that case we derive it heuristically
Expand Down Expand Up @@ -584,6 +585,11 @@ public final MessageSerializer getDefaultSerializer() {
*/
public abstract BitcoinSerializer getSerializer(boolean parseRetain);

/**
* Construct and return a custom serializer that parses according to given protocol version.
*/
public abstract BitcoinSerializer getSerializer(boolean parseRetain, int protocolVersion);

/**
* The number of blocks in the last {@link #getMajorityWindow()} blocks
* at which to trigger a notice to the user to upgrade their client, where
Expand Down Expand Up @@ -678,7 +684,9 @@ public static enum ProtocolVersion {
COINJOIN_PROTX_HASH(70226),
DMN_TYPE(70227),
SMNLE_VERSIONED(70228),
CURRENT(70228);
MNLISTDIFF_VERSION_ORDER(70229),
MNLISTDIFF_CHAINLOCKS(70230),
CURRENT(70230); //testnet is still 70228

private final int bitcoinProtocol;

Expand Down Expand Up @@ -771,6 +779,7 @@ public boolean isDeterministicMasternodesEnabled() {
protected LLMQParameters.LLMQType llmqTypePlatform;
protected LLMQParameters.LLMQType llmqTypeDIP0024InstantSend;
protected LLMQParameters.LLMQType llmqTypeMnhf;
protected LLMQParameters.LLMQType llmqTypeAssetLocks;

public HashMap<LLMQParameters.LLMQType, LLMQParameters> getLlmqs() {
return llmqs;
Expand Down Expand Up @@ -835,9 +844,21 @@ public void setV19Active(int height) {
}
}

@Deprecated
public boolean isDIP24Only() {
return isDIP24Only;
public boolean isV20Active(StoredBlock block) {
return block.getHeight() >= v20BlockHeight;
}

public boolean isV20Active(int height) {
return height >= v20BlockHeight;
}

public int getV20BlockHeight() {
return v20BlockHeight;
}
public void setV20Active(int height) {
if (v20BlockHeight == Integer.MAX_VALUE) {
v20BlockHeight = height;
}
}

protected int basicBLSSchemeActivationHeight = Integer.MAX_VALUE;
Expand All @@ -864,6 +885,10 @@ public int getCoinType() {
return new String[0];
}

public String [] getDefaultHPMasternodeList() {
return new String[0];
}

protected List<Sha256Hash> assumeValidQuorums = new ArrayList<>();

public List<Sha256Hash> getAssumeValidQuorums() {
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ private void processVersionMessage(VersionMessage peerVersionMessage) throws Pro
if (peerVersionMessage.bestHeight < 0)
// In this case, it's a protocol violation.
throw new ProtocolException("Peer reports invalid best height: " + peerVersionMessage.bestHeight);
// set the serializer based on the protocol version
setMessageSerializer(params.getSerializer(false, vPeerVersionMessage.clientVersion));

// Now it's our turn ...
// Send an ACK message stating we accept the peers protocol version.
sendMessage(new VersionAck());
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,10 @@ public void triggerMnListDownloadComplete() {
queueMasternodeListDownloadedListeners(MasternodeListDownloadedListener.Stage.Complete, null);
}

public void addMnListDownloadCompleteListener(Runnable listener, Executor executor) {
mnListDownloadedFuture.addListener(listener, executor);
}

FutureCallback<Boolean> headersDownloadedCallback;
FutureCallback<Integer> mnListDownloadedCallback;
FutureCallback<Boolean> preBlocksDownloadCallback;
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/bitcoinj/core/PeerSocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public abstract class PeerSocketHandler extends AbstractTimeoutHandler implements StreamConnection {
private static final Logger log = LoggerFactory.getLogger(PeerSocketHandler.class);

private final MessageSerializer serializer;
private MessageSerializer serializer;
protected PeerAddress peerAddress;
// If we close() before we know our writeTarget, set this to true to call writeTarget.closeConnection() right away.
private boolean closePending = false;
Expand Down Expand Up @@ -242,4 +242,8 @@ private void exceptionCaught(Exception e) {

close();
}

protected void setMessageSerializer(MessageSerializer messageSerializer) {
this.serializer = messageSerializer;
}
}
12 changes: 12 additions & 0 deletions core/src/main/java/org/bitcoinj/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public enum Type {
TRANSACTION_PROVIDER_UPDATE_REVOKE(4),
TRANSACTION_COINBASE(5),
TRANSACTION_QUORUM_COMMITMENT(6),
TRANSACTION_ASSET_LOCK(8),
TRANSACTION_ASSET_UNLOCK(9),
TRANSACTION_TYPE_MAX(10),
TRANSACTION_UNKNOWN(1024);

final int value;
Expand Down Expand Up @@ -1704,7 +1707,15 @@ protected void setExtraPayloadObject() {
case TRANSACTION_QUORUM_COMMITMENT:
extraPayloadObject = new FinalCommitmentTxPayload(params, this);
break;
case TRANSACTION_ASSET_LOCK:
extraPayloadObject = new AssetLockPayload(params, this);
break;
case TRANSACTION_ASSET_UNLOCK:
extraPayloadObject = new AssetUnlockPayload(params, this);
break;
}
if (extraPayloadObject != null)
extraPayloadObject.setParent(this);
}

/* returns false if inputs > 4 or there are less than the required confirmations */
Expand All @@ -1723,6 +1734,7 @@ public boolean isSimple() {
public boolean requiresInputs() {
switch (getType()) {
case TRANSACTION_QUORUM_COMMITMENT:
case TRANSACTION_ASSET_UNLOCK:
return false;
default:
return true;
Expand Down
Loading

0 comments on commit 1f64d0e

Please sign in to comment.