Skip to content

Commit

Permalink
Merge branch 'dsproofs'
Browse files Browse the repository at this point in the history
  • Loading branch information
pokkst committed Oct 28, 2021
2 parents 2fe529d + 38f1df3 commit c1f136c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 29 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class BitcoinSerializer extends MessageSerializer {
names.put(GetUTXOsMessage.class, "getutxos");
names.put(UTXOsMessage.class, "utxos");
names.put(SendHeadersMessage.class, "sendheaders");
names.put(DSProofMessage.class, "dsproof-beta");
}

/**
Expand Down Expand Up @@ -257,6 +258,8 @@ private Message makeMessage(String command, int length, byte[] payloadBytes, byt
return new SendHeadersMessage(params, payloadBytes);
} else if (command.equals("feefilter")) {
return new FeeFilterMessage(params, payloadBytes);
} else if(command.equals("dsproof-beta")) {
return new DSProofMessage(params, payloadBytes);
} else {
log.warn("No support for deserializing message with name {}", command);
return new UnknownMessage(params, command, payloadBytes);
Expand Down
49 changes: 49 additions & 0 deletions core/src/main/java/org/bitcoinj/core/DSProofMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2017 Anton Kumaigorodski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.bitcoinj.core;

import java.util.Arrays;

/**
* <p>
* A new message, "sendheaders", which indicates that a node prefers to receive new block announcements via a "headers"
* message rather than an "inv".
* </p>
*
* <p>
* See <a href="https://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki">BIP 130</a>.
* </p>
*/
public class DSProofMessage extends EmptyMessage {
private Sha256Hash id;
private byte[] txPrevHash;
private byte[] txPrevIndex;

public DSProofMessage() {
}

// this is needed by the BitcoinSerializer
public DSProofMessage(NetworkParameters params, byte[] payload) {
this.id = Sha256Hash.twiceOf(payload);
this.txPrevHash = Arrays.copyOfRange(payload, 0, 32);
this.txPrevIndex = Arrays.copyOfRange(payload, 32, 36);
}

public Sha256Hash getId() {
return id;
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/org/bitcoinj/core/GetDataMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void addFilteredBlock(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.FILTERED_BLOCK, hash));
}

public void addDsProof(Sha256Hash hash) {
addItem(new InventoryItem(InventoryItem.Type.DSPROOF, hash));
}

public Sha256Hash getHashOf(int i) {
return getItems().get(i).hash;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/InventoryItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class InventoryItem {
public enum Type {
ERROR(0x0), TRANSACTION(0x1), BLOCK(0x2),
// BIP37 extension:
FILTERED_BLOCK(0x3);
FILTERED_BLOCK(0x3), DSPROOF(0x94a0);

public final int code;

Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public class Peer extends PeerSocketHandler {
// to keep it pinned to the root set if they care about this data.
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final HashSet<TransactionConfidence> pendingTxDownloads = new HashSet<>();

private final HashSet<Sha256Hash> pendingDsProofDownloads = new HashSet<>();

private static final int PENDING_TX_DOWNLOADS_LIMIT = 100;
// The lowest version number we're willing to accept. Lower than this will result in an immediate disconnect.
private volatile int vMinProtocolVersion;
Expand Down Expand Up @@ -516,6 +519,8 @@ protected void processMessage(Message m) throws Exception {
log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m);
} else if (m instanceof SendHeadersMessage) {
// We ignore this message, because we don't announce new blocks.
} else if(m instanceof DSProofMessage) {
processDsProof((DSProofMessage) m);
} else {
log.warn("{}: Received unhandled message: {}", this, m);
}
Expand Down Expand Up @@ -763,6 +768,10 @@ protected void processGetData(GetDataMessage getdata) {
}
}

protected void processDsProof(DSProofMessage dsProof) {
// TODO at a later date
}

protected void processTransaction(final Transaction tx) throws VerificationException {
// Check a few basic syntax issues to ensure the received TX isn't nonsense.
tx.verify();
Expand Down Expand Up @@ -1183,6 +1192,7 @@ protected void processInv(InventoryMessage inv) {
// Separate out the blocks and transactions, we'll handle them differently
List<InventoryItem> transactions = new LinkedList<>();
List<InventoryItem> blocks = new LinkedList<>();
List<InventoryItem> dsproofs = new LinkedList<>();

for (InventoryItem item : items) {
switch (item.type) {
Expand All @@ -1192,6 +1202,9 @@ protected void processInv(InventoryMessage inv) {
case BLOCK:
blocks.add(item);
break;
case DSPROOF:
dsproofs.add(item);
break;
default:
throw new IllegalStateException("Not implemented: " + item.type);
}
Expand All @@ -1215,6 +1228,13 @@ protected void processInv(InventoryMessage inv) {

GetDataMessage getdata = new GetDataMessage(params);

Iterator<InventoryItem> dsproofIterator = dsproofs.iterator();
while (dsproofIterator.hasNext()) {
InventoryItem item = dsproofIterator.next();
getdata.addDsProof(item.hash);
pendingDsProofDownloads.add(item.hash);
}

Iterator<InventoryItem> it = transactions.iterator();
while (it.hasNext()) {
InventoryItem item = it.next();
Expand Down
28 changes: 0 additions & 28 deletions wallettemplate/src/main/java/wallettemplate/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,6 @@ protected void onSetupCompleted() {
.setUserAgent(APP_NAME, "1.0");
if (seed != null)
bitcoin.restoreWalletFromSeed(seed);

new Thread() {
@Override
public void run() {
while(true) {
if(bitcoin.isRunning()) {
//System.out.println(bitcoin.wallet().getRecentTransactions(0, false));
bitcoin.recalculateSlpUtxos();
bitcoin.recalculateNftUtxos();

System.out.println("SLPs::");
for(SlpTokenBalance slpTokenBalance : bitcoin.getSlpBalances()) {
System.out.println(bitcoin.getSlpToken(slpTokenBalance.getTokenId()).toString() + ", " + slpTokenBalance.getBalance());
}

System.out.println("NFTs::");
for(SlpTokenBalance nftSlpTokenBalance : bitcoin.getNftBalances()) {
System.out.println(bitcoin.getNft(nftSlpTokenBalance.getTokenId()).toString() + ", " + nftSlpTokenBalance.getBalance());
}
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}.start();
}

private Node stopClickPane = new Pane();
Expand Down

0 comments on commit c1f136c

Please sign in to comment.