Raw block with bitcoinj

0 votes
How can i get a full raw block with bitcoinj?
Aug 28, 2018 in Blockchain by digger
• 26,740 points
636 views

1 answer to this question.

0 votes

This should work:

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;
    }
});
answered Aug 28, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

0 votes
1 answer

Is it that new bitcoins gets created with the every block mined??

Bitcoin mining means validation of a block ...READ MORE

answered Apr 21, 2018 in Blockchain by Perry
• 17,100 points
552 views
0 votes
1 answer

what is the latest block chain development and top players with this tech, such as Digital Asset Holdings ?

The blockchain technology still has a few ...READ MORE

answered Sep 5, 2018 in Blockchain by Christine
• 15,790 points
332 views
0 votes
1 answer

"Migrations" exceeded the block limit (with a gas value you set).

In the error you have posted, in ...READ MORE

answered Jan 17, 2019 in Blockchain by Omkar
• 69,210 points
5,500 views
0 votes
0 answers
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,123 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,655 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,211 views
0 votes
1 answer

How to open bitcoin-qt wallet with bitcoinj?

I found this issue solved at the ...READ MORE

answered Aug 24, 2018 in Blockchain by slayer
• 29,350 points
539 views
0 votes
1 answer

Passing hash string with smart contracts

eth.sendTransaction is a go etherum function. There more ...READ MORE

answered Jul 5, 2018 in Blockchain by slayer
• 29,350 points
1,141 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP