github.com/klaytn/klaytn@v1.10.2/governance/interface.go (about) 1 // Copyright 2022 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package governance 18 19 import ( 20 "github.com/klaytn/klaytn/blockchain" 21 "github.com/klaytn/klaytn/blockchain/state" 22 "github.com/klaytn/klaytn/blockchain/types" 23 "github.com/klaytn/klaytn/common" 24 "github.com/klaytn/klaytn/consensus/istanbul" 25 "github.com/klaytn/klaytn/params" 26 "github.com/klaytn/klaytn/storage/database" 27 ) 28 29 type Engine interface { 30 HeaderEngine 31 ReaderEngine 32 HeaderGov() HeaderEngine 33 ContractGov() ReaderEngine 34 } 35 36 type ReaderEngine interface { 37 // CurrentParams returns the params at the current block. The returned params shall be 38 // used to build the upcoming (head+1) block. Block processing codes 39 // should use this method. 40 CurrentParams() *params.GovParamSet 41 42 // EffectiveParams returns the params at given block number. The returned params 43 // were used to build the block at given number. 44 // The number must be equal or less than current block height (head). 45 EffectiveParams(num uint64) (*params.GovParamSet, error) 46 47 // UpdateParams updates the current params (the ones returned by CurrentParams()). 48 // by reading the latest blockchain states. 49 // This function must be called after every block is mined to 50 // guarantee that CurrentParams() works correctly. 51 UpdateParams(num uint64) error 52 } 53 54 type HeaderEngine interface { 55 // AddVote casts votes from API 56 AddVote(key string, val interface{}) bool 57 ValidateVote(vote *GovernanceVote) (*GovernanceVote, bool) 58 59 // Access database for voting states 60 CanWriteGovernanceState(num uint64) bool 61 WriteGovernanceState(num uint64, isCheckpoint bool) error 62 63 // Access database for network params 64 ReadGovernance(num uint64) (uint64, map[string]interface{}, error) 65 WriteGovernance(num uint64, data GovernanceSet, delta GovernanceSet) error 66 67 // Compose header.Vote and header.Governance 68 GetEncodedVote(addr common.Address, number uint64) []byte 69 GetGovernanceChange() map[string]interface{} 70 71 // Intake header.Vote and header.Governance 72 VerifyGovernance(received []byte) error 73 ClearVotes(num uint64) 74 WriteGovernanceForNextEpoch(number uint64, governance []byte) 75 UpdateCurrentSet(num uint64) 76 HandleGovernanceVote( 77 valset istanbul.ValidatorSet, votes []GovernanceVote, tally []GovernanceTallyItem, 78 header *types.Header, proposer common.Address, self common.Address, writable bool) ( 79 istanbul.ValidatorSet, []GovernanceVote, []GovernanceTallyItem) 80 81 // Get internal fields 82 GetVoteMapCopy() map[string]VoteStatus 83 GetGovernanceTalliesCopy() []GovernanceTallyItem 84 CurrentSetCopy() map[string]interface{} 85 PendingChanges() map[string]interface{} 86 Votes() []GovernanceVote 87 IdxCache() []uint64 88 IdxCacheFromDb() []uint64 89 90 NodeAddress() common.Address 91 TotalVotingPower() uint64 92 MyVotingPower() uint64 93 BlockChain() blockChain 94 DB() database.DBManager 95 96 // Set internal fields 97 SetNodeAddress(addr common.Address) 98 SetTotalVotingPower(t uint64) 99 SetMyVotingPower(t uint64) 100 SetBlockchain(chain blockChain) 101 SetTxPool(txpool txPool) 102 GetTxPool() txPool 103 } 104 105 // blockChain is an interface for blockchain.Blockchain used in governance package. 106 type blockChain interface { 107 blockchain.ChainContext 108 109 CurrentBlock() *types.Block 110 GetHeaderByNumber(val uint64) *types.Header 111 StateAt(root common.Hash) (*state.StateDB, error) 112 Config() *params.ChainConfig 113 }