zawy12 / difficulty-algorithms

See the Issues for difficulty algorithms
MIT License
107 stars 25 forks source link

Maybe you can help me with go Implemetation this version of lwma #75

Closed mraksoll4 closed 1 year ago

mraksoll4 commented 1 year ago
unsigned int LwmaCalculateNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params)
{
    const int64_t T = params.nPowTargetSpacing;
    // N=45 for T=600.  N=60 for T=150.  N=90 for T=60.
    const int64_t N = params.nZawyLwmaAveragingWindow;
    const int64_t k = N*(N+1)*T/2; // BTG's code has a missing N here. They inserted it in the loop
    const int height = pindexLast->nHeight;
    assert(height > N);

    arith_uint256 sum_target;
    int64_t t = 0, j = 0, solvetime;

    // Loop through N most recent blocks.
    for (int i = height - N+1; i <= height; i++) {
        const CBlockIndex* block = pindexLast->GetAncestor(i);
        const CBlockIndex* block_Prev = block->GetAncestor(i - 1);
        solvetime = block->GetBlockTime() - block_Prev->GetBlockTime();
        solvetime = std::max(-6*T, std::min(solvetime, 6*T));
        j++;
        t += solvetime * j;  // Weighted solvetime sum.
        arith_uint256 target;
        target.SetCompact(block->nBits);
        sum_target += target / (k * N); // BTG added the missing N back here.
    }
    // Keep t reasonable to >= 1/10 of expected t.
    if (t < k/10 ) {   t = k/10;  }
    arith_uint256 next_target = t * sum_target;

    const arith_uint256 pow_limit = UintToArith256(params.powLimit);
    if (next_target > pow_limit) {
        next_target = pow_limit;
    }

    return next_target.GetCompact();
}

i try implement it at go like. ( for btcd variant )

it seems that no one coin did this - therefore there is nowhere to see an approximate implementation.

func (b *BlockChain) lwmaCalculateNextWorkRequired(lastNode *blockNode, newBlockTime time.Time) (uint32, error) {
    log.Debugf("Calculating next work required for block at height %d", lastNode.height)

    const (
            N = 90
            T = int64(time.Second * 60) // Assuming 60 second block time
    )
    var k = N * (N + 1) * T / 2
    height := int(lastNode.height)
    if height <= N {
        log.Debugf("Block height %d is less than or equal to N, returning PowLimitBits", height)
    return b.chainParams.PowLimitBits, nil
    }

    var sumTarget = new(big.Int)
    var t, j, solvetime int64

    // Loop through N most recent blocks.
    for i := int32(height - N + 1); i <= int32(height); i++ {
    block := b.bestChain.NodeByHeight(int32(i))
    blockPrev := block.parent
    solvetime = block.timestamp - blockPrev.timestamp
    const minSolveTime = -6 * T
    const maxSolveTime = 6 * T
    if solvetime < minSolveTime {
        solvetime = minSolveTime
    }
    if solvetime > maxSolveTime {
        solvetime = maxSolveTime
    }
    j++;
        t += solvetime * j // Weighted solvetime sum.
        target := CompactToBig(block.bits)
        sumTarget.Add(sumTarget, new(big.Int).Div(target, big.NewInt(k*N)))

        log.Debugf("Block #%d: solvetime=%d, target=%v", block.height, solvetime, target)
    }

    // Keep t reasonable to >= 1/10 of expected t.
    if t < k/10 {
        t = k / 10
    }

    nextTarget := new(big.Int).Mul(big.NewInt(t), sumTarget)
    if nextTarget.Cmp(b.chainParams.PowLimit) > 0 {
        nextTarget.Set(b.chainParams.PowLimit)
    }

    log.Debugf("Calculated next difficulty target: %v", nextTarget)
    newTargetBits := BigToCompact(nextTarget)

    return newTargetBits, nil
}

but i getting wrong result …

023-04-02 15:28:02.582 [DBG] CHAN: Block #394: solvetime=9, target=7341570916718459325044953546406663707260825346279713454619248521405005824
2023-04-02 15:28:02.582 [DBG] CHAN: Block #395: solvetime=40, target=7216908123329554766634413268644284935025927190469885847466625427612631040
2023-04-02 15:28:02.582 [DBG] CHAN: Block #396: solvetime=40, target=7167948860182009204767297969246257285722602136198552167840941271400054784
2023-04-02 15:28:02.582 [DBG] CHAN: Block #397: solvetime=16, target=7119124396767799396099156004923664734572645267649331191077662633238724608
2023-04-02 15:28:02.582 [DBG] CHAN: Block #398: solvetime=139, target=7011877728925874150995970618907500643752916707136464793747832471667343360
2023-04-02 15:28:02.582 [DBG] CHAN: Block #399: solvetime=14, target=7205261426369345690243117118126692454574915944079348320154788667984969728
2023-04-02 15:28:02.582 [DBG] CHAN: Block #400: solvetime=23, target=7093000208447330426138123667304342712449890874703889376343473234907234304
2023-04-02 15:28:02.582 [DBG] CHAN: Calculated next difficulty target: 667970003227042956131968804448465157116937310715054333833022670000000000
2023-04-02 15:28:02.582 [INF] SYNC: Rejected block 000295ab614309174c4dd316c346cb9aac5d207cc6278572cf41e1f99e9d7db7 from 5.45.78.123:1604 (outbound): block difficulty of 520353444 is not the expected value of 509659234
mraksoll4 commented 1 year ago

possible i find soltution

func (b *BlockChain) lwmaCalculateNextWorkRequired(lastNode *blockNode, newBlockTime time.Time) (uint32, error) {
    const T int64 = 60
    const N int64 = 90
    const k int64 = N * (N + 1) * T / 2
    height := int64(lastNode.height)
    if height <= N {
        log.Debugf("lwmaCalculateNextWorkRequired: block height %d must be greater than %d", height, N)
        return 0, nil
    }

    sumTarget := big.NewInt(0)
    t := int64(0)
    j := int64(0)
    solvetime := int64(0)

    for i := height - N + 1; i <= height; i++ {
        blockNode := lastNode.Ancestor(int32(i))
        blockNodePrev := blockNode.Ancestor(int32(i - 1))

        solvetime = blockNode.timestamp - blockNodePrev.timestamp
        solvetime = int64(math.Max(float64(-6*T), math.Min(float64(solvetime), float64(6*T))))

        j++
        t += solvetime * j
        target := CompactToBig(blockNode.bits)
        sumTarget.Add(sumTarget, target.Div(target, big.NewInt(int64(k*N))))
        log.Debugf("lwmaCalculateNextWorkRequired: block height=%d, solvetime=%d, target=%d, sumTarget=%d", blockNode.height, solvetime, target, sumTarget)
    }

    // Calculate the next target using the LWMA formula
    if t < k/10 {
        t = k/10
    }
    nextTarget := new(big.Int).Mul(big.NewInt(t), sumTarget)
    log.Debugf("lwmaCalculateNextWorkRequired: t=%d, sumTarget=%d, nextTarget=%d", t, sumTarget, nextTarget)

    // Set the next target to the maximum difficulty if it exceeds it
    if nextTarget.Cmp(b.chainParams.PowLimit) > 0 {
        nextTarget = b.chainParams.PowLimit
    }

    log.Debugf("lwmaCalculateNextWorkRequired: height=%d, target=%d", height+1, BigToCompact(nextTarget))
    return BigToCompact(nextTarget), nil
}
023-04-05 17:22:37.553 [DBG] CHAN: lwmaCalculateNextWorkRequired: t=141220, sumTarget=66802036891354313219615804761915735396125693608805066181454401407121, nextTarget=9433783649797056112874143948477740152640870451435451446144990566713627620
2023-04-05 17:22:37.553 [DBG] CHAN: lwmaCalculateNextWorkRequired: height=181, target=520443614
2023-04-05 17:22:37.566 [DBG] CHAN: Accepted block 000395d478d63d6621165887323c0f392c4706098ba906c6b245e6cc640fc619
2023-04-05 17:22:37.566 [DBG] PEER: Received block (hash 0002baa91364d588f5610c6a9651e0b12224103c0ed4ed4c5f0fd2ce8f81b28f, ver 536870912, 1 tx, 2021-05-03 08:38:20 +0300 EEST) from 5.45.78.123:1604 (outbound)
2023-04-05 17:22:37.576 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=92, solvetime=67, target=1467067078365171520066935735784690130004356404220167498482984152300, sumTarget=1467067078365171520066935735784690130004356404220167498482984152300
2023-04-05 17:22:37.576 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=93, solvetime=4, target=1479578405308521579560124037183098100427766361081023617272885352878, sumTarget=2946645483673693099627059772967788230432122765301191115755869505178
2023-04-05 17:22:37.576 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=94, solvetime=12, target=1448576844061639362371194771157107272915929644743287368141175381303, sumTarget=4395222327735332461998254544124895503348052410044478483897044886481
2023-04-05 17:22:37.576 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=95, solvetime=3, target=1423054322308366107115844556630282828086104655762078832695083807254, sumTarget=5818276650043698569114099100755178331434157065806557316592128693735
2023-04-05 17:22:37.576 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=96, solvetime=9, target=1391405371214775754905390930046646840537741854149238841692643223956, sumTarget=7209682021258474324019490030801825171971898919955796158284771917691
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=97, solvetime=4, target=1363794377131161566896987552364933207115507855608232359307641032426, sumTarget=8573476398389635890916477583166758379087406775564028517592412950117
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=98, solvetime=30, target=1332945214624088232716891704549310825939953174410586444891263125759, sumTarget=9906421613013724123633369287716069205027359949974614962483676075876
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=99, solvetime=24, target=1319202505863082398512329384011125809119384373617800421601478840735, sumTarget=11225624118876806522145698671727195014146744323592415384085154916611
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=100, solvetime=37, target=1301327962468378128172275428953431475229537036106885243509611611962, sumTarget=12526952081345184650317974100680626489376281359699300627594766528573
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=101, solvetime=12, target=1291689046972945895629579393984047981352370877566451753434289170745, sumTarget=13818641128318130545947553494664674470728652237265752381029055699318
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=102, solvetime=1, target=1265675191682528801553750239146995665534372607293591978865967019436, sumTarget=15084316320000659347501303733811670136263024844559344359895022718754
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=103, solvetime=13, target=1232796077961201145695993260769265579159541218029942806041366008212, sumTarget=16317112397961860493197296994580935715422566062589287165936388726966
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=104, solvetime=6, target=1207628340474202970085207243836178877400580806854864569812567481265, sumTarget=17524740738436063463282504238417114592823146869444151735748956208231
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=105, solvetime=20, target=1178060546571442724243382776333334393020485477115098189682701576333, sumTarget=18702801285007506187525887014750448985843632346559249925431657784564
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=106, solvetime=94, target=1157312372538902155888765887500932635013704085002639660198532931679, sumTarget=19860113657546408343414652902251381620857336431561889585630190716243
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=107, solvetime=7, target=1181876611016257154784571568320960289280987637022960170954616481596, sumTarget=21041990268562665498199224470572341910138324068584849756584807197839
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=108, solvetime=10, target=1152475846132327444720428527176679622085457417796732797672754519801, sumTarget=22194466114694992942919652997749021532223781486381582554257561717640
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=109, solvetime=17, target=1125030661877625321425635908507846858277098273488144599458033614889, sumTarget=23319496776572618264345288906256868390500879759869727153715595332529
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=110, solvetime=16, target=1101857519097245806634461669112062382167397138226568663983501153441, sumTarget=24421354295669864070979750575368930772668276898096295817699096485970
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=111, solvetime=12, target=1078102822725755594281579105825511914838443756870408011235261532309, sumTarget=25499457118395619665261329681194442687506720654966703828934358018279
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=112, solvetime=16, target=1051987774672105401887884561296774950653154215264528254415958889902, sumTarget=26551444893067725067149214242491217638159874870231232083350316908181
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=113, solvetime=7, target=1030547100765873269141128620116458972494307255475499243985321342313, sumTarget=27581991993833598336290342862607676610654182125706731327335638250494
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=114, solvetime=23, target=1003952911074263948567661808734323613027746995389175474821014687840, sumTarget=28585944904907862284858004671342000223681929121095906802156652938334
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=115, solvetime=17, target=986527029569557029805613436887953985151913969330686242131085058152, sumTarget=29572471934477419314663618108229954208833843090426593044287737996486
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=116, solvetime=7, target=965625237607289107378092462499500833238160285797016655253861692072, sumTarget=30538097172084708422041710570729455042072003376223609699541599688558
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=117, solvetime=15, target=939171254756303959172752395870904444400979498039575994027660537313, sumTarget=31477268426841012381214462966600359486472982874263185693569260225871
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=118, solvetime=40, target=917286795719748359104273540861995550510879251922603928905976016716, sumTarget=32394555222560760740318736507462355036983862126185789622475236242587
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=119, solvetime=11, target=908926810448792272771707551514024058287791932277808527761720264195, sumTarget=33303482033009553013090444058976379095271654058463598150236956506782
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=120, solvetime=4, target=884867316597684294401158151806736698286274480641467205582816513250, sumTarget=34188349349607237307491602210783115793557928539105065355819773020032
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=121, solvetime=23, target=857239253855211511495765431285461798874376894164770605709697130450, sumTarget=35045588603462448818987367642068577592432305433269835961529470150482
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=122, solvetime=15, target=839706083637679139095498333018992784777801315367657777380173727068, sumTarget=35885294687100127958082865975087570377210106748637493738909643877550
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=123, solvetime=0, target=818005722278812669701690247208208974061929767699914836594610340877, sumTarget=36703300409378940627784556222295779351272036516337408575504254218427
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=124, solvetime=20, target=788789055072572090312217975262053390040628246922916587648869513490, sumTarget=37492089464451512718096774197557832741312664763260325163153123731917
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=125, solvetime=15, target=769914775944721547434216796744342048184770758893008813824423938929, sumTarget=38262004240396234265530990994302174789497435522153333976977547670846
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=126, solvetime=79, target=748594801840418050030456922786637879524259170934931348488329386746, sumTarget=39010599042236652315561447917088812669021694693088265325465877057592
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=127, solvetime=6, target=758325156579735612378453008682242441203194550559419039392851066907, sumTarget=39768924198816387927939900925771055110224889243647684364858728124499
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=128, solvetime=7, target=732305205339726162982270517116775387531744998880955651435915633002, sumTarget=40501229404156114090922171442887830497756634242528640016294643757501
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=129, solvetime=25, target=707071631597130549911668463516809516955510748525358390610093654015, sumTarget=41208301035753244640833839906404640014712144991053998406904737411516
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=130, solvetime=90, target=690433346779755938539271196981624019745583280070856007729000135765, sumTarget=41898734382533000579373111103386264034457728271124854414633737547281
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=131, solvetime=19, target=703255567152316119370479671529183620695008588617496409338178748752, sumTarget=42601989949685316698743590774915447655152736859742350823971916296033
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=132, solvetime=1, target=683337661454254345647987245106869158328291723948250023383139193711, sumTarget=43285327611139571044391578020022316813481028583690600847355055489744
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=133, solvetime=121, target=655671803824308959756403836869422884224996192756811020500617458814, sumTarget=43940999414963880004147981856891739697706024776447411867855672948558
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=134, solvetime=45, target=680098273840876728412224107627277463004280785010489868674040822054, sumTarget=44621097688804756732560205964519017160710305561457901736529713770612
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=135, solvetime=21, target=670703196329138708683661541794483483265165882694200914143249684688, sumTarget=45291800885133895441243867506313500643975471444152102650672963455300
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=136, solvetime=84, target=650979141828113834148405223335757684638199766723149433946112516207, sumTarget=45942780026962009275392272729649258328613671210875252084619075971507
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=137, solvetime=60, target=657027543013648783002985925268860582832561177363054638128211508223, sumTarget=46599807569975658058395258654918118911446232388238306722747287479730
2023-04-05 17:22:37.577 [DBG] CHAN: lwmaCalculateNextWorkRequired: block height=138, solvetime=48, target=652597006849924936170180791057029110944169851770348427284077717257, sumTarget=47252404576825582994565439445975148022390402240008655150031365196987