Skip to content

Commit

Permalink
Merge pull request #667 from 0xPolygonHermez/header_page_genesis
Browse files Browse the repository at this point in the history
Manage HeaderPage - PageManager interaction + automatic DB extension
  • Loading branch information
rickb80 authored Oct 23, 2023
2 parents 2f68f78 + 1a410bf commit 4e4b240
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 234 deletions.
3 changes: 3 additions & 0 deletions src/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ The configuration parameters can be of different uses:
|`hashDB64`|test|boolean|Use HashDB64 new database (experimental)|false|HASHDB64|
|`kvDBMaxVersions`|production|u64|Maximum number of KV versionn in Database|131072|HASHDB64_MAX_VERSIONS|
|`dbCacheSynchURL`|test|string|URL of the HashDB service to synchronize the Database cache (experimental)|""|DB_CACHE_SYNCH_URL|
|`hashDBFileName`|test|string|core name used for the hashDB files (path,numbering and extension not included). If hashDBFileName is empty in-memory version of the hashDB is used (only for DEBUG purposes). |""|HASHDB_FILE_NAME|
|`hashDBFileSize`|test|u64| hashDB files size in GB|128|HASHDB_FILE_SIZE|
|`hashDBFolder`|test|string|folder containing the hashDB files|hashdb|HASHDB_FOLDER|
|`aggregatorServerPort`|test|u16|Aggregator server GRPC port|50081|AGGREGATOR_SERVER_PORT|
|**`aggregatorClientPort`**|production|u16|Aggregator client GRPC port to connect to|50081|AGGREGATOR_SERVER_PORT|
|**`aggregatorClientHost`**|production|string|Aggregator client GRPC host name to connect to, i.e. Aggregator server host name|"127.0.0.1"|AGGREGATOR_CLIENT_HOST|
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ void Config::load(json &config)
ParseBool(config, "hashDB64", "HASHDB64", hashDB64, false);
ParseU64(config, "kvDBMaxVersions", "HASHDB64_MAX_VERSIONS", kvDBMaxVersions, 131072);
ParseString(config, "dbCacheSynchURL", "DB_CACHE_SYNCH_URL", dbCacheSynchURL, "");
ParseString(config, "hashDBFileName", "HASHDB_FILE_NAME", hashDBFileName, "");
ParseU64(config, "hashDBFileSize", "HASHDB_FILE_SIZE", hashDBFileSize, 128);
ParseString(config, "hashDBFolder", "HASHDB_FOLDER", hashDBFolder, "hashdb");
ParseU16(config, "aggregatorServerPort", "AGGREGATOR_SERVER_PORT", aggregatorServerPort, 50081);
ParseU16(config, "aggregatorClientPort", "AGGREGATOR_CLIENT_PORT", aggregatorClientPort, 50081);
ParseString(config, "aggregatorClientHost", "AGGREGATOR_CLIENT_HOST", aggregatorClientHost, "127.0.0.1");
Expand Down Expand Up @@ -433,6 +436,9 @@ void Config::print(void)
zklog.info(" hashDB64=" + to_string(hashDB64));
zklog.info(" kvDBMaxVersions=" + to_string(kvDBMaxVersions));
zklog.info(" dbCacheSynchURL=" + dbCacheSynchURL);
zklog.info(" hashDBFileName=" + hashDBFileName);
zklog.info(" hashDBFileSize=" + to_string(hashDBFileSize));
zklog.info(" hastDBFolder=" + hashDBFolder);
zklog.info(" aggregatorServerPort=" + to_string(aggregatorServerPort));
zklog.info(" aggregatorClientPort=" + to_string(aggregatorClientPort));
zklog.info(" aggregatorClientHost=" + aggregatorClientHost);
Expand Down
3 changes: 3 additions & 0 deletions src/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Config
bool hashDB64;
uint64_t kvDBMaxVersions;
string dbCacheSynchURL;
string hashDBFileName;
uint64_t hashDBFileSize;
string hashDBFolder;

// Aggregator service (client)
uint16_t aggregatorServerPort;
Expand Down
8 changes: 5 additions & 3 deletions src/hashdb64/database_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@
// Helper functions
string removeBSXIfExists64(string s) {return ((s.at(0) == '\\') && (s.at(1) == 'x')) ? s.substr(2) : s;}

Database64::Database64 (Goldilocks &fr, const Config &config) : headerPageNumber(0), currentFlushId(0), ctx(pageManager, config)
Database64::Database64 (Goldilocks &fr, const Config &config) : headerPageNumber(0), currentFlushId(0), pageManager(), ctx(pageManager, config)
{
// Init mutex
pthread_mutex_init(&mutex, NULL);

zkresult zkr;
headerPageNumber = 0;
zkr = HeaderPage::InitEmptyPage(ctx, headerPageNumber);

// Init page manager
zkr = pageManager.init(ctx);
if (zkr != ZKR_SUCCESS)
{
zklog.error("Database64::Database64() failed calling HeaderPage::InitEmptyPage() result=" + zkresult2string(zkr));
zklog.error("Database64::Database64() failed to init page manager result=" + zkresult2string(zkr));
exitProcess();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/hashdb64/database_64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class Database64
uint64_t headerPageNumber;
pthread_mutex_t mutex;
uint64_t currentFlushId;
PageManager pageManager;
PageContext ctx;


public:

// Constructor and destructor
Expand Down
36 changes: 35 additions & 1 deletion src/hashdb64/page/header_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
#include "page_list_page.hpp"
#include "root_version_page.hpp"


zkresult HeaderPage::Check (PageContext &ctx, const uint64_t headerPageNumber)
{
// Get the header page
HeaderStruct * page = (HeaderStruct *)ctx.pageManager.getPageAddress(headerPageNumber);

// Check the uuid
for(int i=0; i<32; ++i){
if (page->uuid[i] != ctx.uuid[i])
{
zklog.error("HeaderPage::Check() found uuid[" + to_string(i) + "]=" + to_string(page->uuid[i]) + " != config.hashDBUUID[" + to_string(i) + "]=" + to_string(ctx.uuid[i]));
exitProcess();
return ZKR_DB_ERROR;
}
}
return ZKR_SUCCESS;

}
zkresult HeaderPage::InitEmptyPage (PageContext &ctx, const uint64_t headerPageNumber)
{
zkresult zkr;
Expand Down Expand Up @@ -64,13 +82,18 @@ zkresult HeaderPage::InitEmptyPage (PageContext &ctx, const uint64_t headerPageN
}

// Create the free pages page, and init it
page->firstUnusedPage = 2;
page->freePages = ctx.pageManager.getFreePage();
zkr = PageListPage::InitEmptyPage(ctx, page->freePages);
if (zkr != ZKR_SUCCESS)
{
zklog.error("HeaderPage::InitEmptyPage() failed calling PageListPage::InitEmptyPage(freePages) result=" + zkresult2string(zkr));
return zkr;
}
// UUID
for(int i=0; i<32; ++i){
page->uuid[i] = ctx.uuid[i];
}

return ZKR_SUCCESS;
}
Expand Down Expand Up @@ -137,7 +160,18 @@ zkresult HeaderPage::CreateFreePages (PageContext &ctx, uint64_t &headerPageNumb

}

zkresult HeaderPage::setFirstUnusedPage (PageContext &ctx, uint64_t &headerPageNumber, const uint64_t firstUnusedPage)
zkresult HeaderPage::GetFirstUnusedPage (PageContext &ctx,const uint64_t headerPageNumber, uint64_t &firstUnusedPage)
{
// Get header page
HeaderStruct * page = (HeaderStruct *)ctx.pageManager.getPageAddress(headerPageNumber);

// Call the specific method
firstUnusedPage = page->firstUnusedPage;

return ZKR_SUCCESS;
}

zkresult HeaderPage::SetFirstUnusedPage (PageContext &ctx,uint64_t &headerPageNumber, const uint64_t firstUnusedPage)
{
// Get an editable page
headerPageNumber = ctx.pageManager.editPage(headerPageNumber);
Expand Down
10 changes: 9 additions & 1 deletion src/hashdb64/page/header_page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

struct HeaderStruct
{
// UUID
uint8_t uuid[32];

// Raw data
uint64_t firstRawDataPage;
uint64_t rawDataPage;
Expand All @@ -39,6 +42,10 @@ struct HeaderStruct
class HeaderPage
{
public:

// Header uuid
static zkresult Check (PageContext &ctx, const uint64_t headerPageNumber);

// Header-only methods
static zkresult InitEmptyPage (PageContext &ctx, const uint64_t headerPageNumber);
static uint64_t GetLastVersion (PageContext &ctx, const uint64_t headerPageNumber);
Expand All @@ -48,7 +55,8 @@ class HeaderPage
static zkresult GetFreePagesContainer (PageContext &ctx, const uint64_t headerPageNumber, vector<uint64_t> (&containerPages));
static zkresult GetFreePages (PageContext &ctx, const uint64_t headerPageNumber, vector<uint64_t> (&freePages));
static zkresult CreateFreePages (PageContext &ctx, uint64_t &headerPageNumber, vector<uint64_t> (&freePages), vector<uint64_t> (&containerPages));
static zkresult setFirstUnusedPage (PageContext &ctx, uint64_t &headerPageNumber, const uint64_t firstUnusedPage);
static zkresult GetFirstUnusedPage (PageContext &ctx, const uint64_t headerPageNumber, uint64_t &firstUnusedPage);
static zkresult SetFirstUnusedPage (PageContext &ctx, uint64_t &headerPageNumber, const uint64_t firstUnusedPage);

// Root version methods
static zkresult ReadRootVersion (PageContext &ctx, const uint64_t headerPageNumber, const string &root, uint64_t &version);
Expand Down
7 changes: 4 additions & 3 deletions src/hashdb64/page/page_list_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ zkresult PageListPage::GetContainerPages (PageContext &ctx, const uint64_t page
zkresult PageListPage::CreatePages (PageContext &ctx, uint64_t &pageNumber_, vector<uint64_t> (&freePages), vector<uint64_t> (&containerPages))
{
if(containerPages.size() == 0){
pageNumber_ = 0;
return ZKR_SUCCESS;
zklog.error("PageListPage::CreatePages() containerPages.size() == 0");
exitProcess();
return ZKR_DB_ERROR;
}
pageNumber_ = containerPages[0];

Expand Down Expand Up @@ -219,7 +220,7 @@ zkresult PageListPage::CreatePages (PageContext &ctx, uint64_t &pageNumber_, vec
}

// Update the page offset
page->nextPageNumberAndOffset = (offset + 8) << 48;
page->nextPageNumberAndOffset = offset << 48;
page->previousPageNumber = (i == containerPages.size() - 1) ? 0 : containerPages[i + 1];
}

Expand Down
Loading

0 comments on commit 4e4b240

Please sign in to comment.