Skip to content

Commit

Permalink
Use random beacon as salt when computing sigchain miner
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 89eaaa6 commit a4a4559
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion chain/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func GetNextBlockSigner(height uint32, timestamp int64) ([]byte, []byte, pb.Winn
return nil, nil, 0, err
}

publicKey, chordID, err = sigChain.GetMiner(height)
publicKey, chordID, err = sigChain.GetMiner(height, header.UnsignedHeader.RandomBeacon)
if err != nil {
return nil, nil, 0, err
}
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ var (
heights: []uint32{2633000, 0},
values: []int32{10, 0},
}
SigChainMinerSalt = HeightDependentBool{
heights: []uint32{2900000, 0},
values: []bool{true, false},
}
)

var (
Expand Down
12 changes: 9 additions & 3 deletions pb/sigchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (sc *SigChain) GetSignerIndex(pubkey []byte) (int, error) {
return idx, err
}

func (sc *SigChain) GetMiner(height uint32) ([]byte, []byte, error) {
func (sc *SigChain) GetMiner(height uint32, randomBeacon []byte) ([]byte, []byte, error) {
if !sc.IsComplete() {
return nil, nil, errors.New("sigchain is not complete")
}
Expand Down Expand Up @@ -235,15 +235,21 @@ func (sc *SigChain) GetMiner(height uint32) ([]byte, []byte, error) {
return nil, nil, err
}

h := sigHash
if config.SigChainMinerSalt.GetValueAtHeight(height) {
hs := sha256.Sum256(append(sigHash, randomBeacon...))
h = hs[:]
}

x := big.NewInt(0)
x.SetBytes(sigHash)
x.SetBytes(h)
y := big.NewInt(int64(elemLen))
newIndex := big.NewInt(0)
newIndex.Mod(x, y)

originalIndex := minerElems[newIndex.Int64()].index
if originalIndex == 0 {
return sc.SrcPubkey, sc.Elems[0].Id, nil
return nil, nil, errors.New("invalid miner index")
}

return sc.Elems[originalIndex-1].NextPubkey, sc.Elems[originalIndex].Id, nil
Expand Down

0 comments on commit a4a4559

Please sign in to comment.