Skip to content

Commit

Permalink
Add weight when selecting sigchain miner for better security
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Jul 22, 2021
1 parent a4a4559 commit fc71a26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/nknd/nknd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
)

const (
NetVersionNum = 25 // This is temporary and will be removed soon after mainnet is stabilized
NetVersionNum = 26 // This is temporary and will be removed soon after mainnet is stabilized
)

var (
Expand Down
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ var (
heights: []uint32{2900000, 0},
values: []bool{true, false},
}
SigChainMinerWeightBase = HeightDependentInt32{
heights: []uint32{2900000, 0},
values: []int32{2, 1},
}
SigChainMinerWeightMaxExponent = HeightDependentInt32{
heights: []uint32{2900000, 0},
values: []int32{4, 0},
}
)

var (
Expand Down
14 changes: 13 additions & 1 deletion pb/sigchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"math"
"math/big"

"github.com/nknorg/nkn/v2/common"
Expand Down Expand Up @@ -222,7 +223,18 @@ func (sc *SigChain) GetMiner(height uint32, randomBeacon []byte) ([]byte, []byte
index: i,
elem: e,
}
minerElems = append(minerElems, t)
base := config.SigChainMinerWeightBase.GetValueAtHeight(height)
exp := int(config.SigChainMinerWeightMaxExponent.GetValueAtHeight(height))
if exp > i-1 {
exp = i - 1
}
if exp > sc.Length()-2-i {
exp = sc.Length() - 2 - i
}
count := int(math.Pow(float64(base), float64(exp)))
for i := 0; i < count; i++ {
minerElems = append(minerElems, t)
}
}
}
elemLen := len(minerElems)
Expand Down

0 comments on commit fc71a26

Please sign in to comment.