导航菜单

页面标题

页面副标题

imToken v3.28.8 - AbstractBitcoinNetParams.java 源代码

正在查看: imToken v3.28.8 应用的 AbstractBitcoinNetParams.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.bitcoinj.params;

import com.google.common.base.Stopwatch;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractBitcoinNetParams extends NetworkParameters {
    public static final String BITCOIN_SCHEME = "bitcoin";
    private static final Logger log = LoggerFactory.getLogger(AbstractBitcoinNetParams.class);

    @Override
    public String getUriScheme() {
        return "bitcoin";
    }

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

    protected boolean isDifficultyTransitionPoint(StoredBlock storedBlock) {
        return (storedBlock.getHeight() + 1) % getInterval() == 0;
    }

    @Override
    public void checkDifficultyTransitions(StoredBlock storedBlock, Block block, BlockStore blockStore) throws VerificationException, BlockStoreException {
        Block header = storedBlock.getHeader();
        if (!isDifficultyTransitionPoint(storedBlock)) {
            if (block.getDifficultyTarget() == header.getDifficultyTarget()) {
                return;
            }
            throw new VerificationException("Unexpected change in difficulty at height " + storedBlock.getHeight() + ": " + Long.toHexString(block.getDifficultyTarget()) + " vs " + Long.toHexString(header.getDifficultyTarget()));
        }
        Stopwatch createStarted = Stopwatch.createStarted();
        StoredBlock storedBlock2 = blockStore.get(header.getHash());
        for (int i = 0; i < getInterval() - 1; i++) {
            if (storedBlock2 == null) {
                throw new VerificationException("Difficulty transition point but we did not find a way back to the genesis block.");
            }
            storedBlock2 = blockStore.get(storedBlock2.getHeader().getPrevBlockHash());
        }
        createStarted.stop();
        if (createStarted.elapsed(TimeUnit.MILLISECONDS) > 50) {
            log.info("Difficulty transition traversal took {}", createStarted);
        }
        int timeSeconds = (int) (header.getTimeSeconds() - storedBlock2.getHeader().getTimeSeconds());
        int targetTimespan = getTargetTimespan();
        int i2 = targetTimespan / 4;
        if (timeSeconds < i2) {
            timeSeconds = i2;
        }
        int i3 = targetTimespan * 4;
        if (timeSeconds > i3) {
            timeSeconds = i3;
        }
        BigInteger divide = Utils.decodeCompactBits(header.getDifficultyTarget()).multiply(BigInteger.valueOf(timeSeconds)).divide(BigInteger.valueOf(targetTimespan));
        if (divide.compareTo(getMaxTarget()) > 0) {
            log.info("Difficulty hit proof of work limit: {}", divide.toString(16));
            divide = getMaxTarget();
        }
        int difficultyTarget = ((int) (block.getDifficultyTarget() >>> 24)) - 3;
        long difficultyTarget2 = block.getDifficultyTarget();
        long encodeCompactBits = Utils.encodeCompactBits(divide.and(BigInteger.valueOf(16777215L).shiftLeft(difficultyTarget * 8)));
        if (encodeCompactBits == difficultyTarget2) {
            return;
        }
        throw new VerificationException("Network provided difficulty bits do not match what was calculated: " + Long.toHexString(encodeCompactBits) + " vs " + Long.toHexString(difficultyTarget2));
    }

    @Override
    public Coin getMaxMoney() {
        return MAX_MONEY;
    }

    @Override
    public Coin getMinNonDustOutput() {
        return Transaction.MIN_NONDUST_OUTPUT;
    }

    @Override
    public MonetaryFormat getMonetaryFormat() {
        return new MonetaryFormat();
    }

    @Override
    public int getProtocolVersionNum(NetworkParameters.ProtocolVersion protocolVersion) {
        return protocolVersion.getBitcoinProtocolVersion();
    }

    @Override
    public BitcoinSerializer getSerializer(boolean z) {
        return new BitcoinSerializer(this, z);
    }
}