Created
February 3, 2020 21:03
-
-
Save oscarguindzberg/683060f793b98a1a84abac87158c738f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.bitcoinj.tools.playground; | |
import org.bitcoinj.core.*; | |
import org.bitcoinj.kits.WalletAppKit; | |
import org.bitcoinj.params.RegTestParams; | |
import java.io.File; | |
public class FullBlockOnSpv { | |
public static void main(String[] args) { | |
final Context btcContext = new Context(RegTestParams.get()); | |
WalletAppKit kit = new WalletAppKit(btcContext.getParams(), new File("."), "FullBlockOnSpv"); | |
kit.startAsync(); | |
kit.awaitRunning(); | |
kit.peerGroup().addBlocksDownloadedEventListener((peer, block, filteredBlock, blocksLeft) -> { | |
Context.propagate(btcContext); | |
// filteredBlock may be null if we are downloading just headers before fastCatchupTimeSecs | |
// or we are receiving full blocks | |
if (filteredBlock != null) { | |
System.out.println("Received filteredBlock " + filteredBlock.getHash()); | |
boolean isBlockRelevant = false; | |
for (Transaction tx : filteredBlock.getAssociatedTransactions().values()) { | |
if (kit.wallet().isTransactionRelevant(tx)) { | |
isBlockRelevant = true; | |
} | |
} | |
System.out.println("isBlockRelevant = " + isBlockRelevant); | |
if (isBlockRelevant) { | |
// block is sending a tx that we care about, request the full block in order to receive witnesses. | |
GetDataMessage getDataMessage = new GetDataMessage(btcContext.getParams()); | |
getDataMessage.addBlock(filteredBlock.getHash(), true); | |
kit.peerGroup().getDownloadPeer().sendMessage(getDataMessage); | |
} | |
// Don't invoke BtcToRskClient.onBlock() with a filtered block | |
} else { | |
System.out.println("Received block " + block.getHash()); | |
if (block.getTransactions()!=null) { | |
System.out.println("block = " + block); | |
// Here, invoke BtcToRskClient.onBlock() with a full Block. | |
} | |
} | |
}); | |
System.out.println("kit.wallet().currentReceiveAddress() = " + kit.wallet().currentReceiveAddress()); | |
kit.awaitTerminated(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment