TASK
Implementation
Research and implement Tendermint-style BFT voting (used in Cosmos blockchain). Tendermint simplifies PBFT with clear round structure and a rotating proposer.
Round structure:
- Propose: Round proposer broadcasts a block
- Prevote: Each validator broadcasts prevote (for the block or nil)
- Precommit: If 2/3+ prevotes for a block, broadcast precommit. If 2/3+ precommits, commit.
Request: {"type": "tendermint_propose", "msg_id": 1, "round": 0, "proposer": "v1", "block": {"height": 1, "txs": ["tx1", "tx2"]}}
Response: {"type": "tendermint_propose_ok", "in_reply_to": 1, "round": 0, "block_hash": "abc123"}
Request: {"type": "tendermint_prevote", "msg_id": 2, "round": 0, "validator": "v2", "block_hash": "abc123"}
Response: {"type": "tendermint_prevote_ok", "in_reply_to": 2, "prevotes_for_block": 2, "total_prevotes": 3, "threshold": 3}
Request: {"type": "tendermint_status", "msg_id": 3, "round": 0}
Response: {"type": "tendermint_status_ok", "in_reply_to": 3, "phase": "precommit", "block_committed": false, "prevote_count": 3, "precommit_count": 2}Sample Test Cases
Proposer broadcasts blockTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3","n4"]}}
{"src":"c1","dest":"n1","body":{"type":"tendermint_propose","msg_id":2,"round":0,"proposer":"n1","block":{"height":1,"txs":["tx1"]}}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Prevotes accumulate toward thresholdTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3","n4"]}}
{"src":"c1","dest":"n1","body":{"type":"tendermint_propose","msg_id":2,"round":0,"proposer":"n1","block":{"height":1,"txs":["tx1"]}}}
{"src":"c1","dest":"n1","body":{"type":"tendermint_prevote","msg_id":3,"round":0,"validator":"n2","block_hash":"abc"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
Tendermint simplifies PBFT by using rounds with a rotating proposer
Hint 2▾
Each round has: Propose, Prevote, Precommit phases
Hint 3▾
A block is committed when 2/3+ of validators precommit for it
Hint 4▾
Validators lock on a value after precommitting (prevents equivocation across rounds)
Hint 5▾
The lock is released only if a higher round proposes a different value with enough prevotes
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
TendermintCosmosblockchainvoting roundslock mechanism
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()