Skip to content

Commit

Permalink
Merge pull request #81 from iknite/new-riot
Browse files Browse the repository at this point in the history
New riot
  • Loading branch information
aalda authored Mar 14, 2019
2 parents 2951295 + eb603af commit d9fd34f
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 849 deletions.
41 changes: 25 additions & 16 deletions balloon/balloon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package balloon

import (
"errors"
"fmt"
"sync"

Expand Down Expand Up @@ -265,35 +266,38 @@ func (b Balloon) QueryDigestMembership(keyDigest hashing.Digest, version uint64)
leaf, err = b.store.Get(storage.IndexPrefix, proof.KeyDigest)
switch {
case err != nil && err != storage.ErrKeyNotFound:
return nil, fmt.Errorf("Error reading leaf %v data: %v", proof.KeyDigest, err)
return nil, fmt.Errorf("error reading leaf %v data: %v", proof.KeyDigest, err)

case err != nil && err == storage.ErrKeyNotFound:
proof.Exists = false
proof.ActualVersion = version
leaf = &storage.KVPair{keyDigest, util.Uint64AsBytes(version)}
leaf = &storage.KVPair{Key: keyDigest, Value: util.Uint64AsBytes(version)}

case err == nil:
proof.Exists = true
proof.ActualVersion = util.BytesAsUint64(leaf.Value)
}

if proof.ActualVersion <= version {
wg.Add(1)
go func() {
defer wg.Done()
historyProof, historyErr = b.historyTree.ProveMembership(proof.ActualVersion, version)
}()
} else {
return nil, fmt.Errorf("Query version %d is not on history tree which version is %d", version, proof.ActualVersion)
if proof.ActualVersion <= version {
wg.Add(1)
go func() {
defer wg.Done()
historyProof, historyErr = b.historyTree.ProveMembership(proof.ActualVersion, version)
}()
} else {
return nil, fmt.Errorf("query version %d is not on history tree which version is %d", version, proof.ActualVersion)
}

}

hyperProof, hyperErr = b.hyperTree.QueryMembership(leaf.Key, leaf.Value)

wg.Wait()
if hyperErr != nil {
return nil, fmt.Errorf("Unable to get proof from hyper tree: %v", err)
return nil, fmt.Errorf("unable to get proof from hyper tree: %v", err)
}

if historyErr != nil {
return nil, fmt.Errorf("Unable to get proof from history tree: %v", err)
return nil, fmt.Errorf("unable to get proof from history tree: %v", err)
}

proof.HyperProof = hyperProof
Expand All @@ -310,20 +314,25 @@ func (b Balloon) QueryConsistency(start, end uint64) (*IncrementalProof, error)

// Metrics
metrics.QedBalloonIncrementalTotal.Inc()
//timer := prometheus.NewTimer(metrics.QedBalloonIncrementalDurationSeconds)
//defer timer.ObserveDuration()

stats := metrics.Balloon
stats.AddFloat("QueryConsistency", 1)
var proof IncrementalProof

if start >= b.version ||
end >= b.version ||
start >= end {

return nil, errors.New("unable to process proof from history tree: invalid range")
}

proof.Start = start
proof.End = end
proof.Hasher = b.hasherF()

historyProof, err := b.historyTree.ProveConsistency(start, end)
if err != nil {
return nil, fmt.Errorf("Unable to get proof from history tree: %v", err)
return nil, fmt.Errorf("unable to get proof from history tree: %v", err)
}
proof.AuditPath = historyProof.AuditPath

Expand Down
7 changes: 7 additions & 0 deletions balloon/balloon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func TestQueryMembership(t *testing.T) {
{[]byte{0x5a}, uint64(0)},
}

// Asking for a future/wrong membership should not fail
_, err = balloon.QueryMembership([]byte{0x10}, 15)
require.NoError(t, err)

for i, c := range testCases {
_, mutations, err := balloon.Add(c.key)
require.NoErrorf(t, err, "Error adding event %d", i)
Expand Down Expand Up @@ -165,6 +169,9 @@ func TestQueryConsistencyProof(t *testing.T) {
balloon, err := NewBalloon(store, hashing.NewFakeXorHasher)
require.NoError(t, err)

_, err = balloon.QueryConsistency(uint64(30), uint64(600))
require.Error(t, err, "Asking for a future/wrong consitency should fail")

for j := 0; j <= int(c.end); j++ {
_, mutations, err := balloon.Add(util.Uint64AsBytes(uint64(j)))
require.NoErrorf(t, err, "Error adding event %d", j)
Expand Down
8 changes: 0 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/bbva/qed/balloon"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/metrics"
"github.com/bbva/qed/protocol"
)

Expand Down Expand Up @@ -212,7 +211,6 @@ func (c HTTPClient) Ping() error {
// Add will do a request to the server with a post data to store a new event.
func (c *HTTPClient) Add(event string) (*protocol.Snapshot, error) {

metrics.ClientEventAdd.Inc()
data, _ := json.Marshal(&protocol.Event{Event: []byte(event)})

body, err := c.doReq("POST", "/events", data)
Expand All @@ -230,8 +228,6 @@ func (c *HTTPClient) Add(event string) (*protocol.Snapshot, error) {
// Membership will ask for a Proof to the server.
func (c *HTTPClient) Membership(key []byte, version uint64) (*protocol.MembershipResult, error) {

metrics.ClientQueryMembership.Inc()

query, _ := json.Marshal(&protocol.MembershipQuery{
Key: key,
Version: version,
Expand All @@ -252,8 +248,6 @@ func (c *HTTPClient) Membership(key []byte, version uint64) (*protocol.Membershi
// Membership will ask for a Proof to the server.
func (c *HTTPClient) MembershipDigest(keyDigest hashing.Digest, version uint64) (*protocol.MembershipResult, error) {

metrics.ClientQueryMembership.Inc()

query, _ := json.Marshal(&protocol.MembershipDigest{
KeyDigest: keyDigest,
Version: version,
Expand All @@ -274,8 +268,6 @@ func (c *HTTPClient) MembershipDigest(keyDigest hashing.Digest, version uint64)
// Incremental will ask for an IncrementalProof to the server.
func (c *HTTPClient) Incremental(start, end uint64) (*protocol.IncrementalResponse, error) {

metrics.ClientQueryIncremental.Inc()

query, _ := json.Marshal(&protocol.IncrementalRequest{
Start: start,
End: end,
Expand Down
25 changes: 0 additions & 25 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,6 @@ var (
},
)

// Client

ClientEventAdd = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "client_event_add",
Help: "Number of events added into the cluster.",
},
)
ClientQueryMembership = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "client_query_membership",
Help: "Number of single events directly verified into the cluster.",
},
)
ClientQueryIncremental = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "client_query_incremental",
Help: "Number of range of verified events queried into the cluster.",
},
)

// PROMETHEUS

metricsList = []prometheus.Collector{
Expand All @@ -190,10 +169,6 @@ var (

QedSenderInstancesCount,
QedSenderBatchesSentTotal,

ClientEventAdd,
ClientQueryMembership,
ClientQueryIncremental,
}

registerMetrics sync.Once
Expand Down
8 changes: 7 additions & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ type IncrementalResponse struct {
// ToMembershipProof translates internal api balloon.MembershipProof to the
// public struct protocol.MembershipResult.
func ToMembershipResult(key []byte, mp *balloon.MembershipProof) *MembershipResult {

var serialized map[string]hashing.Digest
if mp.HistoryProof != nil && mp.HistoryProof.AuditPath != nil {
serialized = mp.HistoryProof.AuditPath.Serialize()
}

return &MembershipResult{
mp.Exists,
mp.HyperProof.AuditPath,
mp.HistoryProof.AuditPath.Serialize(),
serialized,
mp.CurrentVersion,
mp.QueryVersion,
mp.ActualVersion,
Expand Down
Loading

0 comments on commit d9fd34f

Please sign in to comment.