From b59796ca6a55577edb8c1853843255cb3930e16d Mon Sep 17 00:00:00 2001 From: yakud Date: Mon, 6 May 2024 22:23:45 +0300 Subject: [PATCH] Add Environment variables section to README.md --- README.md | 47 +++++++---------- cmd/galacticad-merkle/cmd/root.go | 2 +- cmd/tree-generator/main.go | 88 +++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 cmd/tree-generator/main.go diff --git a/README.md b/README.md index 2580206..8611895 100644 --- a/README.md +++ b/README.md @@ -65,43 +65,23 @@ And the global flags: The Merkle Proof Service provides two distinct API interfaces: `gRPC` and `gRPC-gateway`. The `gRPC-gateway` interface is an implementation of `OpenAPI v2`, which allows for the generation of client libraries in various programming languages. -### gRPC-gateway - -By default, the `gRPC-gateway` interface is available at `http://localhost:8480`. The OpenAPI v2 specification can be accessed at `http://localhost:8480/swagger.json` and documentation can be viewed at `http://localhost:8480/docs`. - -You can change the address of the `gRPC-gateway` address by several ways: - -- Pass the `--grpc-gateway.address` flag to the `start` command -- Set the `GRPC_GATEWAY_ADDRESS` environment variable -- Change the address in the configuration YAML file: - ```yaml - grpc_gateway: - address: localhost:8480 - ``` - -The service provides the following endpoints: - -- `GET /v1/galactica/merkle/proof/{registry}/{leaf}`: Retrieves the Merkle proof for a given contract address and leaf index. -- `GET /v1/galactica/merkle/empty_index/{registry}`: Retrieves the random empty index for a given contract address. - ### gRPC The `gRPC` interface is available at `http://localhost:50651`. You cain find the gRPC query server definition in the following file: [proto/galactica/merkle/query.proto](proto/galactica/merkle/query.proto). The service provides the following methods: -- `Proof`: Retrieves the Merkle proof for a given contract address and leaf index. +- `Proof`: Retrieves the Merkle proof for a given contract address and leaf index. - `GetEmptyIndex`: Retrieves the random empty index for a given contract address. -You can change the address of the `gRPC` server by several ways: +### gRPC-gateway + +By default, the `gRPC-gateway` interface is available at `http://localhost:8480`. The OpenAPI v2 specification can be accessed at `http://localhost:8480/swagger.json` and documentation can be viewed at `http://localhost:8480/docs`. + +The service provides the following endpoints: -- Pass the `--grpc.address` flag to the `start` command -- Set the `GRPC_ADDRESS` environment variable -- Change the address in the configuration YAML file: - ```yaml - grpc: - address: localhost:50651 - ``` +- `GET /v1/galactica/merkle/proof/{registry}/{leaf}`: Retrieves the Merkle proof for a given contract address and leaf index. +- `GET /v1/galactica/merkle/empty_index/{registry}`: Retrieves the random empty index for a given contract address. ## Configuration @@ -129,6 +109,17 @@ The `jobs` section of the configuration file is used to specify the Ethereum con You can add multiple jobs to the configuration file to index multiple contracts. +### Environment variables + +The Merkle Proof Service can be configured using environment variables. The following environment variables can be used to configure the service: + +- `MERKLE_HOME` - home directory to store data (default is `$HOME/.galacticad-merkle`) +- `MERKLE_CONFIG` - config file (default is `$HOME/.galacticad-merkle/merkle.yaml`) +- `DB_BACKEND` - database backend, available options: `goleveldb` (default), `cleveldb`, `memdb`, `boltdb`, `rocksdb`, `badgerdb`, `pebbledb` +- `EVM_RPC` - EVM RPC endpoint (default `ws://localhost:8546`) +- `GRPC_ADDRESS` - gRPC server address (default `localhost:50651`) +- `GRPC_GATEWAY_ADDRESS` - gRPC gateway server address (default `localhost:8480`) + ## License The Merkle Proof Service is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text. diff --git a/cmd/galacticad-merkle/cmd/root.go b/cmd/galacticad-merkle/cmd/root.go index 7c1343b..ccf58ad 100644 --- a/cmd/galacticad-merkle/cmd/root.go +++ b/cmd/galacticad-merkle/cmd/root.go @@ -37,7 +37,7 @@ import ( const ( FlagHome = "home" - EnvHome = "GALACTICA_HOME" + EnvHome = "MERKLE_HOME" ViperHome = "home" FlagConfig = "config" diff --git a/cmd/tree-generator/main.go b/cmd/tree-generator/main.go new file mode 100644 index 0000000..cf39b1f --- /dev/null +++ b/cmd/tree-generator/main.go @@ -0,0 +1,88 @@ +/* + * Copyright 2024 Galactica Network + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "log" + "time" + + db "github.com/cometbft/cometbft-db" + "github.com/holiman/uint256" + + "github.com/Galactica-corp/merkle-proof-service/internal/merkle" +) + +func main() { + //kv, err := db.NewGoLevelDB("merkle", "./testdb/") + kv, err := db.NewDB("merkle", db.PebbleDBBackend, "./testdb/") + if err != nil { + panic(err) + } + defer kv.Close() + treeStorage := merkle.NewSparseTreeStorage(kv, 0) + + sparseTree, err := merkle.NewSparseTree(32, merkle.EmptyLeafValue, treeStorage) + if err != nil { + panic(err) + } + + totalLeaves := 1000000 + batchSize := 10000 + + totalInserts := 0 + start := time.Now() + avgLeavesPerSecond := 0.0 + + for i := 0; i < totalLeaves; i += batchSize { + genStart := time.Now() + leaves := make([]merkle.Leaf, batchSize) + for j := 0; j < batchSize; j++ { + rndIndex, err := sparseTree.GetRandomEmptyLeafIndex() + if err != nil { + panic(err) + } + + leaves[j] = merkle.Leaf{Index: rndIndex, Value: uint256.NewInt(uint64(i))} + } + log.Printf("Generated %d leaves in %s\n", batchSize, time.Since(genStart)) + + insertStart := time.Now() + batch := merkle.NewBatchWithLeavesBuffer(treeStorage.NewBatch(), 0) + if err := sparseTree.InsertLeaves(batch, leaves); err != nil { + panic(err) + } + log.Printf("Inserted %d leaves in %s\n", batchSize, time.Since(insertStart)) + + writeStart := time.Now() + if err := batch.WriteSync(); err != nil { + panic(err) + } + log.Printf("Wrote %d leaves in %s\n", batchSize, time.Since(writeStart)) + + percent := float64(i+batchSize) / float64(totalLeaves) * 100 + leavesPerSecond := float64(batchSize) / time.Since(insertStart).Seconds() + totalInserts += batchSize + avgLeavesPerSecond = float64(totalInserts) / time.Since(start).Seconds() + + // calculate approx how long it take to insert all leaves: + timeLeft := time.Duration(float64(totalLeaves-totalInserts) / avgLeavesPerSecond * float64(time.Second)) + + log.Printf("Inserted %d leaves (%.2f%%) - %.2f leaves/s - %s left\n", totalInserts, percent, leavesPerSecond, timeLeft) + } + + log.Printf("Inserted %d leaves in %s - average %.2f leaves/s\n", totalInserts, time.Since(start), avgLeavesPerSecond) +}