github.com/klaytn/klaytn@v1.12.1/tests/staking_info_test.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 tests 18 19 import ( 20 "math/big" 21 "os" 22 "testing" 23 24 "github.com/klaytn/klaytn/blockchain" 25 "github.com/klaytn/klaytn/blockchain/types" 26 "github.com/klaytn/klaytn/common" 27 rewardcontract "github.com/klaytn/klaytn/contracts/reward/contract" 28 "github.com/klaytn/klaytn/crypto" 29 "github.com/klaytn/klaytn/governance" 30 "github.com/klaytn/klaytn/log" 31 "github.com/klaytn/klaytn/params" 32 "github.com/klaytn/klaytn/reward" 33 "github.com/stretchr/testify/assert" 34 "github.com/stretchr/testify/require" 35 ) 36 37 func TestAddressBookConnector(t *testing.T) { 38 log.EnableLogForTest(log.LvlCrit, log.LvlWarn) 39 40 fullNode, node, validator, chainId, workspace := newBlockchain(t, nil, nil) 41 defer os.RemoveAll(workspace) 42 defer fullNode.Stop() 43 44 var ( 45 chain = node.BlockChain().(*blockchain.BlockChain) 46 config = chain.Config() 47 txpool = node.TxPool() 48 db = node.ChainDB() 49 gov = governance.NewMixedEngine(config, db) 50 51 deployAddr common.Address 52 deployBlock uint64 53 ) 54 55 // Deploy AddressBook 56 { 57 sender := validator 58 signer := types.LatestSignerForChainID(chainId) 59 60 values := map[types.TxValueKeyType]interface{}{ 61 types.TxValueKeyNonce: sender.GetNonce(), 62 types.TxValueKeyAmount: new(big.Int).SetUint64(0), 63 types.TxValueKeyGasLimit: uint64(1e9), 64 types.TxValueKeyGasPrice: big.NewInt(25 * params.Ston), 65 types.TxValueKeyHumanReadable: false, 66 types.TxValueKeyFrom: sender.GetAddr(), 67 types.TxValueKeyData: common.FromHex(rewardcontract.AddressBookBin), 68 types.TxValueKeyCodeFormat: params.CodeFormatEVM, 69 types.TxValueKeyTo: (*common.Address)(nil), 70 } 71 72 tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values) 73 require.Nil(t, err) 74 75 err = tx.SignWithKeys(signer, sender.GetTxKeys()) 76 require.Nil(t, err) 77 78 err = txpool.AddLocal(tx) 79 require.True(t, err == nil || err == blockchain.ErrAlreadyNonceExistInPool, "err=%v", err) 80 81 deployAddr = crypto.CreateAddress(sender.Addr, sender.Nonce) 82 sender.AddNonce() 83 84 receipt := waitReceipt(chain, tx.Hash()) 85 require.NotNil(t, receipt) 86 require.Equal(t, types.ReceiptStatusSuccessful, receipt.Status) 87 88 _, _, deployBlock, _ = chain.GetTxAndLookupInfo(tx.Hash()) 89 t.Logf("AddressBook deployed at block=%2d", deployBlock) 90 } 91 92 // Temporarily use the newly deployed address in StakingManager 93 oldAddr := common.HexToAddress(rewardcontract.AddressBookContractAddress) 94 reward.SetTestAddressBookAddress(deployAddr) 95 defer reward.SetTestAddressBookAddress(oldAddr) 96 97 // Temporarily lower the StakingUpdateInterval 98 oldInterval := params.StakingUpdateInterval() 99 params.SetStakingUpdateInterval(3) 100 defer func() { params.SetStakingUpdateInterval(oldInterval) }() 101 102 // Create the StakingManager singleton 103 oldStakingManager := reward.GetStakingManager() 104 reward.SetTestStakingManagerWithChain(chain, gov, db) 105 defer reward.SetTestStakingManager(oldStakingManager) 106 107 // Attempt to read contract 108 require.NotNil(t, waitBlock(chain, deployBlock+3)) 109 stakingInfo := reward.GetStakingInfo(deployBlock + 6) 110 assert.NotNil(t, stakingInfo) 111 112 t.Logf("StakingInfo=%s", stakingInfo) 113 }