Skip to content

Commit

Permalink
fix verification response processing (#1009)
Browse files Browse the repository at this point in the history
* fix verification response processing

* update verifier test
  • Loading branch information
kstoykov authored and hexoscott committed Aug 22, 2024
1 parent f1beb0d commit 23e91c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
22 changes: 11 additions & 11 deletions zk/legacy_executor_verifier/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (e *Executor) CheckOnline() bool {
return true
}

func (e *Executor) Verify(p *Payload, request *VerifierRequest, oldStateRoot common.Hash) (bool, *executor.ProcessBatchResponseV2, error) {
func (e *Executor) Verify(p *Payload, request *VerifierRequest, oldStateRoot common.Hash) (bool, *executor.ProcessBatchResponseV2, error, error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

Expand All @@ -183,12 +183,12 @@ func (e *Executor) Verify(p *Payload, request *VerifierRequest, oldStateRoot com
if e.outputLocation != "" {
asJson, err := json.Marshal(grpcRequest)
if err != nil {
return false, nil, err
return false, nil, nil, err
}
file := path.Join(e.outputLocation, fmt.Sprintf("payload_%d.json", request.BatchNumber))
err = os.WriteFile(file, asJson, 0644)
if err != nil {
return false, nil, err
return false, nil, nil, err
}

// now save the witness as a hex string along with the datastream
Expand All @@ -197,20 +197,23 @@ func (e *Executor) Verify(p *Payload, request *VerifierRequest, oldStateRoot com
witnessAsHex := "0x" + hex.EncodeToString(p.Witness)
err = os.WriteFile(witnessHexFile, []byte(witnessAsHex), 0644)
if err != nil {
return false, nil, err
return false, nil, nil, err
}

dataStreamHexFile := path.Join(e.outputLocation, fmt.Sprintf("datastream_%d.hex", request.BatchNumber))
dataStreamAsHex := "0x" + hex.EncodeToString(p.DataStream)
err = os.WriteFile(dataStreamHexFile, []byte(dataStreamAsHex), 0644)
if err != nil {
return false, nil, err
return false, nil, nil, err
}
}

resp, err := e.client.ProcessStatelessBatchV2(ctx, grpcRequest, grpc.MaxCallSendMsgSize(size), grpc.MaxCallRecvMsgSize(size))
if err != nil {
return false, nil, fmt.Errorf("failed to process stateless batch: %w", err)
return false, nil, nil, fmt.Errorf("failed to process stateless batch: %w", err)
}
if resp == nil {
return false, nil, nil, fmt.Errorf("nil response")
}

counters := map[string]int{
Expand Down Expand Up @@ -266,14 +269,11 @@ func (e *Executor) Verify(p *Payload, request *VerifierRequest, oldStateRoot com

log.Debug("Received response from executor", "grpcUrl", e.grpcUrl, "response", resp)

return responseCheck(resp, request)
ok, executorResponse, executorErr := responseCheck(resp, request)
return ok, executorResponse, executorErr, nil
}

func responseCheck(resp *executor.ProcessBatchResponseV2, request *VerifierRequest) (bool, *executor.ProcessBatchResponseV2, error) {
if resp == nil {
return false, nil, fmt.Errorf("nil response")
}

if resp.ForkId != request.ForkId {
log.Warn("Executor fork id mismatch", "executor", resp.ForkId, "our", request.ForkId)
}
Expand Down
8 changes: 4 additions & 4 deletions zk/legacy_executor_verifier/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestExecutor_Verify(t *testing.T) {
}

for _, tt := range tests {
go func(tt struct {
func(tt struct {
name string
expectedStateRoot *common.Hash
shouldError bool
Expand Down Expand Up @@ -81,9 +81,9 @@ func TestExecutor_Verify(t *testing.T) {
ContextId: "cdk-erigon-test",
}

_, _, err := executor.Verify(payload, &VerifierRequest{StateRoot: *tt.expectedStateRoot}, common.Hash{})
if (err != nil) != tt.wantErr {
t.Errorf("Executor.Verify() error = %v, wantErr %v", err, tt.wantErr)
_, _, executorErr, generalErr := executor.Verify(payload, &VerifierRequest{StateRoot: *tt.expectedStateRoot}, common.Hash{})
if (executorErr != nil || generalErr != nil) != tt.wantErr {
t.Errorf("Executor.Verify() executorErr = %v, generalErr = %v, wantErr %v", executorErr, generalErr, tt.wantErr)
}
})
}(tt)
Expand Down
10 changes: 8 additions & 2 deletions zk/legacy_executor_verifier/legacy_executor_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ func (v *LegacyExecutorVerifier) VerifySync(tx kv.Tx, request *VerifierRequest,
return err
}

_, _, executorErr := e.Verify(payload, request, previousBlock.Root())
_, _, executorErr, generalErr := e.Verify(payload, request, previousBlock.Root())
if generalErr != nil {
return generalErr
}
return executorErr
}

Expand Down Expand Up @@ -263,7 +266,10 @@ func (v *LegacyExecutorVerifier) VerifyAsync(request *VerifierRequest, blockNumb
return verifierBundle, err
}

ok, executorResponse, executorErr := e.Verify(payload, request, previousBlock.Root())
ok, executorResponse, executorErr, generalErr := e.Verify(payload, request, previousBlock.Root())
if generalErr != nil {
return verifierBundle, generalErr
}

if executorErr != nil {
if errors.Is(executorErr, ErrExecutorStateRootMismatch) {
Expand Down

0 comments on commit 23e91c6

Please sign in to comment.