-1

I'm trying to access the transactions contained in the blocks I have downloaded but none of the blocks have any transactions; the size of every Transaction list returned is zero. Am I conceptually misunderstanding something about the bitcoin blockchain or is there something wrong with my code?

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
Connor
  • 29
  • 3

2 Answers2

-1

you need to use FullPrunedBlockChain, the blockchain only supports SPV.

See https://bitcoinj.github.io/full-verification

-1

It depends on how you downloaded those Blocks. If you downloaded them for example via the BlocksDownloadedEventListener then you only received the Blockheaders which do not contain the Transactions. If you want to get the Transactions too you can use Peer.getBlock(blockHash) to request a download of the full Block from that Peer, which will also contain the Transactions and information related to them. (i.e. Blockreward)

Also you would need to use another type of BlockStore for persisting your Blocks as the SPVBlockstore (which is Standard for WalletAppKit) only saves Blockheaders (so no Transactions).

You can find all types of Blockstores here, so you can choose what suits you best, but always read the description on what they are saving, for not running into that problem again.

yuri206
  • 79
  • 5