Skip to content

Commit

Permalink
Merge pull request #599 from 0xPolygonHermez/597-revise-zkresults-of-…
Browse files Browse the repository at this point in the history
…function-readkv

Check the zkresult outputs
  • Loading branch information
rickb80 authored Sep 21, 2023
2 parents a9e3263 + 7cd948a commit 55fd3d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
40 changes: 15 additions & 25 deletions src/hashdb64/database_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ zkresult Database64::readKV(const Goldilocks::Element (&root)[4], const Goldiloc
struct timeval t;
if (dbReadLog != NULL) gettimeofday(&t, NULL);

zkresult rkv = ZKR_UNSPECIFIED;
zkresult rv = ZKR_UNSPECIFIED;
zkresult rout = ZKR_UNSPECIFIED;

string keyStr = "";
Expand All @@ -334,43 +332,39 @@ zkresult Database64::readKV(const Goldilocks::Element (&root)[4], const Goldiloc
}

uint64_t version;
rv = readVersion(root, version, dbReadLog);
rout = readVersion(root, version, dbReadLog);

if( rout == ZKR_SUCCESS){

if( rv == ZKR_SUCCESS){

// If the key is found in local database (cached) simply return it
if(dbKVACache.findKey(version, key, value)){

if (dbReadLog != NULL) dbReadLog->add(keyStr, value, true, TimeDiff(t));
rkv = ZKR_SUCCESS;
rout = rkv;

rout = ZKR_SUCCESS;
}
// If the key is pending to be stored in database, but already deleted from cache
else if (config.dbMultiWrite && multiWrite.findKeyValue(version, key, value))
{
// Add to the read log
if (dbReadLog != NULL) dbReadLog->add(keyStr, value, true, TimeDiff(t));
// We do not store into cache as we do not want to manage the chain of versions
rkv = ZKR_SUCCESS;
rout = ZKR_SUCCESS;
}
else if(useRemoteDB)
{
vector<VersionValue> upstreamVersionValues;
rkv = readRemoteKV(version, key, value, upstreamVersionValues);
if (rkv == ZKR_SUCCESS)
rout = readRemoteKV(version, key, value, upstreamVersionValues);
if (rout == ZKR_SUCCESS)
{
dbKVACache.uploadKeyValueVersions(key, upstreamVersionValues);
if (dbReadLog != NULL) dbReadLog->add(keyStr, value, false, TimeDiff(t));
} else {

if( rkv == ZKR_DB_KEY_NOT_FOUND){
rout = rkv;
if( rout == ZKR_DB_KEY_NOT_FOUND){
// Add a zero into the cache to avoid future remote access for this key (not problematic management of versions as there is only one version)
mpz_class zero(0);
dbKVACache.addKeyValueVersion(0, key, zero);
}else if( rkv == ZKR_DB_VERSION_NOT_FOUND_GLOBAL){
rout = rkv;
}else if( rout == ZKR_DB_VERSION_NOT_FOUND_GLOBAL){
// Add a zero into the cache to avoid future remote access for this key (not problematic management of versions as there is only one version)
dbKVACache.uploadKeyValueVersions(key, upstreamVersionValues);
}else{
Expand All @@ -388,8 +382,7 @@ zkresult Database64::readKV(const Goldilocks::Element (&root)[4], const Goldiloc
}
} else {
zklog.warning("Database64::readKV() requested a root that does not exist in the table statedb.version " + keyStr + " , "
+ zkresult2string(rv) );
rout = rv;
+ zkresult2string(rout) );
}

#ifdef LOG_DB_READ
Expand Down Expand Up @@ -425,15 +418,12 @@ zkresult Database64::writeKV(const Goldilocks::Element (&root)[4], const Goldilo
{


zkresult rkv = ZKR_UNSPECIFIED;
zkresult rv = ZKR_UNSPECIFIED;
zkresult rout = ZKR_UNSPECIFIED;

uint64_t version;
rv = readVersion(root, version, NULL);
if( rv == ZKR_SUCCESS){
writeKV(version, key, value, persistent);
}else{
rkv=rv;
rout = readVersion(root, version, NULL);
if( rout == ZKR_SUCCESS){
rout = writeKV(version, key, value, persistent);
}

#ifdef LOG_DB_WRITE
Expand All @@ -451,7 +441,7 @@ zkresult Database64::writeKV(const Goldilocks::Element (&root)[4], const Goldilo
}
#endif

return rkv;
return rout;
}

zkresult Database64::writeKV(const uint64_t& version, const Goldilocks::Element (&key)[4], const mpz_class &value, bool persistent)
Expand Down
2 changes: 1 addition & 1 deletion src/hashdb64/state_manager_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ zkresult StateManager64::set (const string &batchUUID, uint64_t tx, Database64 &

// Write the key-value pair
string hashString = fea2string(fr, key);
uint64_t level;
uint64_t level=0;
uint64_t stateManagerLevel;
uint64_t databaseLevel;
zkr = stateManager64.write(batchUUID, tx, hashString, value, persistence, stateManagerLevel);
Expand Down

0 comments on commit 55fd3d3

Please sign in to comment.