Skip to content

Commit

Permalink
Merge branch 'main' into integratiointest
Browse files Browse the repository at this point in the history
  • Loading branch information
ping-ke authored Sep 2, 2024
2 parents a246cce + 2eac50b commit 2bd749f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 deletions.
28 changes: 17 additions & 11 deletions ethstorage/miner/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
78 changes: 78 additions & 0 deletions ethstorage/miner/algorithm_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
7 changes: 5 additions & 2 deletions ethstorage/miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2bd749f

Please sign in to comment.