github.com/klaytn/klaytn@v1.10.2/governance/contract.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  	"errors"
    21  	"math/big"
    22  
    23  	"github.com/klaytn/klaytn/common"
    24  	"github.com/klaytn/klaytn/params"
    25  )
    26  
    27  var (
    28  	errContractEngineNotReady = errors.New("ContractEngine is not ready")
    29  	errParamsAtFail           = errors.New("headerGov EffectiveParams() failed")
    30  	errGovParamNotExist       = errors.New("GovParam does not exist")
    31  	errInvalidGovParam        = errors.New("GovParam conversion failed")
    32  )
    33  
    34  type ContractEngine struct {
    35  	currentParams *params.GovParamSet
    36  
    37  	// for headerGov.EffectiveParams() and BlockChain()
    38  	headerGov *Governance
    39  }
    40  
    41  func NewContractEngine(headerGov *Governance) *ContractEngine {
    42  	e := &ContractEngine{
    43  		currentParams: params.NewGovParamSet(),
    44  		headerGov:     headerGov,
    45  	}
    46  
    47  	return e
    48  }
    49  
    50  // CurrentParams effective at upcoming block (head+1)
    51  func (e *ContractEngine) CurrentParams() *params.GovParamSet {
    52  	return e.currentParams
    53  }
    54  
    55  // Parameters effective at requested block (num)
    56  func (e *ContractEngine) EffectiveParams(num uint64) (*params.GovParamSet, error) {
    57  	pset, err := e.contractGetAllParamsAt(num)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return pset, nil
    62  }
    63  
    64  // if UpdateParam fails, leave currentParams as-is
    65  func (e *ContractEngine) UpdateParams(num uint64) error {
    66  	// request the parameters required for generating the next block
    67  	pset, err := e.contractGetAllParamsAt(num + 1)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	e.currentParams = pset
    73  	return nil
    74  }
    75  
    76  // contractGetAllParamsAt sets evmCtx.BlockNumber as num
    77  func (e *ContractEngine) contractGetAllParamsAt(num uint64) (*params.GovParamSet, error) {
    78  	chain := e.headerGov.BlockChain()
    79  	if chain == nil {
    80  		logger.Crit("headerGov.BlockChain() is nil")
    81  		return nil, errContractEngineNotReady
    82  	}
    83  
    84  	config := chain.Config()
    85  	if !config.IsKoreForkEnabled(new(big.Int).SetUint64(num)) {
    86  		logger.Trace("ContractEngine disabled: hardfork block not passed")
    87  		return params.NewGovParamSet(), nil
    88  	}
    89  
    90  	addr, err := e.contractAddrAt(num)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	if common.EmptyAddress(addr) {
    95  		logger.Trace("ContractEngine disabled: GovParamContract address not set")
    96  		return params.NewGovParamSet(), nil
    97  	}
    98  
    99  	caller := &contractCaller{
   100  		chain:        chain,
   101  		contractAddr: addr,
   102  	}
   103  	return caller.getAllParamsAt(new(big.Int).SetUint64(num))
   104  }
   105  
   106  // contractAddrAt returns the GovParamContract address effective at given block number
   107  func (e *ContractEngine) contractAddrAt(num uint64) (common.Address, error) {
   108  	headerParams, err := e.headerGov.EffectiveParams(num)
   109  	if err != nil {
   110  		logger.Error("headerGov.EffectiveParams failed", "err", err, "num", num)
   111  		return common.Address{}, errParamsAtFail
   112  	}
   113  
   114  	// this happens when GovParamContract has not been voted
   115  	param, ok := headerParams.Get(params.GovParamContract)
   116  	if !ok {
   117  		logger.Debug("Could not find GovParam contract address")
   118  		return common.Address{}, nil
   119  	}
   120  
   121  	addr, ok := param.(common.Address)
   122  	if !ok {
   123  		logger.Error("Could not convert GovParam contract address into common.Address", "param", param)
   124  		return common.Address{}, errInvalidGovParam
   125  	}
   126  
   127  	return addr, nil
   128  }