Skip to content

Commit

Permalink
Create protocol package. Include protocol file. New struct for batch …
Browse files Browse the repository at this point in the history
…of snapshots

Co-authored-by: Gabriel Díaz <[email protected]>
  • Loading branch information
Jose Luis Lucas and gdiazlo committed Nov 16, 2018
1 parent 2b236ef commit d7798c7
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 179 deletions.
15 changes: 8 additions & 7 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/bbva/qed/log"
"github.com/bbva/qed/protocol"
"github.com/bbva/qed/raftwal"
"github.com/bbva/qed/sign"
)
Expand Down Expand Up @@ -93,7 +94,7 @@ func Add(balloon raftwal.RaftBalloonApi, signer sign.Signer) http.HandlerFunc {
return
}

var event Event
var event protocol.Event
err := json.NewDecoder(r.Body).Decode(&event)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -106,7 +107,7 @@ func Add(balloon raftwal.RaftBalloonApi, signer sign.Signer) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
snapshot := &Snapshot{
snapshot := &protocol.Snapshot{
response.HistoryDigest,
response.HyperDigest,
response.Version,
Expand All @@ -119,7 +120,7 @@ func Add(balloon raftwal.RaftBalloonApi, signer sign.Signer) http.HandlerFunc {
return
}

out, err := json.Marshal(SignedSnapshot{snapshot, signature})
out, err := json.Marshal(protocol.SignedSnapshot{snapshot, signature})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -156,7 +157,7 @@ func Membership(balloon raftwal.RaftBalloonApi) http.HandlerFunc {
return
}

var query MembershipQuery
var query protocol.MembershipQuery
err := json.NewDecoder(r.Body).Decode(&query)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -170,7 +171,7 @@ func Membership(balloon raftwal.RaftBalloonApi) http.HandlerFunc {
return
}

out, err := json.Marshal(ToMembershipResult(query.Key, proof))
out, err := json.Marshal(protocol.ToMembershipResult(query.Key, proof))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -203,7 +204,7 @@ func Incremental(balloon raftwal.RaftBalloonApi) http.HandlerFunc {
return
}

var request IncrementalRequest
var request protocol.IncrementalRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -217,7 +218,7 @@ func Incremental(balloon raftwal.RaftBalloonApi) http.HandlerFunc {
return
}

out, err := json.Marshal(ToIncrementalResponse(proof))
out, err := json.Marshal(protocol.ToIncrementalResponse(proof))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
21 changes: 11 additions & 10 deletions api/apihttp/apihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/bbva/qed/balloon"
"github.com/bbva/qed/balloon/visitor"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/protocol"
"github.com/bbva/qed/raftwal"
"github.com/bbva/qed/sign"
"github.com/bbva/qed/storage/badger"
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestHealthCheckHandler(t *testing.T) {
func TestAdd(t *testing.T) {
// Create a request to pass to our handler. We pass a message as a data.
// If it's nil it will fail.
data, _ := json.Marshal(&Event{[]byte("this is a sample event")})
data, _ := json.Marshal(&protocol.Event{[]byte("this is a sample event")})

req, err := http.NewRequest("POST", "/events", bytes.NewBuffer(data))
if len(data) == 0 {
Expand All @@ -131,7 +132,7 @@ func TestAdd(t *testing.T) {
}

// Check the body response
signedSnapshot := &SignedSnapshot{}
signedSnapshot := &protocol.SignedSnapshot{}

json.Unmarshal([]byte(rr.Body.String()), signedSnapshot)

Expand All @@ -147,7 +148,7 @@ func TestAdd(t *testing.T) {
t.Errorf("Version is not consistent")
}

if !bytes.Equal(signedSnapshot.Snapshot.Event, []byte("this is a sample event")) {
if !bytes.Equal(signedSnapshot.Snapshot.EventDigest, []byte("this is a sample event")) {
t.Errorf("Event is not consistent ")
}

Expand All @@ -162,7 +163,7 @@ func TestAdd(t *testing.T) {
func TestMembership(t *testing.T) {
var version uint64 = 1
key := []byte("this is a sample event")
query, _ := json.Marshal(MembershipQuery{
query, _ := json.Marshal(protocol.MembershipQuery{
key,
version,
})
Expand All @@ -175,7 +176,7 @@ func TestMembership(t *testing.T) {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := Membership(fakeRaftBalloon{})
expectedResult := &MembershipResult{Exists: true, Hyper: visitor.AuditPath{}, History: visitor.AuditPath{}, CurrentVersion: 0x1, QueryVersion: 0x1, ActualVersion: 0x2, KeyDigest: []uint8{0x0}, Key: []uint8{0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x65, 0x76, 0x65, 0x6e, 0x74}}
expectedResult := &protocol.MembershipResult{Exists: true, Hyper: visitor.AuditPath{}, History: visitor.AuditPath{}, CurrentVersion: 0x1, QueryVersion: 0x1, ActualVersion: 0x2, KeyDigest: []uint8{0x0}, Key: []uint8{0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x65, 0x76, 0x65, 0x6e, 0x74}}

// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
Expand All @@ -188,7 +189,7 @@ func TestMembership(t *testing.T) {
}

// Check the body response
actualResult := new(MembershipResult)
actualResult := new(protocol.MembershipResult)
json.Unmarshal([]byte(rr.Body.String()), actualResult)

assert.Equal(t, expectedResult, actualResult, "Incorrect proof")
Expand All @@ -198,7 +199,7 @@ func TestMembership(t *testing.T) {
func TestIncremental(t *testing.T) {
start := uint64(2)
end := uint64(8)
query, _ := json.Marshal(IncrementalRequest{
query, _ := json.Marshal(protocol.IncrementalRequest{
start,
end,
})
Expand All @@ -209,7 +210,7 @@ func TestIncremental(t *testing.T) {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := Incremental(fakeRaftBalloon{})
expectedResult := &IncrementalResponse{
expectedResult := &protocol.IncrementalResponse{
start,
end,
visitor.AuditPath{"0|0": []uint8{0x0}},
Expand All @@ -224,7 +225,7 @@ func TestIncremental(t *testing.T) {
assert.Equalf(t, http.StatusOK, status, "handler returned wrong status code: got %v want %v", status, http.StatusOK)

// Check the body response
actualResult := new(IncrementalResponse)
actualResult := new(protocol.IncrementalResponse)
json.Unmarshal([]byte(rr.Body.String()), actualResult)

assert.Equal(t, expectedResult, actualResult, "Incorrect proof")
Expand Down Expand Up @@ -337,7 +338,7 @@ func BenchmarkApiAdd(b *testing.B) {
b.ResetTimer()
b.N = 10000
for i := 0; i < b.N; i++ {
data, _ := json.Marshal(&Event{rand.Bytes(128)})
data, _ := json.Marshal(&protocol.Event{rand.Bytes(128)})
req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer(data))
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
Expand Down
112 changes: 0 additions & 112 deletions api/apihttp/protocol.go

This file was deleted.

30 changes: 15 additions & 15 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"net/http"
"time"

"github.com/bbva/qed/api/apihttp"
"github.com/bbva/qed/balloon"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/protocol"
)

// HttpClient ist the stuct that has the required information for the cli.
Expand Down Expand Up @@ -102,26 +102,26 @@ func (c HttpClient) doReq(method, path string, data []byte) ([]byte, error) {
}

// Add will do a request to the server with a post data to store a new event.
func (c HttpClient) Add(event string) (*apihttp.SignedSnapshot, error) {
func (c HttpClient) Add(event string) (*protocol.SignedSnapshot, error) {

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

body, err := c.doReq("POST", "/events", data)
if err != nil {
return nil, err
}

var signedSnapshot apihttp.SignedSnapshot
var signedSnapshot protocol.SignedSnapshot
json.Unmarshal(body, &signedSnapshot)

return &signedSnapshot, nil

}

// Membership will ask for a Proof to the server.
func (c HttpClient) Membership(key []byte, version uint64) (*apihttp.MembershipResult, error) {
func (c HttpClient) Membership(key []byte, version uint64) (*protocol.MembershipResult, error) {

query, _ := json.Marshal(&apihttp.MembershipQuery{
query, _ := json.Marshal(&protocol.MembershipQuery{
key,
version,
})
Expand All @@ -131,17 +131,17 @@ func (c HttpClient) Membership(key []byte, version uint64) (*apihttp.MembershipR
return nil, err
}

var proof *apihttp.MembershipResult
var proof *protocol.MembershipResult
json.Unmarshal(body, &proof)

return proof, nil

}

// Incremental will ask for an IncrementalProof to the server.
func (c HttpClient) Incremental(start, end uint64) (*apihttp.IncrementalResponse, error) {
func (c HttpClient) Incremental(start, end uint64) (*protocol.IncrementalResponse, error) {

query, _ := json.Marshal(&apihttp.IncrementalRequest{
query, _ := json.Marshal(&protocol.IncrementalRequest{
start,
end,
})
Expand All @@ -151,7 +151,7 @@ func (c HttpClient) Incremental(start, end uint64) (*apihttp.IncrementalResponse
return nil, err
}

var response *apihttp.IncrementalResponse
var response *protocol.IncrementalResponse
json.Unmarshal(body, &response)

return response, nil
Expand All @@ -166,21 +166,21 @@ func uint2bytes(i uint64) []byte {
// Verify will compute the Proof given in Membership and the snapshot from the
// add and returns a proof of existence.

func (c HttpClient) Verify(result *apihttp.MembershipResult, snap *apihttp.Snapshot, hasherF func() hashing.Hasher) bool {
func (c HttpClient) Verify(result *protocol.MembershipResult, snap *protocol.Snapshot, hasherF func() hashing.Hasher) bool {

proof := apihttp.ToBalloonProof([]byte(c.apiKey), result, hasherF)
proof := protocol.ToBalloonProof([]byte(c.apiKey), result, hasherF)

return proof.Verify(snap.Event, &balloon.Commitment{
return proof.Verify(snap.EventDigest, &balloon.Commitment{
snap.HistoryDigest,
snap.HyperDigest,
snap.Version,
})

}

func (c HttpClient) VerifyIncremental(result *apihttp.IncrementalResponse, startSnapshot, endSnapshot *apihttp.SignedSnapshot, hasher hashing.Hasher) bool {
func (c HttpClient) VerifyIncremental(result *protocol.IncrementalResponse, startSnapshot, endSnapshot *protocol.SignedSnapshot, hasher hashing.Hasher) bool {

proof := apihttp.ToIncrementalProof(result, hasher)
proof := protocol.ToIncrementalProof(result, hasher)

startCommitment := &balloon.Commitment{
startSnapshot.Snapshot.HistoryDigest,
Expand Down
Loading

0 comments on commit d7798c7

Please sign in to comment.