github.com/klaytn/klaytn@v1.12.1/tests/kip103_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"math/big"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/klaytn/klaytn/accounts/abi/bind"
     9  	"github.com/klaytn/klaytn/accounts/abi/bind/backends"
    10  	"github.com/klaytn/klaytn/blockchain"
    11  	"github.com/klaytn/klaytn/consensus/istanbul"
    12  	"github.com/klaytn/klaytn/contracts/kip103"
    13  	"github.com/klaytn/klaytn/log"
    14  	"github.com/klaytn/klaytn/params"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestRebalanceTreasury_EOA(t *testing.T) {
    19  	log.EnableLogForTest(log.LvlError, log.LvlInfo)
    20  
    21  	// prepare chain configuration
    22  	config := params.CypressChainConfig.Copy()
    23  	config.LondonCompatibleBlock = big.NewInt(0)
    24  	config.IstanbulCompatibleBlock = big.NewInt(0)
    25  	config.EthTxTypeCompatibleBlock = big.NewInt(0)
    26  	config.MagmaCompatibleBlock = big.NewInt(0)
    27  	config.KoreCompatibleBlock = big.NewInt(0)
    28  	config.Istanbul.SubGroupSize = 1
    29  	config.Istanbul.ProposerPolicy = uint64(istanbul.RoundRobin)
    30  	config.Governance.Reward.MintingAmount = new(big.Int).Mul(big.NewInt(9000000000000000000), big.NewInt(params.KLAY))
    31  
    32  	// make a blockchain node
    33  	fullNode, node, validator, _, workspace := newBlockchain(t, config, nil)
    34  	defer func() {
    35  		fullNode.Stop()
    36  		os.RemoveAll(workspace)
    37  	}()
    38  
    39  	optsOwner := bind.NewKeyedTransactor(validator.Keys[0])
    40  	transactor := backends.NewBlockchainContractBackend(node.BlockChain(), node.TxPool().(*blockchain.TxPool), nil)
    41  	// We need to wait for the following contract executions to be processed, so let's have enough number of blocks
    42  	targetBlockNum := new(big.Int).Add(node.BlockChain().CurrentBlock().Number(), big.NewInt(10))
    43  
    44  	contractAddr, tx, contract, err := kip103.DeployTreasuryRebalance(optsOwner, transactor, targetBlockNum)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	receipt := waitReceipt(node.BlockChain().(*blockchain.BlockChain), tx.Hash())
    49  	if receipt == nil {
    50  		t.Fatal("timeout")
    51  	}
    52  
    53  	// set kip103 hardfork config
    54  	node.BlockChain().Config().Kip103CompatibleBlock = targetBlockNum
    55  	node.BlockChain().Config().Kip103ContractAddress = contractAddr
    56  
    57  	t.Log("ContractOwner Addr:", validator.GetAddr().String())
    58  	t.Log("Contract Addr:", contractAddr.String())
    59  	t.Log("Target Block:", targetBlockNum.Int64())
    60  
    61  	// prepare newbie accounts
    62  	numNewbie := 3
    63  	newbieAccs := make([]TestAccount, numNewbie)
    64  	newbieAllocs := make([]*big.Int, numNewbie)
    65  
    66  	state, err := node.BlockChain().State()
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	totalNewbieAlloc := state.GetBalance(validator.Addr)
    71  	t.Log("Total Newbie amount: ", totalNewbieAlloc)
    72  
    73  	for i := 0; i < numNewbie; i++ {
    74  		newbieAccs[i] = genKlaytnLegacyAccount(t)
    75  		newbieAllocs[i] = new(big.Int).Div(totalNewbieAlloc, big.NewInt(2))
    76  		totalNewbieAlloc.Sub(totalNewbieAlloc, newbieAllocs[i])
    77  
    78  		t.Log("Newbie", i, "Addr:", newbieAccs[i].GetAddr().String())
    79  		t.Log("Newbie", i, "Amount:", newbieAllocs[i])
    80  	}
    81  
    82  	// register RegisterRetired
    83  	if _, err := contract.RegisterRetired(optsOwner, validator.Addr); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	// register newbies
    88  	for i, newbie := range newbieAccs {
    89  		if _, err := contract.RegisterNewbie(optsOwner, newbie.GetAddr(), newbieAllocs[i]); err != nil {
    90  			t.Fatal(err)
    91  		}
    92  	}
    93  
    94  	// initialized -> registered
    95  	if tx, err = contract.FinalizeRegistration(optsOwner); err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	// Should wait for this tx to be processed, or next tx will be failed when estimating gas
    99  	receipt = waitReceipt(node.BlockChain().(*blockchain.BlockChain), tx.Hash())
   100  	if receipt == nil {
   101  		t.Fatal("timeout")
   102  	}
   103  
   104  	// approve
   105  	if tx, err = contract.Approve(optsOwner, validator.GetAddr()); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	// Should wait for this tx to be processed, or next tx will be failed when estimating gas
   109  	receipt = waitReceipt(node.BlockChain().(*blockchain.BlockChain), tx.Hash())
   110  	if receipt == nil {
   111  		t.Fatal("timeout")
   112  	}
   113  
   114  	// registered -> approved
   115  	if _, err := contract.FinalizeApproval(optsOwner); err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	header := waitBlock(node.BlockChain(), targetBlockNum.Uint64())
   120  	if header == nil {
   121  		t.Fatal("timeout")
   122  	}
   123  
   124  	curState, err := node.BlockChain().StateAt(header.Root)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	balRetired := curState.GetBalance(validator.GetAddr())
   130  	assert.Equal(t, balRetired, big.NewInt(0))
   131  
   132  	for j, newbie := range newbieAccs {
   133  		balNewbie := curState.GetBalance(newbie.GetAddr())
   134  		assert.Equal(t, newbieAllocs[j], balNewbie)
   135  	}
   136  }