From e45ab26a0941aec8378e935075c8f016798230a3 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 28 Feb 2024 10:45:01 -0500 Subject: [PATCH] refactor: add error message support to verify --- bao/bao.go | 4 ++-- bao/plugin.go | 13 +++++++++---- bao/proto/bao.proto | 1 + bao/rust/src/main.rs | 4 ++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/bao/bao.go b/bao/bao.go index e11cb0e..6af13cf 100644 --- a/bao/bao.go +++ b/bao/bao.go @@ -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) diff --git a/bao/plugin.go b/bao/plugin.go index d24c0a3..71b73ca 100644 --- a/bao/plugin.go +++ b/bao/plugin.go @@ -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 } diff --git a/bao/proto/bao.proto b/bao/proto/bao.proto index fc0641a..e2e01d0 100644 --- a/bao/proto/bao.proto +++ b/bao/proto/bao.proto @@ -39,6 +39,7 @@ message VerifyRequest { message VerifyResponse { bool status = 1; + string error = 2; } diff --git a/bao/rust/src/main.rs b/bao/rust/src/main.rs index 4af6543..28fc56e 100644 --- a/bao/rust/src/main.rs +++ b/bao/rust/src/main.rs @@ -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(""), })) } }