Skip to content

Commit

Permalink
Add length limit for sigchain bit shift
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Apr 18, 2021
1 parent 2946fbd commit f51617d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
19 changes: 17 additions & 2 deletions chain/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ func GetNextBlockSigner(height uint32, timestamp int64) ([]byte, []byte, pb.Winn

switch winnerType {
case pb.WinnerType_TXN_SIGNER:
whash, _ := common.Uint256ParseFromBytes(header.UnsignedHeader.WinnerHash)
whash, err := common.Uint256ParseFromBytes(header.UnsignedHeader.WinnerHash)
if err != nil {
return nil, nil, 0, err
}

txn, err := DefaultLedger.Store.GetTransaction(whash)
if err != nil {
return nil, nil, 0, err
Expand All @@ -277,7 +281,18 @@ func GetNextBlockSigner(height uint32, timestamp int64) ([]byte, []byte, pb.Winn
sigChainTxn := payload.(*pb.SigChainTxn)
sigChain := &pb.SigChain{}
proto.Unmarshal(sigChainTxn.SigChain, sigChain)
publicKey, chordID, err = sigChain.GetMiner()

blockHash, err := common.Uint256ParseFromBytes(sigChain.BlockHash)
if err != nil {
return nil, nil, 0, err
}

height, err := DefaultLedger.Store.GetHeightByBlockHash(blockHash)
if err != nil {
return nil, nil, 0, err
}

publicKey, chordID, err = sigChain.GetMiner(height)
if err != nil {
return nil, nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/nknd/nknd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
)

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

var (
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ var (
heights: []uint32{1200000, 0},
values: []bool{false, true},
}
SigChainBitShiftMaxLength = HeightDependentInt32{
heights: []uint32{2543000, 0},
values: []int32{14, 0},
}
)

var (
Expand Down
15 changes: 10 additions & 5 deletions pb/sigchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/nkn/v2/common/serialization"
"github.com/nknorg/nkn/v2/config"
)

const (
Expand Down Expand Up @@ -204,7 +205,7 @@ func (sc *SigChain) GetSignerIndex(pubkey []byte) (int, error) {
return idx, err
}

func (sc *SigChain) GetMiner() ([]byte, []byte, error) {
func (sc *SigChain) GetMiner(height uint32) ([]byte, []byte, error) {
if !sc.IsComplete() {
return nil, nil, errors.New("sigchain is not complete")
}
Expand Down Expand Up @@ -233,7 +234,7 @@ func (sc *SigChain) GetMiner() ([]byte, []byte, error) {
return nil, nil, errors.New("no mining element")
}

sigHash, err := sc.SignatureHash()
sigHash, err := sc.SignatureHash(height)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -281,12 +282,12 @@ func (sc *SigChain) LastRelayHash() ([]byte, error) {
return prevHash, nil
}

func (sc *SigChain) SignatureHash() ([]byte, error) {
func (sc *SigChain) SignatureHash(height uint32) ([]byte, error) {
lastRelayHash, err := sc.LastRelayHash()
if err != nil {
return nil, err
}
sigHash := ComputeSignatureHash(lastRelayHash, sc.Length())
sigHash := ComputeSignatureHash(lastRelayHash, sc.Length(), height)
return sigHash, nil
}

Expand All @@ -307,9 +308,13 @@ func (sc *SigChain) ToMap() map[string]interface{} {
}
}

func ComputeSignatureHash(lastRelayHash []byte, sigChainLen int) []byte {
func ComputeSignatureHash(lastRelayHash []byte, sigChainLen int, height uint32) []byte {
h := sha256.Sum256(lastRelayHash)
sigHash := h[:]
maxSigChainLen := int(config.SigChainBitShiftMaxLength.GetValueAtHeight(height))
if maxSigChainLen > 0 && sigChainLen > maxSigChainLen {
sigChainLen = maxSigChainLen
}
rightShiftBytes(sigHash, bitShiftPerSigChainElement*sigChainLen)
return sigHash
}
Expand Down
2 changes: 1 addition & 1 deletion por/porpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewPorPackage(txn *transaction.Transaction, shouldVerify bool) (*PorPackage
}

txHash := txn.Hash()
sigHash, err := sigChain.SignatureHash()
sigHash, err := sigChain.SignatureHash(height)
if err != nil {
return nil, err
}
Expand Down
27 changes: 24 additions & 3 deletions por/porserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,20 @@ func (ps *PorServer) ShouldSignDestSigChainElem(blockHash, lastHash []byte, sigC
if _, ok := ps.finalizedBlockCache.Get(blockHash); ok {
return false
}

blockHashUint256, err := common.Uint256ParseFromBytes(blockHash)
if err != nil {
return false
}

height, err := Store.GetHeightByBlockHash(blockHashUint256)
if err != nil {
return false
}

if v, ok := ps.destSigChainElemCache.Get(blockHash); ok {
if currentDestSigChainElem, ok := v.(*destSigChainElem); ok {
sigHash := pb.ComputeSignatureHash(lastHash, sigChainLen)
sigHash := pb.ComputeSignatureHash(lastHash, sigChainLen, height)
if bytes.Compare(sigHash, currentDestSigChainElem.sigHash) >= 0 {
return false
}
Expand All @@ -386,8 +397,18 @@ func (ps *PorServer) AddDestSigChainElem(blockHash, lastHash []byte, sigChainLen
return false, nil
}

err := ps.destSigChainElemCache.Set(blockHash, &destSigChainElem{
sigHash: pb.ComputeSignatureHash(lastHash, sigChainLen),
blockHashUint256, err := common.Uint256ParseFromBytes(blockHash)
if err != nil {
return false, err
}

height, err := Store.GetHeightByBlockHash(blockHashUint256)
if err != nil {
return false, err
}

err = ps.destSigChainElemCache.Set(blockHash, &destSigChainElem{
sigHash: pb.ComputeSignatureHash(lastHash, sigChainLen, height),
sigChainElem: destElem,
prevHash: lastHash,
})
Expand Down

0 comments on commit f51617d

Please sign in to comment.