3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Add basic support for using AbstractBlockChains in Peer/PeerGroup.

This commit is contained in:
Matt Corallo 2012-07-10 01:25:23 +02:00 committed by Mike Hearn
parent 03d8c71df3
commit 8edd6c5399
2 changed files with 13 additions and 10 deletions

View File

@ -47,7 +47,7 @@ public class Peer {
private static final Logger log = LoggerFactory.getLogger(Peer.class); private static final Logger log = LoggerFactory.getLogger(Peer.class);
private final NetworkParameters params; private final NetworkParameters params;
private final BlockChain blockChain; private final AbstractBlockChain blockChain;
// When an API user explicitly requests a block or transaction from a peer, the InventoryItem is put here // When an API user explicitly requests a block or transaction from a peer, the InventoryItem is put here
// whilst waiting for the response. Synchronized on itself. Is not used for downloads Peer generates itself. // whilst waiting for the response. Synchronized on itself. Is not used for downloads Peer generates itself.
// TODO: Make this work for transactions as well. // TODO: Make this work for transactions as well.
@ -87,9 +87,9 @@ public class Peer {
/** /**
* Construct a peer that reads/writes from the given block chain. * Construct a peer that reads/writes from the given block chain.
*/ */
public Peer(NetworkParameters params, BlockChain blockChain, VersionMessage ver) { public Peer(NetworkParameters params, AbstractBlockChain chain2, VersionMessage ver) {
this.params = params; this.params = params;
this.blockChain = blockChain; this.blockChain = chain2;
this.versionMessage = ver; this.versionMessage = ver;
this.pendingGetBlockFutures = new ArrayList<GetDataFuture<Block>>(); this.pendingGetBlockFutures = new ArrayList<GetDataFuture<Block>>();
this.eventListeners = new CopyOnWriteArrayList<PeerEventListener>(); this.eventListeners = new CopyOnWriteArrayList<PeerEventListener>();
@ -103,7 +103,7 @@ public class Peer {
* Construct a peer that reads/writes from the given chain. Automatically creates a VersionMessage for you from the * Construct a peer that reads/writes from the given chain. Automatically creates a VersionMessage for you from the
* given software name/version strings, which should be something like "MySimpleTool", "1.0" * given software name/version strings, which should be something like "MySimpleTool", "1.0"
*/ */
public Peer(NetworkParameters params, BlockChain blockChain, String thisSoftwareName, String thisSoftwareVersion) { public Peer(NetworkParameters params, AbstractBlockChain blockChain, String thisSoftwareName, String thisSoftwareVersion) {
this(params, blockChain, null); this(params, blockChain, null);
this.versionMessage = new VersionMessage(params, blockChain.getBestChainHeight()); this.versionMessage = new VersionMessage(params, blockChain.getBestChainHeight());
this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null); this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null);

View File

@ -94,6 +94,7 @@ public class PeerGroup {
private int maxConnections; private int maxConnections;
private final NetworkParameters params; private final NetworkParameters params;
private final AbstractBlockChain chain;
private int connectionDelayMillis; private int connectionDelayMillis;
private long fastCatchupTimeSecs; private long fastCatchupTimeSecs;
private ArrayList<Wallet> wallets; private ArrayList<Wallet> wallets;
@ -125,10 +126,10 @@ public class PeerGroup {
* about blocks or pending transactions, you can just provide a MemoryBlockStore and a newly created Wallet. * about blocks or pending transactions, you can just provide a MemoryBlockStore and a newly created Wallet.
* *
* @param params Network parameters * @param params Network parameters
* @param chain a BlockChain object that will receive and handle block messages. * @param chain2 a BlockChain object that will receive and handle block messages.
*/ */
public PeerGroup(NetworkParameters params, BlockChain chain) { public PeerGroup(NetworkParameters params, AbstractBlockChain chain2) {
this(params, chain, DEFAULT_CONNECTION_DELAY_MILLIS); this(params, chain2, DEFAULT_CONNECTION_DELAY_MILLIS);
} }
/** /**
@ -139,7 +140,7 @@ public class PeerGroup {
* @param connectionDelayMillis how long to wait between attempts to connect to nodes or read * @param connectionDelayMillis how long to wait between attempts to connect to nodes or read
* from any added peer discovery sources * from any added peer discovery sources
*/ */
public PeerGroup(final NetworkParameters params, final BlockChain chain, public PeerGroup(final NetworkParameters params, final AbstractBlockChain chain,
int connectionDelayMillis) { int connectionDelayMillis) {
this(params, chain, connectionDelayMillis, new ClientBootstrap( this(params, chain, connectionDelayMillis, new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
@ -148,9 +149,10 @@ public class PeerGroup {
bootstrap.setPipelineFactory(makePipelineFactory(params, chain)); bootstrap.setPipelineFactory(makePipelineFactory(params, chain));
} }
PeerGroup(final NetworkParameters params, final BlockChain chain, PeerGroup(final NetworkParameters params, final AbstractBlockChain chain,
int connectionDelayMillis, ClientBootstrap bootstrap) { int connectionDelayMillis, ClientBootstrap bootstrap) {
this.params = params; this.params = params;
this.chain = chain;
this.connectionDelayMillis = connectionDelayMillis; this.connectionDelayMillis = connectionDelayMillis;
this.fastCatchupTimeSecs = params.genesisBlock.getTimeSeconds(); this.fastCatchupTimeSecs = params.genesisBlock.getTimeSeconds();
this.wallets = new ArrayList<Wallet>(1); this.wallets = new ArrayList<Wallet>(1);
@ -188,7 +190,7 @@ public class PeerGroup {
// of the higher level {@code Peer}. Received packets will first be decoded, then passed // of the higher level {@code Peer}. Received packets will first be decoded, then passed
// {@code Peer}. Sent packets will be created by the {@code Peer}, then encoded and sent. // {@code Peer}. Sent packets will be created by the {@code Peer}, then encoded and sent.
private ChannelPipelineFactory makePipelineFactory( private ChannelPipelineFactory makePipelineFactory(
final NetworkParameters params, final BlockChain chain) { final NetworkParameters params, final AbstractBlockChain chain) {
return new ChannelPipelineFactory() { return new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception { public ChannelPipeline getPipeline() throws Exception {
VersionMessage ver = getVersionMessage().duplicate(); VersionMessage ver = getVersionMessage().duplicate();
@ -726,6 +728,7 @@ public class PeerGroup {
* {@link Peer#setFastCatchupTime(long)} for further explanation. Call this before starting block chain download. * {@link Peer#setFastCatchupTime(long)} for further explanation. Call this before starting block chain download.
*/ */
public synchronized void setFastCatchupTimeSecs(long secondsSinceEpoch) { public synchronized void setFastCatchupTimeSecs(long secondsSinceEpoch) {
Preconditions.checkState(!chain.shouldVerifyTransactions(), "Fast catchup is incompatible with fully verifying");
fastCatchupTimeSecs = secondsSinceEpoch; fastCatchupTimeSecs = secondsSinceEpoch;
if (downloadPeer != null) { if (downloadPeer != null) {
downloadPeer.setFastCatchupTime(secondsSinceEpoch); downloadPeer.setFastCatchupTime(secondsSinceEpoch);