-
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.
Merge pull request #74 from aalda/flatten_history
Flatten history tree package
- Loading branch information
Showing
41 changed files
with
1,448 additions
and
1,311 deletions.
There are no files selected for viewing
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
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,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 | ||
} |
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,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) | ||
} |
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
Oops, something went wrong.