Skip to content

Commit

Permalink
Merge branch 'main' into integratiointest
Browse files Browse the repository at this point in the history
  • Loading branch information
ping-ke committed Aug 14, 2024
2 parents 4cb38a5 + 87a1fe7 commit ae01aa7
Show file tree
Hide file tree
Showing 24 changed files with 260 additions and 489 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ jobs:
- name: Create package
run: |
mkdir -p ${{ env.BIN_DIR }}/snarkbuild
mkdir -p ${{ env.BIN_DIR }}/snark_lib/zkey
cp -r ethstorage/prover/snark_lib ${{ env.BIN_DIR }}
cp init.sh ${{ env.BUILD_DIR }}
cp run.sh ${{ env.BUILD_DIR }}
- name: Build
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ RUN make
# Pull ES node into a second stage deploy alpine container
FROM alpine:latest
COPY --from=builder /es-node/build/ /es-node/build/
RUN apk add --no-cache bash curl libstdc++ gcompat libgomp
RUN apk add --no-cache bash curl libstdc++ gcompat libgomp nodejs npm
RUN npm install -g snarkjs

# Entrypoint
COPY --from=builder /es-node/init.sh /es-node/
COPY --from=builder /es-node/run.sh /es-node/
RUN chmod +x /es-node/run.sh
RUN chmod +x /es-node/init.sh /es-node/run.sh
WORKDIR /es-node

EXPOSE 9545 9222 30305/udp
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LDFLAGS := -ldflags "$(LDFLAGSSTRING)"

es-node: build
cp -r ethstorage/prover/snark_lib build/bin
mkdir -p build/bin/snark_lib/zkey
mkdir -p build/bin/snarkbuild

build:
Expand Down
9 changes: 7 additions & 2 deletions cmd/es-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewConfig(ctx *cli.Context, log log.Logger) (*node.Config, error) {
}

dlConfig := NewDownloaderConfig(ctx)
minerConfig, err := NewMinerConfig(ctx, client, storageConfig.L1Contract)
minerConfig, err := NewMinerConfig(ctx, client, storageConfig.L1Contract, storageConfig.Miner)
if err != nil {
return nil, fmt.Errorf("failed to load miner config: %w", err)
}
Expand Down Expand Up @@ -124,11 +124,15 @@ func NewConfig(ctx *cli.Context, log log.Logger) (*node.Config, error) {
return cfg, nil
}

func NewMinerConfig(ctx *cli.Context, client *ethclient.Client, l1Contract common.Address) (*miner.Config, error) {
func NewMinerConfig(ctx *cli.Context, client *ethclient.Client, l1Contract, minerAddr common.Address) (*miner.Config, error) {
cliConfig := miner.ReadCLIConfig(ctx)
if !cliConfig.Enabled {
log.Info("Miner is not enabled.")
return nil, nil
}
if minerAddr == (common.Address{}) {
return nil, fmt.Errorf("miner address cannot be empty")
}
log.Debug("Read mining config from cli", "config", fmt.Sprintf("%+v", cliConfig))
err := cliConfig.Check()
if err != nil {
Expand Down Expand Up @@ -243,6 +247,7 @@ func NewRollupConfig(ctx *cli.Context) (*rollup.EsConfig, error) {
func NewStorageConfig(ctx *cli.Context, client *ethclient.Client) (*storage.StorageConfig, error) {
l1Contract := common.HexToAddress(ctx.GlobalString(flags.StorageL1Contract.Name))
miner := common.HexToAddress(ctx.GlobalString(flags.StorageMiner.Name))
log.Info("Loaded storage config", "l1Contract", l1Contract, "miner", miner)
storageCfg, err := initStorageConfig(context.Background(), client, l1Contract, miner)
if err != nil {
log.Error("Failed to load storage config from contract", "error", err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/es-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ func EsNodeInit(ctx *cli.Context) error {
}
log := eslog.NewLogger(logCfg)
log.Info("Will create data files for storage node")
l1Rpc := readRequiredFlag(ctx, flags.L1NodeAddr.Name)
contract := readRequiredFlag(ctx, flags.StorageL1Contract.Name)
l1Rpc := readRequiredFlag(ctx, flags.L1NodeAddr)
contract := readRequiredFlag(ctx, flags.StorageL1Contract)
if !common.IsHexAddress(contract) {
return fmt.Errorf("invalid contract address %s", contract)
}

datadir := readRequiredFlag(ctx, flags.DataDir.Name)
datadir := readRequiredFlag(ctx, flags.DataDir)
encodingType := ethstorage.ENCODE_BLOB_POSEIDON
miner := "0x"
if ctx.IsSet(encodingTypeFlagName) {
Expand All @@ -198,7 +198,7 @@ func EsNodeInit(ctx *cli.Context) error {
}
}
if encodingType != ethstorage.NO_ENCODE {
miner = readRequiredFlag(ctx, flags.StorageMiner.Name)
miner = readRequiredFlag(ctx, flags.StorageMiner)
if !common.IsHexAddress(miner) {
return fmt.Errorf("invalid miner address %s", miner)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/es-node/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ func sortBigIntSlice(slice []*big.Int) []int {
return indices
}

func readRequiredFlag(ctx *cli.Context, name string) string {
func readRequiredFlag(ctx *cli.Context, flag cli.StringFlag) string {
name := flag.GetName()
if !ctx.IsSet(name) {
log.Crit("Flag is required", "flag", name)
log.Crit("Flag or environment variable is required", "flag", name, "envVar", flag.EnvVar)
}
value := ctx.String(name)
log.Info("Read flag", "name", name, "value", value)
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ services:
- '30305:30305/udp'
volumes:
- ./es-data:/es-node/es-data
- ./build/bin/snark_lib/zkey:/es-node/build/bin/snark_lib/zkey
environment:
- ES_NODE_STORAGE_MINER=${ES_NODE_STORAGE_MINER}
- ES_NODE_SIGNER_PRIVATE_KEY=${ES_NODE_SIGNER_PRIVATE_KEY}
command: ["bash", "-c", "/es-node/run.sh --miner.zk-prover-impl=2"]
command: ["bash", "-c", "/es-node/run.sh"]
container_name: es
tty: true
31 changes: 16 additions & 15 deletions ethstorage/miner/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
GasPriceFlagName = "miner.gas-price"
PriorityGasPriceFlagName = "miner.priority-gas-price"
ZKeyFileNameFlagName = "miner.zkey"
ZKWorkingDirFlagName = "miner.zk-working-dir"
ZKProverModeFlagName = "miner.zk-prover-mode"
ZKProverImplFlagName = "miner.zk-prover-impl"
ThreadsPerShardFlagName = "miner.threads-per-shard"
Expand Down Expand Up @@ -55,16 +54,10 @@ func CLIFlags(envPrefix string) []cli.Flag {
},
cli.StringFlag{
Name: ZKeyFileNameFlagName,
Usage: "zkey file name which should be put in the snark_lib folder",
Value: DefaultConfig.ZKeyFileName,
Usage: "zkey file name with path",
Value: DefaultConfig.ZKeyFile,
EnvVar: rollup.PrefixEnvVar(envPrefix, "ZKEY_FILE"),
},
cli.StringFlag{
Name: ZKWorkingDirFlagName,
Usage: "Path to the snark_lib folder",
Value: DefaultConfig.ZKWorkingDir,
EnvVar: rollup.PrefixEnvVar(envPrefix, "ZK_WORKING_DIR"),
},
cli.Uint64Flag{
Name: ZKProverModeFlagName,
Usage: "ZK prover mode, 1: one proof per sample, 2: one proof for multiple samples",
Expand Down Expand Up @@ -92,7 +85,7 @@ type CLIConfig struct {
GasPrice *big.Int
PriorityGasPrice *big.Int
MinimumProfit *big.Int
ZKeyFileName string
ZKeyFile string
ZKWorkingDir string
ZKProverMode uint64
ZKProverImpl uint64
Expand All @@ -118,14 +111,22 @@ func (c CLIConfig) ToMinerConfig() (Config, error) {
}
zkWorkingDir = dir
}
zkFile := c.ZKeyFile
if !filepath.IsAbs(zkFile) {
dir, err := filepath.Abs(zkFile)
if err != nil {
return Config{}, fmt.Errorf("check ZKeyFileName error: %v", err)
}
zkFile = dir
}
cfg := DefaultConfig
cfg.ZKWorkingDir = zkWorkingDir
cfg.ZKeyFile = zkFile
cfg.ZKProverMode = c.ZKProverMode
cfg.ZKProverImpl = c.ZKProverImpl
cfg.GasPrice = c.GasPrice
cfg.PriorityGasPrice = c.PriorityGasPrice
cfg.MinimumProfit = c.MinimumProfit
cfg.ZKeyFileName = c.ZKeyFileName
cfg.ZKProverMode = c.ZKProverMode
cfg.ZKProverImpl = c.ZKProverImpl
cfg.ThreadsPerShard = c.ThreadsPerShard
return cfg, nil
}
Expand All @@ -136,8 +137,8 @@ func ReadCLIConfig(ctx *cli.Context) CLIConfig {
GasPrice: types.GlobalBig(ctx, GasPriceFlagName),
PriorityGasPrice: types.GlobalBig(ctx, PriorityGasPriceFlagName),
MinimumProfit: types.GlobalBig(ctx, MinimumProfitFlagName),
ZKeyFileName: ctx.GlobalString(ZKeyFileNameFlagName),
ZKWorkingDir: ctx.GlobalString(ZKWorkingDirFlagName),
ZKeyFile: ctx.GlobalString(ZKeyFileNameFlagName),
ZKWorkingDir: DefaultConfig.ZKWorkingDir,
ZKProverMode: ctx.GlobalUint64(ZKProverModeFlagName),
ZKProverImpl: ctx.GlobalUint64(ZKProverImplFlagName),
ThreadsPerShard: ctx.GlobalUint64(ThreadsPerShardFlagName),
Expand Down
5 changes: 3 additions & 2 deletions ethstorage/miner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"

"github.com/ethereum/go-ethereum/common"
"github.com/ethstorage/go-ethstorage/ethstorage/prover"
"github.com/ethstorage/go-ethstorage/ethstorage/signer"
)

Expand All @@ -29,7 +30,7 @@ type Config struct {
// cli
GasPrice *big.Int
PriorityGasPrice *big.Int
ZKeyFileName string
ZKeyFile string
ZKWorkingDir string
ZKProverMode uint64
ZKProverImpl uint64
Expand All @@ -48,7 +49,7 @@ var DefaultConfig = Config{

GasPrice: nil,
PriorityGasPrice: nil,
ZKeyFileName: "blob_poseidon2.zkey",
ZKeyFile: filepath.Join("build", "bin", prover.SnarkLib, "zkey", "blob_poseidon2.zkey"),
ZKWorkingDir: filepath.Join("build", "bin"),
ZKProverMode: 2,
ZKProverImpl: 1,
Expand Down
37 changes: 16 additions & 21 deletions ethstorage/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
package miner

import (
"math/big"
"os"
"path/filepath"
"runtime/debug"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/event"
es "github.com/ethstorage/go-ethstorage/ethstorage"
Expand All @@ -38,7 +36,6 @@ var (
kvSize uint64 = 1 << kvSizeBits
kvEntries uint64 = 1 << kvEntriesBits
shardID = uint64(0)
value = hexutil.EncodeUint64(10000000000000)
lg = esLog.NewLogger(esLog.DefaultCLIConfig())
)

Expand All @@ -54,34 +51,32 @@ func initStorageManager(t *testing.T, client *eth.PollingClient) *es.StorageMana
}

func newMiner(t *testing.T, storageMgr *es.StorageManager, client *eth.PollingClient) *Miner {
defaultConfig := &Config{
RandomChecks: 2,
NonceLimit: 1048576,
MinimumDiff: new(big.Int).SetUint64(5000000),
Cutoff: new(big.Int).SetUint64(60),
DiffAdjDivisor: new(big.Int).SetUint64(1024),
GasPrice: nil,
PriorityGasPrice: new(big.Int).SetUint64(10),
ThreadsPerShard: 1,
ZKProverMode: 2,
ZKProverImpl: 1,
ZKeyFileName: "blob_poseidon2.zkey",
}
l1api := NewL1MiningAPI(client, nil, lg)
testConfig := &DefaultConfig

zkWorkingDir, _ := filepath.Abs("../prover")
zkey := filepath.Join(zkWorkingDir, prover.SnarkLib, defaultConfig.ZKeyFileName)
testConfig.ZKWorkingDir = zkWorkingDir
dir := filepath.Join(zkWorkingDir, prover.SnarkLib, "zkey")
zkey := filepath.Join(dir, "blob_poseidon2.zkey")
testConfig.ZKeyFile = zkey
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0755)
if err != nil {
t.Fatalf("Mkdir failed %v", err)
}
}
if _, err := os.Stat(zkey); os.IsNotExist(err) {
_, err := os.Create(zkey)
if err != nil {
t.Fatalf("Create failed %v", err)
}
defer os.Remove(zkey)
defer os.RemoveAll(dir)
}
pvr := prover.NewKZGPoseidonProver(zkWorkingDir, defaultConfig.ZKeyFileName, defaultConfig.ZKProverMode, defaultConfig.ZKProverImpl, lg)
pvr := prover.NewKZGPoseidonProver(zkWorkingDir, zkey, testConfig.ZKProverMode, testConfig.ZKProverImpl, lg)
fd := new(event.Feed)
db := rawdb.NewMemoryDatabase()
br := blobs.NewBlobReader(downloader.NewBlobMemCache(), storageMgr, lg)
miner := New(defaultConfig, db, storageMgr, l1api, br, &pvr, fd, lg)
l1api := NewL1MiningAPI(client, nil, lg)
miner := New(testConfig, db, storageMgr, l1api, br, &pvr, fd, lg)
return miner
}

Expand Down
11 changes: 7 additions & 4 deletions ethstorage/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ func (n *EsNode) initL1(ctx context.Context, cfg *Config) error {
}
n.l1Source = client

if cfg.L1.L1BeaconURL != "" {
n.l1Beacon = eth.NewBeaconClient(cfg.L1.L1BeaconURL, cfg.L1.L1BeaconBasedTime, cfg.L1.L1BeaconBasedSlot, cfg.L1.L1BeaconSlotTime)
} else if cfg.L1.DAURL != "" {
if cfg.L1.DAURL != "" {
n.daClient = eth.NewDAClient(cfg.L1.DAURL)
n.log.Info("Using DA URL", "url", cfg.L1.DAURL)
} else if cfg.L1.L1BeaconURL != "" {
n.l1Beacon = eth.NewBeaconClient(cfg.L1.L1BeaconURL, cfg.L1.L1BeaconBasedTime, cfg.L1.L1BeaconBasedSlot, cfg.L1.L1BeaconSlotTime)
n.log.Info("Using L1 Beacon URL", "url", cfg.L1.L1BeaconURL)
} else {
return fmt.Errorf("no L1 beacon or DA URL provided")
}
Expand All @@ -173,6 +175,7 @@ func (n *EsNode) initL1(ctx context.Context, cfg *Config) error {
return fmt.Errorf("failed to create randao source: %w", err)
}
n.randaoSource = rc
n.log.Info("Using randao source", "url", cfg.RandaoSourceURL)
}
return nil
}
Expand Down Expand Up @@ -300,7 +303,7 @@ func (n *EsNode) initMiner(ctx context.Context, cfg *Config) error {
l1api := miner.NewL1MiningAPI(n.l1Source, n.randaoSource, n.log)
pvr := prover.NewKZGPoseidonProver(
cfg.Mining.ZKWorkingDir,
cfg.Mining.ZKeyFileName,
cfg.Mining.ZKeyFile,
cfg.Mining.ZKProverMode,
cfg.Mining.ZKProverImpl,
n.log,
Expand Down
7 changes: 3 additions & 4 deletions ethstorage/prover/kzg_poseidon_prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ type KZGPoseidonProver struct {

// Prover that can be used directly by miner to prove both KZG and Poseidon hash
// workingDir specifies the working directory of the command relative to the caller.
// zkeyFileName specifies the zkey file name to generate snark proof
// zkeyFile specifies the zkey file with path to generate snark proof
// zkProverMode specifies the mode of the zk prover, 1 for per sample, 2 for samples
// zkProverImpl specifies the implementation of the snark prover, 1 for snarkjs, 2 for go-rapidsnark
// lg specifies the logger to log the info
// returns a prover that can generate a combined KZG + zk proof
func NewKZGPoseidonProver(workingDir, zkeyFileName string, zkProverMode, zkProverImpl uint64, lg log.Logger) KZGPoseidonProver {
func NewKZGPoseidonProver(workingDir, zkeyFile string, zkProverMode, zkProverImpl uint64, lg log.Logger) KZGPoseidonProver {
// check dependencies when es-node starts
libDir := filepath.Join(workingDir, SnarkLib)
if _, err := os.Stat(libDir); errors.Is(err, os.ErrNotExist) {
lg.Crit("Init ZK prover failed", "error", "snark lib does not exist", "dir", libDir)
}
zkeyFile := filepath.Join(libDir, zkeyFileName)
if _, err := os.Stat(zkeyFile); errors.Is(err, os.ErrNotExist) {
lg.Crit("Init ZK prover failed", "error", "zkey does not exist", "dir", zkeyFile)
}
Expand All @@ -71,7 +70,7 @@ func NewKZGPoseidonProver(workingDir, zkeyFileName string, zkProverMode, zkProve
zkProverMode: zkProverMode,
zkProverImpl: zkProverImpl,
libDir: libDir,
zkey: zkeyFileName,
zkey: zkeyFile,
wasm: wasmName,
lg: lg,
}
Expand Down
8 changes: 4 additions & 4 deletions ethstorage/prover/zk_prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ const (
)

type ZKProver struct {
dir, zkeyName, wasmName string
dir, zkeyFile, wasmName string
lg log.Logger
}

func NewZKProver(workingDir, zkeyName, wasmName string, lg log.Logger) *ZKProver {
func NewZKProver(workingDir, zkey, wasmName string, lg log.Logger) *ZKProver {
return &ZKProver{
dir: workingDir,
zkeyName: zkeyName,
zkeyFile: zkey,
wasmName: wasmName,
lg: lg,
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (p *ZKProver) prove(dir string, pubInputs []byte) ([]byte, string, error) {
proofFile := filepath.Join(buildDir, proofName)
publicFile := filepath.Join(buildDir, publicName)
cmd = exec.Command("snarkjs", "groth16", "prove",
filepath.Join(libDir, p.zkeyName),
p.zkeyFile,
wtnsFile,
proofFile,
publicFile,
Expand Down
Loading

0 comments on commit ae01aa7

Please sign in to comment.