From e25061b0b8a273f681ff73f1a31d105da95ddf62 Mon Sep 17 00:00:00 2001 From: yc1111 Date: Fri, 1 Dec 2023 13:36:00 +0000 Subject: [PATCH] Fix merkle tree get proof error --- distributed/store/strongstore/client.cc | 17 ++++++++-------- distributed/store/strongstore/client.h | 4 ++-- exps/process_ycsb.py | 14 ------------- ledger/ledgerdb/merkletree.cc | 22 +++++++++++--------- ledger/ledgerdb/mpt/trie.cc | 5 ++--- test/ledger/test_merkle_tree.cc | 27 +++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 37 deletions(-) diff --git a/distributed/store/strongstore/client.cc b/distributed/store/strongstore/client.cc index 4f4bc63..da459e8 100644 --- a/distributed/store/strongstore/client.cc +++ b/distributed/store/strongstore/client.cc @@ -441,9 +441,9 @@ Client::Abort() } } -int Client::Verify(std::map>>& keys) { +bool Client::Verify(std::map>>& keys) { // Contact the appropriate shard to set the value. - int status = REPLY_OK; + bool is_successful = true; #ifndef AMZQLDB list promises; @@ -491,19 +491,20 @@ int Client::Verify(std::map>>& 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& seqs) { +bool Client::Audit(std::map& seqs) { // Contact the appropriate shard to set the value. - int status = REPLY_OK; + bool status = true; list promises; if (seqs.size() == 0) { @@ -520,7 +521,7 @@ int Client::Audit(std::map& 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]; } diff --git a/distributed/store/strongstore/client.h b/distributed/store/strongstore/client.h index 3d29f55..10c0c86 100644 --- a/distributed/store/strongstore/client.h +++ b/distributed/store/strongstore/client.h @@ -39,9 +39,9 @@ class Client : public ::Client bool Commit(); bool Commit(std::map>>& keys); void Abort(); - int Verify(std::map>>& keys); + bool Verify(std::map>>& keys); std::vector Stats(); - int Audit(std::map& seqs); + bool Audit(std::map& seqs); private: /* Private helper functions. */ diff --git a/exps/process_ycsb.py b/exps/process_ycsb.py index f2842e2..9352155 100644 --- a/exps/process_ycsb.py +++ b/exps/process_ycsb.py @@ -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 @@ -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) @@ -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.." diff --git a/ledger/ledgerdb/merkletree.cc b/ledger/ledgerdb/merkletree.cc index fd4326a..f2262ad 100644 --- a/ledger/ledgerdb/merkletree.cc +++ b/ledger/ledgerdb/merkletree.cc @@ -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()); } @@ -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); diff --git a/ledger/ledgerdb/mpt/trie.cc b/ledger/ledgerdb/mpt/trie.cc index 1bdd3c0..ac1ac80 100644 --- a/ledger/ledgerdb/mpt/trie.cc +++ b/ledger/ledgerdb/mpt/trie.cc @@ -439,8 +439,9 @@ bool MPTProof::VerifyProof(const Hash& digest, const std::string& key) const { auto nodestr = GetMapChunk(i); Chunk chunk(reinterpret_cast(nodestr.c_str())); auto calculated = chunk.hash(); - if (target != calculated) + if (target != calculated) { return false; + } if (chunk.type() == ChunkType::kMPTFull) { ++pos; @@ -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; } } diff --git a/test/ledger/test_merkle_tree.cc b/test/ledger/test_merkle_tree.cc index 5cc60b1..6caea65 100644 --- a/test/ledger/test_merkle_tree.cc +++ b/test/ledger/test_merkle_tree.cc @@ -6,6 +6,33 @@ #include "ledger/common/db.h" #include "ledger/ledgerdb/merkletree.h" +#include "ledger/ledgerdb/types.h" + +TEST(MERKLETREE, UPD) { + std::vector 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 hashes;