Add mock proof server

This commit is contained in:
Eduard S
2020-12-16 13:16:24 +01:00
parent 6dd766be4d
commit 885f584fd2
4 changed files with 275 additions and 14 deletions

View File

@@ -58,11 +58,28 @@ func (p *Proof) UnmarshalJSON(data []byte) error {
return nil
}
// PublicInputs are the public inputs of the proof
type PublicInputs []*big.Int
// UnmarshalJSON unmarshals the JSON into the public inputs where the bigInts
// are in decimal as quoted strings
func (p *PublicInputs) UnmarshalJSON(data []byte) error {
pubInputs := []*bigInt{}
if err := json.Unmarshal(data, &pubInputs); err != nil {
return err
}
*p = make([]*big.Int, len(pubInputs))
for i, v := range pubInputs {
([]*big.Int)(*p)[i] = (*big.Int)(v)
}
return nil
}
// Client is the interface to a ServerProof that calculates zk proofs
type Client interface {
// Non-blocking
CalculateProof(ctx context.Context, zkInputs *common.ZKInputs) error
// Blocking
// Blocking. Returns the Proof and Public Data (public inputs)
GetProof(ctx context.Context) (*Proof, []*big.Int, error)
// Non-Blocking
Cancel(ctx context.Context) error
@@ -207,11 +224,11 @@ func (p *ProofServerClient) CalculateProof(ctx context.Context, zkInputs *common
return tracerr.Wrap(p.apiInput(ctx, zkInputs))
}
// GetProof retreives the Proof from the ServerProof, blocking until the proof
// is ready.
// GetProof retreives the Proof and Public Data (public inputs) from the
// ServerProof, blocking until the proof is ready.
func (p *ProofServerClient) GetProof(ctx context.Context) (*Proof, []*big.Int, error) {
if err := p.WaitReady(ctx); err != nil {
return nil, nil, err
return nil, nil, tracerr.Wrap(err)
}
status, err := p.apiStatus(ctx)
if err != nil {
@@ -219,11 +236,14 @@ func (p *ProofServerClient) GetProof(ctx context.Context) (*Proof, []*big.Int, e
}
if status.Status == StatusCodeSuccess {
var proof Proof
err := json.Unmarshal([]byte(status.Proof), &proof)
if err != nil {
if err := json.Unmarshal([]byte(status.Proof), &proof); err != nil {
return nil, nil, tracerr.Wrap(err)
}
return &proof, nil, nil
var pubInputs PublicInputs
if err := json.Unmarshal([]byte(status.PubData), &pubInputs); err != nil {
return nil, nil, tracerr.Wrap(err)
}
return &proof, pubInputs, nil
}
return nil, nil, fmt.Errorf("status != StatusCodeSuccess, status = %v", status.Status)
}
@@ -269,7 +289,7 @@ func (p *MockClient) GetProof(ctx context.Context) (*Proof, []*big.Int, error) {
// Simulate a delay
select {
case <-time.After(500 * time.Millisecond): //nolint:gomnd
return &Proof{}, nil, nil
return &Proof{}, []*big.Int{big.NewInt(1234)}, nil //nolint:gomnd
case <-ctx.Done():
return nil, nil, tracerr.Wrap(common.ErrDone)
}

View File

@@ -53,13 +53,16 @@ func testCalculateProof(t *testing.T) {
}
func testGetProof(t *testing.T) {
proof, _, err := proofServerClient.GetProof(context.Background())
proof, pubInputs, err := proofServerClient.GetProof(context.Background())
require.NoError(t, err)
require.NotNil(t, proof)
require.NotNil(t, proof.PiA)
require.NotNil(t, proof.PiB)
require.NotNil(t, proof.PiC)
require.NotNil(t, proof.Protocol)
assert.NotNil(t, proof.PiA)
assert.NotEqual(t, [2]*big.Int{}, proof.PiA)
assert.NotNil(t, proof.PiB)
assert.NotEqual(t, [3][2]*big.Int{}, proof.PiB)
assert.NotNil(t, proof.PiC)
assert.NotEqual(t, [2]*big.Int{}, proof.PiC)
assert.NotNil(t, proof.Protocol)
assert.NotEqual(t, 0, len(pubInputs))
}
func testCancel(t *testing.T) {