refactor: add error message support to verify

This commit is contained in:
Derrick Hammer 2024-02-28 10:45:01 -05:00
parent 72fed662e8
commit e45ab26a09
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 16 additions and 6 deletions

View File

@ -52,8 +52,8 @@ func (v *Verifier) Read(p []byte) (int, error) {
return n, err // Return any read error immediately
}
if !bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash) {
return n, ErrVerifyFailed
if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status {
return n, errors.Join(ErrVerifyFailed, err)
}
v.read += uint64(bytesRead)

View File

@ -2,6 +2,7 @@ package bao
import (
"context"
"errors"
"github.com/docker/go-units"
@ -19,7 +20,7 @@ type Bao interface {
NewHasher() uuid.UUID
Hash(id uuid.UUID, data []byte) bool
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 {
@ -79,12 +80,16 @@ func (b *BaoGRPC) Finish(id uuid.UUID) Result {
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})
if err != nil {
panic(err)
return false, err
}
return ret.Status
if ret.Error != "" {
err = errors.New(ret.Error)
}
return ret.Status, err
}

View File

@ -39,6 +39,7 @@ message VerifyRequest {
message VerifyResponse {
bool status = 1;
string error = 2;
}

View File

@ -80,12 +80,16 @@ impl Bao for BaoService {
);
if res.is_err() {
Ok(Response::new(VerifyResponse {
status: false,
error: res.unwrap_err().to_string(),
}))
} else {
Ok(Response::new(VerifyResponse {
status: true,
error: String::from(""),
}))
}
}