Skip to content

Commit

Permalink
New features and bug fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaven committed May 8, 2024
2 parents cae7999 + 585bbc8 commit 7f22c2b
Show file tree
Hide file tree
Showing 78 changed files with 9,510 additions and 30,466 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
os: [ ubuntu-22.04, windows-latest, macos-latest ]
std: [ 11, 20 ]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Haofan Zheng
Copyright (c) 2022 Haofan Zheng, Tuan Tran

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ An implementation of eclipse monitor in C++ introduced in the paper
## Code Status
- [![Unit Tests](https://github.com/zhenghaven/EclipseMonitor/actions/workflows/unit-tests.yaml/badge.svg?branch=main)](https://github.com/zhenghaven/EclipseMonitor/actions/workflows/unit-tests.yaml)
- Testing environments
- OS: `ubuntu-latest`, `windows-latest`, `macos-latest`
- OS: `ubuntu-22.04`, `windows-latest`, `macos-latest`
- C++ std: `11`, `20` (by setting CXX_STANDARD in CMake)
13 changes: 13 additions & 0 deletions include/EclipseMonitor/Config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023 Haofan Zheng
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

#pragma once


#ifdef ECLIPSEMONITOR_DEV_MODE
# define ECLIPSEMONITOR_DEV_USE_DEV_SESSION_ID
# define ECLIPSEMONITOR_DEV_USE_DEV_SYNC_NONCE
# define ECLIPSEMONITOR_DEV_DISABLE_REFRESH_SYNC
#endif
39 changes: 39 additions & 0 deletions include/EclipseMonitor/DataTypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023 Haofan Zheng
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

#pragma once


#include <cstdint>

#include <array>


namespace EclipseMonitor
{

/**
* @brief type for Trusted Timestamp, which received from a trusted source
*
*/
using TrustedTimestamp = uint64_t;


/**
* @brief type for Eclipse Monitor Session ID, which is a 128-bit unique random
* bytes, similar to UUID
*
*/
using SessionID = std::array<uint8_t, 16>;


/**
* @brief type for Sync Message Nonce, which is a 256-bit unique random bytes
*
*/
using SyncNonce = std::array<uint8_t, 32>;


} // namespace EclipseMonitor
183 changes: 183 additions & 0 deletions include/EclipseMonitor/EclipseMonitorBase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright (c) 2022 Haofan Zheng
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

#pragma once

#include <cstdint>

#include <memory>
#include <string>
#include <vector>

#include "Config.hpp"
#include "DataTypes.hpp"
#include "Logging.hpp"
#include "MonitorReport.hpp"
#include "PlatformInterfaces.hpp"

namespace EclipseMonitor
{

enum class Phases
{
BootstrapI,
BootstrapII,
Sync,
Runtime,
}; // enum class Phases


class EclipseMonitorBase
{
public: // Static members:

using TimestamperType = std::unique_ptr<TimestamperBase>;
using RandomGeneratorType = std::unique_ptr<RandomGeneratorBase>;

public:

EclipseMonitorBase(
const MonitorConfig& conf,
TimestamperType timestamper,
RandomGeneratorType randGen
) :
m_mConfig(conf),
m_mId(),
m_mSecState(),
m_phase(Phases::BootstrapI),
m_timestamper(std::move(timestamper)),
m_randGen(std::move(randGen)),
m_logger(LoggerFactory::GetLogger("EclipseMonitorBase"))
{
m_mId.get_sessionID().resize(std::tuple_size<SessionID>::value);
#ifdef ECLIPSEMONITOR_DEV_USE_DEV_SESSION_ID
// use development session ID
// sessionID = 0x52fdfc072182654f163f5f0f9a621d72
static constexpr SessionID sk_devSessId = {
0X52U, 0XFDU, 0XFCU, 0X07U, 0X21U, 0X82U, 0X65U, 0X4FU,
0X16U, 0X3FU, 0X5FU, 0X0FU, 0X9AU, 0X62U, 0X1DU, 0X72U,
};
std::copy(
sk_devSessId.begin(),
sk_devSessId.end(),
m_mId.get_sessionID().begin()
);
#else // ECLIPSEMONITOR_DEV_USE_DEV_SESSION_ID
// generate a random session ID
m_randGen->GenerateRandomBytes(
&(m_mId.get_sessionID()[0]),
m_mId.get_sessionID().size()
);
#endif // ECLIPSEMONITOR_DEV_USE_DEV_SESSION_ID
}

virtual ~EclipseMonitorBase() = default;

/**
* @brief Re-generate the sync nonce and the timestamp that marks
* the beginning of the sync process
* NOTE: a call to this function is needed at the beginning of EACH
* sync process, even the first one, otherwise, the beginning
* timestamp will be zero and the sync process will be failed
* eventually.
*
*/
// virtual std::vector<uint8_t> ResetSyncNonce()
// {
// m_syncStartTime = m_timestamper.NowInSec();

// // TODO: generate nonce
// }

const MonitorConfig& GetMonitorConfig() const
{
return m_mConfig;
}

const MonitorId& GetMonitorId() const
{
return m_mId;
}

const MonitorSecState& GetMonitorSecState() const
{
return m_mSecState;
}

const TimestamperBase& GetTimestamper() const
{
return *m_timestamper;
}

const RandomGeneratorBase& GetRandomGenerator() const
{
return *m_randGen;
}

virtual void Update(const std::vector<uint8_t>& hdrBinary) = 0;

virtual void EndBootstrapI()
{
m_phase = Phases::BootstrapII;
m_logger.Debug("Bootstrap I phase ended");
}

virtual void EndBootstrapII()
{
m_phase = Phases::Sync;
m_logger.Debug("Bootstrap II phase ended");
}

virtual void EndSync()
{
m_phase = Phases::Runtime;
m_logger.Debug("Entering runtime phase");
}

Phases GetPhase() const
{
return m_phase;
}

protected:

TimestamperBase& GetTimestamper()
{
return *m_timestamper;
}

RandomGeneratorBase& GetRandomGenerator()
{
return *m_randGen;
}

MonitorSecState& GetMonitorSecState()
{
return m_mSecState;
}

Logger& GetLogger()
{
return m_logger;
}

const Logger& GetLogger() const
{
return m_logger;
}

private:

MonitorConfig m_mConfig;
MonitorId m_mId;
MonitorSecState m_mSecState;
Phases m_phase;
TimestamperType m_timestamper;
RandomGeneratorType m_randGen;
Logger m_logger;

}; // class EclipseMonitorBase

} // namespace EclipseMonitor
Loading

0 comments on commit 7f22c2b

Please sign in to comment.