3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 23:32:16 +00:00

NetworkParameters: hide more params behind a getter.

This commit is contained in:
Mike Hearn 2013-05-08 14:49:36 +02:00
parent 1f005d7b3d
commit f6f290cc05
5 changed files with 52 additions and 51 deletions

View File

@ -753,14 +753,15 @@ public abstract class AbstractBlockChain {
Block blockIntervalAgo = cursor.getHeader();
int timespan = (int) (prev.getTimeSeconds() - blockIntervalAgo.getTimeSeconds());
// Limit the adjustment step.
if (timespan < params.targetTimespan / 4)
timespan = params.targetTimespan / 4;
if (timespan > params.targetTimespan * 4)
timespan = params.targetTimespan * 4;
final int targetTimespan = params.getTargetTimespan();
if (timespan < targetTimespan / 4)
timespan = targetTimespan / 4;
if (timespan > targetTimespan * 4)
timespan = targetTimespan * 4;
BigInteger newDifficulty = Utils.decodeCompactBits(prev.getDifficultyTarget());
newDifficulty = newDifficulty.multiply(BigInteger.valueOf(timespan));
newDifficulty = newDifficulty.divide(BigInteger.valueOf(params.targetTimespan));
newDifficulty = newDifficulty.divide(BigInteger.valueOf(targetTimespan));
if (newDifficulty.compareTo(params.proofOfWorkLimit) > 0) {
log.info("Difficulty hit proof of work limit: {}", newDifficulty.toString(16));

View File

@ -39,7 +39,7 @@ public class Address extends VersionedChecksummedBytes {
* <pre>new Address(NetworkParameters.prodNet(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));</pre>
*/
public Address(NetworkParameters params, byte[] hash160) {
super(params.addressHeader, hash160);
super(params.getAddressHeader(), hash160);
if (hash160.length != 20) // 160 = 8 * 20
throw new RuntimeException("Addresses are 160-bit hashes, so you must provide 20 bytes");
}
@ -58,14 +58,14 @@ public class Address extends VersionedChecksummedBytes {
super(address);
if (params != null) {
boolean found = false;
for (int v : params.acceptableAddressCodes) {
for (int v : params.getAcceptableAddressCodes()) {
if (version == v) {
found = true;
break;
}
}
if (!found) {
throw new WrongNetworkException(version, params.acceptableAddressCodes);
throw new WrongNetworkException(version, params.getAcceptableAddressCodes());
}
}
}
@ -89,7 +89,7 @@ public class Address extends VersionedChecksummedBytes {
new NetworkParameters[] { NetworkParameters.testNet(), NetworkParameters.prodNet() };
for (NetworkParameters params : networks) {
for (int code : params.acceptableAddressCodes) {
for (int code : params.getAcceptableAddressCodes()) {
if (code == version) {
return params;
}

View File

@ -32,7 +32,7 @@ public class DumpedPrivateKey extends VersionedChecksummedBytes {
// Used by ECKey.getPrivateKeyEncoded()
DumpedPrivateKey(NetworkParameters params, byte[] keyBytes, boolean compressed) {
super(params.dumpedPrivateKeyHeader, encode(keyBytes, compressed));
super(params.getDumpedPrivateKeyHeader(), encode(keyBytes, compressed));
this.compressed = compressed;
}
@ -58,9 +58,9 @@ public class DumpedPrivateKey extends VersionedChecksummedBytes {
*/
public DumpedPrivateKey(NetworkParameters params, String encoded) throws AddressFormatException {
super(encoded);
if (params != null && version != params.dumpedPrivateKeyHeader)
if (params != null && version != params.getDumpedPrivateKeyHeader())
throw new AddressFormatException("Mismatched version number, trying to cross networks? " + version +
" vs " + params.dumpedPrivateKeyHeader);
" vs " + params.getDumpedPrivateKeyHeader());
if (bytes.length == 33) {
compressed = true;
bytes = Arrays.copyOf(bytes, 32); // Chop off the additional marker byte.

View File

@ -63,22 +63,11 @@ public class NetworkParameters implements Serializable {
public /*final*/ BigInteger proofOfWorkLimit;
private final int port;
private final long packetMagic;
/**
* First byte of a base58 encoded address. See {@link Address}. This is the same as acceptableAddressCodes[0] and
* is the one used for "normal" addresses. Other types of address may be encountered with version codes found in
* the acceptableAddressCodes array.
*/
public final int addressHeader;
/** First byte of a base58 encoded dumped private key. See {@link DumpedPrivateKey}. */
public final int dumpedPrivateKeyHeader;
private final int addressHeader;
private final int dumpedPrivateKeyHeader;
/** How many blocks pass between difficulty adjustment periods. Bitcoin standardises this to be 2015. */
public /*final*/ int interval;
/**
* How much time in seconds is supposed to pass between "interval" blocks. If the actual elapsed time is
* significantly different from this value, the network difficulty formula will produce a different value. Both
* test and production Bitcoin networks use 2 weeks (1209600 seconds).
*/
public final int targetTimespan;
private final int targetTimespan;
/**
* The key used to sign {@link AlertMessage}s. You can use {@link ECKey#verify(byte[], byte[], byte[])} to verify
* signatures using it.
@ -95,36 +84,15 @@ public class NetworkParameters implements Serializable {
* The depth of blocks required for a coinbase transaction to be spendable.
*/
private final int spendableCoinbaseDepth;
/**
* Returns the number of blocks between subsidy decreases
*/
private /*final*/ int subsidyDecreaseBlockCount;
/**
* If we are running in testnet-in-a-box mode, we allow connections to nodes with 0 non-genesis blocks
*/
boolean allowEmptyPeerChains;
/**
* The version codes that prefix addresses which are acceptable on this network. Although Satoshi intended these to
* be used for "versioning", in fact they are today used to discriminate what kind of data is contained in the
* address and to prevent accidentally sending coins across chains which would destroy them.
*/
public final int[] acceptableAddressCodes;
/**
* DNS names that when resolved, give active peers on the network. Used to bootstrap the P2P connectivity.
*/
private final int[] acceptableAddressCodes;
private final String[] dnsSeeds;
/**
* Block checkpoints are a safety mechanism that hard-codes the hashes of blocks at particular heights. Re-orgs
* beyond this point will never be accepted. This field should be accessed using
* {@link NetworkParameters#passesCheckpoint(int, Sha256Hash)} and {@link NetworkParameters#isCheckpoint(int)}.
*/
public Map<Integer, Sha256Hash> checkpoints = new HashMap<Integer, Sha256Hash>();
private Map<Integer, Sha256Hash> checkpoints = new HashMap<Integer, Sha256Hash>();
private NetworkParameters(int type) {
alertSigningKey = SATOSHI_KEY;
@ -401,4 +369,36 @@ public class NetworkParameters implements Serializable {
public long getPacketMagic() {
return packetMagic;
}
/**
* First byte of a base58 encoded address. See {@link com.google.bitcoin.core.Address}. This is the same as acceptableAddressCodes[0] and
* is the one used for "normal" addresses. Other types of address may be encountered with version codes found in
* the acceptableAddressCodes array.
*/
public int getAddressHeader() {
return addressHeader;
}
/** First byte of a base58 encoded dumped private key. See {@link com.google.bitcoin.core.DumpedPrivateKey}. */
public int getDumpedPrivateKeyHeader() {
return dumpedPrivateKeyHeader;
}
/**
* How much time in seconds is supposed to pass between "interval" blocks. If the actual elapsed time is
* significantly different from this value, the network difficulty formula will produce a different value. Both
* test and production Bitcoin networks use 2 weeks (1209600 seconds).
*/
public int getTargetTimespan() {
return targetTimespan;
}
/**
* The version codes that prefix addresses which are acceptable on this network. Although Satoshi intended these to
* be used for "versioning", in fact they are today used to discriminate what kind of data is contained in the
* address and to prevent accidentally sending coins across chains which would destroy them.
*/
public int[] getAcceptableAddressCodes() {
return acceptableAddressCodes;
}
}

View File

@ -74,8 +74,8 @@ public class AddressTest {
fail();
} catch (WrongNetworkException e) {
// Success.
assertEquals(e.verCode, NetworkParameters.prodNet().addressHeader);
assertTrue(Arrays.equals(e.acceptableVersions, NetworkParameters.testNet().acceptableAddressCodes));
assertEquals(e.verCode, NetworkParameters.prodNet().getAddressHeader());
assertTrue(Arrays.equals(e.acceptableVersions, NetworkParameters.testNet().getAcceptableAddressCodes()));
} catch (AddressFormatException e) {
fail();
}