Skip to content

Commit

Permalink
Add Environment variables section to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yakud committed May 6, 2024
1 parent f165743 commit b59796c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 29 deletions.
47 changes: 19 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion cmd/galacticad-merkle/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

const (
FlagHome = "home"
EnvHome = "GALACTICA_HOME"
EnvHome = "MERKLE_HOME"
ViperHome = "home"

FlagConfig = "config"
Expand Down
88 changes: 88 additions & 0 deletions cmd/tree-generator/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b59796c

Please sign in to comment.