Skip to content

Commit

Permalink
Merge pull request #663 from 0xPolygonHermez/fractasy_hashdb_64
Browse files Browse the repository at this point in the history
Implement ReadTree() with hashValues
  • Loading branch information
fractasy authored Oct 20, 2023
2 parents e9a6978 + 5752d3d commit 2dea484
Show file tree
Hide file tree
Showing 11 changed files with 1,202 additions and 43 deletions.
24 changes: 3 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,11 @@ sudo docker run --rm --network host -ti -p 50051:50051 -p 50061:50061 -p 50071:5

## Usage

To run the Prover, supply a `config.json` file containing the parameters that help customize various Prover settings. By default, the Prover accesses the `config.json` file from the `testvectors` directory. Below are some of the key parameters, accompanied by their default values from the given `config.json`:

| Parameter | Description |
| ---------------------- | ----------- |
| `runStateDBServer` | Enables StateDB GRPC service, provides SMT (Sparse Merkle Tree) and Database access |
| `runExecutorServer` | Enables Executor GRPC service, provides a service to process transaction batches |
| `runAggregatorClient` | Enables Aggregator GRPC client, connects to the Aggregator and process its requests |
| `aggregatorClientHost` | IP address of the Aggregator server to which the Aggregator client must connect to |
| `runProverServer` | Enables Prover GRPC service |
| `runFileProcessBatch` | Processes a batch using as input a JSON file defined in the `"inputFile"` parameter |
| `runFileGenProof` | Generates a proof using as input a JSON file defined in the `"inputFile"` parameter |
| `inputFile` | Input JSON file with path relative to the `testvectors` folder |
| `outputPath` | Output path to store the result files, relative to the `testvectors` folder |
| `saveRequestToFile` | Saves service received requests to a text file |
| `saveResponseToFile` | Saves service returned responses to a text file |
| `saveInputToFile` | Saves service received input data to a JSON file |
| `saveOutputToFile` | Saves service returned output data to a JSON file |
| `databaseURL` | For the StateDB service, if the value is `"local"`, data is stored in memory; otherwise, use the PostgreSQL format: `"postgresql://<user>:<password>@<ip>:<port>/<database>"`, e.g., `"postgresql://statedb:statedb@127.0.0.1:5432/testdb"`. |
| `stateDBURL` | For the StateDB service, if the value is "`local"`, a local client replaces the GRPC service. Use the format: `"<ip>:<port>", e.g., "127.0.0.1:50061"`. |
To run the Prover, supply a `config.json` file containing the parameters that help customize various Prover settings. By default, the Prover accesses the file `config/config.json`. You can specify a different config file location using the '-c <file>' argument. In order to know about the different available configuration parameters, please read the src/config/README.md file.

To execute a proof test:

1. Modify the `config.json` file, setting the `"runFileGenProof"` parameter to `"true"`. Ensure all other parameters are set to `"false"`. If you prefer not to use a PostgreSQL database for the test, adjust the `"databaseURL"` to `"local"`.
2. For the `"inputFile"` parameter, specify the desired input test data file. As an example, the `testvectors` directory contains the `input_executor.json` file.
3. Launch the Prover from the `testvectors` directory using the command: `../build/zkProver`.
2. For the `"inputFile"` parameter, specify the desired input test data file. As an example, use the file `testvectors/batchProof/input_executor_0.json`.
3. Launch the Prover from the project root directory using the command: `build/zkProver`.
4. The proof's result files will be saved in the directory defined by the `"outputPath"` configuration parameter.
36 changes: 36 additions & 0 deletions src/hashdb64/child.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,40 @@ string Child::print (Goldilocks &fr) const
return "INVALID TYPE type=" + to_string(type);
}
}
}

zkresult Child::getHash (Goldilocks::Element (&hash)[4]) const
{
switch (type)
{
case ZERO:
{
hash[0] = fr.zero();
hash[1] = fr.zero();
hash[2] = fr.zero();
hash[3] = fr.zero();
return ZKR_SUCCESS;
}
case LEAF:
{
hash[0] = leaf.hash[0];
hash[1] = leaf.hash[1];
hash[2] = leaf.hash[2];
hash[3] = leaf.hash[3];
return ZKR_SUCCESS;
}
case INTERMEDIATE:
{
hash[0] = intermediate.hash[0];
hash[1] = intermediate.hash[1];
hash[2] = intermediate.hash[2];
hash[3] = intermediate.hash[3];
return ZKR_SUCCESS;
}
default:
{
zklog.error("Child::getHash() found unexpected type=" + to_string(type));
return ZKR_DB_ERROR;
}
}
}
2 changes: 2 additions & 0 deletions src/hashdb64/child.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Child
}
}
}

zkresult getHash (Goldilocks::Element (&hash)[4]) const;
};

#endif
21 changes: 7 additions & 14 deletions src/hashdb64/database_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "key_value_page.hpp"
#include "raw_data_page.hpp"
#include "zkglobals.hpp"
#include "key_value_history_page.hpp"

// Helper functions
string removeBSXIfExists64(string s) {return ((s.at(0) == '\\') && (s.at(1) == 'x')) ? s.substr(2) : s;}
Expand Down Expand Up @@ -397,7 +398,8 @@ zkresult Database64::WriteTree (const Goldilocks::Element (&oldRoot)[4], const v
pageManager.flushPages();

headerPageNumber = 0;
//HeaderPage::Print(headerPageNumber, true);

//KeyValueHistoryPage::Print(versionData.keyValueHistoryPage, true, "version=" + to_string(version) + " ");

return ZKR_SUCCESS;
}
Expand Down Expand Up @@ -449,20 +451,11 @@ zkresult Database64::ReadTree (const Goldilocks::Element (&root)[4], vector<KeyV
}

// Read all key-values
string keyString;
string key;
uint64_t level;
for (uint64_t i=0; i<keyValues.size(); i++)
zkr = HeaderPage::KeyValueHistoryReadTree(versionData.keyValueHistoryPage, version, keyValues, hashValues);
if (zkr != ZKR_SUCCESS)
{
keyString = fea2string(fr, keyValues[i].key);
key = string2ba(keyString);
zkr = HeaderPage::KeyValueHistoryRead(versionData.keyValueHistoryPage, key, version, keyValues[i].value, level);
if (zkr != ZKR_SUCCESS)
{
zklog.error("Database64::ReadTree() failed calling HeaderPage::KeyValueHistoryRead() result=" + zkresult2string(zkr) + " rootString=" + rootString + " version=" + to_string(version));
return zkr;
}
//zklog.info("Database64::ReadTree() called HeaderPage::KeyValueHistoryRead() rootString=" + rootString + " version=" + to_string(version) + " key=" + keyString + " value=" + keyValues[i].value.get_str(16));
zklog.error("Database64::ReadTree() failed calling HeaderPage::KeyValueHistoryReadTree() result=" + zkresult2string(zkr) + " rootString=" + rootString + " version=" + to_string(version));
return zkr;
}

return ZKR_SUCCESS;
Expand Down
6 changes: 6 additions & 0 deletions src/hashdb64/page/header_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ zkresult HeaderPage::KeyValueHistoryReadLevel (const uint64_t &headerPageNumber,
return KeyValueHistoryPage::ReadLevel(headerPage->keyValueHistoryPage, key, keyLevel);
}

zkresult HeaderPage::KeyValueHistoryReadTree (const uint64_t keyValueHistoryPage, const uint64_t version, vector<KeyValue> &keyValues, vector<HashValueGL> *hashValues)
{
// Call the specific method
return KeyValueHistoryPage::ReadTree(keyValueHistoryPage, version, keyValues, hashValues);
}

zkresult HeaderPage::KeyValueHistoryWrite (uint64_t &headerPageNumber, const string &key, const uint64_t version, const mpz_class &value)
{
// Get an editable page
Expand Down
3 changes: 3 additions & 0 deletions src/hashdb64/page/header_page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "zkassert.hpp"
#include "version_data_page.hpp"
#include "scalar.hpp"
#include "key_value.hpp"
#include "hash_value_gl.hpp"

struct HeaderStruct
{
Expand Down Expand Up @@ -58,6 +60,7 @@ class HeaderPage
// Key-Value-History methods
static zkresult KeyValueHistoryRead (const uint64_t keyValueHistoryPage, const string &key, const uint64_t version, mpz_class &value, uint64_t &keyLevel);
static zkresult KeyValueHistoryReadLevel (const uint64_t &headerPageNumber, const string &key, uint64_t &keyLevel);
static zkresult KeyValueHistoryReadTree (const uint64_t keyValueHistoryPage, const uint64_t version, vector<KeyValue> &keyValues, vector<HashValueGL> *hashValues);
static zkresult KeyValueHistoryWrite ( uint64_t &headerPageNumber, const string &key, const uint64_t version, const mpz_class &value);
static zkresult KeyValueHistoryCalculateHash ( uint64_t &headerPageNumber, Goldilocks::Element (&hash)[4]);
static zkresult KeyValueHistoryPrint (const uint64_t headerPageNumber, const string &root);
Expand Down
Loading

0 comments on commit 2dea484

Please sign in to comment.