Skip to content

Commit 0d71005

Browse files
ezdaculope
authored andcommitted
fix: get seen eons / epochs from db instead of map
1 parent 0903a0e commit 0d71005

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

rolling-shutter/db/snpdb/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ INSERT INTO decryption_key (
1616
)
1717
ON CONFLICT DO NOTHING;
1818

19-
-- name: InsertEonPublicKey :exec
19+
-- name: InsertEonPublicKey :execrows
2020
INSERT INTO eon_public_key (
2121
eon_id,
2222
eon_public_key

rolling-shutter/db/snpdb/query.sqlc.gen.go

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rolling-shutter/snapshot/handler.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616
)
1717

1818
func NewDecryptionKeyHandler(config *Config, snapshot *Snapshot) p2p.MessageHandler {
19-
return &DecryptionKeyHandler{config: config, snapshot: snapshot}
19+
return &DecryptionKeyHandler{config: config, snapshot: snapshot, dbpool: snapshot.dbpool}
2020
}
2121

2222
type DecryptionKeyHandler struct {
2323
config *Config
2424
snapshot *Snapshot
25+
dbpool *pgxpool.Pool
2526
}
2627

2728
func NewEonPublicKeyHandler(config *Config, snapshot *Snapshot) p2p.MessageHandler {
@@ -111,23 +112,33 @@ func (handler *EonPublicKeyHandler) ValidateMessage(_ context.Context, msg p2pms
111112
return true, nil
112113
}
113114

114-
func (handler *DecryptionKeyHandler) HandleMessage(_ context.Context, m p2pmsg.Message) ([]p2pmsg.Message, error) {
115+
func (handler *DecryptionKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.Message) ([]p2pmsg.Message, error) {
115116
var result []p2pmsg.Message
116117
key := m.(*p2pmsg.DecryptionKey)
117-
_, seen := seenProposals[string(key.EpochID)]
118-
if seen {
118+
db := snpdb.New(handler.dbpool)
119+
120+
rows, err := db.InsertDecryptionKey(
121+
ctx, snpdb.InsertDecryptionKeyParams{
122+
EpochID: key.EpochID,
123+
Key: key.Key,
124+
},
125+
)
126+
if err != nil {
127+
return result, err
128+
}
129+
130+
// already seen
131+
if rows == 0 {
119132
return result, nil
120133
}
121134
log.Printf("Sending key %X for proposal %X to hub", key.Key, key.EpochID)
122135

123136
metricKeysGenerated.Inc()
124137

125-
err := handler.snapshot.hubapi.SubmitProposalKey(key.EpochID, key.Key)
138+
err = handler.snapshot.hubapi.SubmitProposalKey(key.EpochID, key.Key)
126139
if err != nil {
127140
return result, err
128141
}
129-
// FIXME: Apart from needing to be in DB we need to keep track of the proposals better
130-
seenProposals[string(key.EpochID)] = struct{}{}
131142
return result, nil
132143
}
133144

@@ -137,7 +148,7 @@ func (handler *EonPublicKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.
137148
eonID := eonPubKeyMsg.GetEon()
138149
key := eonPubKeyMsg.GetPublicKey()
139150
db := snpdb.New(handler.dbpool)
140-
err := db.InsertEonPublicKey(
151+
rows, err := db.InsertEonPublicKey(
141152
ctx, snpdb.InsertEonPublicKeyParams{
142153
EonID: int64(eonID),
143154
EonPublicKey: key,
@@ -146,8 +157,8 @@ func (handler *EonPublicKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.
146157
if err != nil {
147158
return nil, err
148159
}
149-
_, seen := seenEons[eonID]
150-
if seen {
160+
// we have already seen the eon
161+
if rows == 0 {
151162
return nil, nil
152163
}
153164

@@ -158,7 +169,6 @@ func (handler *EonPublicKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.
158169
if err != nil {
159170
return nil, err
160171
}
161-
seenEons[eonID] = struct{}{}
162172

163173
return nil, nil
164174
}

rolling-shutter/snapshot/snapshot.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ import (
2020
"github.com/shutter-network/rolling-shutter/rolling-shutter/snapshot/snpjrpc"
2121
)
2222

23-
// FIXME: Needs to be in DB.
24-
var (
25-
seenEons = make(map[uint64]struct{})
26-
seenProposals = make(map[string]struct{})
27-
zeroTXHash = make([]byte, 32)
28-
)
23+
var zeroTXHash = make([]byte, 32)
2924

3025
type Snapshot struct {
3126
Config *Config

0 commit comments

Comments
 (0)