Skip to content

Commit

Permalink
Reduce non-optimal sigchain verification overhead
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed May 11, 2021
1 parent d30240d commit e22f43e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
2 changes: 1 addition & 1 deletion node/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func (localNode *LocalNode) requestSignatureChainTransaction(neighbor *RemoteNod
return nil, err
}

porPkg, err := por.NewPorPackage(txn, false)
porPkg, err := por.NewPorPackage(txn)
if err != nil {
return nil, fmt.Errorf("create por package from txn error: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions por/minercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package por
import (
"sort"
"sync"
"time"

"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/nkn/v2/config"
Expand All @@ -21,8 +20,8 @@ type RecentMiner map[string]int
type SkipMiner [][]byte

func init() {
recentMinerCache = common.NewGoCache(5*time.Minute, time.Minute)
skipMinerCache = common.NewGoCache(5*time.Minute, time.Minute)
recentMinerCache = common.NewGoCache(10*config.ConsensusDuration, config.ConsensusDuration)
skipMinerCache = common.NewGoCache(10*config.ConsensusDuration, config.ConsensusDuration)
}

func GetRecentMiner(blockHash []byte) (RecentMiner, error) {
Expand Down
15 changes: 4 additions & 11 deletions por/porpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c PorPackages) Less(i, j int) bool {
return false
}

func NewPorPackage(txn *transaction.Transaction, shouldVerify bool) (*PorPackage, error) {
func NewPorPackage(txn *transaction.Transaction) (*PorPackage, error) {
if txn.UnsignedTx.Payload.Type != pb.PayloadType_SIG_CHAIN_TXN_TYPE {
return nil, errors.New("Transaction type should be sigchain")
}
Expand Down Expand Up @@ -96,16 +96,9 @@ func NewPorPackage(txn *transaction.Transaction, shouldVerify bool) (*PorPackage
return nil, err
}

if shouldVerify {
err = VerifyID(sigChain, height)
if err != nil {
return nil, err
}

err = VerifySigChain(sigChain, height)
if err != nil {
return nil, err
}
err = VerifySigChainLight(sigChain, height)
if err != nil {
return nil, err
}

txHash := txn.Hash()
Expand Down
4 changes: 2 additions & 2 deletions por/porserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func VerifyID(sc *pb.SigChain, height uint32) error {
}

func (ps *PorServer) AddSigChainFromTx(txn *transaction.Transaction, currentHeight uint32) (*PorPackage, error) {
porPkg, err := NewPorPackage(txn, false)
porPkg, err := NewPorPackage(txn)
if err != nil {
return nil, err
}
Expand All @@ -428,7 +428,7 @@ func (ps *PorServer) AddSigChainFromTx(txn *transaction.Transaction, currentHeig
return nil, err
}

err = VerifySigChain(porPkg.SigChain, porPkg.Height)
err = VerifySigChainSignatures(porPkg.SigChain)
if err != nil {
return nil, err
}
Expand Down
8 changes: 3 additions & 5 deletions por/sigchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
"github.com/nknorg/nnet/overlay/chord"
)

func VerifySigChain(sc *pb.SigChain, height uint32) error {
// VerifySigChainLight performs light-weighted sigchain verification without
// verifying signature (CPU intensive) and ID (IO intensive).
func VerifySigChainLight(sc *pb.SigChain, height uint32) error {
if err := VerifySigChainMeta(sc, height); err != nil {
return err
}
Expand All @@ -24,10 +26,6 @@ func VerifySigChain(sc *pb.SigChain, height uint32) error {
return err
}

if err := VerifySigChainSignatures(sc); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit e22f43e

Please sign in to comment.