github.com/klaytn/klaytn@v1.12.1/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  //go:generate mockgen -package governance -destination=interface_mock_test.go github.com/klaytn/klaytn/governance Engine
    30  type Engine interface {
    31  	HeaderEngine
    32  	ReaderEngine
    33  	HeaderGov() HeaderEngine
    34  	ContractGov() ReaderEngine
    35  }
    36  
    37  type ReaderEngine interface {
    38  	// CurrentParams returns the params at the current block. The returned params shall be
    39  	// used to build the upcoming (head+1) block. Block processing codes
    40  	// should use this method.
    41  	CurrentParams() *params.GovParamSet
    42  
    43  	// EffectiveParams returns the params at given block number. The returned params
    44  	// were used to build the block at given number.
    45  	// The number must be equal or less than current block height (head).
    46  	EffectiveParams(num uint64) (*params.GovParamSet, error)
    47  
    48  	// UpdateParams updates the current params (the ones returned by CurrentParams()).
    49  	// by reading the latest blockchain states.
    50  	// This function must be called after every block is mined to
    51  	// guarantee that CurrentParams() works correctly.
    52  	UpdateParams(num uint64) error
    53  }
    54  
    55  type HeaderEngine interface {
    56  	// Governance cache initialization
    57  	InitGovCache()
    58  
    59  	// Reset the latest block number that contains governance data
    60  	InitLastGovStateBlkNum()
    61  
    62  	// AddVote casts votes from API
    63  	AddVote(key string, val interface{}) bool
    64  	ValidateVote(vote *GovernanceVote) (*GovernanceVote, bool)
    65  
    66  	// Access database for voting states
    67  	CanWriteGovernanceState(num uint64) bool
    68  	WriteGovernanceState(num uint64, isCheckpoint bool) error
    69  
    70  	// Access database for network params
    71  	ReadGovernance(num uint64) (uint64, map[string]interface{}, error)
    72  	WriteGovernance(num uint64, data GovernanceSet, delta GovernanceSet) error
    73  
    74  	// Compose header.Vote and header.Governance
    75  	GetEncodedVote(addr common.Address, number uint64) []byte
    76  	GetGovernanceChange() map[string]interface{}
    77  
    78  	// Intake header.Vote and header.Governance
    79  	VerifyGovernance(received []byte) error
    80  	ClearVotes(num uint64)
    81  	WriteGovernanceForNextEpoch(number uint64, governance []byte)
    82  	UpdateCurrentSet(num uint64)
    83  	HandleGovernanceVote(
    84  		valset istanbul.ValidatorSet, votes []GovernanceVote, tally []GovernanceTallyItem,
    85  		header *types.Header, proposer common.Address, self common.Address, writable bool) (
    86  		istanbul.ValidatorSet, []GovernanceVote, []GovernanceTallyItem)
    87  
    88  	// Get internal fields
    89  	GetVoteMapCopy() map[string]VoteStatus
    90  	GetGovernanceTalliesCopy() []GovernanceTallyItem
    91  	CurrentSetCopy() map[string]interface{}
    92  	PendingChanges() map[string]interface{}
    93  	Votes() []GovernanceVote
    94  	IdxCache() []uint64
    95  	IdxCacheFromDb() []uint64
    96  
    97  	NodeAddress() common.Address
    98  	TotalVotingPower() uint64
    99  	MyVotingPower() uint64
   100  	BlockChain() blockChain
   101  	DB() database.DBManager
   102  
   103  	// Set internal fields
   104  	SetNodeAddress(addr common.Address)
   105  	SetTotalVotingPower(t uint64)
   106  	SetMyVotingPower(t uint64)
   107  	SetBlockchain(chain blockChain)
   108  	SetTxPool(txpool txPool)
   109  	GetTxPool() txPool
   110  }
   111  
   112  // blockChain is an interface for blockchain.Blockchain used in governance package.
   113  type blockChain interface {
   114  	blockchain.ChainContext
   115  
   116  	// Subset of consensus.ChainReader
   117  	Config() *params.ChainConfig
   118  	CurrentHeader() *types.Header
   119  	GetHeaderByNumber(val uint64) *types.Header
   120  	GetBlock(hash common.Hash, number uint64) *types.Block
   121  	State() (*state.StateDB, error)
   122  	StateAt(root common.Hash) (*state.StateDB, error)
   123  
   124  	CurrentBlock() *types.Block
   125  }