From eff0db051cf8082520e332842c7ffda657714dd4 Mon Sep 17 00:00:00 2001 From: Oscar Guindzberg Date: Fri, 28 Dec 2018 17:31:49 -0300 Subject: [PATCH] Remove Networks and AbstractBitcoinNetParams since they are not used --- .../params/btc/AbstractBitcoinNetParams.java | 180 ------------------ .../validation/params/btc/Networks.java | 84 -------- 2 files changed, 264 deletions(-) delete mode 100644 core/src/main/java/bisq/core/payment/validation/params/btc/AbstractBitcoinNetParams.java delete mode 100644 core/src/main/java/bisq/core/payment/validation/params/btc/Networks.java diff --git a/core/src/main/java/bisq/core/payment/validation/params/btc/AbstractBitcoinNetParams.java b/core/src/main/java/bisq/core/payment/validation/params/btc/AbstractBitcoinNetParams.java deleted file mode 100644 index 6729061fecd..00000000000 --- a/core/src/main/java/bisq/core/payment/validation/params/btc/AbstractBitcoinNetParams.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -/* - * Copyright 2013 Google Inc. - * Copyright 2015 Andreas Schildbach - * - * 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 bisq.core.payment.validation.params.btc; - -import org.bitcoinj.core.BitcoinSerializer; -import org.bitcoinj.core.Block; -import org.bitcoinj.core.Coin; -import org.bitcoinj.core.NetworkParameters; -import org.bitcoinj.core.StoredBlock; -import org.bitcoinj.core.Transaction; -import org.bitcoinj.core.Utils; -import org.bitcoinj.core.VerificationException; -import org.bitcoinj.store.BlockStore; -import org.bitcoinj.store.BlockStoreException; -import org.bitcoinj.utils.MonetaryFormat; - -import com.google.common.base.Stopwatch; - -import java.math.BigInteger; - -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Parameters for Bitcoin-like networks. - */ -public abstract class AbstractBitcoinNetParams extends NetworkParameters { - /** - * Scheme part for Bitcoin URIs. - */ - public static final String BITCOIN_SCHEME = "bitcoin"; - - private static final Logger log = LoggerFactory.getLogger(AbstractBitcoinNetParams.class); - - public AbstractBitcoinNetParams() { - super(); - } - - /** - * Checks if we are at a difficulty transition point. - * - * @param storedPrev The previous stored block - * @return If this is a difficulty transition point - */ - protected boolean isDifficultyTransitionPoint(StoredBlock storedPrev) { - return ((storedPrev.getHeight() + 1) % this.getInterval()) == 0; - } - - @Override - public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block nextBlock, - final BlockStore blockStore) throws VerificationException, BlockStoreException { - Block prev = storedPrev.getHeader(); - - // Is this supposed to be a difficulty transition point? - if (!isDifficultyTransitionPoint(storedPrev)) { - - // No ... so check the difficulty didn't actually change. - if (nextBlock.getDifficultyTarget() != prev.getDifficultyTarget()) - throw new VerificationException("Unexpected change in difficulty at height " + storedPrev.getHeight() + - ": " + Long.toHexString(nextBlock.getDifficultyTarget()) + " vs " + - Long.toHexString(prev.getDifficultyTarget())); - return; - } - - // We need to find a block far back in the chain. It's OK that this is expensive because it only occurs every - // two weeks after the initial block chain download. - final Stopwatch watch = Stopwatch.createStarted(); - StoredBlock cursor = blockStore.get(prev.getHash()); - for (int i = 0; i < this.getInterval() - 1; i++) { - if (cursor == null) { - // This should never happen. If it does, it means we are following an incorrect or busted chain. - throw new VerificationException( - "Difficulty transition point but we did not find a way back to the genesis block."); - } - cursor = blockStore.get(cursor.getHeader().getPrevBlockHash()); - } - watch.stop(); - if (watch.elapsed(TimeUnit.MILLISECONDS) > 50) - log.info("Difficulty transition traversal took {}", watch); - - Block blockIntervalAgo = cursor.getHeader(); - int timespan = (int) (prev.getTimeSeconds() - blockIntervalAgo.getTimeSeconds()); - // Limit the adjustment step. - final int targetTimespan = this.getTargetTimespan(); - if (timespan < targetTimespan / 4) - timespan = targetTimespan / 4; - if (timespan > targetTimespan * 4) - timespan = targetTimespan * 4; - - BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget()); - newTarget = newTarget.multiply(BigInteger.valueOf(timespan)); - newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan)); - - if (newTarget.compareTo(this.getMaxTarget()) > 0) { - log.info("Difficulty hit proof of work limit: {}", newTarget.toString(16)); - newTarget = this.getMaxTarget(); - } - - int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3; - long receivedTargetCompact = nextBlock.getDifficultyTarget(); - - // The calculated difficulty is to a higher precision than received, so reduce here. - BigInteger mask = BigInteger.valueOf(0xFFFFFFL).shiftLeft(accuracyBytes * 8); - newTarget = newTarget.and(mask); - long newTargetCompact = Utils.encodeCompactBits(newTarget); - - if (newTargetCompact != receivedTargetCompact) - throw new VerificationException("Network provided difficulty bits do not match what was calculated: " + - Long.toHexString(newTargetCompact) + " vs " + Long.toHexString(receivedTargetCompact)); - } - - @Override - public Coin getMaxMoney() { - return MAX_MONEY; - } - - @Override - public Coin getMinNonDustOutput() { - return Transaction.MIN_NONDUST_OUTPUT; - } - - @Override - public MonetaryFormat getMonetaryFormat() { - return MonetaryFormat.BTC; - } - - @Override - public int getProtocolVersionNum(final ProtocolVersion version) { - return version.getBitcoinProtocolVersion(); - } - - @Override - public BitcoinSerializer getSerializer(boolean parseRetain) { - return new BitcoinSerializer(this, parseRetain); - } - - @Override - public String getUriScheme() { - return BITCOIN_SCHEME; - } - - @Override - public boolean hasMaxMoney() { - return true; - } -} diff --git a/core/src/main/java/bisq/core/payment/validation/params/btc/Networks.java b/core/src/main/java/bisq/core/payment/validation/params/btc/Networks.java deleted file mode 100644 index 79171f8a21c..00000000000 --- a/core/src/main/java/bisq/core/payment/validation/params/btc/Networks.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -/* - * Copyright 2014 Giannis Dzegoutanis - * - * 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 bisq.core.payment.validation.params.btc; - -import org.bitcoinj.core.NetworkParameters; -import org.bitcoinj.params.MainNetParams; -import org.bitcoinj.params.TestNet3Params; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; - -import java.util.Collection; -import java.util.Set; - -/** - * Utility class that holds all the registered NetworkParameters types used for Address auto discovery. - * By default only MainNetParams and TestNet3Params are used. If you want to use TestNet2, RegTestParams or - * UnitTestParams use the register and unregister the TestNet3Params as they don't have their own address - * version/type code. - */ -public class Networks { - /** - * Registered networks - */ - private static Set networks = ImmutableSet.of(TestNet3Params.get(), MainNetParams.get()); - - public static Set get() { - return networks; - } - - public static void register(NetworkParameters network) { - register(Lists.newArrayList(network)); - } - - public static void register(Collection networks) { - ImmutableSet.Builder builder = ImmutableSet.builder(); - builder.addAll(Networks.networks); - builder.addAll(networks); - Networks.networks = builder.build(); - } - - public static void unregister(NetworkParameters network) { - if (networks.contains(network)) { - ImmutableSet.Builder builder = ImmutableSet.builder(); - for (NetworkParameters parameters : networks) { - if (parameters.equals(network)) - continue; - builder.add(parameters); - } - networks = builder.build(); - } - } -}