-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign gossip package to refine the API
- Loading branch information
Showing
29 changed files
with
2,028 additions
and
856 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package auditor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/bbva/qed/gossip" | ||
"github.com/bbva/qed/hashing" | ||
"github.com/bbva/qed/log" | ||
"github.com/bbva/qed/protocol" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var ( | ||
QedAuditorInstancesCount = prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Name: "qed_auditor_instances_count", | ||
Help: "Number of auditor agents running.", | ||
}, | ||
) | ||
|
||
QedAuditorBatchesProcessSeconds = prometheus.NewSummary( | ||
prometheus.SummaryOpts{ | ||
Name: "qed_auditor_batches_process_seconds", | ||
Help: "Duration of Auditor batch processing", | ||
}, | ||
) | ||
|
||
QedAuditorBatchesReceivedTotal = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "qed_auditor_batches_received_total", | ||
Help: "Number of batches received by auditors.", | ||
}, | ||
) | ||
|
||
QedAuditorGetMembershipProofErrTotal = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "qed_auditor_get_membership_proof_err_total", | ||
Help: "Number of errors trying to get membership proofs by auditors.", | ||
}, | ||
) | ||
) | ||
|
||
type Auditor struct{} | ||
|
||
func (a Auditor) Metrics() []prometheus.Collector { | ||
return []prometheus.Collector{ | ||
QedAuditorInstancesCount, | ||
QedAuditorBatchesProcessSeconds, | ||
QedAuditorBatchesReceivedTotal, | ||
QedAuditorGetMembershipProofErrTotal, | ||
} | ||
} | ||
|
||
func (a Auditor) Process(agent *gossip.Agent, ctx context.Context) error { | ||
QedAuditorBatchesReceivedTotal.Inc() | ||
|
||
qed := agent.Qed() | ||
store := agent.SnapshotStore() | ||
b := ctx.Value("batch").(*protocol.BatchSnapshots) | ||
s := b.Snapshots[0] | ||
|
||
return agent.Task(func() error { | ||
timer := prometheus.NewTimer(QedAuditorBatchesProcessSeconds) | ||
defer timer.ObserveDuration() | ||
|
||
proof, err := qed.MembershipDigest(s.Snapshot.EventDigest, s.Snapshot.Version) | ||
if err != nil { | ||
log.Infof("Auditor is unable to get membership proof from QED server: %v", err) | ||
|
||
switch fmt.Sprintf("%T", err) { | ||
case "*errors.errorString": | ||
agent.Alert(fmt.Sprintf("Auditor is unable to get membership proof from QED server: %v", err)) | ||
default: | ||
QedAuditorGetMembershipProofErrTotal.Inc() | ||
} | ||
|
||
return err | ||
} | ||
|
||
storedSnap, err := store.GetSnapshot(proof.CurrentVersion) | ||
if err != nil { | ||
log.Infof("Unable to get snapshot from storage: %v", err) | ||
return err | ||
} | ||
|
||
checkSnap := &protocol.Snapshot{ | ||
HistoryDigest: s.Snapshot.HistoryDigest, | ||
HyperDigest: storedSnap.Snapshot.HyperDigest, | ||
Version: s.Snapshot.Version, | ||
EventDigest: s.Snapshot.EventDigest, | ||
} | ||
|
||
ok := qed.DigestVerify(proof, checkSnap, hashing.NewSha256Hasher) | ||
if !ok { | ||
agent.Alert(fmt.Sprintf("Unable to verify snapshot %v", s.Snapshot)) | ||
log.Infof("Unable to verify snapshot %v", s.Snapshot) | ||
} | ||
|
||
log.Infof("MembershipTask.Do(): Snapshot %v has been verified by QED", s.Snapshot) | ||
return nil | ||
}) | ||
} |
Oops, something went wrong.