Skip to content

Commit 48a988e

Browse files
committed
feat(models): acceptor role
1 parent e2a74c8 commit 48a988e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
File renamed without changes.

konsensus/models/roles/acceptor.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from . import Role
2+
from ..node import Node
3+
from ...constants import NULL_BALLOT
4+
from konsensus.entities.messages_types import Accepting, Promise, Accepted
5+
6+
7+
class Acceptor(Role):
8+
"""
9+
This implements the Acceptor Role in the protocol. Therefore, it must store the ballot number representing
10+
its most recent promise, along with the set of accepted proposal for each slot. It then responds to Prepare
11+
and Accept messages according to the protocol
12+
13+
This looks like a Simple Paxos with the addition of slot numbers to the messages
14+
"""
15+
16+
def __init__(self, node: Node):
17+
super().__init__(node)
18+
self.ballot_num = NULL_BALLOT
19+
# {slot: (ballot_num, proposal)}
20+
self.accepted_proposals = {}
21+
22+
def do_prepare(self, sender, ballot_num: NULL_BALLOT):
23+
if ballot_num > self.ballot_num:
24+
self.ballot_num = ballot_num
25+
# We have heard from a scout, so it might be the next leader
26+
self.node.send([self.node.address], Accepting(leader=sender))
27+
28+
self.node.send([sender], Promise(ballot_num=self.ballot_num, accepted_proposals=self.accepted_proposals))
29+
30+
def do_accept(self, sender, ballot_num, slot, proposal):
31+
if ballot_num >= self.ballot_num:
32+
self.ballot_num = ballot_num
33+
acc = self.accepted_proposals
34+
if slot not in acc or acc[slot][0] < ballot_num:
35+
acc[slot] = (ballot_num, proposal)
36+
37+
self.node.send([sender], Accepted(slot=slot, ballot_num=self.ballot_num))

0 commit comments

Comments
 (0)