Skip to content

Commit 9dd443a

Browse files
committed
check if decryption key is already validated by checking the db and skip validation if key exists
1 parent d8b6751 commit 9dd443a

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

rolling-shutter/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/google/go-cmp v0.6.0
1717
github.com/google/uuid v1.4.0
1818
github.com/hashicorp/go-multierror v1.1.1
19+
github.com/hashicorp/golang-lru/v2 v2.0.7
1920
github.com/ipfs/go-log/v2 v2.5.1
2021
github.com/jackc/pgconn v1.14.1
2122
github.com/jackc/pgx/v4 v4.18.1
@@ -126,7 +127,6 @@ require (
126127
github.com/hashicorp/errwrap v1.1.0 // indirect
127128
github.com/hashicorp/go-bexpr v0.1.11 // indirect
128129
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
129-
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
130130
github.com/hashicorp/hcl v1.0.0 // indirect
131131
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
132132
github.com/holiman/uint256 v1.2.4 // indirect

rolling-shutter/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
418418
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
419419
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
420420
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
421-
github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
422-
github.com/hashicorp/golang-lru/v2 v2.0.5/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
421+
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
422+
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
423423
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
424424
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
425425
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7 h1:3JQNjnMRil1yD0IfZKHF9GxxWKDJGj8I0IqOUol//sw=

rolling-shutter/keyper/epochkghandler/key.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package epochkghandler
33
import (
44
"bytes"
55
"context"
6-
"math"
76

7+
lru "github.com/hashicorp/golang-lru/v2"
88
"github.com/jackc/pgx/v4"
99
"github.com/jackc/pgx/v4/pgxpool"
1010
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -13,6 +13,7 @@ import (
1313
"github.com/shutter-network/shutter/shlib/shcrypto"
1414

1515
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/database"
16+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
1617
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2p"
1718
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
1819
"github.com/shutter-network/rolling-shutter/rolling-shutter/shdb"
@@ -21,12 +22,16 @@ import (
2122
const MaxNumKeysPerMessage = 128
2223

2324
func NewDecryptionKeyHandler(config Config, dbpool *pgxpool.Pool) p2p.MessageHandler {
24-
return &DecryptionKeyHandler{config: config, dbpool: dbpool}
25+
// Not catching the error as it only can happen if non-positive size was applied
26+
cache, _ := lru.New[shcrypto.EpochSecretKey, []byte](1024)
27+
return &DecryptionKeyHandler{config: config, dbpool: dbpool, cache: cache}
2528
}
2629

2730
type DecryptionKeyHandler struct {
2831
config Config
2932
dbpool *pgxpool.Pool
33+
// keep 1024 verified keys in Cache to skip additional verifications
34+
cache *lru.Cache[shcrypto.EpochSecretKey, []byte]
3035
}
3136

3237
func (*DecryptionKeyHandler) MessagePrototypes() []p2pmsg.Message {
@@ -39,23 +44,23 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
3944
return pubsub.ValidationReject,
4045
errors.Errorf("instance ID mismatch (want=%d, have=%d)", handler.config.GetInstanceID(), key.GetInstanceID())
4146
}
42-
if key.Eon > math.MaxInt64 {
43-
return pubsub.ValidationReject, errors.Errorf("eon %d overflows int64", key.Eon)
47+
eon, err := medley.Uint64ToInt64Safe(key.Eon)
48+
if err != nil {
49+
return pubsub.ValidationReject, errors.Wrapf(err, "overflow error while converting eon to int64 %d", eon)
4450
}
45-
46-
dkgResultDB, err := database.New(handler.dbpool).GetDKGResultForKeyperConfigIndex(ctx, int64(key.Eon))
47-
if err == pgx.ErrNoRows {
48-
return pubsub.ValidationReject, errors.Errorf("no DKG result found for eon %d", key.Eon)
51+
dkgResultDB, err := database.New(handler.dbpool).GetDKGResultForKeyperConfigIndex(ctx, eon)
52+
if errors.Is(err, pgx.ErrNoRows) {
53+
return pubsub.ValidationReject, errors.Errorf("no DKG result found for eon %d", eon)
4954
}
5055
if err != nil {
51-
return pubsub.ValidationReject, errors.Wrapf(err, "failed to get dkg result for eon %d from db", key.Eon)
56+
return pubsub.ValidationReject, errors.Wrapf(err, "failed to get dkg result for eon %d from db", eon)
5257
}
5358
if !dkgResultDB.Success {
54-
return pubsub.ValidationReject, errors.Errorf("no successful DKG result found for eon %d", key.Eon)
59+
return pubsub.ValidationReject, errors.Errorf("no successful DKG result found for eon %d", eon)
5560
}
5661
pureDKGResult, err := shdb.DecodePureDKGResult(dkgResultDB.PureResult)
5762
if err != nil {
58-
return pubsub.ValidationReject, errors.Wrapf(err, "error while decoding pure DKG result for eon %d", key.Eon)
63+
return pubsub.ValidationReject, errors.Wrapf(err, "error while decoding pure DKG result for eon %d", eon)
5964
}
6065

6166
if len(key.Keys) == 0 {
@@ -64,19 +69,26 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
6469
if len(key.Keys) > MaxNumKeysPerMessage {
6570
return pubsub.ValidationReject, errors.Errorf("too many keys in message (%d > %d)", len(key.Keys), MaxNumKeysPerMessage)
6671
}
72+
6773
for i, k := range key.Keys {
6874
epochSecretKey, err := k.GetEpochSecretKey()
6975
if err != nil {
7076
return pubsub.ValidationReject, err
7177
}
78+
identity, exists := handler.cache.Get(*epochSecretKey)
79+
if exists {
80+
if bytes.Equal(k.Identity, identity) {
81+
continue
82+
}
83+
return pubsub.ValidationReject, errors.Errorf("epoch secret key for identity %x is not valid", k.Identity)
84+
}
7285
ok, err := shcrypto.VerifyEpochSecretKey(epochSecretKey, pureDKGResult.PublicKey, k.Identity)
7386
if err != nil {
7487
return pubsub.ValidationReject, errors.Wrapf(err, "error while checking epoch secret key for identity %x", k.Identity)
7588
}
7689
if !ok {
7790
return pubsub.ValidationReject, errors.Errorf("epoch secret key for identity %x is not valid", k.Identity)
7891
}
79-
8092
if i > 0 && bytes.Compare(k.Identity, key.Keys[i-1].Identity) < 0 {
8193
return pubsub.ValidationReject, errors.Errorf("keys not ordered")
8294
}
@@ -87,7 +99,15 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
8799
func (handler *DecryptionKeyHandler) HandleMessage(ctx context.Context, msg p2pmsg.Message) ([]p2pmsg.Message, error) {
88100
metricsEpochKGDecryptionKeysReceived.Inc()
89101
key := msg.(*p2pmsg.DecryptionKeys)
90-
// Insert the key into the db. We assume that it's valid as it already passed the libp2p
91-
// validator.
102+
// We assume that it's valid as it already passed the libp2p validator.
103+
// Insert the key into the cache.
104+
for _, k := range key.Keys {
105+
epochSecretKey, err := k.GetEpochSecretKey()
106+
if err != nil {
107+
return nil, err
108+
}
109+
handler.cache.Add(*epochSecretKey, k.Identity)
110+
}
111+
// Insert the key into the db.
92112
return nil, database.New(handler.dbpool).InsertDecryptionKeysMsg(ctx, key)
93113
}

0 commit comments

Comments
 (0)