github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/consensus/tribe/types.go (about) 1 package tribe 2 3 import ( 4 "crypto/ecdsa" 5 "errors" 6 "fmt" 7 "math/big" 8 "sync" 9 "time" 10 11 "github.com/SmartMeshFoundation/Spectrum/accounts" 12 "github.com/SmartMeshFoundation/Spectrum/common" 13 "github.com/SmartMeshFoundation/Spectrum/common/hexutil" 14 "github.com/SmartMeshFoundation/Spectrum/consensus" 15 "github.com/SmartMeshFoundation/Spectrum/core/types" 16 "github.com/SmartMeshFoundation/Spectrum/params" 17 lru "github.com/hashicorp/golang-lru" 18 ) 19 20 const ( 21 // None -> Volunteer -> Signer -> Sinner 22 LevelNone = "None" 23 LevelVolunteer = "Volunteer" 24 LevelSigner = "Signer" 25 LevelSinner = "Sinner" //黑名单 26 27 historyLimit = 2048 28 wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers 29 CHIEF_NUMBER = int64(3) //最早版本chief合约开始生效的块数 30 ) 31 32 var ( 33 blockPeriod = uint64(15) // Default minimum difference between two consecutive block's timestamps 34 _extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity 35 _extraVrf = 161 // before SIP100 extra format is bytes[extraVanity+extraSeal], after is bytes[extraVrf+extraSeal] 36 extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal 37 nonceSync = hexutil.MustDecode("0xffffffffffffffff") // TODO Reserved to control behavior 38 nonceAsync = hexutil.MustDecode("0x0000000000000000") // TODO Reserved to control behavior 39 uncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. 40 // less than SIP100 >>>>>>>>>>>>> 41 // new role is in turn 6 ~ 1 42 diffInTurnMain = big.NewInt(3) // Block difficulty for in-turn Main 43 diffInTurn = big.NewInt(2) // Block difficulty for in-turn Sub 44 diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn Other 45 // less than SIP100 <<<<<<<<<<<<< 46 diff = int64(6) // SIP100 max diff is 6 47 48 extraVanityFn = func(num *big.Int) int { 49 if params.IsSIP100Block(num) { 50 return _extraVrf 51 } 52 return _extraVanity 53 } 54 ) 55 56 // Various error messages to mark blocks invalid. These should be private to 57 // prevent engine specific errors from being referenced in the remainder of the 58 // codebase, inherently breaking if the engine is swapped out. Please put common 59 // error types into the consensus package. 60 var ( 61 // errUnknownBlock is returned when the list of signers is requested for a block 62 // that is not part of the local blockchain. 63 errUnknownBlock = errors.New("unknown block") 64 65 // errInvalidCheckpointBeneficiary is returned if a checkpoint/epoch transition 66 // block has a beneficiary set to non-zeroes. 67 errInvalidCheckpointBeneficiary = errors.New("beneficiary in checkpoint block non-zero") 68 69 // only accept sync or async flag 70 // allowed constants of 0x00..0 or 0xff..f. 71 errInvalidNonce = errors.New("nonce not 0x00..0 or 0xff..f") 72 73 // errMissingVanity is returned if a block's extra-data section is shorter than 74 // 32 bytes, which is required to store the signer vanity. 75 errMissingVanity = errors.New("extra-data 32 byte vanity prefix missing") 76 77 // errMissingSignature is returned if a block's extra-data section doesn't seem 78 // to contain a 65 byte secp256k1 signature. 79 errMissingSignature = errors.New("extra-data 65 byte suffix signature missing") 80 81 // errExtraSigners is returned if non-checkpoint block contain signer data in 82 // their extra-data fields. 83 errExtraSigners = errors.New("non-checkpoint block contains extra signer list") 84 85 // errInvalidMixDigest is returned if a block's mix digest is non-zero. 86 errInvalidMixDigest = errors.New("non-zero mix digest") 87 88 // errInvalidUncleHash is returned if a block contains an non-empty uncle list. 89 errInvalidUncleHash = errors.New("non empty uncle hash") 90 91 // errInvalidDifficulty is returned if the difficulty of a block is not either 92 // of 1 - 3 , or if the value does not match the turn of the signer. 93 errInvalidDifficulty = errors.New("invalid__difficulty") 94 95 // ErrInvalidTimestamp is returned if the timestamp of a block is lower than 96 // the previous block's timestamp + the minimum block period. 97 ErrInvalidTimestamp = errors.New("invalid timestamp") 98 ErrInvalidTimestampSIP002 = errors.New("invalid timestamp (SIP002)") 99 100 // errUnauthorized is returned if a header is signed by a non-authorized entity. 101 errUnauthorized = errors.New("unauthorized") 102 103 // errWaitTransactions is returned if an empty block is attempted to be sealed 104 // on an instant chain (0 second period). It's important to refuse these as the 105 // block reward is zero, so an empty block just bloats the chain... fast. 106 errWaitTransactions = errors.New("waiting for transactions") 107 // for tribe consensus block validator 108 ErrTribeNotAllowEmptyTxList = errors.New("tribe not allow empty tx list") 109 ErrTribeMustContainChiefTx = errors.New("tribe must contain chief tx") 110 ErrTribeChiefVolunteerLowBalance = errors.New("tribe chief volunteer low balance") 111 ErrTribeChiefVolunteerFail = errors.New("tribe chief volunteer check fail") 112 ErrTribeChiefTxMustAtPositionZero = errors.New("tribe chief tx must at postion 0") 113 ErrTribeChiefTxSignerAndBlockSignerNotMatch = errors.New("tribe chief update tx signer and block signer not match") 114 ErrTribeValdateTxSenderCannotInSignerList = errors.New("tx sender cannot in signerlist") 115 116 Chief100BlockReward, _ = new(big.Int).SetString("35000000000000000000", 10) //Block reward in wei for successfully mining a block 117 BlockRewardReducedInterval = 4505000 //half reward about two years 118 SmartMeshFoundationAccount = common.HexToAddress("0xe0014a4268ad3d7b131551be47bc6ed72a6937e4") 119 SmartMeshFoundationAccountDestroyBalance, _ = new(big.Int).SetString("376991118360000000000000000", 10) 120 ) 121 122 type Tribe struct { 123 accman *accounts.Manager 124 config *params.TribeConfig // Consensus engine configuration parameters 125 sigcache *lru.ARCCache // mapping block.hash -> signer 126 Status *TribeStatus 127 //SealErrorCounter uint32 // less then 3 , retry commit new work 128 isInit bool 129 lock sync.Mutex 130 } 131 132 type API struct { 133 accman *accounts.Manager 134 chain consensus.ChainReader 135 tribe *Tribe 136 } 137 138 // SignerFn is a signer callback function to request a hash to be signed by a 139 // backing account. 140 type SignerFn func(accounts.Account, []byte) ([]byte, error) 141 142 type Signer struct { 143 Address common.Address `json:"address"` // 签名人 144 Score int64 `json:"score"` // 分数 145 } 146 147 // append chief vsn 0.0.6 148 type Volunteer struct { 149 Address common.Address `json:"address"` // 候选人 150 Weight int64 `json:"weight"` // 权重 151 } 152 153 func (self *Signer) String() string { 154 return fmt.Sprintf("%s:%d", self.Address.Hex(), self.Score) 155 } 156 157 type History struct { 158 Number int64 `json:"number"` 159 Hash common.Hash `json:"hash"` 160 Signer common.Address `json:"signer"` 161 Difficulty *big.Int `json:"difficulty"` 162 Timestamp *big.Int `json:"timestamp"` 163 } 164 165 type TribeStatus struct { 166 Signers []*Signer `json:"signers"` 167 Leaders []common.Address `json:"leaders"` 168 SignerLevel string `json:"signerLevel"` // None -> Volunteer -> Signer 169 Number int64 `json:"number"` // last block.number 170 // for watch the set method result 171 Epoch *big.Int `json:"epoch"` 172 LeaderLimit *big.Int `json:"leaderLimit"` 173 SignerLimit *big.Int `json:"signerLimit"` 174 Vsn string `json:"version"` // chief version 175 176 blackList []common.Address 177 nodeKey *ecdsa.PrivateKey 178 tribe *Tribe 179 } 180 181 type TribeVolunteers struct { 182 Length *big.Int `json:"length"` 183 Volunteers []*Volunteer `json:"volunteers"` 184 } 185 186 type TribeMiner struct { 187 Address common.Address `json:"address"` 188 Balance *big.Int `json:"balance"` 189 Level string `json:"level"` // None 、 Volunteer 、 Signer 190 }