我正在尝试接收有关比特币区块链中新区块的通知。我正在使用此代码,但这会打印从 2010 年左右开始的数百个块。
import org.bitcoinj.core.*;
import org.bitcoinj.net.discovery.DnsDiscovery;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.MemoryBlockStore;
public class BlockChainMonitorTest {
BlockChainMonitorTest() throws Exception {
NetworkParameters params = MainNetParams.get();
BlockStore bs = new MemoryBlockStore(params);
BlockChain bc = new BlockChain(params, bs);
PeerGroup peerGroup = new PeerGroup(params, bc);
peerGroup.setUserAgent("PeerMonitor", "1.0");
peerGroup.setMaxConnections(4);
peerGroup.addPeerDiscovery(new DnsDiscovery(params));
bc.addNewBestBlockListener((StoredBlock block) -> {
System.out.println("addNewBestBlockListener");
System.out.println(block);
});
//peerGroup.setFastCatchupTimeSecs(1483228800); // 2017-01-01
peerGroup.start();
peerGroup.waitForPeers(4).get();
Thread.sleep(1000 * 60 * 30);
peerGroup.stop();
}
public static void main(String[] args) throws Exception {
new BlockChainMonitorTest();
}
}
我只想听新的块。有任何想法吗 ?
我试过setFastCatchupTimeSecs
了,但似乎没有收到任何事件。