refactor: add error message support to verify
This commit is contained in:
parent
72fed662e8
commit
e45ab26a09
|
@ -52,8 +52,8 @@ func (v *Verifier) Read(p []byte) (int, error) {
|
||||||
return n, err // Return any read error immediately
|
return n, err // Return any read error immediately
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash) {
|
if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status {
|
||||||
return n, ErrVerifyFailed
|
return n, errors.Join(ErrVerifyFailed, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.read += uint64(bytesRead)
|
v.read += uint64(bytesRead)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package bao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ type Bao interface {
|
||||||
NewHasher() uuid.UUID
|
NewHasher() uuid.UUID
|
||||||
Hash(id uuid.UUID, data []byte) bool
|
Hash(id uuid.UUID, data []byte) bool
|
||||||
Finish(id uuid.UUID) Result
|
Finish(id uuid.UUID) Result
|
||||||
Verify(data []byte, offset uint64, proof []byte, hash []byte) bool
|
Verify(data []byte, offset uint64, proof []byte, hash []byte) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaoPlugin struct {
|
type BaoPlugin struct {
|
||||||
|
@ -79,12 +80,16 @@ func (b *BaoGRPC) Finish(id uuid.UUID) Result {
|
||||||
return Result{Hash: ret.Hash, Proof: ret.Proof}
|
return Result{Hash: ret.Hash, Proof: ret.Proof}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaoGRPC) Verify(data []byte, offset uint64, proof []byte, hash []byte) bool {
|
func (b *BaoGRPC) Verify(data []byte, offset uint64, proof []byte, hash []byte) (bool, error) {
|
||||||
ret, err := b.client.Verify(context.Background(), &proto.VerifyRequest{Data: data, Offset: offset, Proof: proof, Hash: hash})
|
ret, err := b.client.Verify(context.Background(), &proto.VerifyRequest{Data: data, Offset: offset, Proof: proof, Hash: hash})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret.Status
|
if ret.Error != "" {
|
||||||
|
err = errors.New(ret.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret.Status, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ message VerifyRequest {
|
||||||
|
|
||||||
message VerifyResponse {
|
message VerifyResponse {
|
||||||
bool status = 1;
|
bool status = 1;
|
||||||
|
string error = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -80,12 +80,16 @@ impl Bao for BaoService {
|
||||||
);
|
);
|
||||||
|
|
||||||
if res.is_err() {
|
if res.is_err() {
|
||||||
|
|
||||||
Ok(Response::new(VerifyResponse {
|
Ok(Response::new(VerifyResponse {
|
||||||
status: false,
|
status: false,
|
||||||
|
error: res.unwrap_err().to_string(),
|
||||||
|
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
Ok(Response::new(VerifyResponse {
|
Ok(Response::new(VerifyResponse {
|
||||||
status: true,
|
status: true,
|
||||||
|
error: String::from(""),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue