This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prover): update
ZkevmRpcdProducer
to make it connecting to a r…
…eal proverd service (#121) Co-authored-by: alexshliu <[email protected]>
- Loading branch information
1 parent
6c00fc5
commit 8c8ee9c
Showing
11 changed files
with
460 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package producer | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/taikoxyz/taiko-client/bindings" | ||
) | ||
|
||
// ZkevmCmdProducer is responsible for generating zk proofs from the given command line binary file. | ||
type ZkevmCmdProducer struct { | ||
CmdPath string | ||
L2Endpoint string // a L2 execution engine's RPC endpoint | ||
} | ||
|
||
// NewZkevmCmdProducer creates a new NewZkevmCmdProducer instance. | ||
func NewZkevmCmdProducer( | ||
cmdPath string, | ||
l2Endpoint string, | ||
) (*ZkevmCmdProducer, error) { | ||
return &ZkevmCmdProducer{cmdPath, l2Endpoint}, nil | ||
} | ||
|
||
// RequestProof implements the ProofProducer interface. | ||
func (d *ZkevmCmdProducer) RequestProof( | ||
opts *ProofRequestOptions, | ||
blockID *big.Int, | ||
meta *bindings.TaikoDataBlockMetadata, | ||
header *types.Header, | ||
resultCh chan *ProofWithHeader, | ||
) error { | ||
log.Info( | ||
"Request proof from ZKEVM CMD", | ||
"blockID", blockID, | ||
"beneficiary", meta.Beneficiary, | ||
"height", header.Number, | ||
"hash", header.Hash(), | ||
"cmd", d.CmdPath, | ||
) | ||
|
||
var ( | ||
proof []byte | ||
err error | ||
) | ||
if err := backoff.Retry(func() error { | ||
if proof, err = d.ExecProverCmd(opts.Height); err != nil { | ||
log.Error("Execute prover cmd error", "error", err) | ||
return err | ||
} | ||
|
||
return nil | ||
}, backoff.NewConstantBackOff(3*time.Second)); err != nil { | ||
log.Error("Failed to generate proof", "error", err) | ||
} | ||
|
||
resultCh <- &ProofWithHeader{ | ||
BlockID: blockID, | ||
Header: header, | ||
Meta: meta, | ||
ZkProof: proof, | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type ProverCmdOutput struct { | ||
Instances []string `json:"instances"` | ||
Proof []byte `json:"proof"` | ||
} | ||
|
||
func (d *ZkevmCmdProducer) ExecProverCmd(height *big.Int) ([]byte, error) { | ||
start := time.Now() | ||
cmd := exec.Command(d.CmdPath, d.L2Endpoint, height.String()) | ||
|
||
var stdout, stderr bytes.Buffer | ||
|
||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
|
||
if err := cmd.Run(); err != nil { | ||
log.Info("Exec output", "stdout", stdout.String(), "stderr", stderr.String()) | ||
return nil, err | ||
} | ||
|
||
wd, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputPath := filepath.Join(wd, fmt.Sprintf("./block-%s_proof.json", height)) | ||
|
||
log.Info("Exec prover cmd finished", "outputPath", outputPath, "time", time.Since(start)) | ||
|
||
if _, err := os.Stat(outputPath); err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
if err := os.Remove(outputPath); err != nil { | ||
log.Warn("Remove prover cmd output file error", "error", err) | ||
} | ||
}() | ||
|
||
outputJSONBytes, err := os.ReadFile(outputPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var proverCmdOutput ProverCmdOutput | ||
if err := json.Unmarshal(outputJSONBytes, &proverCmdOutput); err != nil { | ||
return nil, err | ||
} | ||
|
||
return d.outputToCalldata(&proverCmdOutput), nil | ||
} | ||
|
||
func (d *ZkevmCmdProducer) outputToCalldata(output *ProverCmdOutput) []byte { | ||
calldata := []byte{} | ||
bufLen := len(output.Instances)*32 + len(output.Proof) | ||
|
||
for i := 0; i < len(output.Instances); i++ { | ||
uint256Bytes := [32]byte{} | ||
evenHexLen := len(output.Instances[i]) - 2 + (len(output.Instances[i]) % 2) | ||
instanceHex := output.Instances[i][2:] | ||
if len(instanceHex) < evenHexLen { | ||
instanceHex = strings.Repeat("0", evenHexLen-len(instanceHex)) + instanceHex | ||
} | ||
instanceBytes := common.Hex2Bytes(instanceHex) | ||
|
||
for j := 0; j < len(instanceBytes); j++ { | ||
uint256Bytes[31-j] = instanceBytes[len(instanceBytes)-1-j] | ||
} | ||
for k := 0; k < 32; k++ { | ||
calldata = append(calldata, uint256Bytes[k]) | ||
} | ||
} | ||
|
||
for i := 0; i < len(output.Proof); i++ { | ||
calldata = append(calldata, output.Proof...) | ||
} | ||
|
||
return calldata[:bufLen] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package producer | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestZkevmCmdProducerOutputToCalldata(t *testing.T) { | ||
output, err := os.ReadFile("../../testutils/testdata/block-5_proof.json") | ||
require.Nil(t, err) | ||
|
||
var ( | ||
testCalldataHexHash = common.HexToHash("0xfbc74eec1aa02cadd59cf2fdfb8c311b199a4f83d0046fd20a1a53081bb0de22") | ||
proverCmdOutput ProverCmdOutput | ||
) | ||
require.Nil(t, json.Unmarshal(output, &proverCmdOutput)) | ||
|
||
calldata := new(ZkevmCmdProducer).outputToCalldata(&proverCmdOutput) | ||
|
||
require.Equal(t, testCalldataHexHash, crypto.Keccak256Hash(calldata)) | ||
} |
Oops, something went wrong.