Block, FilteredBlock: remove params from constructors

This commit is contained in:
Andreas Schildbach 2023-04-02 02:14:32 +02:00
parent 6bd8e8e6bc
commit c613a7d98a
15 changed files with 39 additions and 45 deletions

View File

@ -298,7 +298,7 @@ public class BitcoinSerializer extends MessageSerializer {
*/
@Override
public Block makeBlock(ByteBuffer payload) throws ProtocolException {
return new Block(params, payload);
return new Block(payload);
}
/**
@ -316,7 +316,7 @@ public class BitcoinSerializer extends MessageSerializer {
*/
@Override
public FilteredBlock makeFilteredBlock(ByteBuffer payload) throws ProtocolException {
return new FilteredBlock(params, payload);
return new FilteredBlock(payload);
}
/**

View File

@ -134,8 +134,8 @@ public class Block extends Message {
private Sha256Hash hash;
/** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */
Block(NetworkParameters params, long setVersion) {
super(params);
Block(long setVersion) {
super(new DummySerializer(NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion()));
// Set up a few basic things. We are not complete after this though.
version = setVersion;
difficultyTarget = 0x1d07fff8L;
@ -145,18 +145,15 @@ public class Block extends Message {
/**
* Construct a block object from the Bitcoin wire format.
* @param params NetworkParameters object.
* @param payload the payload to extract the block from.
* @throws ProtocolException
*/
public Block(NetworkParameters params, ByteBuffer payload)
throws ProtocolException {
super(params, payload);
public Block(ByteBuffer payload) throws ProtocolException {
super(payload, new DummySerializer(NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion()));
}
/**
* Construct a block initialized with all the given fields.
* @param params Which network the block is for.
* @param version This should usually be set to 1 or 2, depending on if the height is in the coinbase input.
* @param prevBlockHash Reference to previous block in the chain or {@link Sha256Hash#ZERO_HASH} if genesis.
* @param merkleRoot The root of the merkle tree formed by the transactions.
@ -165,9 +162,9 @@ public class Block extends Message {
* @param nonce Arbitrary number to make the block hash lower than the target.
* @param transactions List of transactions including the coinbase.
*/
public Block(NetworkParameters params, long version, Sha256Hash prevBlockHash, Sha256Hash merkleRoot, Instant time,
public Block(long version, Sha256Hash prevBlockHash, Sha256Hash merkleRoot, Instant time,
long difficultyTarget, long nonce, List<Transaction> transactions) {
super(params);
super();
this.version = version;
this.prevBlockHash = prevBlockHash;
this.merkleRoot = merkleRoot;
@ -180,7 +177,6 @@ public class Block extends Message {
/**
* Construct a block initialized with all the given fields.
* @param params Which network the block is for.
* @param version This should usually be set to 1 or 2, depending on if the height is in the coinbase input.
* @param prevBlockHash Reference to previous block in the chain or {@link Sha256Hash#ZERO_HASH} if genesis.
* @param merkleRoot The root of the merkle tree formed by the transactions.
@ -188,12 +184,12 @@ public class Block extends Message {
* @param difficultyTarget Number which this block hashes lower than.
* @param nonce Arbitrary number to make the block hash lower than the target.
* @param transactions List of transactions including the coinbase.
* @deprecated use {@link #Block(NetworkParameters, long, Sha256Hash, Sha256Hash, Instant, long, long, List)}
* @deprecated use {@link #Block(long, Sha256Hash, Sha256Hash, Instant, long, long, List)}
*/
@Deprecated
public Block(NetworkParameters params, long version, Sha256Hash prevBlockHash, Sha256Hash merkleRoot, long time,
public Block(long version, Sha256Hash prevBlockHash, Sha256Hash merkleRoot, long time,
long difficultyTarget, long nonce, List<Transaction> transactions) {
this(params, version, prevBlockHash, merkleRoot, Instant.ofEpochSecond(time), difficultyTarget, nonce,
this(version, prevBlockHash, merkleRoot, Instant.ofEpochSecond(time), difficultyTarget, nonce,
transactions);
}
@ -236,8 +232,8 @@ public class Block extends Message {
parseTransactions(payload);
}
public static Block createGenesis(NetworkParameters n) {
Block genesisBlock = new Block(n, BLOCK_VERSION_GENESIS);
public static Block createGenesis() {
Block genesisBlock = new Block(BLOCK_VERSION_GENESIS);
Transaction tx = Transaction.coinbase(genesisTxInputScriptBytes);
tx.addOutput(new TransactionOutput(tx, FIFTY_COINS, genesisTxScriptPubKeyBytes));
genesisBlock.addTransaction(tx);
@ -381,7 +377,7 @@ public class Block extends Message {
* @return new, header-only {@code Block}
*/
public Block cloneAsHeader() {
Block block = new Block(params, version);
Block block = new Block(version);
block.difficultyTarget = difficultyTarget;
block.time = time;
block.nonce = nonce;
@ -861,7 +857,7 @@ public class Block extends Message {
@VisibleForTesting
Block createNextBlock(@Nullable Address to, long version, @Nullable TransactionOutPoint prevOut, Instant time,
byte[] pubKey, Coin coinbaseValue, int height) {
Block b = new Block(params, version);
Block b = new Block(version);
b.setDifficultyTarget(difficultyTarget);
b.addCoinbaseTransaction(pubKey, coinbaseValue, height);

View File

@ -333,7 +333,7 @@ public class BloomFilter extends Message {
}
}
PartialMerkleTree pmt = PartialMerkleTree.buildFromLeaves(bits, txHashes);
FilteredBlock filteredBlock = new FilteredBlock(block.getParams(), block.cloneAsHeader(), pmt);
FilteredBlock filteredBlock = new FilteredBlock(block.cloneAsHeader(), pmt);
for (Transaction transaction : matched)
filteredBlock.provideTransaction(transaction);
return filteredBlock;

View File

@ -47,12 +47,12 @@ public class FilteredBlock extends Message {
// These were relayed as a part of the filteredblock getdata, ie likely weren't previously received as loose transactions
private Map<Sha256Hash, Transaction> associatedTransactions = new HashMap<>();
public FilteredBlock(NetworkParameters params, ByteBuffer payload) throws ProtocolException {
super(params, payload);
public FilteredBlock(ByteBuffer payload) throws ProtocolException {
super(payload);
}
public FilteredBlock(NetworkParameters params, Block header, PartialMerkleTree pmt) {
super(params);
public FilteredBlock(Block header, PartialMerkleTree pmt) {
super();
this.header = header;
this.merkleTree = pmt;
}
@ -69,7 +69,7 @@ public class FilteredBlock extends Message {
@Override
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
byte[] headerBytes = Buffers.readBytes(payload, Block.HEADER_SIZE);
header = new Block(params, ByteBuffer.wrap(headerBytes));
header = new Block(ByteBuffer.wrap(headerBytes));
merkleTree = new PartialMerkleTree(payload);
}

View File

@ -75,10 +75,8 @@ public class HeadersMessage extends Message {
MAX_HEADERS);
blockHeaders = new ArrayList<>();
final BitcoinSerializer serializer = this.params.getSerializer();
for (int i = 0; i < numHeaders; ++i) {
final Block newBlockHeader = new Block(params, payload);
final Block newBlockHeader = new Block(payload);
if (newBlockHeader.hasTransactions()) {
throw new ProtocolException("Block header does not end with a null byte");
}

View File

@ -134,7 +134,7 @@ public class MainNetParams extends BitcoinNetworkParams {
public Block getGenesisBlock() {
synchronized (GENESIS_HASH) {
if (genesisBlock == null) {
genesisBlock = Block.createGenesis(this);
genesisBlock = Block.createGenesis();
genesisBlock.setDifficultyTarget(Block.STANDARD_MAX_DIFFICULTY_TARGET);
genesisBlock.setTime(Instant.ofEpochSecond(GENESIS_TIME));
genesisBlock.setNonce(GENESIS_NONCE);

View File

@ -82,7 +82,7 @@ public class RegTestParams extends BitcoinNetworkParams {
public Block getGenesisBlock() {
synchronized (GENESIS_HASH) {
if (genesisBlock == null) {
genesisBlock = Block.createGenesis(this);
genesisBlock = Block.createGenesis();
genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET);
genesisBlock.setTime(Instant.ofEpochSecond(GENESIS_TIME));
genesisBlock.setNonce(GENESIS_NONCE);

View File

@ -79,7 +79,7 @@ public class SigNetParams extends BitcoinNetworkParams {
public Block getGenesisBlock() {
synchronized (GENESIS_HASH) {
if (genesisBlock == null) {
genesisBlock = Block.createGenesis(this);
genesisBlock = Block.createGenesis();
genesisBlock.setDifficultyTarget(GENESIS_DIFFICULTY);
genesisBlock.setTime(Instant.ofEpochSecond(GENESIS_TIME));
genesisBlock.setNonce(GENESIS_NONCE);

View File

@ -88,7 +88,7 @@ public class TestNet3Params extends BitcoinNetworkParams {
public Block getGenesisBlock() {
synchronized (GENESIS_HASH) {
if (genesisBlock == null) {
genesisBlock = Block.createGenesis(this);
genesisBlock = Block.createGenesis();
genesisBlock.setDifficultyTarget(Block.STANDARD_MAX_DIFFICULTY_TARGET);
genesisBlock.setTime(Instant.ofEpochSecond(GENESIS_TIME));
genesisBlock.setNonce(GENESIS_NONCE);

View File

@ -73,7 +73,7 @@ public class UnitTestParams extends BitcoinNetworkParams {
public Block getGenesisBlock() {
synchronized (this) {
if (genesisBlock == null) {
genesisBlock = Block.createGenesis(this);
genesisBlock = Block.createGenesis();
genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET);
genesisBlock.setTime(TimeUtils.currentTime());
genesisBlock.solve();

View File

@ -217,7 +217,7 @@ public class BlockChainTest {
assertTrue(testNetChain.add(getBlock1()));
Block b2 = getBlock2();
assertTrue(testNetChain.add(b2));
Block bad = new Block(TESTNET, Block.BLOCK_VERSION_GENESIS);
Block bad = new Block(Block.BLOCK_VERSION_GENESIS);
// Merkle root can be anything here, doesn't matter.
bad.setMerkleRoot(Sha256Hash.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
// Nonce was just some number that made the hash < difficulty limit set below, it can be anything.
@ -414,7 +414,7 @@ public class BlockChainTest {
// Some blocks from the test net.
private static Block getBlock2() throws Exception {
Block b2 = new Block(TESTNET, Block.BLOCK_VERSION_GENESIS);
Block b2 = new Block(Block.BLOCK_VERSION_GENESIS);
b2.setMerkleRoot(Sha256Hash.wrap("20222eb90f5895556926c112bb5aa0df4ab5abc3107e21a6950aec3b2e3541e2"));
b2.setNonce(875942400L);
b2.setTime(Instant.ofEpochSecond(1296688946L));
@ -426,7 +426,7 @@ public class BlockChainTest {
}
private static Block getBlock1() throws Exception {
Block b1 = new Block(TESTNET, Block.BLOCK_VERSION_GENESIS);
Block b1 = new Block(Block.BLOCK_VERSION_GENESIS);
b1.setMerkleRoot(Sha256Hash.wrap("f0315ffc38709d70ad5647e22048358dd3745f3ce3874223c80a7c92fab0c8ba"));
b1.setNonce(1924588547);
b1.setTime(Instant.ofEpochSecond(1296688928));

View File

@ -311,7 +311,7 @@ public class BlockTest {
@Test
public void parseBlockWithHugeDeclaredTransactionsSize() {
Context.propagate(new Context(100, Transaction.DEFAULT_TX_FEE, false, true));
Block block = new Block(TESTNET, 1, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 1, 1, 1, new ArrayList<Transaction>()) {
Block block = new Block(1, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 1, 1, 1, new ArrayList<Transaction>()) {
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
ByteUtils.writeInt32LE(getVersion(), stream);
@ -335,7 +335,7 @@ public class BlockTest {
@Test
public void testGenesisBlock() {
Block genesisBlock = Block.createGenesis(MainNetParams.get());
Block genesisBlock = Block.createGenesis();
genesisBlock.setDifficultyTarget(0x1d00ffffL);
genesisBlock.setTime(Instant.ofEpochSecond(1231006505L));
genesisBlock.setNonce(2083236893);

View File

@ -898,7 +898,7 @@ public class FullBlockTestGenerator {
TransactionOutPointWithValue out14 = spendableOutputs.poll();
// A valid block created exactly like b44 to make sure the creation itself works
Block b44 = new Block(params, Block.BLOCK_VERSION_GENESIS);
Block b44 = new Block(Block.BLOCK_VERSION_GENESIS);
byte[] outScriptBytes = ScriptBuilder.createP2PKOutputScript(ECKey.fromPublicOnly(coinbaseOutKeyPubKey)).getProgram();
{
b44.setDifficultyTarget(b43.block.getDifficultyTarget());
@ -922,7 +922,7 @@ public class FullBlockTestGenerator {
TransactionOutPointWithValue out15 = spendableOutputs.poll();
// A block with a non-coinbase as the first tx
Block b45 = new Block(params, Block.BLOCK_VERSION_GENESIS);
Block b45 = new Block(Block.BLOCK_VERSION_GENESIS);
{
b45.setDifficultyTarget(b44.getDifficultyTarget());
//b45.addCoinbaseTransaction(pubKey, coinbaseValue);
@ -948,7 +948,7 @@ public class FullBlockTestGenerator {
blocks.add(new BlockAndValidity(b45, false, true, b44.getHash(), chainHeadHeight + 15, "b45"));
// A block with no txn
Block b46 = new Block(params, Block.BLOCK_VERSION_GENESIS);
Block b46 = new Block(Block.BLOCK_VERSION_GENESIS);
{
b46.transactions = new ArrayList<>();
b46.setDifficultyTarget(b44.getDifficultyTarget());

View File

@ -150,7 +150,7 @@ public class SPVBlockStoreTest {
Stopwatch watch = Stopwatch.start();
for (int i = 0; i < ITERATIONS; i++) {
// Using i as the nonce so that the block hashes are different.
Block block = new Block(TESTNET, 0, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 0, 0, i,
Block block = new Block(0, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 0, 0, i,
Collections.emptyList());
StoredBlock b = new StoredBlock(block, BigInteger.ZERO, i);
store.put(b);