Skip to content

Commit

Permalink
Flatten history tree package
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda authored and panchoh committed Feb 26, 2019
1 parent bba3ff3 commit 4db5fcd
Show file tree
Hide file tree
Showing 41 changed files with 1,448 additions and 1,311 deletions.
7 changes: 3 additions & 4 deletions api/apihttp/apihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/bbva/qed/balloon"
"github.com/bbva/qed/balloon/history"
"github.com/bbva/qed/balloon/history/navigation"
"github.com/bbva/qed/balloon/hyper"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/protocol"
Expand Down Expand Up @@ -57,7 +56,7 @@ func (b fakeRaftBalloon) QueryDigestMembership(keyDigest hashing.Digest, version
return &balloon.MembershipProof{
Exists: true,
HyperProof: hyper.NewQueryProof([]byte{0x0}, []byte{0x0}, hyper.AuditPath{}, nil),
HistoryProof: history.NewMembershipProof(0, 0, navigation.AuditPath{}, nil),
HistoryProof: history.NewMembershipProof(0, 0, history.AuditPath{}, nil),
CurrentVersion: 1,
QueryVersion: 1,
ActualVersion: 2,
Expand All @@ -71,7 +70,7 @@ func (b fakeRaftBalloon) QueryMembership(event []byte, version uint64) (*balloon
return &balloon.MembershipProof{
Exists: true,
HyperProof: hyper.NewQueryProof([]byte{0x0}, []byte{0x0}, hyper.AuditPath{}, nil),
HistoryProof: history.NewMembershipProof(0, 0, navigation.AuditPath{}, nil),
HistoryProof: history.NewMembershipProof(0, 0, history.AuditPath{}, nil),
CurrentVersion: 1,
QueryVersion: 1,
ActualVersion: 2,
Expand All @@ -85,7 +84,7 @@ func (b fakeRaftBalloon) QueryConsistency(start, end uint64) (*balloon.Increment
ip := balloon.IncrementalProof{
Start: 2,
End: 8,
AuditPath: navigation.AuditPath{pathKey: hashing.Digest{0x00}},
AuditPath: history.AuditPath{pathKey: hashing.Digest{0x00}},
Hasher: hashing.NewFakeXorHasher(),
}
return &ip, nil
Expand Down
5 changes: 2 additions & 3 deletions balloon/balloon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/balloon/history"
historynav "github.com/bbva/qed/balloon/history/navigation"
"github.com/bbva/qed/balloon/hyper"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/metrics"
Expand Down Expand Up @@ -146,13 +145,13 @@ func (p MembershipProof) Verify(event []byte, snapshot *Snapshot) bool {

type IncrementalProof struct {
Start, End uint64
AuditPath historynav.AuditPath
AuditPath history.AuditPath
Hasher hashing.Hasher
}

func NewIncrementalProof(
start, end uint64,
auditPath historynav.AuditPath,
auditPath history.AuditPath,
hasher hashing.Hasher,
) *IncrementalProof {
return &IncrementalProof{
Expand Down
80 changes: 80 additions & 0 deletions balloon/history/audit_visitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2018 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 history

import (
"fmt"

"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/hashing"
)

type auditPathVisitor struct {
hasher hashing.Hasher
cache cache.Cache

auditPath AuditPath
}

func newAuditPathVisitor(hasher hashing.Hasher, cache cache.Cache) *auditPathVisitor {
return &auditPathVisitor{
hasher: hasher,
cache: cache,
auditPath: make(AuditPath),
}
}

func (v auditPathVisitor) Result() AuditPath {
return v.auditPath
}

func (v *auditPathVisitor) VisitLeafHashOp(op leafHashOp) hashing.Digest {
return v.hasher.Salted(op.Position().Bytes(), op.Value)
}

func (v *auditPathVisitor) VisitInnerHashOp(op innerHashOp) hashing.Digest {
leftHash := op.Left.Accept(v)
rightHash := op.Right.Accept(v)
return v.hasher.Salted(op.Position().Bytes(), leftHash, rightHash)
}

func (v *auditPathVisitor) VisitPartialInnerHashOp(op partialInnerHashOp) hashing.Digest {
leftHash := op.Left.Accept(v)
return v.hasher.Salted(op.Position().Bytes(), leftHash)
}

func (v *auditPathVisitor) VisitGetCacheOp(op getCacheOp) hashing.Digest {
hash, ok := v.cache.Get(op.Position().Bytes())
if !ok {
panic(fmt.Sprintf("Oops, something went wrong. There should be a cached element at position %v", op.Position()))
}
return hash
}

func (v *auditPathVisitor) VisitPutCacheOp(op putCacheOp) hashing.Digest {
return op.operation.Accept(v)
}

func (v *auditPathVisitor) VisitMutateOp(op mutateOp) hashing.Digest {
return op.operation.Accept(v)
}

func (v *auditPathVisitor) VisitCollectOp(op collectOp) hashing.Digest {
hash := op.operation.Accept(v)
v.auditPath[op.Position().FixedBytes()] = hash
return hash
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
package pruning
/*
Copyright 2018 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 history

import (
"testing"

"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/balloon/history/navigation"
"github.com/bbva/qed/hashing"
"github.com/stretchr/testify/require"
)

func TestAuditPathVisitor(t *testing.T) {

testCases := []struct {
op Operation
expectedAuditPath navigation.AuditPath
op operation
expectedAuditPath AuditPath
}{
{
op: leafnil(pos(0, 0)),
expectedAuditPath: navigation.AuditPath{},
expectedAuditPath: AuditPath{},
},
{
op: inner(pos(0, 1),
collect(getCache(pos(0, 0))),
leafnil(pos(1, 0)),
),
expectedAuditPath: navigation.AuditPath{
expectedAuditPath: AuditPath{
pos(0, 0).FixedBytes(): hashing.Digest{0x0},
},
},
Expand All @@ -35,7 +50,7 @@ func TestAuditPathVisitor(t *testing.T) {
leafnil(pos(2, 0)),
),
),
expectedAuditPath: navigation.AuditPath{
expectedAuditPath: AuditPath{
pos(0, 1).FixedBytes(): hashing.Digest{0x0},
},
},
Expand All @@ -47,7 +62,7 @@ func TestAuditPathVisitor(t *testing.T) {
leafnil(pos(3, 0)),
),
),
expectedAuditPath: navigation.AuditPath{
expectedAuditPath: AuditPath{
pos(0, 1).FixedBytes(): hashing.Digest{0x0},
pos(2, 0).FixedBytes(): hashing.Digest{0x0},
},
Expand All @@ -61,7 +76,7 @@ func TestAuditPathVisitor(t *testing.T) {
),
),
),
expectedAuditPath: navigation.AuditPath{
expectedAuditPath: AuditPath{
pos(0, 2).FixedBytes(): hashing.Digest{0x0},
},
},
Expand All @@ -75,15 +90,15 @@ func TestAuditPathVisitor(t *testing.T) {
),
),
),
expectedAuditPath: navigation.AuditPath{
expectedAuditPath: AuditPath{
pos(0, 2).FixedBytes(): hashing.Digest{0x0},
pos(4, 0).FixedBytes(): hashing.Digest{0x0},
},
},
}

for i, c := range testCases {
visitor := NewAuditPathVisitor(hashing.NewFakeXorHasher(), cache.NewFakeCache([]byte{0x0}))
visitor := newAuditPathVisitor(hashing.NewFakeXorHasher(), cache.NewFakeCache([]byte{0x0}))
c.op.Accept(visitor)
auditPath := visitor.Result()
require.Equalf(t, c.expectedAuditPath, auditPath, "The audit path %v should be equal to the expected %v in test case %d", auditPath, c.expectedAuditPath, i)
Expand Down
71 changes: 71 additions & 0 deletions balloon/history/compute_visitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2018 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 history

import (
"fmt"

"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/hashing"
)

type computeHashVisitor struct {
hasher hashing.Hasher
cache cache.Cache
}

func newComputeHashVisitor(hasher hashing.Hasher, cache cache.Cache) *computeHashVisitor {
return &computeHashVisitor{
hasher: hasher,
cache: cache,
}
}

func (v *computeHashVisitor) VisitLeafHashOp(op leafHashOp) hashing.Digest {
return v.hasher.Salted(op.Position().Bytes(), op.Value)
}

func (v *computeHashVisitor) VisitInnerHashOp(op innerHashOp) hashing.Digest {
leftHash := op.Left.Accept(v)
rightHash := op.Right.Accept(v)
return v.hasher.Salted(op.Position().Bytes(), leftHash, rightHash)
}

func (v *computeHashVisitor) VisitPartialInnerHashOp(op partialInnerHashOp) hashing.Digest {
leftHash := op.Left.Accept(v)
return v.hasher.Salted(op.Position().Bytes(), leftHash)
}

func (v *computeHashVisitor) VisitGetCacheOp(op getCacheOp) hashing.Digest {
hash, ok := v.cache.Get(op.Position().Bytes())
if !ok { // TODO maybe we should return an error
panic(fmt.Sprintf("Oops, something went wrong. There should be a cached element at position %v", op.Position()))
}
return hash
}

func (v *computeHashVisitor) VisitPutCacheOp(op putCacheOp) hashing.Digest {
return op.operation.Accept(v)
}

func (v *computeHashVisitor) VisitMutateOp(op mutateOp) hashing.Digest {
return op.operation.Accept(v)
}

func (v *computeHashVisitor) VisitCollectOp(op collectOp) hashing.Digest {
return op.operation.Accept(v)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package pruning
/*
Copyright 2018 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 history

import (
"testing"
Expand All @@ -11,7 +27,7 @@ import (
func TestComputeHashVisitor(t *testing.T) {

testCases := []struct {
op Operation
op operation
expectedDigest hashing.Digest
}{
{
Expand Down Expand Up @@ -56,7 +72,7 @@ func TestComputeHashVisitor(t *testing.T) {
},
}

visitor := NewComputeHashVisitor(hashing.NewFakeXorHasher(), cache.NewFakeCache([]byte{0x0}))
visitor := newComputeHashVisitor(hashing.NewFakeXorHasher(), cache.NewFakeCache([]byte{0x0}))

for i, c := range testCases {
digest := c.op.Accept(visitor)
Expand Down
Loading

0 comments on commit 4db5fcd

Please sign in to comment.