使用 bitcoinj 获取原始块
Get raw block with bitcoinj
我正在尝试使用 bitcoinj 获取原始块。下载时我使用 Block.bitcoinSerialize() 来获取每个块的字节数,但不包括交易。我怎样才能得到完整的原始块?
我找到了一个临时解决方案:
BlockStore blockStore = new MyCustomBlockStore(NETWORK_PARAMETERS);
blockStore.getChainHead();
blockChain = new BlockChain(NETWORK_PARAMETERS, blockStore);
PeerGroup peerGroup = new PeerGroup(NETWORK_PARAMETERS, blockChain);
peerGroup.setDownloadTxDependencies(1000);
peerGroup.setBloomFilteringEnabled(false);
peerGroup.addPeerDiscovery(new PeerDiscovery() {
private final PeerDiscovery normalPeerDiscovery = new DnsDiscovery(NETWORK_PARAMETERS);
@Override
public InetSocketAddress[] getPeers(long services, long timeoutValue, TimeUnit timeoutUnit) throws PeerDiscoveryException {
final List<InetSocketAddress> peers = new LinkedList<InetSocketAddress>();
peers.addAll(Arrays.asList(normalPeerDiscovery.getPeers(services, timeoutValue, timeoutUnit)));
InetSocketAddress[] isas = new InetSocketAddress[0];
return peers.toArray(isas);
}
@Override
public void shutdown() {
normalPeerDiscovery.shutdown();
}
});
peerGroup.startAsync();
peerGroup.startBlockChainDownload(new PeerDataEventListener() {
public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int i) {
List<Transaction> transactionsList = block.getTransactions();
int transactions = transactionsList == null ? 0 : transactionsList.size();
long height = peer.getBestHeight() - i;
//If the block contains transactions, it is likely to be complete.
Log.e(TAG, "Downloaded block " + height + " with " + transactions + " transactions");
blockUpdate(block);
}
private void blockUpdate(Block fBlock) throws IOException {
//TODO: Update blockchain database with the full block.
}
public void onChainDownloadStarted(Peer peer, int i) {
Log.i(TAG, "Started to download chain on peer " + peer);
}
@Nullable
public List<Message> getData(Peer peer, GetDataMessage getDataMessage) {
//Log.i(TAG, "getData from " + peer);
return null;
}
public Message onPreMessageReceived(Peer peer, Message message) {
//Log.i(TAG, "onPreMessageReceived (" + message.getClass().getSimpleName() + ") from " + peer);
return message;
}
});
在我的例子中,我创建了一个 BlockStore,它将块存储在 SQLite3 数据库中。当我得到完整的块时,我用完整的信息(交易、原始块等...)覆盖块寄存器
我正在尝试使用 bitcoinj 获取原始块。下载时我使用 Block.bitcoinSerialize() 来获取每个块的字节数,但不包括交易。我怎样才能得到完整的原始块?
我找到了一个临时解决方案:
BlockStore blockStore = new MyCustomBlockStore(NETWORK_PARAMETERS);
blockStore.getChainHead();
blockChain = new BlockChain(NETWORK_PARAMETERS, blockStore);
PeerGroup peerGroup = new PeerGroup(NETWORK_PARAMETERS, blockChain);
peerGroup.setDownloadTxDependencies(1000);
peerGroup.setBloomFilteringEnabled(false);
peerGroup.addPeerDiscovery(new PeerDiscovery() {
private final PeerDiscovery normalPeerDiscovery = new DnsDiscovery(NETWORK_PARAMETERS);
@Override
public InetSocketAddress[] getPeers(long services, long timeoutValue, TimeUnit timeoutUnit) throws PeerDiscoveryException {
final List<InetSocketAddress> peers = new LinkedList<InetSocketAddress>();
peers.addAll(Arrays.asList(normalPeerDiscovery.getPeers(services, timeoutValue, timeoutUnit)));
InetSocketAddress[] isas = new InetSocketAddress[0];
return peers.toArray(isas);
}
@Override
public void shutdown() {
normalPeerDiscovery.shutdown();
}
});
peerGroup.startAsync();
peerGroup.startBlockChainDownload(new PeerDataEventListener() {
public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int i) {
List<Transaction> transactionsList = block.getTransactions();
int transactions = transactionsList == null ? 0 : transactionsList.size();
long height = peer.getBestHeight() - i;
//If the block contains transactions, it is likely to be complete.
Log.e(TAG, "Downloaded block " + height + " with " + transactions + " transactions");
blockUpdate(block);
}
private void blockUpdate(Block fBlock) throws IOException {
//TODO: Update blockchain database with the full block.
}
public void onChainDownloadStarted(Peer peer, int i) {
Log.i(TAG, "Started to download chain on peer " + peer);
}
@Nullable
public List<Message> getData(Peer peer, GetDataMessage getDataMessage) {
//Log.i(TAG, "getData from " + peer);
return null;
}
public Message onPreMessageReceived(Peer peer, Message message) {
//Log.i(TAG, "onPreMessageReceived (" + message.getClass().getSimpleName() + ") from " + peer);
return message;
}
});
在我的例子中,我创建了一个 BlockStore,它将块存储在 SQLite3 数据库中。当我得到完整的块时,我用完整的信息(交易、原始块等...)覆盖块寄存器