github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/validator.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 8 . "github.com/intfoundation/go-common" 9 "github.com/intfoundation/go-crypto" 10 "github.com/intfoundation/go-wire" 11 "github.com/intfoundation/intchain/common" 12 ethTypes "github.com/intfoundation/intchain/core/types" 13 "math/big" 14 ) 15 16 // Volatile state for each Validator 17 // TODO: make non-volatile identity 18 type Validator struct { 19 Address []byte `json:"address"` 20 PubKey crypto.PubKey `json:"pub_key"` 21 VotingPower *big.Int `json:"voting_power"` 22 RemainingEpoch uint64 `json:"remain_epoch"` 23 24 //LastBlockTime *big.Int `json:"last_block_time"` 25 //IsForbidden bool `json:"is_forbidden"` 26 } 27 28 func NewValidator(address []byte, pubKey crypto.PubKey, votingPower *big.Int) *Validator { 29 return &Validator{ 30 Address: address, 31 PubKey: pubKey, 32 VotingPower: votingPower, 33 } 34 } 35 36 // Creates a new copy of the validator so we can mutate accum. 37 // Panics if the validator is nil. 38 func (v *Validator) Copy() *Validator { 39 vCopy := *v 40 vCopy.VotingPower = new(big.Int).Set(v.VotingPower) 41 return &vCopy 42 } 43 44 func (v *Validator) Equals(other *Validator) bool { 45 46 return bytes.Equal(v.Address, other.Address) && 47 v.PubKey.Equals(other.PubKey) && 48 v.VotingPower.Cmp(other.VotingPower) == 0 49 } 50 51 func (v *Validator) String() string { 52 if v == nil { 53 return "nil-Validator" 54 } 55 return fmt.Sprintf("Validator{ADD:%X PK:%X VP:%v EP:%d}", 56 v.Address, 57 v.PubKey, 58 v.VotingPower, 59 v.RemainingEpoch) 60 } 61 62 func (v *Validator) Hash() []byte { 63 return wire.BinaryRipemd160(v) 64 } 65 66 //------------------------------------- 67 68 var ValidatorCodec = validatorCodec{} 69 70 type validatorCodec struct{} 71 72 func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int, err *error) { 73 wire.WriteBinary(o.(*Validator), w, n, err) 74 } 75 76 func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} { 77 return wire.ReadBinary(&Validator{}, r, 0, n, err) 78 } 79 80 func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int { 81 PanicSanity("ValidatorCodec.Compare not implemented") 82 return 0 83 } 84 85 //------------------------------------- 86 87 type RefundValidatorAmount struct { 88 Address common.Address 89 Amount *big.Int // Amount will be nil when Voteout is true 90 Voteout bool // Voteout means refund all the amount (self deposit + delegate) 91 } 92 93 // SwitchEpoch op 94 type SwitchEpochOp struct { 95 ChainId string 96 NewValidators *ValidatorSet 97 //NewCandidates *CandidateSet 98 } 99 100 func (op *SwitchEpochOp) Conflict(op1 ethTypes.PendingOp) bool { 101 if _, ok := op1.(*SwitchEpochOp); ok { 102 // Only one SwitchEpochOp is allowed in each block 103 return true 104 } 105 return false 106 } 107 108 func (op *SwitchEpochOp) String() string { 109 return fmt.Sprintf("SwitchEpochOp - ChainId:%v, New Validators: %v", op.ChainId, op.NewValidators) 110 }