github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/validation/state_validate_test.go (about)

     1  package validation_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"math/big"
     8  
     9  	"github.com/sixexorg/magnetic-ring/common"
    10  	"github.com/sixexorg/magnetic-ring/config"
    11  	"github.com/sixexorg/magnetic-ring/core/mainchain/types"
    12  	"github.com/sixexorg/magnetic-ring/store/mainchain/genesis"
    13  	"github.com/sixexorg/magnetic-ring/store/mainchain/states"
    14  	"github.com/sixexorg/magnetic-ring/store/mainchain/storages"
    15  	"github.com/sixexorg/magnetic-ring/store/mainchain/validation"
    16  
    17  	_ "github.com/sixexorg/magnetic-ring/mock"
    18  )
    19  
    20  var (
    21  	stateValidate *validation.StateValidate
    22  	tx1           *types.Transaction //crystal
    23  	tx2           *types.Transaction //crystal
    24  	tx3           *types.Transaction //energy
    25  	tx4           *types.Transaction //crystal
    26  	tx5           *types.Transaction //crystal
    27  
    28  	tx6 *types.Transaction //createLeague
    29  
    30  	tx7  *types.Transaction //createLeague
    31  	tx8  *types.Transaction //createLeague
    32  	tx9  *types.Transaction //createLeague
    33  	tx10 *types.Transaction //join
    34  	tx11 *types.Transaction //join
    35  	tx12 *types.Transaction //join
    36  
    37  	txs []*types.Transaction
    38  
    39  	nodeId, _    = common.ToAddress("ct3qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda")
    40  	Address_1, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda")
    41  	Address_2, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdb")
    42  	Address_3, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdc")
    43  	Address_4, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdd")
    44  	Address_5, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfde")
    45  
    46  	League_1, _                                                        = common.ToAddress("ct2qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda")
    47  	League_1_minbox, league_1_frozenBox, league_1_nonce, league_1_rate = uint64(20), big.NewInt(2000), uint64(1), uint32(1000)
    48  
    49  	ledger *storages.LedgerStoreImp
    50  
    51  	gss *genesis.Genesis
    52  )
    53  
    54  func TestMain(m *testing.M) {
    55  
    56  	dbdir := "test"
    57  	ledger, _ = storages.NewLedgerStore(dbdir)
    58  	genconf := &config.GenesisConfig{
    59  		ChainId: "1",
    60  		Crystal:     10000000,
    61  		Energy:    20000000,
    62  	}
    63  	gss, _ = genesis.InitGenesis(genconf)
    64  	err := gss.GenesisBlock(ledger)
    65  	if err != nil {
    66  		return
    67  	}
    68  	stateValidate = validation.NewStateValidate(ledger)
    69  
    70  	//height := uint64(50)
    71  	var (
    72  		tp1, tp2, tp3, tp4, tp5, tp6    = types.TransferBox, types.TransferBox, types.TransferEnergy, types.TransferBox, types.TransferBox, types.CreateLeague
    73  		tp7, tp8, tp9, tp10, tp11, tp12 = types.CreateLeague, types.CreateLeague, types.CreateLeague, types.JoinLeague, types.JoinLeague, types.JoinLeague
    74  
    75  		balance1, balance2                                = big.NewInt(10000), big.NewInt(2000)
    76  		energy1, energy2                                      = big.NewInt(5000), big.NewInt(4000)
    77  		nonce1, n1, n2, n3, n4, n5, n6             uint64 = 1, 2, 3, 4, 5, 6, 7
    78  		nonce2, m1, m2, m3, m4, m5                 uint64 = 1, 2, 3, 4, 5, 6
    79  		fee1, fee2, fee3, fee4, fee5, fee6                = big.NewInt(20), big.NewInt(10), big.NewInt(2), big.NewInt(20), big.NewInt(5), big.NewInt(100)
    80  		tx1a1, tx1a2, touta1_1, touta1_2, touta1_3        = big.NewInt(20), big.NewInt(108), big.NewInt(30), big.NewInt(50), big.NewInt(28)
    81  		tx2a1, tx2a2, touta2_1, touta2_2, touta2_3        = big.NewInt(80), big.NewInt(300), big.NewInt(50), big.NewInt(50), big.NewInt(280)
    82  		tx3a1, tx3a2, touta3_1, touta3_2, touta3_3        = big.NewInt(800), big.NewInt(500), big.NewInt(500), big.NewInt(300), big.NewInt(500)
    83  		tx4a1, tx4a2, touta4_1, touta4_2, touta4_3        = big.NewInt(200), big.NewInt(20), big.NewInt(20), big.NewInt(100), big.NewInt(100)
    84  		tx5a1, tx5a2, touta5_1, touta5_2, touta5_3        = big.NewInt(3000), big.NewInt(40), big.NewInt(1000), big.NewInt(1000), big.NewInt(1040)
    85  		//addr1,addr1,addr2,addr1,addr1,addr2
    86  		ln7, ln8, ln9, ln10, ln11, ln12       uint64 = 8, 9, 7, 10, 11, 8
    87  		fee7, fee8, fee9, fee10, fee11, fee12        = big.NewInt(50), big.NewInt(50), big.NewInt(50), big.NewInt(50), big.NewInt(50), big.NewInt(50)
    88  	)
    89  	//league data
    90  	var (
    91  		rate6, rate7, rate8, rate9             uint32 = 1000, 2000, 2000, 4000
    92  		minBox6, minBox7, minBox8, minBox9     uint64 = 100, 20, 20, 50
    93  		metaBox6, metaBox7, metaBox8, metaBox9        = big.NewInt(1000), big.NewInt(1000), big.NewInt(1000), big.NewInt(1000)
    94  		jminBox10, jminBox11, jminBox12        uint64 = 20, 20, 20
    95  		leageNonce1, leageNonce2, leageNonce3  uint64 = 2, 2, 3
    96  	)
    97  
    98  	ass := make(states.AccountStates, 0, 5)
    99  	ass = append(ass, &states.AccountState{
   100  		Address: Address_1,
   101  		Height:  0,
   102  		Data: &states.Account{
   103  			Nonce:       nonce1,
   104  			Balance:     balance1,
   105  			EnergyBalance: energy1,
   106  			BonusHeight: 0,
   107  		}}, &states.AccountState{
   108  		Address: Address_2,
   109  		Height:  0,
   110  		Data: &states.Account{
   111  			Nonce:       nonce2,
   112  			Balance:     balance2,
   113  			EnergyBalance: energy2,
   114  			BonusHeight: 0,
   115  		}}, &states.AccountState{
   116  		Address: Address_3,
   117  		Height:  0,
   118  		Data: &states.Account{
   119  			Nonce:       0,
   120  			Balance:     big.NewInt(10000),
   121  			EnergyBalance: big.NewInt(20000),
   122  			BonusHeight: 0,
   123  		}}, &states.AccountState{
   124  		Address: Address_4,
   125  		Height:  0,
   126  		Data: &states.Account{
   127  			Nonce:       0,
   128  			Balance:     big.NewInt(0),
   129  			EnergyBalance: big.NewInt(0),
   130  			BonusHeight: 0,
   131  		}}, &states.AccountState{
   132  		Address: Address_5,
   133  		Height:  0,
   134  		Data: &states.Account{
   135  			Nonce:       0,
   136  			Balance:     big.NewInt(0),
   137  			EnergyBalance: big.NewInt(0),
   138  			BonusHeight: 0,
   139  		}},
   140  	)
   141  	ledger.SaveAccounts(ass)
   142  
   143  	stateValidate.ParentBonusHeight = 1000
   144  	stateValidate.MemoAccountState[Address_1] = ass[0]
   145  	stateValidate.MemoAccountState[Address_2] = ass[1]
   146  	stateValidate.MemoAccountState[Address_3] = ass[2]
   147  	stateValidate.MemoAccountState[Address_4] = ass[3]
   148  	stateValidate.MemoAccountState[Address_5] = ass[4]
   149  
   150  	/*	league1 := &states.LeagueState{
   151  			Address: League_1,
   152  			Height:  0,
   153  			MinBox:  League_1_minbox,
   154  			Creator: Address_5,
   155  			Rate:    league_1_rate,
   156  			Data: &states.League{
   157  				Nonce:     league_1_nonce,
   158  				FrozenBox: league_1_frozenBox,
   159  				Private:   false,
   160  			},
   161  		}
   162  		member1 := &states.LeagueMember{
   163  			LeagueId: League_1,
   164  			Height:   0,
   165  			Data: &states.LeagueAccount{
   166  				Account: Address_5,
   167  				Status:  states.LAS_Normal,
   168  			},
   169  		}
   170  		ledger.SaveLeagues(states.LeagueStates{league1}, states.LeagueMembers{member1})*/
   171  
   172  	//stateValidate.MemoLeagueState[League_1] = league1
   173  	common.DeepCopy(&stateValidate.DirtyAccountState, stateValidate.MemoAccountState)
   174  	common.DeepCopy(&stateValidate.DirtyLeagueState, stateValidate.MemoLeagueState)
   175  	froms := &common.TxIns{}
   176  	froms.Tis = append(froms.Tis,
   177  		&common.TxIn{
   178  			Address: Address_1,
   179  			Nonce:   n1,
   180  			Amount:  tx1a1,
   181  		},
   182  		&common.TxIn{
   183  			Address: Address_2,
   184  			Nonce:   m1,
   185  			Amount:  tx1a2,
   186  		},
   187  	)
   188  	tos := &common.TxOuts{}
   189  	tos.Tos = append(tos.Tos,
   190  		&common.TxOut{
   191  			Address: Address_3,
   192  			Amount:  touta1_1,
   193  		},
   194  		&common.TxOut{
   195  			Address: Address_4,
   196  			Amount:  touta1_2,
   197  		},
   198  		&common.TxOut{
   199  			Address: Address_5,
   200  			Amount:  touta1_3,
   201  		},
   202  	)
   203  	txd1 := &types.TxData{
   204  		Froms: froms,
   205  		Tos:   tos,
   206  		Fee:   fee1,
   207  		From:  Address_1,
   208  	}
   209  
   210  	tx1 = &types.Transaction{
   211  		Version: 0x01,
   212  		TxType:  tp1,
   213  		TxData:  txd1,
   214  	}
   215  	common.DeepCopy(&tx2, tx1)
   216  	tx2.TxType = tp2
   217  	tx2.TxData.Froms.Tis[0].Nonce = n2
   218  	tx2.TxData.Froms.Tis[0].Amount = tx2a1
   219  	tx2.TxData.Froms.Tis[1].Nonce = m2
   220  	tx2.TxData.Froms.Tis[1].Amount = tx2a2
   221  	tx2.TxData.Tos.Tos[0].Amount = touta2_1
   222  	tx2.TxData.Tos.Tos[1].Amount = touta2_2
   223  	tx2.TxData.Tos.Tos[2].Amount = touta2_3
   224  	tx2.TxData.Fee = fee2
   225  	common.DeepCopy(&tx3, tx1)
   226  	tx3.TxType = tp3
   227  	tx3.TxData.Froms.Tis[0].Nonce = n3
   228  	tx3.TxData.Froms.Tis[0].Amount = tx3a1
   229  	tx3.TxData.Froms.Tis[1].Nonce = m3
   230  	tx3.TxData.Froms.Tis[1].Amount = tx3a2
   231  	tx3.TxData.Tos.Tos[0].Amount = touta3_1
   232  	tx3.TxData.Tos.Tos[1].Amount = touta3_2
   233  	tx3.TxData.Tos.Tos[2].Amount = touta3_3
   234  	tx3.TxData.Fee = fee3
   235  	common.DeepCopy(&tx4, tx1)
   236  	tx4.TxType = tp4
   237  	tx4.TxData.Froms.Tis[0].Nonce = n4
   238  	tx4.TxData.Froms.Tis[0].Amount = tx4a1
   239  	tx4.TxData.Froms.Tis[1].Nonce = m4
   240  	tx4.TxData.Froms.Tis[1].Amount = tx4a2
   241  	tx4.TxData.Tos.Tos[0].Amount = touta4_1
   242  	tx4.TxData.Tos.Tos[1].Amount = touta4_2
   243  	tx4.TxData.Tos.Tos[2].Amount = touta4_3
   244  	tx4.TxData.Fee = fee4
   245  	common.DeepCopy(&tx5, tx1)
   246  	tx5.TxType = tp5
   247  	tx5.TxData.Froms.Tis[0].Nonce = n5
   248  	tx5.TxData.Froms.Tis[0].Amount = tx5a1
   249  	tx5.TxData.Froms.Tis[1].Nonce = m5
   250  	tx5.TxData.Froms.Tis[1].Amount = tx5a2
   251  	tx5.TxData.Tos.Tos[0].Amount = touta5_1
   252  	tx5.TxData.Tos.Tos[1].Amount = touta5_2
   253  	tx5.TxData.Tos.Tos[2].Amount = touta5_3
   254  	tx5.TxData.Fee = fee5
   255  
   256  	addr_a_bal := big.NewInt(0).Set(balance1)
   257  	addr_a_bal.Sub(addr_a_bal, tx1a1)
   258  	addr_a_bal.Sub(addr_a_bal, tx2a1)
   259  	addr_a_bal.Sub(addr_a_bal, tx3a1)
   260  	addr_a_bal.Sub(addr_a_bal, tx4a1)
   261  	addr_a_bal.Sub(addr_a_bal, tx5a1)
   262  
   263  	addr_b_bal := big.NewInt(0).Set(balance2)
   264  	addr_b_bal.Sub(addr_b_bal, tx1a2)
   265  	addr_b_bal.Sub(addr_b_bal, tx2a2)
   266  	addr_b_bal.Sub(addr_b_bal, tx3a2)
   267  	addr_b_bal.Sub(addr_b_bal, tx4a2)
   268  	addr_b_bal.Sub(addr_b_bal, tx5a2)
   269  
   270  	tx6 = &types.Transaction{
   271  		Version: 0x01,
   272  		TxType:  tp6,
   273  		TxData: &types.TxData{
   274  			From:    Address_1,
   275  			Nonce:   n6,
   276  			Fee:     fee6,
   277  			Rate:    rate6,
   278  			MinBox:  minBox6,
   279  			MetaBox: metaBox6,
   280  			NodeId:  nodeId,
   281  			Private: false,
   282  		},
   283  	}
   284  	common.DeepCopy(&tx7, tx6)
   285  	tx7.TxType = tp7
   286  	tx7.TxData.From = Address_1
   287  	tx7.TxData.Nonce = ln7
   288  	tx7.TxData.Rate = rate7
   289  	tx7.TxData.MetaBox = metaBox7
   290  	tx7.TxData.MinBox = minBox7
   291  	tx7.TxData.Private = true
   292  	tx7.TxData.Fee = fee7
   293  
   294  	common.DeepCopy(&tx8, tx6)
   295  	tx8.TxType = tp8
   296  	tx8.TxData.From = Address_1
   297  	tx8.TxData.Nonce = ln8
   298  	tx8.TxData.Rate = rate8
   299  	tx8.TxData.MetaBox = metaBox8
   300  	tx8.TxData.MinBox = minBox8
   301  	tx8.TxData.Private = true
   302  	tx8.TxData.Fee = fee8
   303  
   304  	common.DeepCopy(&tx9, tx6)
   305  	tx9.TxType = tp9
   306  	tx9.TxData.From = Address_2
   307  	tx9.TxData.Nonce = ln9
   308  	tx9.TxData.Rate = rate9
   309  	tx9.TxData.MetaBox = metaBox9
   310  	tx9.TxData.MinBox = minBox9
   311  	tx9.TxData.Private = true
   312  	tx9.TxData.Fee = fee9
   313  
   314  	common.DeepCopy(&tx10, tx6)
   315  	tx10.TxType = tp10
   316  	tx10.TxData.From = Address_1
   317  	tx10.TxData.Account = Address_1
   318  	tx10.TxData.Nonce = ln10
   319  	tx10.TxData.LeagueNonce = leageNonce1
   320  	tx10.TxData.MinBox = jminBox10
   321  	tx10.TxData.LeagueId = League_1
   322  	tx10.TxData.Fee = fee10
   323  
   324  	common.DeepCopy(&tx11, tx6)
   325  	tx11.TxType = tp11
   326  	tx11.TxData.From = Address_1
   327  	tx11.TxData.Account = Address_1
   328  	tx11.TxData.Nonce = ln11
   329  	tx11.TxData.LeagueNonce = leageNonce2
   330  	tx11.TxData.MinBox = jminBox11
   331  	tx11.TxData.LeagueId = League_1
   332  	tx11.TxData.Fee = fee11
   333  
   334  	common.DeepCopy(&tx12, tx6)
   335  	tx12.TxType = tp12
   336  	tx12.TxData.From = Address_2
   337  	tx11.TxData.Account = Address_1
   338  	tx12.TxData.Nonce = ln12
   339  	tx12.TxData.LeagueNonce = leageNonce3
   340  	tx12.TxData.MinBox = jminBox12
   341  	tx12.TxData.LeagueId = League_1
   342  	tx12.TxData.Fee = fee12
   343  
   344  	txs = append(txs, tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8, tx9, tx10, tx11, tx12)
   345  
   346  	m.Run()
   347  	os.RemoveAll(dbdir)
   348  }
   349  func TestStateValidate_VerifyTx(t *testing.T) {
   350  
   351  	for k, v := range txs[:] {
   352  		//fmt.Println("num:", k, ",hash:", v.Hash())
   353  		result, err := stateValidate.VerifyTx(v)
   354  		switch result {
   355  		case -1:
   356  			t.Log("no.", k, " throw:", err)
   357  		case 0:
   358  			t.Log("no.", k, " back to queue:", err)
   359  		case 1:
   360  			t.Log("no.", k, " err:", err)
   361  		}
   362  	}
   363  
   364  	/*	t.Log("-----------------------MemoAccountState---------------------------")
   365  		for k, v := range stateValidate.MemoAccountState {
   366  			t.Logf("address:%s, nonce:%v, bonusheight:%v, balance:%d, energybalance:%d ", k.ToString(), v.Data.Nonce, v.Data.BonusHeight, v.Data.Balance.Uint64(), v.Data.EnergyBalance.Uint64())
   367  		}*/
   368  	/*	t.Log("-----------------------DirtyAccountState---------------------------")
   369  		for k, v := range stateValidate.DirtyAccountState {
   370  			t.Logf("address:%s, nonce:%v, bonusheight:%v, balance:%d, energybalance:%d ", k.ToString(), v.Data.Nonce, v.Data.BonusHeight, v.Data.Balance.Uint64(), v.Data.EnergyBalance.Uint64())
   371  		}*/
   372  	//t.Log("--------------------------------------------------")
   373  	blockInfo := stateValidate.ExecuteOplogs()
   374  	//t.Log("-----------------------Execute---------------------------")
   375  	/*t.Log("\nblockheader:", blockInfo.Block.Header, "\ntxCount:", blockInfo.Block.Transactions.Len())
   376  	t.Log("\nreceiptsCount:", blockInfo.Receipts.Len())*/
   377  	/*for _, v := range blockInfo.AccountStates {
   378  		fmt.Printf("States1: address:%s, nonce:%d, bonusheight:%d, balance:%d, energybalance:%d \n", v.Address.ToString(), v.Data.Nonce, v.Data.BonusHeight, v.Data.Balance.Uint64(), v.Data.EnergyBalance.Uint64())
   379  	}*/
   380  	/*	for _, v := range blockInfo.LeagueStates {
   381  		t.Logf("address:%s, nonce:%d, MemberRoot:%d, FrozenBox:%d, Private:%t, minBox:%d, rate:%d, creator:%s  ", v.Address.ToString(), v.Data.Nonce, v.Data.MemberRoot, v.Data.FrozenBox.Uint64(), v.Data.Private, v.MinBox, v.Rate, v.Creator.ToString())
   382  	}*/
   383  
   384  	/*for _, v := range blockInfo.Members {
   385  
   386  		t.Logf("league:%s, account:%s,state:%d ", v.LeagueId.ToString(), v.Data.Account.ToString(), v.Data.Status)
   387  	}*/
   388  	t.Log(blockInfo.FeeSum.String())
   389  	//t.Log("--------------------------------------------------")
   390  	/*	fmt.Printf("block:\n txroot:%s\n receipt:%s\n account:%s\n league:%s\n",
   391  		blockInfo.Block.Header.TxRoot.String(),
   392  		blockInfo.Block.Header.ReceiptsRoot.String(),
   393  		blockInfo.Block.Header.StateRoot.String(),
   394  		blockInfo.Block.Header.LeagueRoot.String(),
   395  	)*/
   396  	/*for k, v := range blockInfo.Receipts {
   397  		fmt.Printf("Receipt1: num:%d, status:%t, txhash:%s, gasused:%d \n", k, v.Status, v.TxHash.String(), v.GasUsed)
   398  	}*/
   399  
   400  	//blockInfo.Block.Transactions = types.Transactions{}
   401  	_, err := validation.ValidateBlock(blockInfo.Block, ledger)
   402  	if err != nil {
   403  		t.Error(err)
   404  		t.Fail()
   405  		return
   406  	}
   407  }
   408  
   409  func TestRemoveElementInLeagueForCreate(t *testing.T) {
   410  
   411  	leagueKVs := []*storages.LeagueKV{
   412  		&storages.LeagueKV{Address_1, common.Hash{}},
   413  		&storages.LeagueKV{Address_2, common.Hash{}},
   414  		&storages.LeagueKV{Address_3, common.Hash{}},
   415  		&storages.LeagueKV{Address_4, common.Hash{}},
   416  		&storages.LeagueKV{Address_5, common.Hash{}},
   417  	}
   418  	s := &validation.StateValidate{
   419  		LeagueForCreate: leagueKVs,
   420  	}
   421  	s.RemoveElementInLeagueForCreate(Address_5)
   422  	for _, v := range s.LeagueForCreate {
   423  		t.Log(v.K.ToString())
   424  	}
   425  }