Skip to content

Commit

Permalink
Merge pull request #257 from dashpay/feat-core-21-updates
Browse files Browse the repository at this point in the history
feat: other core 21 updates
  • Loading branch information
HashEngineering authored Aug 2, 2024
2 parents 9840285 + ea5dfa5 commit cde88a3
Show file tree
Hide file tree
Showing 41 changed files with 1,840 additions and 1,105 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ protected void processInv(InventoryMessage inv) {
}

// The New InstantSendLock (ISLOCK)
if(context.instantSendManager != null && context.instantSendManager.isNewInstantSendEnabled() &&
if(context.instantSendManager != null && context.instantSendManager.isInstantSendEnabled() &&
context.masternodeSync != null && context.masternodeSync.hasSyncFlag(MasternodeSync.SYNC_FLAGS.SYNC_INSTANTSENDLOCKS)) {
it = instantSendLocks.iterator();
while (it.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.io.IOException;
import java.io.OutputStream;

import java.util.Arrays;

public abstract class BLSAbstractLazyObject extends Message {

Expand Down Expand Up @@ -59,4 +59,11 @@ public void bitcoinSerialize(OutputStream stream, boolean legacy) throws IOExcep
public boolean isInitialized() {
return initialized;
}

public abstract byte[] getBuffer();

@Override
public int hashCode() {
return Arrays.hashCode(getBuffer());
}
}
6 changes: 5 additions & 1 deletion core/src/main/java/org/bitcoinj/crypto/BLSLazyPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public boolean equals(Object o) {
if (that.isInitialized())
return Objects.equals(publicKey, that.publicKey);
else
return Objects.equals(publicKey.getBuffer(), that.buffer);
return Arrays.equals(publicKey.getBuffer(), that.buffer);
} else {
if (that.isInitialized())
return Arrays.equals(buffer, that.publicKey.getBuffer());
Expand All @@ -157,4 +157,8 @@ public boolean isValid() {
return buffer != null;
}
}

public byte [] getBuffer() {
return buffer != null ? buffer : publicKey.getBuffer();
}
}
27 changes: 24 additions & 3 deletions core/src/main/java/org/bitcoinj/crypto/BLSLazySignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.concurrent.locks.ReentrantLock;

public class BLSLazySignature extends BLSAbstractLazyObject {
ReentrantLock lock = Threading.lock("BLSLazySignature");
Logger log = LoggerFactory.getLogger(BLSLazySignature.class);
BLSSignature signature;
private static final Logger log = LoggerFactory.getLogger(BLSLazySignature.class);
private BLSSignature signature;

@Deprecated
public BLSLazySignature() {
Expand Down Expand Up @@ -80,6 +81,7 @@ protected void bitcoinSerializeToStream(OutputStream stream, boolean legacy) thr
}
}

@Deprecated
public BLSLazySignature assign(BLSLazySignature blsLazySignature) {
lock.lock();
try {
Expand Down Expand Up @@ -135,7 +137,11 @@ public BLSSignature getSignature() {

@Override
public String toString() {
return initialized ? signature.toString() : (buffer == null ? invalidSignature.toString() : Utils.HEX.encode(buffer));
if (initialized) {
return signature.toString();
} else {
return buffer == null ? invalidSignature.toString() : Utils.HEX.encode(buffer);
}
}

public boolean isValid() {
Expand All @@ -145,4 +151,19 @@ public boolean isValid() {
return buffer != null;
}
}

public byte [] getBuffer() {
return buffer != null ? buffer : signature.getBuffer();
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BLSLazySignature)) return false;

BLSLazySignature that = (BLSLazySignature) o;
byte[] thisBuffer = getBuffer();
byte[] thatBuffer = that.getBuffer();
return legacy == that.legacy && Arrays.equals(thisBuffer, thatBuffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,21 @@
public abstract class AbstractDiffMessage extends Message {
private static final Logger log = LoggerFactory.getLogger(AbstractDiffMessage.class);

public AbstractDiffMessage(NetworkParameters params) {
protected AbstractDiffMessage(NetworkParameters params) {
super(params);
}

public AbstractDiffMessage(NetworkParameters params, byte [] payload, int offset, int protocolVersion) {
protected AbstractDiffMessage(NetworkParameters params, byte [] payload, int offset, int protocolVersion) {
super(params, payload, offset, protocolVersion);
}

protected abstract String getShortName();

public void dump(long startHeight, long endHeight) {
if (!Utils.isAndroidRuntime() && Context.get().isDebugMode()) {
try {
File dumpFile = new File(getShortName() + "-" + params.getNetworkName() + "-" + startHeight + "-" + endHeight + ".dat");
OutputStream stream = new FileOutputStream(dumpFile);
File dumpFile = new File(getShortName() + "-" + params.getNetworkName() + "-" + startHeight + "-" + endHeight + ".dat");
try (OutputStream stream = new FileOutputStream(dumpFile)) {
stream.write(bitcoinSerialize());
stream.close();
log.info("dump successful");
} catch (FileNotFoundException x) {
log.warn("could not dump {} - file not found.", getShortName(), x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

public abstract class AbstractQuorumRequest extends Message {

public AbstractQuorumRequest() {
protected AbstractQuorumRequest() {
super();
}

public AbstractQuorumRequest(NetworkParameters params) {
protected AbstractQuorumRequest(NetworkParameters params) {
super(params);
}

public AbstractQuorumRequest(NetworkParameters params, byte [] payload, int offset) {
protected AbstractQuorumRequest(NetworkParameters params, byte [] payload, int offset) {
super(params, payload, offset);
}

Expand Down
26 changes: 17 additions & 9 deletions core/src/main/java/org/bitcoinj/evolution/AbstractQuorumState.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.bitcoinj.quorums.SimplifiedQuorumList;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.utils.ContextPropagatingThreadFactory;
import org.bitcoinj.utils.DebugReentrantLock;
import org.bitcoinj.utils.Threading;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -89,7 +90,8 @@ public abstract class AbstractQuorumState<Request extends AbstractQuorumRequest,
public static final int MIN_CACHE_SIZE = 1;

private static final Logger log = LoggerFactory.getLogger(AbstractQuorumState.class);
protected final ReentrantLock lock = Threading.lock("AbstractQuorumState");
// TODO: Revert to ReentrantLock with 21.0.1
protected final DebugReentrantLock lock = Threading.debugLock("AbstractQuorumState");

Context context;
DualBlockChain blockChain;
Expand Down Expand Up @@ -119,19 +121,19 @@ boolean initChainTipSyncComplete() {
String bootstrapFilePath = null;
InputStream bootstrapStream = null;
int bootStrapFileFormat = 0;
public SettableFuture<Boolean> bootStrapLoaded;
private SettableFuture<Boolean> bootStrapLoaded;

boolean isLoadingBootstrap = false;
protected static Random random = new Random();

public AbstractQuorumState(Context context) {
protected AbstractQuorumState(Context context) {
super(context.getParams());
this.context = context;
initializeOnce();
initialize();
}

public AbstractQuorumState(NetworkParameters params, byte[] payload, int offset, int protocolVersion) {
protected AbstractQuorumState(NetworkParameters params, byte[] payload, int offset, int protocolVersion) {
super(params, payload, offset, protocolVersion);
initialize();
}
Expand Down Expand Up @@ -200,6 +202,10 @@ public boolean reachedMaxFailedAttempts() {
return failedAttempts > MAX_ATTEMPTS;
}

public SettableFuture<Boolean> getBootStrapLoadedFuture() {
return bootStrapLoaded;
}

abstract int getBlockHeightOffset();

public abstract int getUpdateInterval();
Expand All @@ -218,6 +224,7 @@ public abstract void processDiff(@Nullable Peer peer, DiffMessage difference,
public abstract void requestUpdate(Peer peer, StoredBlock block);

public void retryLastUpdate(Peer peer) {
log.info("retryLastUpdate: {}", lastRequest.getRequestMessage());
if (peer != null) {
peer.sendMessage(lastRequest.getRequestMessage());
} else {
Expand All @@ -231,13 +238,13 @@ public void retryLastUpdate(Peer peer) {

public abstract SimplifiedMasternodeList getMasternodeListAtTip();

public abstract LinkedHashMap<Sha256Hash, SimplifiedMasternodeList> getMasternodeListCache();
public abstract Map<Sha256Hash, SimplifiedMasternodeList> getMasternodeListCache();

public abstract LinkedHashMap<Sha256Hash, SimplifiedQuorumList> getQuorumsCache();
public abstract Map<Sha256Hash, SimplifiedQuorumList> getQuorumsCache();

public abstract SimplifiedQuorumList getQuorumListAtTip();

public abstract ArrayList<Masternode> getAllQuorumMembers(LLMQParameters.LLMQType llmqType, Sha256Hash blockHash);
public abstract List<Masternode> getAllQuorumMembers(LLMQParameters.LLMQType llmqType, Sha256Hash blockHash);

public void resetMNList() {
resetMNList(false, true);
Expand Down Expand Up @@ -405,7 +412,8 @@ boolean requestNextMNListDiff() {
log.info("sending {} from {} to {}; \n From {}\n To {}", lastRequest.request.getClass().getSimpleName(),
getMasternodeListAtTip().getHeight(), nextBlock.getHeight(), getMasternodeListAtTip().getBlockHash(), nextBlock.getHeader().getHash());
requestUpdate(downloadPeer, nextBlock);
log.info("message = {}", lastRequest.getRequestMessage().toString(blockChain));
// This was causing a dead lock when using toString(DualBlockchain). Use toString() instead.
log.info("message = {}", lastRequest.getRequestMessage());
waitingForMNListDiff = true;
return true;
} else {
Expand Down Expand Up @@ -741,7 +749,7 @@ private void retryLastRequest(Peer peer, Exception e) {
try {
if (isLocked) {
downloadPeer = context.peerGroup.getDownloadPeer();
log.info("{}: lock acquired, obtaining downloadPeer from peerGroup: {}",
log.info("{}: peergroup lock acquired, obtaining downloadPeer from peerGroup: {}",
Thread.currentThread().getName(), downloadPeer);
if (downloadPeer == null) {
chooseRandomDownloadPeer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DeterministicMasternodeList extends Message {
this.blockHash = other.blockHash;
this.height = other.height;

mnMap = new HashMap<Sha256Hash, DeterministicMasternode>(other.mnMap.size());
mnMap = new HashMap<>(other.mnMap.size());
for(Map.Entry<Sha256Hash, DeterministicMasternode> entry : mnMap.entrySet()) {
mnMap.put(entry.getKey(), new DeterministicMasternode(entry.getValue()));
}
Expand All @@ -35,7 +35,7 @@ protected void parse() throws ProtocolException {
blockHash = readHash();
height = (int)readUint32();
int size = (int)readVarInt();
mnMap = new HashMap<Sha256Hash, DeterministicMasternode>(size);
mnMap = new HashMap<>(size);
for(int i = 0; i < size; ++i)
{
Sha256Hash hash = readHash();
Expand All @@ -45,13 +45,13 @@ protected void parse() throws ProtocolException {
}

size = (int)readVarInt();
mnUniquePropertyMap = new HashMap<Sha256Hash, Pair<Sha256Hash, Integer>>(size);
mnUniquePropertyMap = new HashMap<>(size);
for(long i = 0; i < size; ++i)
{
Sha256Hash hash = readHash();
Sha256Hash first = readHash();
int second = (int)readUint32();
mnUniquePropertyMap.put(hash, new Pair<Sha256Hash, Integer>(first, second));
mnUniquePropertyMap.put(hash, new Pair<>(first, second));
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ void deleteUniqueProperty(DeterministicMasternode dmn, T oldValue)
if (p.getSecond() == 1) {
mnUniquePropertyMap.remove(oldHash);
} else {
mnUniquePropertyMap.put(oldHash, new Pair<Sha256Hash, Integer>(dmn.proRegTxHash, p.getSecond() - 1));
mnUniquePropertyMap.put(oldHash, new Pair<>(dmn.proRegTxHash, p.getSecond() - 1));
}
}
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GetSimplifiedMasternodeListDiff extends AbstractQuorumRequest {
Sha256Hash baseBlockHash;
Sha256Hash blockHash;

public static int MESSAGE_SIZE = 64;
public static final int MESSAGE_SIZE = 64;

public GetSimplifiedMasternodeListDiff(Sha256Hash baseBlockHash, Sha256Hash blockHash) {
super();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.bitcoinj.evolution;

public class MasternodeListDiffException extends Exception {
boolean requireReset;
boolean findNewPeer;
private final boolean requireReset;
private final boolean findNewPeer;

boolean sameHeight;
boolean merkleRootMismatch;
private final boolean sameHeight;
private final boolean merkleRootMismatch;
public MasternodeListDiffException(String message, boolean requireReset, boolean findNewPeer, boolean sameHeight, boolean merkleRootMismatch) {
super(message);
this.requireReset = requireReset;
Expand All @@ -25,4 +25,8 @@ public boolean isRequiringNewPeer() {
public boolean hasMerkleRootMismatch() {
return merkleRootMismatch;
}

public boolean isSameHeight() {
return sameHeight;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.bitcoinj.evolution;

import org.bitcoinj.core.*;
import org.bitcoinj.governance.GovernanceObject;

import java.util.ArrayList;
import java.util.List;

public class MasternodeMetaDataManager extends AbstractManager {

Expand All @@ -19,8 +19,8 @@ public void removeGovernanceObject(Sha256Hash governanceObject) {

}

public ArrayList<Sha256Hash> getAndClearDirtyGovernanceObjectHashes() {
return new ArrayList<Sha256Hash>();
public List<Sha256Hash> getAndClearDirtyGovernanceObjectHashes() {
return new ArrayList<>();
}

@Override
Expand Down
Loading

0 comments on commit cde88a3

Please sign in to comment.