NewYorkCoin-NYC / ToDo

ToDo repo
6 stars 2 forks source link

Update and clean source code #1

Open drarko opened 6 years ago

drarko commented 6 years ago

I'm working on this. If someone want to help, comment here.

cmelion commented 6 years ago

There was consensus reached in the discord dev channel that we should NewYorkCoinIfy the current DogeCoin master branch.

We identified the branch point from Dogecoin as https://github.com/dogecoin/dogecoin/tree/1.4-archive

Excluding copyright and bulk conversion of /Dogecoin/ --> /newyorkc/ there are roughly 100 sloc that were changed.

Changes of significance:

main.cpp

-- starting at line 825 (this is also where we would address block reward reduction)

int64 static GetBlockValue(int nHeight, int64 nFees, uint256 prevHash) {
    int64 nSubsidy = 10000 * COIN;

    std::string cseed_str = prevHash.ToString().substr(7,7);
    const char* cseed = cseed_str.c_str();
    long seed = hex2long(cseed);
    int rand = generateMTRandom(seed, 999999);
    int rand1 = 0;
    int rand2 = 0;
    int rand3 = 0;
    int rand4 = 0;
    int rand5 = 0;

    if (nHeight == 1) {
        nSubsidy = 97000000 * COIN;
    }
    else if (nHeight < 101) {
        nSubsidy = 1 * COIN;
    }

    if (nHeight < 100000) {
        nSubsidy = (1 + rand) * COIN;
    } else if (nHeight < 200000) {
        cseed_str = prevHash.ToString().substr(7,7);
        cseed = cseed_str.c_str();
        seed = hex2long(cseed);
        rand1 = generateMTRandom(seed, 499999);
        nSubsidy = (1 + rand1) * COIN;
    } else if (nHeight < 300000) {
        cseed_str = prevHash.ToString().substr(6,7);
        cseed = cseed_str.c_str();
        seed = hex2long(cseed);
        rand2 = generateMTRandom(seed, 249999);
        nSubsidy = (1 + rand2) * COIN;
    } else if (nHeight < 400000) {
        cseed_str = prevHash.ToString().substr(7,7);
        cseed = cseed_str.c_str();
        seed = hex2long(cseed);
        rand3 = generateMTRandom(seed, 124999);
        nSubsidy = (1 + rand3) * COIN;
    } else if (nHeight < 500000) {
        cseed_str = prevHash.ToString().substr(7,7);
        cseed = cseed_str.c_str();
        seed = hex2long(cseed);
        rand4 = generateMTRandom(seed, 62499);
        nSubsidy = (1 + rand4) * COIN;
    } else if (nHeight < 600000) {
        cseed_str = prevHash.ToString().substr(6,7);
        cseed = cseed_str.c_str();
        seed = hex2long(cseed);
        rand5 = generateMTRandom(seed, 31249);
        nSubsidy = (1 + rand5) * COIN;
    }
    return nSubsidy + nFees;
}

static const int64 nTargetTimespan = 2 * 60 * 60; // every 2 hours
static const int64 nTargetSpacing = 30; // 30 seconds
static const int64 nInterval = nTargetTimespan / nTargetSpacing;

-- Starting at line 910

unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const CBlock *pblock, uint64 TargetBlocksSpacingSeconds, uint64 PastBlocksMin, uint64 PastBlocksMax) {
    const CBlockIndex  *BlockLastSolved = pindexLast;
    const CBlockIndex  *BlockReading    = pindexLast;
    const CBlock *BlockCreating         = pblock;
    BlockCreating                       = BlockCreating;
    uint64 PastBlocksMass               = 0;
    int64 PastRateActualSeconds         = 0;
    int64 PastRateTargetSeconds         = 0;
    double PastRateAdjustmentRatio      = double(1);
    CBigNum PastDifficultyAverage;
    CBigNum PastDifficultyAveragePrev;
    double EventHorizonDeviation;
    double EventHorizonDeviationFast;
    double EventHorizonDeviationSlow;

    if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64)BlockLastSolved->nHeight < PastBlocksMin) { return bnProofOfWorkLimit.GetCompact(); }

    for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
            if (PastBlocksMax > 0 && i > PastBlocksMax) { break; }
            PastBlocksMass++;

            if (i == 1)        { PastDifficultyAverage.SetCompact(BlockReading->nBits); }
            else                { PastDifficultyAverage = ((CBigNum().SetCompact(BlockReading->nBits) - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev; }
            PastDifficultyAveragePrev = PastDifficultyAverage;

            PastRateActualSeconds                        = BlockLastSolved->GetBlockTime() - BlockReading->GetBlockTime();
            PastRateTargetSeconds                        = TargetBlocksSpacingSeconds * PastBlocksMass;
            PastRateAdjustmentRatio                        = double(1);
            if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; }
            if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
            PastRateAdjustmentRatio                        = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
            }
            EventHorizonDeviation                        = 1 + (0.7084 * pow((double(PastBlocksMass)/double(144)), -1.228));
            EventHorizonDeviationFast                = EventHorizonDeviation;
            EventHorizonDeviationSlow                = 1 / EventHorizonDeviation;

            if (PastBlocksMass >= PastBlocksMin) {
                    if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) { assert(BlockReading); break; }
            }
            if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
            BlockReading = BlockReading->pprev;
    }

    CBigNum bnNew(PastDifficultyAverage);
    if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
        bnNew *= PastRateActualSeconds;
        bnNew /= PastRateTargetSeconds;
    }

    if (bnNew > bnProofOfWorkLimit) { bnNew = bnProofOfWorkLimit; }

    printf("Difficulty Retarget - Kimoto Gravity Well\n");
    printf("PastRateAdjustmentRatio = %g\n", PastRateAdjustmentRatio);
    printf("Before: %08x  %s\n", BlockLastSolved->nBits, CBigNum().SetCompact(BlockLastSolved->nBits).getuint256().ToString().c_str());
    printf("After:  %08x  %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());

    return bnNew.GetCompact();
}

unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlock *pblock) {
        static const int64 BlocksTargetSpacing  = 0.5 * 60; // 30 seconds
        unsigned int TimeDaySeconds             = 60 * 60 * 24;
        int64 PastSecondsMin                    = TimeDaySeconds * 0.01;
        int64 PastSecondsMax                    = TimeDaySeconds * 0.14;
        uint64 PastBlocksMin                    = PastSecondsMin / BlocksTargetSpacing;
        uint64 PastBlocksMax                    = PastSecondsMax / BlocksTargetSpacing;

        return KimotoGravityWell(pindexLast, pblock, BlocksTargetSpacing, PastBlocksMin, PastBlocksMax);
}

unsigned int static GetNextWorkRequired_V1(const CBlockIndex* pindexLast, const CBlock *pblock) {

-- Starting at line 2092

bool LoadBlockIndex(bool fAllowNew)
{
    if (fTestNet)
    {
        pchMessageStart[0] = 0xac;
        pchMessageStart[1] = 0xb1;
        pchMessageStart[2] = 0xc5;
        pchMessageStart[3] = 0xdc;
        hashGenesisBlock = uint256("0x24463e4d3c625b0a9059f309044c2cf0d7e196cf2a6ecce901f24f681be33c8f");
    }

    //
    // Load block index
    //
    CTxDB txdb("cr");
    if (!txdb.LoadBlockIndex())
        return false;
    txdb.Close();

    //
    // Init with genesis block
    //
    if (mapBlockIndex.empty())
    {
        if (!fAllowNew) {
            return false;
        }

        // Genesis block
        const char* pszTimestamp = "A Coin for New York City";
        CTransaction txNew;
        txNew.vin.resize(1);
        txNew.vout.resize(1);
        txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
        txNew.vout[0].nValue = 88 * COIN;
        txNew.vout[0].scriptPubKey = CScript() << ParseHex("040184710fa689ad5023690c80f3a49c8f13f8d45b8c857fbcbc8bc4a8e4d3eb4b10f4d4604fa08dce601aaf0f470216fe1b51850b4acf21b179c45070ac7b03a9") << OP_CHECKSIG;
        CBlock block;
        block.vtx.push_back(txNew);
        block.hashPrevBlock = 0;
        block.hashMerkleRoot = block.BuildMerkleTree();
        block.nVersion = 1;
        block.nTime    = 1394102925;
        block.nBits    = 0x1e0ffff0;
        block.nNonce   = 2482334;

        if (fTestNet)
        {
            block.nTime    = 1394101189;
            block.nNonce   = 1556996;
        }

        //// debug print
        printf("block.GetHash() = %s\n", block.GetHash().ToString().c_str());
        printf("hashGenesisBlock = %s\n", hashGenesisBlock.ToString().c_str());
        printf("block.hashMerkleRoot = %s\n", block.hashMerkleRoot.ToString().c_str());
        assert(block.hashMerkleRoot == uint256("0x2bad42ac6e0ccc4808d8df0fd50ac8634eea335b1412b1ef52864b430a87b262"));

        if (false && block.GetHash() != hashGenesisBlock) {

-- Starting at line 2522

        if (pfrom->nVersion < MIN_PROTO_VERSION)
        {
            // Since February 20, 2012, the protocol is initiated at version 209,
            // and earlier versions are no longer supported

main.h

-- line 51

static const uint256 hashGenesisBlockOfficial("0x5597f25c062a3038c7fd815fe46c67dedfcb3c839fbc8e01ed4044540d08fe48");

-- starting at line 525

    static bool AllowFree(double dPriority)
    {
        // Large (in bytes) low-priority (new, small-coin) transactions
        // need a fee.
        return dPriority > 100 * COIN * 2880 / 250;
    }

net.cpp

-- line 1141

// DNS seeds
// Each pair gives a source name and a seed name.
// The first name is used as information source for addrman.
// The second name should resolve to a list of seed addresses.
static const char *strDNSSeed[][2] = {
       // default hypernode dns seed , change later when network grows
       {"107.155.190.222", "107.155.190.222"}
};

bitcoinrpc.cpp

-- line 2893

    // Try a dual IPv6/IPv4 socket, falling back to separate IPv4 and IPv6 sockets
    const bool loopback = !mapArgs.count("-rpcallowip");
    asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any();
    ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 18823));

-- line 3172

Object CallRPC(const string& strMethod, const Array& params)
{
    if (mapArgs["-rpcuser"] == "" && mapArgs["-rpcpassword"] == "")
.
.
.
    if (!d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", "18823")))
        throw runtime_error("couldn't connect to server");

Additionally checkpoints are not included in the original codebase and should be added in checkpoints.cpp.

ex:

    static MapCheckpoints mapCheckpoints =
            boost::assign::map_list_of
            (  0, hashGenesisBlockOfficial )
            (  42279, uint256("0x8444c3ef39a46222e87584ef956ad2c9ef401578bd8b51e8e4b9a86ec3134d3a"))
            (  42400, uint256("0x557bb7c17ed9e6d4a6f9361cfddf7c1fc0bdc394af7019167442b41f507252b4"))
            (  104679, uint256("0x35eb87ae90d44b98898fec8c39577b76cb1eb08e1261cfc10706c8ce9a1d01cf"))
            ;
jamesburrell2 commented 6 years ago

Ok great thanks for the info!