3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-11 17:55:53 +00:00

Add Regtest mode for Litecoin (#20)

This commit is contained in:
MikeR 2017-06-19 10:58:47 +02:00 committed by Ross Nicoll
parent 1419b81241
commit 70ce836956
3 changed files with 146 additions and 15 deletions

View File

@ -16,26 +16,16 @@
package org.libdohj.params;
import java.math.BigInteger;
import org.bitcoinj.core.AltcoinBlock;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Coin;
import static org.bitcoinj.core.Coin.COIN;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.VerificationException;
import org.bitcoinj.core.*;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.Utils;
import org.bitcoinj.utils.MonetaryFormat;
import org.libdohj.core.AltcoinNetworkParameters;
import org.libdohj.core.AltcoinSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import static org.bitcoinj.core.Coin.COIN;
/**
* Common parameters for Litecoin networks.
@ -51,7 +41,7 @@ public abstract class AbstractLitecoinParams extends NetworkParameters implement
public static final int LITE_TARGET_TIMESPAN = (int) (3.5 * 24 * 60 * 60); // 3.5 days
public static final int LITE_TARGET_SPACING = (int) (2.5 * 60); // 2.5 minutes
public static final int LITE_INTERVAL = LITE_TARGET_TIMESPAN / LITE_TARGET_SPACING;
/**
* The maximum number of coins to be generated
*/
@ -82,6 +72,8 @@ public abstract class AbstractLitecoinParams extends NetworkParameters implement
public static final String ID_LITE_MAINNET = "org.litecoin.production";
/** The string returned by getId() for the testnet. */
public static final String ID_LITE_TESTNET = "org.litecoin.test";
/** The string returned by getId() for regtest. */
public static final String ID_LITE_REGTEST = "regtest";
public static final int LITECOIN_PROTOCOL_VERSION_MINIMUM = 70002;
public static final int LITECOIN_PROTOCOL_VERSION_CURRENT = 70003;
@ -249,7 +241,7 @@ public abstract class AbstractLitecoinParams extends NetworkParameters implement
}
/**
*
*
* @param previousHeight Height of the block immediately previous to the one we're calculating difficulty of.
* @param previousBlockTime Time of the block immediately previous to the one we're calculating difficulty of.
* @param lastDifficultyTarget Compact difficulty target of the last retarget block.

View File

@ -0,0 +1,97 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.libdohj.params;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.VerificationException;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import java.math.BigInteger;
import static com.google.common.base.Preconditions.checkState;
/**
* Network parameters for the regression test mode of bitcoind in which all blocks are trivially solvable.
*/
public class LitecoinRegTestParams extends LitecoinTestNet3Params {
private static final BigInteger MAX_TARGET = new BigInteger("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
public LitecoinRegTestParams() {
super();
// Difficulty adjustments are disabled for regtest.
// By setting the block interval for difficulty adjustments to Integer.MAX_VALUE we make sure difficulty never changes.
interval = Integer.MAX_VALUE;
maxTarget = MAX_TARGET;
subsidyDecreaseBlockCount = 150;
port = 19444;
id = ID_LITE_REGTEST;
packetMagic = 0xfabfb5da;
}
@Override
public boolean allowEmptyPeerChain() {
return true;
}
private static Block genesis;
/**
* Extract from Litecoin source code, definition of regtest params.
* https://github.com/litecoin-project/litecoin/blob/edc66b374ea68107c721062152dd95e6aa037d53/src/chainparams.cpp
*/
@Override
public Block getGenesisBlock() {
synchronized (LitecoinRegTestParams.class) {
if (genesis == null) {
genesis = super.getGenesisBlock();
genesis.setNonce(0);
genesis.setDifficultyTarget(0x207fffffL);
genesis.setTime(1296688602L);
checkState(genesis.getVersion() == 1);
checkState(genesis.getMerkleRoot().toString().equals("97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9"));
checkState(genesis.getHashAsString().toLowerCase().equals("530827f38f93b43ed12af0b3ad25a288dc02ed74d6d7857862df51fc56c416f9"));
genesis.verifyHeader();
}
return genesis;
}
}
private static LitecoinRegTestParams instance;
public static synchronized LitecoinRegTestParams get() {
if (instance == null) {
instance = new LitecoinRegTestParams();
}
return instance;
}
@Override
public String getPaymentProtocolId() {
return ID_LITE_REGTEST;
}
@Override
/** the testnet rules don't work for regtest, where difficulty stays the same */
public long calculateNewDifficultyTarget(StoredBlock storedPrev, Block nextBlock, BlockStore blockStore)
throws VerificationException, BlockStoreException {
final Block prev = storedPrev.getHeader();
return prev.getDifficultyTarget();
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2015 J. Ross Nicoll
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.libdohj.params;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Context;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
*
* @author Mike Rosseel
*/
public class LitecoinRegTestParamsTest {
private static final LitecoinRegTestParams params = LitecoinRegTestParams.get();
@Before
public void setUp() throws Exception {
Context context = new Context(params);
}
@Test
public void testRegTestGenesisBlock() {
Block genesis = params.getGenesisBlock();
assertEquals("530827f38f93b43ed12af0b3ad25a288dc02ed74d6d7857862df51fc56c416f9", genesis.getHashAsString());
}
}