-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCasper-FFG.sol
107 lines (78 loc) · 3.37 KB
/
Casper-FFG.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
pragma solidity ^0.5.0;
contract CasperFFG {
// The minimum deposit required to become a validator
uint constant MIN_DEPOSIT = 32 ether;
// The duration of the deposit lock-in period in blocks
uint constant WITHDRAWAL_DELAY = 86400;
// The maximum number of validators
uint constant MAX_VALIDATORS = 100;
// The current validator set
address[MAX_VALIDATORS] validators;
// The current total deposit size
uint totalDepositSize;
// The current epoch number
uint currentEpoch;
// The current checkpoint block number
uint currentCheckpoint;
// The number of blocks per epoch
uint blocksPerEpoch;
// The number of blocks per checkpoint
uint blocksPerCheckpoint;
// The total number of blocks finalized
uint totalFinalizedBlocks;
// The threshold required for finalization
uint finalizationThreshold;
// The last finalized block number
uint lastFinalizedBlock;
// The validator struct
struct Validator {
uint deposit;
uint withdrawalEpoch;
bool isActive;
}
// The mapping of validator addresses to their respective validator struct
mapping (address => Validator) validatorMap;
// The deposit function for becoming a validator
function deposit() public payable {
require(msg.value >= MIN_DEPOSIT, "Deposit amount too low");
require(totalDepositSize < MAX_VALIDATORS * MIN_DEPOSIT, "Maximum number of validators reached");
require(validatorMap[msg.sender].deposit == 0, "Already a validator");
// Create a new validator struct
Validator memory validator;
validator.deposit = msg.value;
validator.withdrawalEpoch = currentEpoch + WITHDRAWAL_DELAY / blocksPerEpoch;
validator.isActive = true;
// Add the validator to the validator set
validators[totalDepositSize / MIN_DEPOSIT] = msg.sender;
// Add the deposit to the total deposit size
totalDepositSize += msg.value;
// Add the validator to the validator map
validatorMap[msg.sender] = validator;
}
// The withdraw function for withdrawing as a validator
function withdraw() public {
Validator storage validator = validatorMap[msg.sender];
require(validator.deposit > 0, "Not a validator");
require(currentEpoch >= validator.withdrawalEpoch, "Withdrawal lock-in period not over");
// Remove the validator from the validator set
for (uint i = 0; i < validators.length; i++) {
if (validators[i] == msg.sender) {
validators[i] = validators[validators.length - 1];
validators.length--;
break;
}
}
// Subtract the deposit from the total deposit size
totalDepositSize -= validator.deposit;
// Remove the validator from the validator map
delete validatorMap[msg.sender];
// Transfer the deposit back to the validator
msg.sender.transfer(validator.deposit);
}
// The function for submitting a vote
function vote(uint blockNumber) public {
Validator storage validator = validatorMap[msg.sender];
require(validator.isActive, "Inactive validator");
require(blockNumber % blocksPerCheckpoint == 0, "Block number not a checkpoint");
require(blockNumber > lastFinalizedBlock, "Block already finalized");
//