github.com/lino-network/lino@v0.6.11/x/account/manager/testutils_test.go (about)

     1  //nolint:deadcode,unused
     2  package manager
     3  
     4  import (
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/lino-network/lino/param"
     9  	"github.com/lino-network/lino/types"
    10  	"github.com/lino-network/lino/x/account/model"
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/cosmos/cosmos-sdk/store"
    14  	"github.com/tendermint/tendermint/crypto/secp256k1"
    15  	"github.com/tendermint/tendermint/libs/log"
    16  
    17  	sdk "github.com/cosmos/cosmos-sdk/types"
    18  	abci "github.com/tendermint/tendermint/abci/types"
    19  	dbm "github.com/tendermint/tm-db"
    20  )
    21  
    22  var (
    23  	testAccountKVStoreKey = sdk.NewKVStoreKey("account")
    24  	testParamKVStoreKey   = sdk.NewKVStoreKey("param")
    25  
    26  	accountReferrer = types.AccountKey("referrer")
    27  
    28  	l0    = types.LNO("0")
    29  	l100  = types.LNO("100")
    30  	l200  = types.LNO("200")
    31  	l1600 = types.LNO("1600")
    32  	l1800 = types.LNO("1800")
    33  	l1900 = types.LNO("1900")
    34  	l1999 = types.LNO("1999")
    35  	l2000 = types.LNO("2000")
    36  	c0    = types.NewCoinFromInt64(0)
    37  	c100  = types.NewCoinFromInt64(100 * types.Decimals)
    38  	c200  = types.NewCoinFromInt64(200 * types.Decimals)
    39  	c300  = types.NewCoinFromInt64(300 * types.Decimals)
    40  	c400  = types.NewCoinFromInt64(400 * types.Decimals)
    41  	c500  = types.NewCoinFromInt64(500 * types.Decimals)
    42  	c600  = types.NewCoinFromInt64(600 * types.Decimals)
    43  	c1000 = types.NewCoinFromInt64(1000 * types.Decimals)
    44  	c1500 = types.NewCoinFromInt64(1500 * types.Decimals)
    45  	c1600 = types.NewCoinFromInt64(1600 * types.Decimals)
    46  	c1800 = types.NewCoinFromInt64(1800 * types.Decimals)
    47  	c1900 = types.NewCoinFromInt64(1900 * types.Decimals)
    48  	c2000 = types.NewCoinFromInt64(2000 * types.Decimals)
    49  
    50  	coin0   = types.NewCoinFromInt64(0)
    51  	coin1   = types.NewCoinFromInt64(1)
    52  	coin2   = types.NewCoinFromInt64(2)
    53  	coin3   = types.NewCoinFromInt64(3)
    54  	coin4   = types.NewCoinFromInt64(4)
    55  	coin10  = types.NewCoinFromInt64(10)
    56  	coin50  = types.NewCoinFromInt64(50)
    57  	coin100 = types.NewCoinFromInt64(100)
    58  	coin200 = types.NewCoinFromInt64(200)
    59  	coin300 = types.NewCoinFromInt64(300)
    60  	coin400 = types.NewCoinFromInt64(400)
    61  )
    62  
    63  func setupTest(t *testing.T, height int64) (sdk.Context, AccountManager) {
    64  	ctx := getContext(height)
    65  	ph := param.NewParamHolder(testParamKVStoreKey)
    66  	err := ph.InitParam(ctx)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  	accManager := NewAccountManager(testAccountKVStoreKey, ph)
    71  	accManager.storage.SetPool(ctx, &model.Pool{
    72  		Name:    types.InflationValidatorPool,
    73  		Balance: types.MustLinoToCoin("10000000000"),
    74  	})
    75  	accManager.storage.SetPool(ctx, &model.Pool{
    76  		Name:    types.AccountVestingPool,
    77  		Balance: types.MustLinoToCoin("10000000000"),
    78  	})
    79  
    80  	assert.Nil(t, err)
    81  	return ctx, accManager
    82  }
    83  
    84  func getContext(height int64) sdk.Context {
    85  	db := dbm.NewMemDB()
    86  	ms := store.NewCommitMultiStore(db)
    87  	ms.MountStoreWithDB(testAccountKVStoreKey, sdk.StoreTypeIAVL, db)
    88  	ms.MountStoreWithDB(testParamKVStoreKey, sdk.StoreTypeIAVL, db)
    89  	err := ms.LoadLatestVersion()
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  
    94  	return sdk.NewContext(
    95  		ms, abci.Header{ChainID: "Lino", Height: height, Time: time.Now()},
    96  		false, log.NewNopLogger())
    97  }
    98  
    99  func createTestAccount(ctx sdk.Context, am AccountManager, username string) (secp256k1.PrivKeySecp256k1, secp256k1.PrivKeySecp256k1) {
   100  	signingKey := secp256k1.GenPrivKey()
   101  	txPriv := secp256k1.GenPrivKey()
   102  
   103  	accParam := am.paramHolder.GetAccountParam(ctx)
   104  	err := am.GenesisAccount(ctx, types.AccountKey(username), signingKey.PubKey(), txPriv.PubKey())
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  	err = am.MoveFromPool(ctx, types.AccountVestingPool,
   109  		types.NewAccOrAddrFromAcc(types.AccountKey(username)), accParam.RegisterFee)
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  	return signingKey, txPriv
   114  }
   115  
   116  func checkBankKVByUsername(
   117  	t *testing.T, ctx sdk.Context, testName string, username types.AccountKey, bank model.AccountBank) {
   118  	accStorage := model.NewAccountStorage(testAccountKVStoreKey)
   119  	info, err := accStorage.GetInfo(ctx, username)
   120  	if err != nil {
   121  		t.Errorf("%s, failed to get info, got err %v", testName, err)
   122  	}
   123  	bankPtr, err := accStorage.GetBank(ctx, info.Address)
   124  	if err != nil {
   125  		t.Errorf("%s, failed to get bank, got err %v", testName, err)
   126  	}
   127  	if !assert.Equal(t, bank, *bankPtr) {
   128  		t.Errorf("%s: diff bank, got %v, want %v", testName, *bankPtr, bank)
   129  	}
   130  }
   131  
   132  // func checkPendingCoinDay(
   133  // 	t *testing.T, ctx sdk.Context, testName string, username types.AccountKey, pendingCoinDayQueue model.PendingCoinDayQueue) {
   134  // 	accStorage := model.NewAccountStorage(testAccountKVStoreKey)
   135  // 	pendingCoinDayQueuePtr, err := accStorage.GetPendingCoinDayQueue(ctx, username)
   136  // 	assert.Nil(t, err, "%s, failed to get pending coin day queue, got err %v", testName, err)
   137  // 	assert.Equal(t, pendingCoinDayQueue, *pendingCoinDayQueuePtr, "%s: diff pending coin day queue, got %v, want %v", testName, *pendingCoinDayQueuePtr, pendingCoinDayQueue)
   138  // }
   139  
   140  func checkAccountInfo(
   141  	t *testing.T, ctx sdk.Context, testName string, accKey types.AccountKey, accInfo model.AccountInfo) {
   142  	accStorage := model.NewAccountStorage(testAccountKVStoreKey)
   143  	info, err := accStorage.GetInfo(ctx, accKey)
   144  	if err != nil {
   145  		t.Errorf("%s, failed to get account info, got err %v", testName, err)
   146  	}
   147  	if !assert.Equal(t, accInfo, *info) {
   148  		t.Errorf("%s: diff account info, got %v, want %v", testName, *info, accInfo)
   149  	}
   150  }
   151  
   152  func checkAccountMeta(
   153  	t *testing.T, ctx sdk.Context, testName string, accKey types.AccountKey, accMeta model.AccountMeta) {
   154  	accStorage := model.NewAccountStorage(testAccountKVStoreKey)
   155  	metaPtr := accStorage.GetMeta(ctx, accKey)
   156  	if !assert.Equal(t, accMeta, *metaPtr) {
   157  		t.Errorf("%s: diff account meta, got %v, want %v", testName, *metaPtr, accMeta)
   158  	}
   159  }