Skip to content

Commit

Permalink
Support P2P SegWit
Browse files Browse the repository at this point in the history
  • Loading branch information
jprupp authored and Andreas Schildbach committed Aug 14, 2017
1 parent fafe6ff commit af90a56
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/org/bitcoinj/core/GetDataMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,26 @@ public void addTransaction(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.Transaction, hash));
}

public void addWitnessTransaction(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.WitnessTransaction, hash));
}

public void addBlock(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.Block, hash));
}

public void addWitnessBlock(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.WitnessBlock, hash));
}

public void addFilteredBlock(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.FilteredBlock, hash));
}

public void addFilteredWitnessBlock(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.FilteredWitnessBlock, hash));
}

public Sha256Hash getHashOf(int i) {
return getItems().get(i).hash;
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/bitcoinj/core/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,12 @@ private void readObject(java.io.ObjectInputStream in)
this.serializer = params.getDefaultSerializer();
}
}

public void disableWitness() {
transactionOptions &= ~TransactionOptions.WITNESS;
}

public void enableWitness() {
transactionOptions |= TransactionOptions.WITNESS;
}
}
17 changes: 14 additions & 3 deletions core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

// TODO: test peer with segwit

package org.bitcoinj.core;

import com.google.common.base.*;
Expand Down Expand Up @@ -911,7 +913,10 @@ protected ListenableFuture<Object> downloadDependenciesInternal(final int maxDep
if (needToRequest.size() > 1)
log.info("{}: Requesting {} transactions for depth {} dep resolution", getAddress(), needToRequest.size(), depth + 1);
for (Sha256Hash hash : needToRequest) {
getdata.addTransaction(hash);
if (vPeerVersionMessage.isWitnessSupported())
getdata.addWitnessTransaction(hash);
else
getdata.addTransaction(hash);
GetDataRequest req = new GetDataRequest(hash, SettableFuture.create());
futures.add(req.future);
getDataFutures.add(req);
Expand Down Expand Up @@ -1301,7 +1306,10 @@ public ListenableFuture<Block> getBlock(Sha256Hash blockHash) {
// This does not need to be locked.
log.info("Request to fetch block {}", blockHash);
GetDataMessage getdata = new GetDataMessage(params);
getdata.addBlock(blockHash);
if (vPeerVersionMessage.isWitnessSupported())
getdata.addWitnessBlock(blockHash);
else
getdata.addBlock(blockHash);
return sendSingleGetData(getdata);
}

Expand All @@ -1319,7 +1327,10 @@ public ListenableFuture<Transaction> getPeerMempoolTransaction(Sha256Hash hash)
// TODO: Unit test this method.
log.info("Request to fetch peer mempool tx {}", hash);
GetDataMessage getdata = new GetDataMessage(params);
getdata.addTransaction(hash);
if (vPeerVersionMessage.isWitnessSupported())
getdata.addWitnessTransaction(hash);
else
getdata.addTransaction(hash);
return sendSingleGetData(getdata);
}

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 @@ -550,6 +550,10 @@ private List<Message> handleGetData(GetDataMessage m) {
for (Wallet w : wallets) {
Transaction tx = w.getTransaction(item.hash);
if (tx == null) continue;
if (item.type == InventoryItem.Type.WitnessTransaction)
tx.enableWitness();
else
tx.disableWitness();
transactions.add(tx);
it.remove();
break;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/VersionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public VersionMessage(NetworkParameters params, byte[] payload) throws ProtocolE
public VersionMessage(NetworkParameters params, int newBestHeight) {
super(params);
clientVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT);
localServices = 0;
localServices = NODE_WITNESS;
time = System.currentTimeMillis() / 1000;
// Note that the Bitcoin Core doesn't do anything with these, and finding out your own external IP address
// is kind of tricky anyway, so we just put nonsense here for now.
Expand Down

0 comments on commit af90a56

Please sign in to comment.