org.bitcoinj.core.Peer Java Examples

The following examples show how to use org.bitcoinj.core.Peer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WatchMempool.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    BriefLogFormatter.init();
    PeerGroup peerGroup = new PeerGroup(PARAMS);
    peerGroup.setMaxConnections(32);
    peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS));
    peerGroup.addOnTransactionBroadcastListener(new OnTransactionBroadcastListener() {
        @Override
        public void onTransaction(Peer peer, Transaction tx) {
            Result result = DefaultRiskAnalysis.FACTORY.create(null, tx, NO_DEPS).analyze();
            incrementCounter(TOTAL_KEY);
            log.info("tx {} result {}", tx.getHash(), result);
            incrementCounter(result.name());
            if (result == Result.NON_STANDARD)
                incrementCounter(Result.NON_STANDARD + "-" + DefaultRiskAnalysis.isStandard(tx));
        }
    });
    peerGroup.start();

    while (true) {
        Thread.sleep(STATISTICS_FREQUENCY_MS);
        printCounters();
    }
}
 
Example #2
Source File: DownloadProgressTracker.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) {
    if (caughtUp)
        return;

    if (blocksLeft == 0) {
        caughtUp = true;
        if (lastPercent != 100) {
            lastPercent = 100;
            progress(lastPercent, blocksLeft, new Date(block.getTimeSeconds() * 1000));
        }
        doneDownload();
        future.set(peer.getBestHeight());
        return;
    }

    if (blocksLeft < 0 || originalBlocksLeft <= 0)
        return;

    double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft));
    if ((int) pct != lastPercent) {
        progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000));
        lastPercent = (int) pct;
    }
}
 
Example #3
Source File: DownloadProgressTracker.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) {
    if (caughtUp)
        return;

    if (blocksLeft == 0) {
        caughtUp = true;
        if (lastPercent != 100) {
            lastPercent = 100;
            progress(lastPercent, blocksLeft, new Date(block.getTimeSeconds() * 1000));
        }
        doneDownload();
        future.set(peer.getBestHeight());
        return;
    }

    if (blocksLeft < 0 || originalBlocksLeft <= 0)
        return;

    double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft));
    if ((int) pct != lastPercent) {
        progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000));
        lastPercent = (int) pct;
    }
}
 
Example #4
Source File: WatchMempool.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    BriefLogFormatter.init();
    PeerGroup peerGroup = new PeerGroup(PARAMS);
    peerGroup.setMaxConnections(32);
    peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS));
    peerGroup.addOnTransactionBroadcastListener(new OnTransactionBroadcastListener() {
        @Override
        public void onTransaction(Peer peer, Transaction tx) {
            Result result = DefaultRiskAnalysis.FACTORY.create(null, tx, NO_DEPS).analyze();
            incrementCounter(TOTAL_KEY);
            log.info("tx {} result {}", tx.getHash(), result);
            incrementCounter(result.name());
            if (result == Result.NON_STANDARD)
                incrementCounter(Result.NON_STANDARD + "-" + DefaultRiskAnalysis.isStandard(tx));
        }
    });
    peerGroup.start();

    while (true) {
        Thread.sleep(STATISTICS_FREQUENCY_MS);
        printCounters();
    }
}
 
Example #5
Source File: DownloadProgressTracker.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) {
    if (caughtUp)
        return;

    if (blocksLeft == 0) {
        caughtUp = true;
        doneDownload();
        future.set(peer.getBestHeight());
    }

    if (blocksLeft < 0 || originalBlocksLeft <= 0)
        return;

    double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft));
    if ((int) pct != lastPercent) {
        progress(pct, blocksLeft, new Date(block.getTimeSeconds() * 1000));
        lastPercent = (int) pct;
    }
}
 
Example #6
Source File: DownloadProgressTracker.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onChainDownloadStarted(Peer peer, int blocksLeft) {
    if (blocksLeft > 0 && originalBlocksLeft == -1)
        startDownload(blocksLeft);
    // Only mark this the first time, because this method can be called more than once during a chain download
    // if we switch peers during it.
    if (originalBlocksLeft == -1)
        originalBlocksLeft = blocksLeft;
    else
        log.info("Chain download switched to {}", peer);
    if (blocksLeft == 0) {
        doneDownload();
        future.set(peer.getBestHeight());
    }
}
 
Example #7
Source File: DownloadProgressTracker.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onChainDownloadStarted(Peer peer, int blocksLeft) {
    if (blocksLeft > 0 && originalBlocksLeft == -1)
        startDownload(blocksLeft);
    // Only mark this the first time, because this method can be called more than once during a chain download
    // if we switch peers during it.
    if (originalBlocksLeft == -1)
        originalBlocksLeft = blocksLeft;
    else
        log.info("Chain download switched to {}", peer);
    if (blocksLeft == 0) {
        doneDownload();
        future.set(peer.getBestHeight());
    }
}
 
Example #8
Source File: PeerSerializer.java    From consensusj with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Peer value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    jgen.writeStartObject();
    jgen.writeStringField("socketAddress", value.getAddress().toSocketAddress().toString());
    jgen.writeNumberField("remoteVersion", value.getPeerVersionMessage().clientVersion);
    jgen.writeNumberField("bestHeight", value.getPeerVersionMessage().bestHeight);
    jgen.writeEndObject();
}
 
Example #9
Source File: PeerMonitor.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private Object getAddressForPeer(Peer peer) {
    String s;
    synchronized (reverseDnsLookups) {
        s = reverseDnsLookups.get(peer);
    }
    if (s != null)
        return s;
    else
        return peer.getAddress().getAddr().getHostAddress();
}
 
Example #10
Source File: PeerMonitor.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void lookupReverseDNS(final Peer peer) {
    new Thread() {
        @Override
        public void run() {
            // This can take a looooong time.
            String reverseDns = peer.getAddress().getAddr().getCanonicalHostName();
            synchronized (reverseDnsLookups) {
                reverseDnsLookups.put(peer, reverseDns);
            }
            refreshUI();
        }
    }.start();
}
 
Example #11
Source File: RpcServerModule.java    From consensusj with Apache License 2.0 5 votes vote down vote up
public RpcServerModule(NetworkParameters netParams) {
    super("BitcoinJMappingServer", new Version(1, 0, 0, null, null, null));

    this.addDeserializer(Address.class, new AddressDeserializer(netParams))  // Null means use default list of netParams
            .addDeserializer(Coin.class, new CoinDeserializer())
            .addDeserializer(ECKey.class, new ECKeyDeserializer())
            .addDeserializer(Sha256Hash.class, new Sha256HashDeserializer())
            .addSerializer(Address.class, new AddressSerializer())
            .addSerializer(Coin.class, new CoinSerializer())
            .addSerializer(ECKey.class, new ECKeySerializer())
            .addSerializer(Peer.class, new PeerSerializer())
            .addSerializer(Sha256Hash.class, new Sha256HashSerializer())
            .addSerializer(Transaction.class, new TransactionSerializer());
}
 
Example #12
Source File: NetworkMonitorActivity.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onResumeWithService() {

    registerReceiver(uiUpdated, new IntentFilter("PEERGROUP_UPDATED"));

    if (mService.isForcedOff()) {
        // FIXME: Should pass flag to activity so it shows it was forced logged out
        startActivity(new Intent(this, FirstScreenActivity.class));
        finish();
        return;
    }

    mPeers.clear();

    final PeerGroup peerGroup = mService.getSPVPeerGroup();
    if (peerGroup == null || !peerGroup.isRunning())
        return;

    mService.enableSPVPingMonitoring();

    for (final Peer peer : peerGroup.getConnectedPeers())
        mPeers.add(new PrettyPeer(peer));

    final String bloomDetails;
    if (!mPeers.isEmpty())
        bloomDetails = mPeers.get(0).mPeer.getBloomFilter().toString();
    else
        bloomDetails = getString(R.string.network_monitor_bloom_info);

    final int currentBlock = mService.getCurrentBlock();
    final int spvHeight = mService.getSPVHeight();
    mBloomInfoText.setText(getString(R.string.network_monitor_banner, bloomDetails, currentBlock - spvHeight));

    mPeerList.setAdapter(mPeerListAdapter);

    peerGroup.addConnectedEventListener(this);
    peerGroup.addDisconnectedEventListener(this);
    mRefreshCallback.run();
}
 
Example #13
Source File: DownloadProgressTracker.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onChainDownloadStarted(Peer peer, int blocksLeft) {
    if (blocksLeft > 0 && originalBlocksLeft == -1)
        startDownload(blocksLeft);
    // Only mark this the first time, because this method can be called more than once during a chain download
    // if we switch peers during it.
    if (originalBlocksLeft == -1)
        originalBlocksLeft = blocksLeft;
    else
        log.info("Chain download switched to {}", peer);
    if (blocksLeft == 0) {
        doneDownload();
        future.set(peer.getBestHeight());
    }
}
 
Example #14
Source File: NetworkMonitorActivity.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized void onPeerConnected(final Peer peer, final int peerCount) {
    runOnUiThread(new Runnable() {
        public void run() {
            mPeers.add(new PrettyPeer(peer));
            mPeerListAdapter.notifyDataSetChanged();
            mBloomInfoText.setText(peer.getBloomFilter().toString());
        }
    });
}
 
Example #15
Source File: NetworkMonitorActivity.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized void onPeerDisconnected(final Peer peer, final int peerCount) {
    runOnUiThread(new Runnable() {
        public void run() {
            for (final Iterator<PrettyPeer> it = mPeers.iterator(); it.hasNext(); ) {
                if (peer == it.next().mPeer)
                    it.remove();
            }
            mPeerListAdapter.notifyDataSetChanged();
        }
    });
}
 
Example #16
Source File: PeerMonitor.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void lookupReverseDNS(final Peer peer) {
    new Thread() {
        @Override
        public void run() {
            // This can take a looooong time.
            String reverseDns = peer.getAddress().getAddr().getCanonicalHostName();
            synchronized (reverseDnsLookups) {
                reverseDnsLookups.put(peer, reverseDns);
            }
            refreshUI();
        }
    }.start();
}
 
Example #17
Source File: PeerMonitor.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private Object getAddressForPeer(Peer peer) {
    String s;
    synchronized (reverseDnsLookups) {
        s = reverseDnsLookups.get(peer);
    }
    if (s != null)
        return s;
    else
        return peer.getAddress().getAddr().getHostAddress();
}
 
Example #18
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Message onPreMessageReceived(Peer peer, Message m) {
    // Just pass the message right through for further processing.
    return m;
}
 
Example #19
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onChainDownloadStarted(Peer peer, int blocksLeft) {
}
 
Example #20
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onTransaction(Peer peer, Transaction t) {
}
 
Example #21
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft) {
}
 
Example #22
Source File: PeerController.java    From consensusj with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/listPeers")
@ResponseBody
public List<Peer> listPeersREST() {
    return peerService.getPeers();
}
 
Example #23
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<Message> getData(Peer peer, GetDataMessage m) {
    return null;
}
 
Example #24
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onPeerConnected(Peer peer, int peerCount) {
}
 
Example #25
Source File: AbstractPeerEventListener.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onPeerDisconnected(Peer peer, int peerCount) {
}
 
Example #26
Source File: ImportEventListener.java    From jelectrum with MIT License 4 votes vote down vote up
@Override
public void onTransaction(Peer peer, Transaction t)
{
    importer.saveTransaction(t);
}
 
Example #27
Source File: BitcoinNetworkListItem.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public BitcoinNetworkListItem(Peer peer) {
    this.peer = peer;
}
 
Example #28
Source File: WalletsSetup.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public ReadOnlyObjectProperty<List<Peer>> connectedPeersProperty() {
    return connectedPeers;
}
 
Example #29
Source File: WalletsSetup.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public ReadOnlyObjectProperty<Peer> blocksDownloadedFromPeerProperty() {
    return blocksDownloadedFromPeer;
}
 
Example #30
Source File: Network.java    From cate with MIT License 4 votes vote down vote up
protected void onBlocksDownloadedEventListener(Peer peer, Block block, FilteredBlock filteredBlock, int blocksLeft) {
    this.blocksLeft.set(blocksLeft);
}