我正在尝试访问我下载的块中包含的事务,但没有任何块有任何事务;返回的每个Transaction列表的大小为零。我在概念上误解了比特币区块链的某些内容,或者我的代码有问题吗?
static NetworkParameters params = MainNetParams.get();
static WalletAppKit kit = new WalletAppKit(params, new java.io.File("."), "chain");
/* store_TX() gets Transactions from blocks and stores them in a file */
static protected void store_TX() throws BlockStoreException, FileNotFoundException, UnsupportedEncodingException{
File txf = new File("TX.txt");
PrintWriter hwriter = new PrintWriter("TX.txt", "UTF-8");
BlockChain chain = kit.chain();
BlockStore block_store = chain.getBlockStore();
StoredBlock stored_block = block_store.getChainHead();
// if stored_block.prev() returns null then break otherwise get block transactions
while (stored_block!=null){
Block block = stored_block.getHeader();
List<Transaction> tx_list = block.getTransactions();
if (tx_list != null && tx_list.size() > 0){
hwriter.println(block.getHashAsString());
}
stored_block = stored_block.getPrev(block_store);
}
hwriter.close();
}
public static void main(String[] args){
BriefLogFormatter.init();
synchronized(kit.startAndWait()){
try {
store_TX();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (BlockStoreException e) {
e.printStackTrace();
}
}
} //end main
答案 0 :(得分:0)
你需要使用FullPrunedBlockChain,区块链只支持SPV。
答案 1 :(得分:0)
这取决于您下载这些块的方式。如果您通过BlocksDownloadedEventListener下载了它们,那么您只收到了不包含交易的Blockheaders。如果您也想获得交易,您可以使用Peer.getBlock(blockHash)从该对等方请求下载完整的块,该块也将包含与它们相关的交易和信息。 (即Blockreward)
此外,您需要使用其他类型的BlockStore来保存您的块,因为SPVBlockstore(这是WalletAppKit的标准版)仅保存了Blockheaders(因此没有任何事务)。
您可以找到所有类型的Blockstores here,这样您就可以选择最适合您的地方,但请务必阅读有关保存内容的说明,以免再遇到问题。