From 89e882e32463c246c95401c9ae8290e9aee72d0d Mon Sep 17 00:00:00 2001 From: syntrust Date: Tue, 27 Aug 2024 18:50:14 +0800 Subject: [PATCH] fix diff --- ethstorage/miner/algorithm.go | 28 ++++++----- ethstorage/miner/algorithm_test.go | 78 ++++++++++++++++++++++++++++++ ethstorage/miner/worker.go | 7 ++- 3 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 ethstorage/miner/algorithm_test.go diff --git a/ethstorage/miner/algorithm.go b/ethstorage/miner/algorithm.go index 7ba2e3d7..90503147 100644 --- a/ethstorage/miner/algorithm.go +++ b/ethstorage/miner/algorithm.go @@ -40,23 +40,29 @@ func initHash(miner common.Address, mixedHash common.Hash, nonce uint64) common. ) } -func expectedDiff(lastMineTime, minedTime uint64, difficulty, cutoff, diffAdjDivisor, minDiff *big.Int) *big.Int { - interval := new(big.Int).SetUint64(minedTime - lastMineTime) - diff := difficulty - if interval.Cmp(cutoff) < 0 { - // diff = diff + (diff-interval*diff/cutoff)/diffAdjDivisor - diff = new(big.Int).Add(diff, new(big.Int).Div( - new(big.Int).Sub(diff, new(big.Int).Div(new(big.Int).Mul(interval, diff), cutoff)), diffAdjDivisor)) +func expectedDiff(interval uint64, difficulty, cutoff, diffAdjDivisor, minDiff *big.Int) *big.Int { + diff := new(big.Int).Set(difficulty) + x := interval / cutoff.Uint64() + if x == 0 { + // diff = diff + ((1 - interval / _cutoff) * diff) / _diffAdjDivisor; + adjust := new(big.Int).Div(diff, diffAdjDivisor) + diff = new(big.Int).Add(diff, adjust) if diff.Cmp(minDiff) < 0 { diff = minDiff } } else { - // dec := (interval*diff/cutoff - diff) / diffAdjDivisor - dec := new(big.Int).Div(new(big.Int).Sub(new(big.Int).Div(new(big.Int).Mul(interval, diff), cutoff), diff), diffAdjDivisor) - if new(big.Int).Add(dec, minDiff).Cmp(diff) > 0 { + // diff = diff - ((interval / _cutoff - 1) * diff) / _diffAdjDivisor; + adjust := new(big.Int).Div( + new(big.Int).Mul( + new(big.Int).SetUint64(x-1), + diff, + ), + diffAdjDivisor, + ) + if new(big.Int).Add(adjust, minDiff).Cmp(diff) > 0 { diff = minDiff } else { - diff = new(big.Int).Sub(diff, dec) + diff = new(big.Int).Sub(diff, adjust) } } diff --git a/ethstorage/miner/algorithm_test.go b/ethstorage/miner/algorithm_test.go new file mode 100644 index 00000000..b99c8b72 --- /dev/null +++ b/ethstorage/miner/algorithm_test.go @@ -0,0 +1,78 @@ +// Copyright 2022-2023, EthStorage. +// For license information, see https://github.com/ethstorage/es-node/blob/main/LICENSE + +package miner + +import ( + "fmt" + "math/big" + "testing" +) + +func Test_expectedDiff(t *testing.T) { + type args struct { + interval uint64 + difficulty *big.Int + cutoff *big.Int + diffAdjDivisor *big.Int + minDiff *big.Int + } + tests := []struct { + name string + args args + want *big.Int + }{ + { + "decrease: diff not match case 1", + args{ + 1724219196 - 1724218692, + big.NewInt(16933920), + big.NewInt(7200), + big.NewInt(32), + big.NewInt(9437184), + }, + big.NewInt(17463105), + }, + { + "decrease: diff not match case 2", + args{ + 1724243184 - 1724242128, + big.NewInt(71922272), + big.NewInt(7200), + big.NewInt(32), + big.NewInt(9437184), + }, + big.NewInt(74169843), + }, + { + "no change", + args{ + 1723277844 - 1723267236, + big.NewInt(25034417035), + big.NewInt(7200), + big.NewInt(32), + big.NewInt(4718592000), + }, + big.NewInt(25034417035), + }, + { + "increase", + args{ + 1724507676 - 1724507412, + big.NewInt(18788404245), + big.NewInt(7200), + big.NewInt(32), + big.NewInt(4718592000), + }, + big.NewInt(19375541877), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fmt.Println("tt.args.interval", tt.args.interval) + if got := expectedDiff(tt.args.interval, tt.args.difficulty, tt.args.cutoff, tt.args.diffAdjDivisor, tt.args.minDiff); tt.want.Cmp(got) != 0 { + t.Errorf("got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/ethstorage/miner/worker.go b/ethstorage/miner/worker.go index 80010026..d94d67f7 100644 --- a/ethstorage/miner/worker.go +++ b/ethstorage/miner/worker.go @@ -322,9 +322,12 @@ func (w *worker) updateDifficulty(shardIdx, blockTime uint64) (*big.Int, error) return nil, err } w.lg.Info("Mining info retrieved", "shard", shardIdx, "lastMineTime", info.LastMineTime, "difficulty", info.Difficulty, "proofsSubmitted", info.BlockMined) + + if blockTime <= info.LastMineTime { + return nil, errors.New("minedTs too small") + } reqDiff := new(big.Int).Div(maxUint256, expectedDiff( - info.LastMineTime, - blockTime, + blockTime-info.LastMineTime, info.Difficulty, w.config.Cutoff, w.config.DiffAdjDivisor,