Skip to content

Commit

Permalink
Merge pull request #262 from dashpay/bugfix-peergroup-header-sync
Browse files Browse the repository at this point in the history
* fix: masternode group will not do header sync
  • Loading branch information
HashEngineering authored Oct 8, 2024
2 parents ec5a0ce + eed7539 commit 56d9b8b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public MasternodeGroup(NetworkParameters params, @Nullable AbstractBlockChain ch
* @param chain
*/
public MasternodeGroup(Context context, @Nullable AbstractBlockChain chain) {
super(context, chain);
super(context, chain, false);
init();
}

Expand Down
34 changes: 29 additions & 5 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,14 @@ public PeerGroup(Context context) {
this(context, null);
}

/** See {@link #PeerGroup(Context, AbstractBlockChain)} */
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, boolean syncBlockchain) {
this(Context.getOrCreate(params), chain, null, new NioClientManager(), syncBlockchain);
}

/** See {@link #PeerGroup(Context, AbstractBlockChain)} */
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain) {
this(Context.getOrCreate(params), chain, null, new NioClientManager());
this(Context.getOrCreate(params), chain, null, new NioClientManager(), true);
}

/** See {@link #PeerGroup(Context, AbstractBlockChain)} */
Expand All @@ -382,16 +387,32 @@ public PeerGroup(Context context, @Nullable AbstractBlockChain chain) {
this(context, chain, null, new NioClientManager());
}

/**
* Creates a PeerGroup for the given context and chain. Blocks will be passed to the chain as they are broadcast
* and downloaded. This is probably the constructor you want to use.
*/
public PeerGroup(Context context, @Nullable AbstractBlockChain chain, boolean syncsBlockchain) {
this(context, chain, null, new NioClientManager(), syncsBlockchain);
}

/** See {@link #PeerGroup(Context, AbstractBlockChain, AbstractBlockChain, ClientConnectionManager)} */
public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager) {
this(Context.getOrCreate(params), chain, null, connectionManager);
}

/**
* Creates a new PeerGroup allowing you to specify the {@link ClientConnectionManager} which is used to create new
* connections and keep track of existing ones.
* connections and keep track of existing ones. This peer group will initialize headerChain if null
*/
private PeerGroup(Context context, @Nullable AbstractBlockChain chain, @Nullable AbstractBlockChain headerChain, ClientConnectionManager connectionManager) {
this(context, chain, headerChain, connectionManager, true);
}

/**
* Creates a new PeerGroup allowing you to specify the {@link ClientConnectionManager} which is used to create new
* connections and keep track of existing ones.
*/
private PeerGroup(Context context, @Nullable AbstractBlockChain chain, @Nullable AbstractBlockChain headerChain, ClientConnectionManager connectionManager, boolean syncsBlockchain) {
checkNotNull(context);
this.context = context;
this.params = context.getParams();
Expand Down Expand Up @@ -438,15 +459,18 @@ public int compare(PeerAddress a, PeerAddress b) {
vMinRequiredProtocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.MINIMUM);
runningVoteBroadcasts = Collections.synchronizedSet(new HashSet<GovernanceVoteBroadcast>());

//DashSpecific
if (headerChain == null) {
// Dash Specific - if this PeerGroup does not sync the blockchain, then syncsBlockchain should be false
// otherwise, if headerChain is null, then copy the last 100 headers over to it
if (headerChain == null && syncsBlockchain) {
if (context.getSyncFlags().contains(MasternodeSync.SYNC_FLAGS.SYNC_HEADERS_MN_LIST_FIRST)) {
try {
this.headerChain = new BlockChain(params, new MemoryBlockStore(params));
StoredBlock cursor = chain.getChainHead();
while (cursor != null && !cursor.getHeader().equals(params.getGenesisBlock())) {
int headers = 0; // limit headers to 100, enough to sync
while (cursor != null && !cursor.getHeader().equals(params.getGenesisBlock()) && headers < 100) {
this.headerChain.getBlockStore().put(cursor);
cursor = cursor.getPrev(chain.getBlockStore());
headers++;
}
} catch (BlockStoreException x) {
// swallow
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5946,7 +5946,7 @@ public void addReceivingFromFriendKeyChain(FriendKeyChain chain) {
receivingFromFriendsGroup = FriendKeyChainGroup.friendlybuilder(params).build();
}
receivingFromFriendsGroup.addAndActivateHDChain(chain);
saveNow();
saveLater();
} finally {
keyChainGroupLock.unlock();
}
Expand All @@ -5966,7 +5966,7 @@ public void addSendingToFriendKeyChain(FriendKeyChain chain) {
sendingToFriendsGroup = FriendKeyChainGroup.friendlybuilder(params).build();
}
sendingToFriendsGroup.addAndActivateHDChain(chain);
saveNow();
saveLater();
} finally {
keyChainGroupLock.unlock();
}
Expand Down

0 comments on commit 56d9b8b

Please sign in to comment.