Skip to content

Commit

Permalink
Fix merkle tree get proof error
Browse files Browse the repository at this point in the history
  • Loading branch information
yc1111 committed Dec 1, 2023
1 parent 21c6f87 commit e25061b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 37 deletions.
17 changes: 9 additions & 8 deletions distributed/store/strongstore/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ Client::Abort()
}
}

int Client::Verify(std::map<int, std::map<uint64_t, std::vector<std::string>>>& keys) {
bool Client::Verify(std::map<int, std::map<uint64_t, std::vector<std::string>>>& keys) {
// Contact the appropriate shard to set the value.
int status = REPLY_OK;
bool is_successful = true;

#ifndef AMZQLDB
list<Promise*> promises;
Expand Down Expand Up @@ -491,19 +491,20 @@ int Client::Verify(std::map<int, std::map<uint64_t, std::vector<std::string>>>&
std::cout << "verifynkeys " << nkeys << std::endl;

for (auto& p : promises) {
if (p->GetReply() != REPLY_OK) {
status = p->GetReply();
if (p->GetReply() != REPLY_OK ||
p->GetVerifyStatus() != VerifyStatus::PASS) {
is_successful = false;
}
delete p;
}
#endif

return status;
return is_successful;
}

int Client::Audit(std::map<int, uint64_t>& seqs) {
bool Client::Audit(std::map<int, uint64_t>& seqs) {
// Contact the appropriate shard to set the value.
int status = REPLY_OK;
bool status = true;
list<Promise *> promises;

if (seqs.size() == 0) {
Expand All @@ -520,7 +521,7 @@ int Client::Audit(std::map<int, uint64_t>& seqs) {
int n = 0;
for (auto p : promises) {
if (p->GetReply() != REPLY_OK) {
status = p->GetReply();
status = false;
} else if (p->GetVerifyStatus() != VerifyStatus::UNVERIFIED) {
++seqs[n];
}
Expand Down
4 changes: 2 additions & 2 deletions distributed/store/strongstore/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class Client : public ::Client
bool Commit();
bool Commit(std::map<int, std::map<uint64_t, std::vector<std::string>>>& keys);
void Abort();
int Verify(std::map<int, std::map<uint64_t, std::vector<std::string>>>& keys);
bool Verify(std::map<int, std::map<uint64_t, std::vector<std::string>>>& keys);
std::vector<int> Stats();
int Audit(std::map<int, uint64_t>& seqs);
bool Audit(std::map<int, uint64_t>& seqs);

private:
/* Private helper functions. */
Expand Down
14 changes: 0 additions & 14 deletions exps/process_ycsb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
nkey = 0
nkeys = []

tExtra = 0.0
sExtra = 0.0
fExtra = 0.0

xLatency = []

for line in open(sys.argv[1]):
if line.startswith('#') or line.strip() == "":
continue
Expand All @@ -48,11 +42,6 @@
latency = int(line[3])
status = int(line[4])
op = int(line[5])
ttype = -1
extra = 0

if status == 1 and ttype == 2:
xLatency.append(latency)

if op == 0:
rLatency.append(latency)
Expand All @@ -65,14 +54,11 @@
vLatency.append(latency)

tLatency.append(latency)
tExtra += extra

if status == 1:
sLatency.append(latency)
sExtra += extra
else:
fLatency.append(latency)
fExtra += extra

if len(tLatency) == 0:
print "Zero completed transactions.."
Expand Down
22 changes: 12 additions & 10 deletions ledger/ledgerdb/merkletree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ void MerkleTree::update(const uint64_t starting_block_seq,
} else {
auto parent = Hash::ComputeFrom(level_hashes[i].value(),
Hash::kByteLength);
if (i == level_hashes.size() - 1) {
parent_key += "-" + commit_seq;
complete = false;
}
parent_key += "-" + commit_seq;
complete = false;
ledger_->Put(parent_key, parent.ToBase32());
parent_hashes.emplace_back(parent.Clone());
}
Expand Down Expand Up @@ -96,13 +94,17 @@ Proof MerkleTree::getProof(const std::string& commit_seq,
for (int i = 0; i < level; ++i) {
std::string res;
if (ptr % 2 == 0) {
std::string sibling_key =
"mt" + std::to_string(i) + "-" + std::to_string(ptr+1);
if (ptr + 1 == last && i > 0 && !complete) {
sibling_key += "-" + commit_seq;
}
ledger_->Get(sibling_key, &res);
proof.pos.emplace_back(1);
if (ptr == last) {
res = "";
} else {
std::string sibling_key =
"mt" + std::to_string(i) + "-" + std::to_string(ptr+1);
if (ptr + 1 == last && i > 0 && !complete) {
sibling_key += "-" + commit_seq;
}
ledger_->Get(sibling_key, &res);
}
} else {
std::string sibling_key =
"mt" + std::to_string(i) + "-" + std::to_string(ptr-1);
Expand Down
5 changes: 2 additions & 3 deletions ledger/ledgerdb/mpt/trie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,9 @@ bool MPTProof::VerifyProof(const Hash& digest, const std::string& key) const {
auto nodestr = GetMapChunk(i);
Chunk chunk(reinterpret_cast<const unsigned char*>(nodestr.c_str()));
auto calculated = chunk.hash();
if (target != calculated)
if (target != calculated) {
return false;
}

if (chunk.type() == ChunkType::kMPTFull) {
++pos;
Expand Down Expand Up @@ -477,8 +478,6 @@ bool MPTProof::VerifyProof(const Hash& digest, const std::string& key) const {
auto curr_key = key.substr(pos);
size_t match_len = MPTConfig::PrefixLen(curr_key, target_key);
if (match_len != target_key.size()) {
return true;
} else {
return false;
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/ledger/test_merkle_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@

#include "ledger/common/db.h"
#include "ledger/ledgerdb/merkletree.h"
#include "ledger/ledgerdb/types.h"

TEST(MERKLETREE, UPD) {
std::vector<std::string> hashes;

ledgebase::DB db;
db.Open("testdb");
ledgebase::ledgerdb::MerkleTree mt(&db);

uint64_t block_seq = 0;

timeval t0, t1;
gettimeofday(&t0, NULL);
for (size_t i = 0; i < 100; ++i) {
hashes.emplace_back(ledgebase::Hash::ComputeFrom(std::to_string(i)).ToBase32());
}

std::string root_key, root_hash;
mt.update(block_seq, hashes, "", &root_key, &root_hash);

for (int i = 0; i < 100; ++i) {
std::cout << "###### " << i << std::endl;
auto proof = mt.getProof("0", root_key, 99, i);
auto res = proof.Verify();
ASSERT_EQ(true, res);
}
}

TEST(MERKLETREE, TPS) {
std::vector<std::string> hashes;
Expand Down

0 comments on commit e25061b

Please sign in to comment.