mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-04 08:44:28 +00:00
sharding: notary casts votes
Former-commit-id: 8c0007eaf2de0d36c69ea346f85234dee6af89ee [formerly c9ed2d6063398edf0734a6d48a6cc88b6afd637b] Former-commit-id: f508bacaab3632fa4d3d05c26eea3c6b155c2ab1
This commit is contained in:
parent
668bbc7478
commit
3b7c5bf64c
@ -6,6 +6,7 @@ contract SMC {
|
|||||||
event NotaryRegistered(address notary, uint poolIndex);
|
event NotaryRegistered(address notary, uint poolIndex);
|
||||||
event NotaryDeregistered(address notary, uint poolIndex, uint deregisteredPeriod);
|
event NotaryDeregistered(address notary, uint poolIndex, uint deregisteredPeriod);
|
||||||
event NotaryReleased(address notary, uint poolIndex);
|
event NotaryReleased(address notary, uint poolIndex);
|
||||||
|
event Voted(uint indexed shardId, bytes32 chunkRoot, uint period, address notaryAddress);
|
||||||
|
|
||||||
struct Notary {
|
struct Notary {
|
||||||
uint deregisteredPeriod;
|
uint deregisteredPeriod;
|
||||||
@ -49,6 +50,9 @@ contract SMC {
|
|||||||
uint sampleSizeLastUpdatedPeriod;
|
uint sampleSizeLastUpdatedPeriod;
|
||||||
|
|
||||||
// Constant values
|
// Constant values
|
||||||
|
uint constant ONE = 1;
|
||||||
|
uint constant ONES = 255;
|
||||||
|
uint constant LAST_BYTE = 31;
|
||||||
uint constant PERIOD_LENGTH = 5;
|
uint constant PERIOD_LENGTH = 5;
|
||||||
// Number of shards
|
// Number of shards
|
||||||
uint constant SHARD_COUNT = 100;
|
uint constant SHARD_COUNT = 100;
|
||||||
@ -172,7 +176,8 @@ contract SMC {
|
|||||||
|
|
||||||
collationRecords[_shardId][_period] = CollationRecord({
|
collationRecords[_shardId][_period] = CollationRecord({
|
||||||
chunkRoot: _chunkRoot,
|
chunkRoot: _chunkRoot,
|
||||||
proposer: msg.sender
|
proposer: msg.sender,
|
||||||
|
isElected: false
|
||||||
});
|
});
|
||||||
|
|
||||||
lastSubmittedCollation[_shardId] = block.number / PERIOD_LENGTH;
|
lastSubmittedCollation[_shardId] = block.number / PERIOD_LENGTH;
|
||||||
@ -181,7 +186,7 @@ contract SMC {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sampled notary can call the following funtion to submit vote,
|
/// Sampled notary can call the following funtion to submit vote,
|
||||||
/// a vote log will be emitted which client will monitor
|
/// a vote log will be emitted for client to monitor
|
||||||
function submitVote(
|
function submitVote(
|
||||||
uint _shardId,
|
uint _shardId,
|
||||||
uint _period,
|
uint _period,
|
||||||
@ -193,6 +198,12 @@ contract SMC {
|
|||||||
require(!hasVoted(_shardId, _index));
|
require(!hasVoted(_shardId, _index));
|
||||||
require(getNotaryInCommittee(_shardId, _index) == msg.sender);
|
require(getNotaryInCommittee(_shardId, _index) == msg.sender);
|
||||||
|
|
||||||
|
uint8 voteCount = castVote(_shardId, _index);
|
||||||
|
if (voteCount >= QUORUM_SIZE) {
|
||||||
|
lastApprovedCollation[_shardId] = _period;
|
||||||
|
collationRecords[_shardId][_period].isElected = true;
|
||||||
|
}
|
||||||
|
emit Voted(_shardId, _chunkRoot, _period, msg.sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// To keep track of notary size in between periods, we call updateNotarySampleSize
|
/// To keep track of notary size in between periods, we call updateNotarySampleSize
|
||||||
@ -228,13 +239,20 @@ contract SMC {
|
|||||||
return emptySlotsStack[emptySlotsStackTop];
|
return emptySlotsStack[emptySlotsStackTop];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a bit is set from a given byte, this function is used to check
|
/// Check if a bit is set, this function is used to check
|
||||||
/// if a notary has casted the vote, return true if voted, false if not
|
/// if a notary has casted the vote, return true if voted, false if not
|
||||||
function hasVoted(uint _shardId, uint _index) internal returns (bool) {
|
function hasVoted(uint _shardId, uint _index) internal returns (bool) {
|
||||||
uint byteIndex = _index / 8;
|
return currentVote[_shardId] & bytes32(ONE << uint(ONES - _index)) == bytes32(ONE << uint(ONES - _index));
|
||||||
uint bitIndex = _index % 8;
|
}
|
||||||
byte _byte = currentVote[_shardId][byteIndex];
|
|
||||||
byte bitPos = byte(2 ** (7 - bitIndex));
|
/// Set a bit to one, notary uses this function to cast it's vote,
|
||||||
return (_byte & bitPos) == bitPos;
|
/// After the notary casts it's vote, we increase the vote count by 1 (last byte)
|
||||||
}
|
/// Returns the total numbers of votes
|
||||||
}
|
function castVote(uint _shardId, uint _index) internal returns (uint8) {
|
||||||
|
currentVote[_shardId] = currentVote[_shardId] | bytes32(ONE << (ONES - _index));
|
||||||
|
uint8 voteCount = uint8(currentVote[_shardId][LAST_BYTE]) + 1;
|
||||||
|
bytes32 mask = bytes32(0xff);
|
||||||
|
currentVote[_shardId] = (currentVote[_shardId] & ~mask) | bytes32(voteCount);
|
||||||
|
return voteCount;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user