-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(storageincentives): storage incentives phase 4 #4345
Changes from 3 commits
930c52e
0a9afe1
f89ad31
654aaa0
1d0c465
a4671f5
206c71d
3d6c27e
c53032d
84efd85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,9 @@ func (s *Service) rchash(w http.ResponseWriter, r *http.Request) { | |
logger := s.logger.WithName("get_rchash").Build() | ||
|
||
paths := struct { | ||
Depth *uint8 `map:"depth" validate:"required"` | ||
Depth uint8 `map:"depth" validate:"min=0"` | ||
Anchor1 string `map:"anchor1" validate:"required"` | ||
Anchor2 string `map:"anchor2" validate:"required"` | ||
}{} | ||
if response := s.mapStructure(mux.Vars(r), &paths); response != nil { | ||
response("invalid path params", logger, w) | ||
|
@@ -34,7 +35,14 @@ func (s *Service) rchash(w http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
resp, err := s.redistributionAgent.SampleWithProofs(r.Context(), anchor1, *paths.Depth) | ||
anchor2, err := hex.DecodeString(paths.Anchor2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to rather register a new pre-map hook (api.go:281): "decHex": func(v string) (string, error) {
buf, err := hex.DecodeString(v)
return string(buf), err
}, Then simply use |
||
if err != nil { | ||
logger.Error(err, "invalid hex params") | ||
jsonhttp.InternalServerError(w, "invalid hex params") | ||
return | ||
} | ||
|
||
resp, err := s.redistributionAgent.SampleWithProofs(r.Context(), anchor1, anchor2, paths.Depth) | ||
if err != nil { | ||
logger.Error(err, "failed making sample with proofs") | ||
jsonhttp.InternalServerError(w, "failed making sample with proofs") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,19 @@ type Hasher struct { | |
span []byte // The span of the data subsumed under the chunk | ||
} | ||
|
||
// facade | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment should describe what this function does and should start with the: NewHasher... (by Golang convention). |
||
func NewHasher(hasherFact func() hash.Hash) *Hasher { | ||
conf := NewConf(hasherFact, swarm.BmtBranches, 32) | ||
|
||
return &Hasher{ | ||
Conf: conf, | ||
result: make(chan []byte), | ||
errc: make(chan error, 1), | ||
span: make([]byte, SpanSize), | ||
bmt: newTree(conf.segmentSize, conf.maxSize, conf.depth, conf.hasher), | ||
} | ||
} | ||
|
||
// Capacity returns the maximum amount of bytes that will be processed by this hasher implementation. | ||
// since BMT assumes a balanced binary tree, capacity it is always a power of 2 | ||
func (h *Hasher) Capacity() int { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,17 @@ type Proof struct { | |
Index int | ||
} | ||
|
||
// Override base hash function of Hasher to fill buffer with zeros until chunk length | ||
func (p Prover) Hash(b []byte) ([]byte, error) { | ||
mrekucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for i := p.size; i < p.maxSize; i += len(zerosection) { | ||
_, err := p.Write(zerosection) | ||
if err != nil { | ||
return []byte{}, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return |
||
} | ||
} | ||
return p.Hasher.Hash(b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stay consistent, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it calls its ancestor's hash function that contains the logic for the hash There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
i dont think so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're doing the same with |
||
} | ||
|
||
// Proof returns the inclusion proof of the i-th data segment | ||
func (p Prover) Proof(i int) Proof { | ||
index := i | ||
|
@@ -36,34 +47,50 @@ func (p Prover) Proof(i int) Proof { | |
secsize := 2 * p.segmentSize | ||
offset := i * secsize | ||
section := p.bmt.buffer[offset : offset+secsize] | ||
return Proof{section, sisters, p.span, index} | ||
left := section[:p.segmentSize] | ||
right := section[p.segmentSize:] | ||
var segment, firstSegmentSister []byte | ||
if index%2 == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be replaced with: segment, firstSegmentSister := section[:p.segmentSize], section[p.segmentSize:]
if index%2 != 0 {
segment, firstSegmentSister = firstSegmentSister, segment
} |
||
segment, firstSegmentSister = left, right | ||
} else { | ||
segment, firstSegmentSister = right, left | ||
} | ||
sisters = append([][]byte{firstSegmentSister}, sisters...) | ||
return Proof{segment, sisters, p.span, index} | ||
} | ||
|
||
// Verify returns the bmt hash obtained from the proof which can then be checked against | ||
// the BMT hash of the chunk | ||
func (p Prover) Verify(i int, proof Proof) (root []byte, err error) { | ||
var section []byte | ||
if i%2 == 0 { | ||
section = append(append(section, proof.ProveSegment...), proof.ProofSegments[0]...) | ||
} else { | ||
section = append(append(section, proof.ProofSegments[0]...), proof.ProveSegment...) | ||
} | ||
i = i / 2 | ||
n := p.bmt.leaves[i] | ||
hasher := p.hasher() | ||
isLeft := n.isLeft | ||
root, err = doHash(n.hasher, proof.ProveSegment) | ||
root, err = doHash(hasher, section) | ||
if err != nil { | ||
return nil, err | ||
} | ||
n = n.parent | ||
|
||
for _, sister := range proof.ProofSegments { | ||
for _, sister := range proof.ProofSegments[1:] { | ||
if isLeft { | ||
root, err = doHash(n.hasher, root, sister) | ||
root, err = doHash(hasher, root, sister) | ||
} else { | ||
root, err = doHash(n.hasher, sister, root) | ||
root, err = doHash(hasher, sister, root) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
isLeft = n.isLeft | ||
n = n.parent | ||
} | ||
return sha3hash(proof.Span, root) | ||
return doHash(hasher, proof.Span, root) | ||
} | ||
|
||
func (n *node) getSister(isLeft bool) []byte { | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
min=0
tag is not needed, the 0 is the minimum of an unsigned integer.