Skip to content

Commit

Permalink
Add dogecoin regtest support
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarguindzberg authored and rnicoll committed Aug 10, 2021
1 parent 7e68d7e commit 444199b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
public static final String ID_DOGE_MAINNET = "org.dogecoin.production";
/** The string returned by getId() for the testnet. */
public static final String ID_DOGE_TESTNET = "org.dogecoin.test";

public static final String ID_DOGE_REGTEST = "org.dogecoin.regtest";

protected final int newInterval;
protected final int newTargetTimespan;
protected final int diffChangeTarget;
Expand Down
103 changes: 103 additions & 0 deletions core/src/main/java/org/libdohj/params/DogecoinRegTestParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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 dogecoin in which all blocks are trivially solvable.
*/
public class DogecoinRegTestParams extends DogecoinTestNet3Params {
private static final BigInteger MAX_TARGET = new BigInteger("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);

public DogecoinRegTestParams() {
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 = 18444;
id = ID_DOGE_REGTEST;
packetMagic = 0xfabfb5da;
addressHeader = 111;
dumpedPrivateKeyHeader = 239;
}

@Override
public boolean allowEmptyPeerChain() {
return true;
}

private static Block genesis;

@Override
public Block getGenesisBlock() {
synchronized (DogecoinRegTestParams.class) {
if (genesis == null) {
genesis = super.getGenesisBlock();
genesis.setNonce(2);
genesis.setDifficultyTarget(0x207fffffL);
genesis.setTime(1296688602L);
checkState(genesis.getVersion() == 1);
checkState(genesis.getHashAsString().toLowerCase().equals("3d2160a3b5dc4a9d62e7e66a295f70313ac808440ef7400d6c0772171ce973a5"));
genesis.verifyHeader();
}
return genesis;
}
}

private static DogecoinRegTestParams instance;

public static synchronized DogecoinRegTestParams get() {
if (instance == null) {
instance = new DogecoinRegTestParams();
}
return instance;
}

@Override
public String getPaymentProtocolId() {
return ID_DOGE_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();
}

@Override
public boolean allowMinDifficultyBlocks() {
return false;
}

@Override
public boolean isTestNet() {
return false;
}
}

0 comments on commit 444199b

Please sign in to comment.