From f432473fb10b0b8d642cf50445711be6acb306cf Mon Sep 17 00:00:00 2001 From: syntrust Date: Mon, 18 Dec 2023 18:04:13 +0800 Subject: [PATCH] fix kzg --- cmd/es-utils/utils/utils.go | 2 +- integration_tests/common.go | 15 ++-- integration_tests/kzg_poseidon_prover_test.go | 10 +-- integration_tests/kzg_prover_test.go | 77 ++++++++++++++----- integration_tests/run_tests.sh | 27 +++++++ integration_tests/signature_test.go | 6 +- integration_tests/zk_prover_test.go | 14 +++- 7 files changed, 109 insertions(+), 42 deletions(-) create mode 100755 integration_tests/run_tests.sh diff --git a/cmd/es-utils/utils/utils.go b/cmd/es-utils/utils/utils.go index 63972c2b..59a67184 100644 --- a/cmd/es-utils/utils/utils.go +++ b/cmd/es-utils/utils/utils.go @@ -323,7 +323,7 @@ func UploadBlobs( 5000000, "", "", - "30000000000", + "300000000", chainID, calldata, ) diff --git a/integration_tests/common.go b/integration_tests/common.go index cb9b0911..1569fb52 100644 --- a/integration_tests/common.go +++ b/integration_tests/common.go @@ -7,13 +7,11 @@ import ( "context" "fmt" "io" - "log" "math/big" "net/http" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" esLog "github.com/ethstorage/go-ethstorage/ethstorage/log" @@ -27,10 +25,10 @@ const ( ) var ( - contractAddr1GB = common.HexToAddress("0x0c47aa023A3be36CA148a17a91aA8C9268A350AE") - contractAddrDevnet1 = common.HexToAddress("0x9f9F5Fd89ad648f2C000C954d8d9C87743243eC5") + contractAddr1GB = common.HexToAddress("0x24d29453B4B4917117B4af022Ad1B0a9aBc2c7DE") + contractAddrDevnet2 = common.HexToAddress("0xb4B46bdAA835F8E4b4d8e208B6559cD267851051") minerAddr = common.HexToAddress("0x534632D6d7aD1fe5f832951c97FDe73E4eFD9a77") - value = hexutil.EncodeUint64(10000000000000) + value = "1000000000000000" lg = esLog.NewLogger(esLog.DefaultCLIConfig()) ) @@ -61,7 +59,7 @@ func readSlotFromContract(ctx context.Context, client *ethclient.Client, l1Contr } bs, err := client.CallContract(ctx, msg, nil) if err != nil { - return nil, fmt.Errorf("Failed to get %s from contract: %v", fieldName, err) + return nil, fmt.Errorf("failed to get %s from contract: %v", fieldName, err) } return bs, nil } @@ -72,7 +70,7 @@ func readUintFromContract(ctx context.Context, client *ethclient.Client, l1Contr return 0, err } value := new(big.Int).SetBytes(bs).Uint64() - log.Println("Read uint from contract", "field", fieldName, "value", value) + lg.Info("Read uint from contract", "field", fieldName, "value", value) return value, nil } @@ -80,7 +78,8 @@ func callVerify(calldata []byte) error { ctx := context.Background() client, err := ethclient.DialContext(ctx, l1Endpoint) if err != nil { - log.Fatalf("Failed to connect to the Ethereum client: %v", err) + lg.Error("Failed to connect to the Ethereum client", "error", err) + return err } defer client.Close() msg := ethereum.CallMsg{ diff --git a/integration_tests/kzg_poseidon_prover_test.go b/integration_tests/kzg_poseidon_prover_test.go index 3c04e5e3..48ba5c1a 100644 --- a/integration_tests/kzg_poseidon_prover_test.go +++ b/integration_tests/kzg_poseidon_prover_test.go @@ -6,6 +6,7 @@ package integration import ( + "os" "path/filepath" "testing" @@ -14,11 +15,6 @@ import ( "github.com/ethstorage/go-ethstorage/ethstorage/prover" ) -const ( - prPath = "../ethstorage/prover" - zkeyFile = "blob_poseidon.zkey" -) - func TestKZGPoseidonProver_GenerateZKProofs(t *testing.T) { type args struct { encodingKeys []common.Hash @@ -44,6 +40,10 @@ func TestKZGPoseidonProver_GenerateZKProofs(t *testing.T) { } proverPath, _ := filepath.Abs(prPath) + zkeyFull := filepath.Join(proverPath, snarkLibDir, zkeyFile) + if _, err := os.Stat(zkeyFull); os.IsNotExist(err) { + t.Fatalf("%s not found", zkeyFull) + } prv := prover.NewKZGPoseidonProver(proverPath, zkeyFile, esLog.NewLogger(esLog.DefaultCLIConfig())) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/integration_tests/kzg_prover_test.go b/integration_tests/kzg_prover_test.go index f6816a50..595cd1e1 100644 --- a/integration_tests/kzg_prover_test.go +++ b/integration_tests/kzg_prover_test.go @@ -8,17 +8,18 @@ package integration import ( "bytes" "context" - "log" "math/big" "os" "testing" "time" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" + "github.com/ethereum/go-ethereum/rpc" "github.com/ethstorage/go-ethstorage/cmd/es-utils/utils" "github.com/ethstorage/go-ethstorage/ethstorage/prover" ) @@ -81,41 +82,75 @@ func uploadBlob(t *testing.T, data []byte) common.Hash { if err != nil { t.Fatalf("Get chain id failed %v", err) } + pk := os.Getenv(pkName) + key, err := crypto.HexToECDSA(pk) + if err != nil { + t.Fatalf("Invalid private key: %s, err: %v", pk, err) + } + signer := crypto.PubkeyToAddress(key.PublicKey) + lg.Info("Get signer address", "signer", signer.Hex()) + n, err := client.NonceAt(ctx, signer, big.NewInt(rpc.LatestBlockNumber.Int64())) + if err != nil { + t.Fatalf("Error getting nonce: %v", err) + } + blbKey := "0x0000000000000000000000000000000000000000000000000000000000000001" + blbIdx := common.Big0 + length := new(big.Int).SetInt64(128 * 1024) + // calldata := "0x4581a920000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000" - // key: "0x0000000000000000000000000000000000000000000000000000000000000001" - // blobIdx: 0 - // length: 128*1024 - calldata := "0x4581a920000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000" + h := crypto.Keccak256Hash([]byte("putBlob(bytes32,uint256,uint256)")) + mid := h[0:4] + bytes32Type, _ := abi.NewType("bytes32", "", nil) + uint256Type, _ := abi.NewType("uint256", "", nil) + args := abi.Arguments{ + {Type: bytes32Type}, + {Type: uint256Type}, + {Type: uint256Type}, + } + values := []interface{}{common.HexToHash(blbKey), blbIdx, length} + dataField, err := args.Pack(values...) + if err != nil { + t.Fatalf("Error getting calldata: %v", err) + } + calldata := append(mid, dataField...) + valBig, _ := new(big.Int).SetString(value, 10) + estimatedGas, err := client.EstimateGas(ctx, ethereum.CallMsg{ + From: signer, + To: &contractAddr1GB, + Value: valBig, + Data: calldata, + }) + if err != nil { + lg.Crit("Estimate gas failed", "error", err.Error()) + } + lg.Info("Estimated gas done", "gas", estimatedGas) tx := utils.SendBlobTx( l1Endpoint, contractAddr1GB, - os.Getenv(pkName), + pk, data, true, - -1, + int64(n), value, 510000, "", "", - "10000000000", + "100000000", chainID.String(), - calldata, + "0x"+common.Bytes2Hex(calldata), ) - log.Printf("Blob transaction submitted %v", tx.Hash()) + lg.Info("Blob transaction submitted", "hash", tx.Hash()) receipt, err := bind.WaitMined(ctx, client, tx) if err != nil { - log.Fatal("Get transaction receipt err:", err) - } - if receipt.Status == 0 { - log.Fatal("Blob transaction failed") + lg.Crit("Get transaction receipt err", "error", err) } - log.Printf("Blob transaction success! Gas used %v", receipt.GasUsed) - eventTopics := receipt.Logs[0].Topics - kvIndex := new(big.Int).SetBytes(eventTopics[1][:]) - kvSize := new(big.Int).SetBytes(eventTopics[2][:]) - dataHash := eventTopics[3] - log.Printf("Put Blob with kvIndex=%v kvSize=%v, dataHash=%x", kvIndex, kvSize, dataHash) + // if receipt.Status == 0 { + // lg.Crit("Blob transaction failed") + // } + lg.Info("Blob transaction success!", "GasUsed", receipt.GasUsed) + dataHash := receipt.Logs[0].Topics[3] + lg.Info("Put Blob done", "datahash", dataHash) return dataHash } @@ -123,7 +158,7 @@ func verifyInclusive(trunkIdx uint64, peInput []byte) error { ctx := context.Background() client, err := ethclient.DialContext(ctx, l1Endpoint) if err != nil { - log.Fatalf("Failed to connect to the Ethereum client: %v", err) + lg.Crit("Failed to connect to the Ethereum client.", "error", err) } defer client.Close() diff --git a/integration_tests/run_tests.sh b/integration_tests/run_tests.sh new file mode 100755 index 00000000..b4db3f02 --- /dev/null +++ b/integration_tests/run_tests.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# usage: +# env ES_NODE_SIGNER_PRIVATE_KEY= ./run_tests.sh + +if [ -z "$ES_NODE_SIGNER_PRIVATE_KEY" ]; then + echo "Please provide 'ES_NODE_SIGNER_PRIVATE_KEY' as an environment variable" + exit 1 +fi + +if [ ${#ES_NODE_SIGNER_PRIVATE_KEY} -ne 64 ]; then + echo "Error: ES_NODE_SIGNER_PRIVATE_KEY should have a length of 64" + exit 1 +fi + +# download blob_poseidon.zkey if not yet +zkey_file="./ethstorage/prover/snarkjs/blob_poseidon.zkey" +if [ ! -e ${zkey_file} ]; then + echo "${zkey_file} not found, start downloading..." + file_id="1ZLfhYeCXMnbk6wUiBADRAn1mZ8MI_zg-" + html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${file_id}"` + curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Eo 'confirm=[a-zA-Z0-9\-_]+'`&id=${file_id}" -o ${zkey_file} +fi + +# go test -timeout 0 github.com/ethstorage/go-ethstorage/integration_tests + +go test -timeout 0 -run ^TestKZGProver_GenerateKZGProof$ github.com/ethstorage/go-ethstorage/integration_tests diff --git a/integration_tests/signature_test.go b/integration_tests/signature_test.go index d6f22b9a..7da2dcd8 100644 --- a/integration_tests/signature_test.go +++ b/integration_tests/signature_test.go @@ -34,7 +34,7 @@ func TestSignerFactoryFromConfig(t *testing.T) { HDPath: "", }, common.HexToAddress("0xd7cc258C5438a392cA1D7873020d5B9971568c00"), - contractAddrDevnet1, + contractAddrDevnet2, false, }, { @@ -45,7 +45,7 @@ func TestSignerFactoryFromConfig(t *testing.T) { HDPath: "m/44'/60'/0'/0/0", }, common.HexToAddress("0x627306090abab3a6e1400e9345bc60c78a8bef57"), - contractAddrDevnet1, + contractAddrDevnet2, false, }, { @@ -58,7 +58,7 @@ func TestSignerFactoryFromConfig(t *testing.T) { Address: "0x13259366de990b0431e2c97cea949362bb68df12", }, common.HexToAddress("0x13259366DE990B0431E2C97CEa949362BB68df12"), - contractAddrDevnet1, + contractAddrDevnet2, false, }, { diff --git a/integration_tests/zk_prover_test.go b/integration_tests/zk_prover_test.go index 85fd604f..a082caeb 100644 --- a/integration_tests/zk_prover_test.go +++ b/integration_tests/zk_prover_test.go @@ -34,9 +34,16 @@ const ( snarkBuildDir = "snarkbuild" proofName = "proof_blob_poseidon.json" publicName = "public_blob_poseidon.json" + prPath = "../ethstorage/prover" + zkeyFile = "blob_poseidon.zkey" ) func TestZKProver_GenerateZKProof(t *testing.T) { + proverPath, _ := filepath.Abs(prPath) + zkeyFull := filepath.Join(proverPath, snarkLibDir, zkeyFile) + if _, err := os.Stat(zkeyFull); os.IsNotExist(err) { + t.Fatalf("%s not found", zkeyFull) + } type args struct { encodingKey common.Hash chunkIdx uint64 @@ -66,9 +73,8 @@ func TestZKProver_GenerateZKProof(t *testing.T) { false, }, } - path, _ := filepath.Abs("../ethstorage/prover") - libDir := filepath.Join(path, snarkLibDir) - p := prover.NewZKProverInternal(path, "blob_poseidon.zkey", lg) + libDir := filepath.Join(proverPath, snarkLibDir) + p := prover.NewZKProverInternal(proverPath, zkeyFile, lg) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { maskGo, err := GenerateMask(tt.args.encodingKey, tt.args.chunkIdx) @@ -81,7 +87,7 @@ func TestZKProver_GenerateZKProof(t *testing.T) { t.Errorf("ZKProver.GenerateZKProof() error = %v, wantErr %v", err, tt.wantErr) return } - buildDir := filepath.Join(path, snarkBuildDir, strings.Join([]string{ + buildDir := filepath.Join(proverPath, snarkBuildDir, strings.Join([]string{ tt.args.encodingKey.Hex(), fmt.Sprint(tt.args.chunkIdx), }, "-"))