From fc71a260a8aeadae166f419e5127a6ab751c2132 Mon Sep 17 00:00:00 2001 From: Yilun Date: Wed, 21 Jul 2021 00:31:56 -0700 Subject: [PATCH] Add weight when selecting sigchain miner for better security Signed-off-by: Yilun --- cmd/nknd/nknd.go | 2 +- config/config.go | 8 ++++++++ pb/sigchain.go | 14 +++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/nknd/nknd.go b/cmd/nknd/nknd.go index 8b75cf154..adecb25b9 100644 --- a/cmd/nknd/nknd.go +++ b/cmd/nknd/nknd.go @@ -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 ( diff --git a/config/config.go b/config/config.go index d21bb6d3f..a92d0f82c 100644 --- a/config/config.go +++ b/config/config.go @@ -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 ( diff --git a/pb/sigchain.go b/pb/sigchain.go index 8c3c48022..2358829a5 100644 --- a/pb/sigchain.go +++ b/pb/sigchain.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "math" "math/big" "github.com/nknorg/nkn/v2/common" @@ -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)