github.com/klaytn/klaytn@v1.10.2/governance/api_test.go (about)

     1  package governance
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/klaytn/klaytn/blockchain/state"
     8  	"github.com/klaytn/klaytn/blockchain/types"
     9  	"github.com/klaytn/klaytn/common"
    10  	"github.com/klaytn/klaytn/consensus"
    11  	"github.com/klaytn/klaytn/networks/rpc"
    12  	"github.com/klaytn/klaytn/params"
    13  	"github.com/klaytn/klaytn/reward"
    14  	"github.com/klaytn/klaytn/storage/database"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  type testBlockChain struct {
    19  	num    uint64
    20  	config *params.ChainConfig
    21  }
    22  
    23  func newTestBlockchain(config *params.ChainConfig) *testBlockChain {
    24  	return &testBlockChain{
    25  		config: config,
    26  	}
    27  }
    28  
    29  func newTestGovernanceApi() *PublicGovernanceAPI {
    30  	config := params.CypressChainConfig
    31  	config.Governance.KIP71 = params.GetDefaultKIP71Config()
    32  	govApi := NewGovernanceAPI(NewMixedEngine(config, database.NewMemoryDBManager()))
    33  	govApi.governance.SetNodeAddress(common.HexToAddress("0x52d41ca72af615a1ac3301b0a93efa222ecc7541"))
    34  	bc := newTestBlockchain(config)
    35  	govApi.governance.SetBlockchain(bc)
    36  	return govApi
    37  }
    38  
    39  func TestUpperBoundBaseFeeSet(t *testing.T) {
    40  	govApi := newTestGovernanceApi()
    41  
    42  	curLowerBoundBaseFee := govApi.governance.CurrentParams().LowerBoundBaseFee()
    43  	// unexpected case : upperboundbasefee < lowerboundbasefee
    44  	invalidUpperBoundBaseFee := curLowerBoundBaseFee - 100
    45  	_, err := govApi.Vote("kip71.upperboundbasefee", invalidUpperBoundBaseFee)
    46  	assert.Equal(t, err, errInvalidUpperBound)
    47  }
    48  
    49  func TestLowerBoundFeeSet(t *testing.T) {
    50  	govApi := newTestGovernanceApi()
    51  
    52  	curUpperBoundBaseFee := govApi.governance.CurrentParams().UpperBoundBaseFee()
    53  	// unexpected case : upperboundbasefee < lowerboundbasefee
    54  	invalidLowerBoundBaseFee := curUpperBoundBaseFee + 100
    55  	_, err := govApi.Vote("kip71.lowerboundbasefee", invalidLowerBoundBaseFee)
    56  	assert.Equal(t, err, errInvalidLowerBound)
    57  }
    58  
    59  func TestGetRewards(t *testing.T) {
    60  	type expected = map[int]uint64
    61  	type strMap = map[string]interface{}
    62  	type override struct {
    63  		num    int
    64  		strMap strMap
    65  	}
    66  	type testcase struct {
    67  		length   int // total number of blocks to simulate
    68  		override []override
    69  		expected expected
    70  	}
    71  
    72  	var (
    73  		mintAmount = uint64(1)
    74  		koreBlock  = uint64(9)
    75  		epoch      = 3
    76  		latestNum  = rpc.BlockNumber(-1)
    77  		proposer   = common.HexToAddress("0x0000000000000000000000000000000000000000")
    78  		config     = getTestConfig()
    79  	)
    80  
    81  	testcases := []testcase{
    82  		{
    83  			12,
    84  			[]override{
    85  				{
    86  					3,
    87  					strMap{
    88  						"reward.mintingamount": "2",
    89  					},
    90  				},
    91  				{
    92  					6,
    93  					strMap{
    94  						"reward.mintingamount": "3",
    95  					},
    96  				},
    97  			},
    98  			map[int]uint64{
    99  				1:  1,
   100  				2:  1,
   101  				3:  1,
   102  				4:  1,
   103  				5:  1,
   104  				6:  1,
   105  				7:  2, // 2 is minted from now
   106  				8:  2,
   107  				9:  3, // 3 is minted from now
   108  				10: 3,
   109  				11: 3,
   110  				12: 3,
   111  				13: 3,
   112  			},
   113  		},
   114  	}
   115  
   116  	for _, tc := range testcases {
   117  		config.Governance.Reward.MintingAmount = new(big.Int).SetUint64(mintAmount)
   118  		config.Istanbul.Epoch = uint64(epoch)
   119  		config.KoreCompatibleBlock = new(big.Int).SetUint64(koreBlock)
   120  
   121  		bc := newTestBlockchain(config)
   122  
   123  		dbm := database.NewDBManager(&database.DBConfig{DBType: database.MemoryDB})
   124  
   125  		e := NewMixedEngine(config, dbm)
   126  		e.SetBlockchain(bc)
   127  		e.UpdateParams(bc.CurrentBlock().NumberU64())
   128  
   129  		// write initial gov items and overrides to database
   130  		pset, _ := params.NewGovParamSetChainConfig(config)
   131  		gset := NewGovernanceSet()
   132  		gset.Import(pset.StrMap())
   133  		e.headerGov.WriteGovernance(0, NewGovernanceSet(), gset)
   134  		for _, o := range tc.override {
   135  			override := NewGovernanceSet()
   136  			override.Import(o.strMap)
   137  			e.headerGov.WriteGovernance(uint64(o.num), gset, override)
   138  		}
   139  
   140  		govKlayApi := NewGovernanceKlayAPI(e, bc)
   141  
   142  		for num := 1; num <= tc.length; num++ {
   143  			bc.SetBlockNum(uint64(num))
   144  
   145  			rewardSpec, err := govKlayApi.GetRewards(&latestNum)
   146  			assert.Nil(t, err)
   147  
   148  			minted := new(big.Int).SetUint64(tc.expected[num])
   149  			expectedRewardSpec := &reward.RewardSpec{
   150  				Minted:   minted,
   151  				TotalFee: common.Big0,
   152  				BurntFee: common.Big0,
   153  				Proposer: minted,
   154  				Stakers:  common.Big0,
   155  				KFF:      common.Big0,
   156  				KCF:      common.Big0,
   157  				Rewards: map[common.Address]*big.Int{
   158  					proposer: minted,
   159  				},
   160  			}
   161  			assert.Equal(t, expectedRewardSpec, rewardSpec, "wrong at block %d", num)
   162  		}
   163  	}
   164  }
   165  
   166  func (bc *testBlockChain) Engine() consensus.Engine                    { return nil }
   167  func (bc *testBlockChain) GetHeader(common.Hash, uint64) *types.Header { return nil }
   168  func (bc *testBlockChain) GetHeaderByNumber(val uint64) *types.Header {
   169  	return &types.Header{
   170  		Number: new(big.Int).SetUint64(val),
   171  	}
   172  }
   173  func (bc *testBlockChain) GetBlockByNumber(num uint64) *types.Block         { return nil }
   174  func (bc *testBlockChain) StateAt(root common.Hash) (*state.StateDB, error) { return nil, nil }
   175  func (bc *testBlockChain) Config() *params.ChainConfig {
   176  	return bc.config
   177  }
   178  
   179  func (bc *testBlockChain) CurrentBlock() *types.Block {
   180  	return types.NewBlockWithHeader(bc.CurrentHeader())
   181  }
   182  
   183  func (bc *testBlockChain) CurrentHeader() *types.Header {
   184  	return &types.Header{
   185  		Number: new(big.Int).SetUint64(bc.num),
   186  	}
   187  }
   188  
   189  func (bc *testBlockChain) SetBlockNum(num uint64) {
   190  	bc.num = num
   191  }