@@ -16,12 +16,13 @@ import (
16
16
)
17
17
18
18
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 }
20
20
}
21
21
22
22
type DecryptionKeyHandler struct {
23
23
config * Config
24
24
snapshot * Snapshot
25
+ dbpool * pgxpool.Pool
25
26
}
26
27
27
28
func NewEonPublicKeyHandler (config * Config , snapshot * Snapshot ) p2p.MessageHandler {
@@ -111,23 +112,33 @@ func (handler *EonPublicKeyHandler) ValidateMessage(_ context.Context, msg p2pms
111
112
return true , nil
112
113
}
113
114
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 ) {
115
116
var result []p2pmsg.Message
116
117
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 {
119
132
return result , nil
120
133
}
121
134
log .Printf ("Sending key %X for proposal %X to hub" , key .Key , key .EpochID )
122
135
123
136
metricKeysGenerated .Inc ()
124
137
125
- err : = handler .snapshot .hubapi .SubmitProposalKey (key .EpochID , key .Key )
138
+ err = handler .snapshot .hubapi .SubmitProposalKey (key .EpochID , key .Key )
126
139
if err != nil {
127
140
return result , err
128
141
}
129
- // FIXME: Apart from needing to be in DB we need to keep track of the proposals better
130
- seenProposals [string (key .EpochID )] = struct {}{}
131
142
return result , nil
132
143
}
133
144
@@ -137,7 +148,7 @@ func (handler *EonPublicKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.
137
148
eonID := eonPubKeyMsg .GetEon ()
138
149
key := eonPubKeyMsg .GetPublicKey ()
139
150
db := snpdb .New (handler .dbpool )
140
- err := db .InsertEonPublicKey (
151
+ rows , err := db .InsertEonPublicKey (
141
152
ctx , snpdb.InsertEonPublicKeyParams {
142
153
EonID : int64 (eonID ),
143
154
EonPublicKey : key ,
@@ -146,8 +157,8 @@ func (handler *EonPublicKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.
146
157
if err != nil {
147
158
return nil , err
148
159
}
149
- _ , seen := seenEons [ eonID ]
150
- if seen {
160
+ // we have already seen the eon
161
+ if rows == 0 {
151
162
return nil , nil
152
163
}
153
164
@@ -158,7 +169,6 @@ func (handler *EonPublicKeyHandler) HandleMessage(ctx context.Context, m p2pmsg.
158
169
if err != nil {
159
170
return nil , err
160
171
}
161
- seenEons [eonID ] = struct {}{}
162
172
163
173
return nil , nil
164
174
}
0 commit comments