-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'timing-map' into 'main'
Introduce timing map See merge request fireblocks/mpc/mpc-lib!16
- Loading branch information
Showing
10 changed files
with
140 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <mutex> | ||
#include <optional> | ||
#include "cosigner/platform_service.h" | ||
namespace fireblocks | ||
{ | ||
namespace common | ||
{ | ||
namespace cosigner | ||
{ | ||
|
||
class TimingMap final | ||
{ | ||
public: | ||
TimingMap(platform_service& platform_service); | ||
~TimingMap() = default; | ||
|
||
TimingMap(const TimingMap&) = delete; | ||
TimingMap(TimingMap&&) = delete; | ||
TimingMap& operator=(const TimingMap&) = delete; | ||
TimingMap& operator=(TimingMap&&) = delete; | ||
|
||
void insert(const std::string& key); | ||
const std::optional<const uint64_t> extract(const std::string& key); | ||
void erase(const std::string& key); | ||
|
||
private: | ||
const std::optional<const uint64_t> extract_impl(const std::string& key); | ||
std::mutex _lock; | ||
std::map<std::string, uint64_t> _data; | ||
platform_service& _time_service; | ||
}; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "cosigner/timing_map.h" | ||
|
||
namespace fireblocks | ||
{ | ||
namespace common | ||
{ | ||
namespace cosigner | ||
{ | ||
|
||
TimingMap::TimingMap(platform_service& platform_service): | ||
_time_service(platform_service) | ||
{ | ||
|
||
} | ||
void TimingMap::insert(const std::string& key) | ||
{ | ||
std::unique_lock<std::mutex> guard(_lock); | ||
#ifdef DEBUG | ||
// Unit tests may call some phase handlers more then once to test replays | ||
if (_data.find(key) == _data.end()) | ||
#endif | ||
_data[key] = _time_service.now_msec(); | ||
} | ||
|
||
void TimingMap::erase(const std::string& key) | ||
{ | ||
std::unique_lock<std::mutex> guard(_lock); | ||
_data.erase(key); | ||
} | ||
|
||
const std::optional<const uint64_t> TimingMap::extract_impl(const std::string& key) | ||
{ | ||
std::unique_lock<std::mutex> guard(_lock); | ||
auto iterator = _data.find(key); | ||
if (iterator == _data.end()) | ||
{ | ||
return std::nullopt; | ||
} | ||
const uint64_t value = iterator->second; | ||
|
||
_data.erase(iterator); | ||
return value; | ||
} | ||
|
||
const std::optional<const uint64_t> TimingMap::extract(const std::string& key) | ||
{ | ||
const std::optional<uint64_t> extracted = extract_impl(key); | ||
if (!extracted) | ||
{ | ||
return std::nullopt; | ||
} | ||
const uint64_t diff = (_time_service.now_msec() - *extracted); | ||
return diff; | ||
} | ||
|
||
} | ||
} | ||
} |