github.com/lino-network/lino@v0.6.11/test/bandwidth/app_bandwidth_test.go (about)

     1  package bandwidth
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  	"github.com/tendermint/tendermint/crypto/secp256k1"
     9  
    10  	"github.com/lino-network/lino/test"
    11  	linotypes "github.com/lino-network/lino/types"
    12  	bandwidthmodel "github.com/lino-network/lino/x/bandwidth/model"
    13  	devtypes "github.com/lino-network/lino/x/developer/types"
    14  	types "github.com/lino-network/lino/x/vote/types"
    15  )
    16  
    17  // test validator deposit
    18  func TestAppBandwidth(t *testing.T) {
    19  	newAccountTransactionPriv := secp256k1.GenPrivKey()
    20  	newAccountName := "newuser"
    21  	baseT := time.Unix(0, 0)
    22  	baseTime := time.Unix(0, 0).Unix()
    23  	lb := test.NewTestLinoBlockchain(t, test.DefaultNumOfVal, baseT)
    24  
    25  	test.CreateAccount(t, newAccountName, lb, 0,
    26  		secp256k1.GenPrivKey(), newAccountTransactionPriv, "5000000000")
    27  
    28  	voteDepositMsg := types.NewStakeInMsg(newAccountName, linotypes.LNO("3000000"))
    29  	test.SignCheckDeliver(t, lb, voteDepositMsg, 1, true, newAccountTransactionPriv, baseTime)
    30  
    31  	registerAppMsg := devtypes.NewDeveloperRegisterMsg(newAccountName, "dummy", "dummy", "dummy")
    32  	test.SignCheckDeliver(t, lb, registerAppMsg, 2, true, newAccountTransactionPriv, baseTime)
    33  
    34  	// the tx will fail since app bandwidth info will be updated hourly
    35  	voteDepositSmallMsg := types.NewStakeInMsg(newAccountName, linotypes.LNO("1000"))
    36  	test.SignCheckTxFail(t, lb, voteDepositSmallMsg, 2, newAccountTransactionPriv)
    37  
    38  	// the tx will success after one hour
    39  	test.SimulateOneBlock(lb, baseTime+3600)
    40  	test.RepeatSignCheckDeliver(t, lb, voteDepositSmallMsg, 3, true, newAccountTransactionPriv, baseTime+3603, 1000)
    41  	curCredit := "910.849417274954801802"
    42  	curCreditDec, _ := sdk.NewDecFromStr(curCredit)
    43  	test.CheckAppBandwidthInfo(t, bandwidthmodel.AppBandwidthInfo{
    44  		Username:           "newuser",
    45  		MaxBandwidthCredit: sdk.NewDec(2400),
    46  		CurBandwidthCredit: curCreditDec,
    47  		MessagesInCurBlock: 0,
    48  		ExpectedMPS:        sdk.NewDec(240),
    49  		LastRefilledAt:     baseTime + 3603,
    50  	}, linotypes.AccountKey(newAccountName), lb)
    51  
    52  	test.RepeatSignCheckDeliver(t, lb, voteDepositSmallMsg, 1003, true, newAccountTransactionPriv, baseTime+3603+3, 1000)
    53  	curCredit = "7.099448771136388802"
    54  	curCreditDec, _ = sdk.NewDecFromStr(curCredit)
    55  	test.CheckAppBandwidthInfo(t, bandwidthmodel.AppBandwidthInfo{
    56  		Username:           "newuser",
    57  		MaxBandwidthCredit: sdk.NewDec(2400),
    58  		CurBandwidthCredit: curCreditDec,
    59  		MessagesInCurBlock: 0,
    60  		ExpectedMPS:        sdk.NewDec(240),
    61  		LastRefilledAt:     baseTime + 3603 + 3,
    62  	}, linotypes.AccountKey(newAccountName), lb)
    63  
    64  	curCredit = "301.600990083371031122"
    65  	curCreditDec, _ = sdk.NewDecFromStr(curCredit)
    66  	test.RepeatSignCheckDeliver(t, lb, voteDepositSmallMsg, 2003, true, newAccountTransactionPriv, baseTime+3603+3+3, 720)
    67  	test.CheckAppBandwidthInfo(t, bandwidthmodel.AppBandwidthInfo{
    68  		Username:           "newuser",
    69  		MaxBandwidthCredit: sdk.NewDec(2400),
    70  		CurBandwidthCredit: curCreditDec,
    71  		MessagesInCurBlock: 0,
    72  		ExpectedMPS:        sdk.NewDec(240),
    73  		LastRefilledAt:     baseTime + 3603 + 3 + 3,
    74  	}, linotypes.AccountKey(newAccountName), lb)
    75  }