diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp index 0d9a57255b..8521859e84 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp @@ -611,6 +611,11 @@ public: */ int init(uint8_t cluster_size = ClusterManager::ClusterSizeUnknown); + /** + * This function is mostly needed for testing. + */ + Log::Index getCommitIndex() const { return commit_index_; } + /** * Only the leader can call @ref appendLog(). */ @@ -621,7 +626,7 @@ public: * Failures are tolerble because all operations are idempotent. * This method will trigger an assertion failure and return error if the current node is not the leader. */ - int appendLog(const Entry& entry); + int appendLog(const Entry::FieldTypes::unique_id& unique_id, NodeID node_id); /** * This class is used to perform log searches. diff --git a/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp b/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp index 5707b165e0..6d216a484e 100644 --- a/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp +++ b/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp @@ -1513,10 +1513,15 @@ int RaftCore::init(uint8_t cluster_size) return 0; } -int RaftCore::appendLog(const Entry& entry) +int RaftCore::appendLog(const Entry::FieldTypes::unique_id& unique_id, NodeID node_id) { if (isLeader()) { + Entry entry; + entry.node_id = node_id.get(); + entry.unique_id = unique_id; + entry.term = persistent_state_.getCurrentTerm(); + trace(TraceRaftNewLogEntry, entry.node_id); return persistent_state_.getLog().append(entry); } diff --git a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp index 5de7b7a213..2d8dfd97b3 100644 --- a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp +++ b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp @@ -871,6 +871,22 @@ TEST(DynamicNodeIDAllocationServer, RaftCoreBasic) // The one with lower node ID must become a leader ASSERT_TRUE(raft_a.isLeader()); ASSERT_FALSE(raft_b.isLeader()); + + ASSERT_EQ(0, raft_a.getCommitIndex()); + ASSERT_EQ(0, raft_b.getCommitIndex()); + + /* + * Adding some stuff + */ + uavcan::protocol::dynamic_node_id::server::Entry::FieldTypes::unique_id unique_id; + uavcan::fill_n(unique_id.begin(), 16, uint8_t(0xAA)); + + ASSERT_LE(0, raft_a.appendLog(unique_id, uavcan::NodeID(1))); + + nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2000)); + + ASSERT_EQ(1, raft_a.getCommitIndex()); + ASSERT_EQ(1, raft_b.getCommitIndex()); }