Skip to content

Commit

Permalink
Merge pull request #68 from jllucas/client
Browse files Browse the repository at this point in the history
Client
  • Loading branch information
iknite authored Feb 21, 2019
2 parents 0a392b6 + afb3f59 commit bd98a7c
Show file tree
Hide file tree
Showing 24 changed files with 868 additions and 239 deletions.
35 changes: 33 additions & 2 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/bbva/qed/log"
"github.com/bbva/qed/metrics"
"github.com/bbva/qed/protocol"
"github.com/bbva/qed/raftwal"

"github.com/prometheus/client_golang/prometheus"
)

// HealthCheckResponse contains the response from HealthCheckHandler.
Expand Down Expand Up @@ -316,12 +316,14 @@ func AuthHandlerMiddleware(handler http.HandlerFunc) http.HandlerFunc {
// /events -> Add
// /proofs/membership -> Membership
func NewApiHttp(balloon raftwal.RaftBalloonApi) *http.ServeMux {

api := http.NewServeMux()
api.HandleFunc("/health-check", AuthHandlerMiddleware(HealthCheckHandler))
api.HandleFunc("/events", AuthHandlerMiddleware(Add(balloon)))
api.HandleFunc("/proofs/membership", AuthHandlerMiddleware(Membership(balloon)))
api.HandleFunc("/proofs/digest-membership", AuthHandlerMiddleware(DigestMembership(balloon)))
api.HandleFunc("/proofs/incremental", AuthHandlerMiddleware(Incremental(balloon)))
api.HandleFunc("/info/shards", AuthHandlerMiddleware(InfoShardsHandler(balloon)))

return api
}
Expand Down Expand Up @@ -360,3 +362,32 @@ func LogHandler(handle http.Handler) http.HandlerFunc {
}
}
}

func InfoShardsHandler(balloon raftwal.RaftBalloonApi) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.Header().Set("Allow", "GET")
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

var scheme string
if r.TLS != nil {
scheme = "https://"
} else {
scheme = "http://"
}

info := balloon.Info()
info["URIScheme"] = scheme

out, err := json.Marshal(info)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(out)
}
}
48 changes: 26 additions & 22 deletions api/apihttp/apihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,51 @@ func (b fakeRaftBalloon) Add(event []byte) (*balloon.Snapshot, error) {
return &balloon.Snapshot{hashing.Digest{0x02}, hashing.Digest{0x00}, hashing.Digest{0x01}, 0}, nil
}

func (b fakeRaftBalloon) Join(nodeID, addr string) error {
func (b fakeRaftBalloon) Join(nodeID, addr string, metadata map[string]string) error {
return nil
}

func (b fakeRaftBalloon) QueryDigestMembership(keyDigest hashing.Digest, version uint64) (*balloon.MembershipProof, error) {
return &balloon.MembershipProof{
true,
visitor.NewFakeVerifiable(true),
visitor.NewFakeVerifiable(true),
1,
1,
2,
keyDigest,
hashing.NewFakeXorHasher(),
Exists: true,
HyperProof: visitor.NewFakeVerifiable(true),
HistoryProof: visitor.NewFakeVerifiable(true),
CurrentVersion: 1,
QueryVersion: 1,
ActualVersion: 2,
KeyDigest: keyDigest,
Hasher: hashing.NewFakeXorHasher(),
}, nil
}

func (b fakeRaftBalloon) QueryMembership(event []byte, version uint64) (*balloon.MembershipProof, error) {
hasher := hashing.NewFakeXorHasher()
return &balloon.MembershipProof{
true,
visitor.NewFakeVerifiable(true),
visitor.NewFakeVerifiable(true),
1,
1,
2,
hasher.Do(event),
hasher,
Exists: true,
HyperProof: visitor.NewFakeVerifiable(true),
HistoryProof: visitor.NewFakeVerifiable(true),
CurrentVersion: 1,
QueryVersion: 1,
ActualVersion: 2,
KeyDigest: hasher.Do(event),
Hasher: hasher,
}, nil
}

func (b fakeRaftBalloon) QueryConsistency(start, end uint64) (*balloon.IncrementalProof, error) {
ip := balloon.IncrementalProof{
2,
8,
visitor.AuditPath{"0|0": hashing.Digest{0x00}},
hashing.NewFakeXorHasher(),
Start: 2,
End: 8,
AuditPath: visitor.AuditPath{"0|0": hashing.Digest{0x00}},
Hasher: hashing.NewFakeXorHasher(),
}
return &ip, nil
}

func (b fakeRaftBalloon) Info() map[string]interface{} {
return make(map[string]interface{})
}

func TestHealthCheckHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
Expand Down Expand Up @@ -385,7 +389,7 @@ func BenchmarkApiAdd(b *testing.B) {
r, clean := newNodeBench(b, 1)
defer clean()

err := r.Open(true)
err := r.Open(true, map[string]string{"foo": "bar"})
assert.NoError(b, err)

handler := Add(r)
Expand Down
24 changes: 18 additions & 6 deletions api/mgmthttp/mgmthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,46 @@ func NewMgmtHttp(raftBalloon raftwal.RaftBalloonApi) *http.ServeMux {

func joinHandle(raftBalloon raftwal.RaftBalloonApi) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := map[string]string{}
body := make(map[string]interface{})

if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

if len(m) != 2 {
if len(body) != 3 {
w.WriteHeader(http.StatusBadRequest)
return
}

remoteAddr, ok := m["addr"]
remoteAddr, ok := body["addr"].(string)
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}

nodeID, ok := m["id"]
nodeID, ok := body["id"].(string)
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}

if err := raftBalloon.Join(nodeID, remoteAddr); err != nil {
m, ok := body["metadata"].(map[string]interface{})
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
// TO IMPROVE: use map[string]interface{} for nested metadata.
metadata := make(map[string]string)
for k, v := range m {
metadata[k] = v.(string)
}

if err := raftBalloon.Join(nodeID, remoteAddr, metadata); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
}
}
1 change: 0 additions & 1 deletion balloon/history/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func (p IncrementalProof) Verify(startDigest, endDigest hashing.Digest) (correct
// visit the pruned trees
startRecomputed := startPruned.PostOrder(computeHash).(hashing.Digest)
endRecomputed := endPruned.PostOrder(computeHash).(hashing.Digest)

return bytes.Equal(startRecomputed, startDigest) && bytes.Equal(endRecomputed, endDigest)

}
Loading

0 comments on commit bd98a7c

Please sign in to comment.