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

     1  package account_level
     2  
     3  import (
     4  	"testing"
     5  
     6  	"math/big"
     7  
     8  	"fmt"
     9  
    10  	"os"
    11  
    12  	"github.com/sixexorg/magnetic-ring/common/sink"
    13  	"github.com/sixexorg/magnetic-ring/mock"
    14  	"github.com/sixexorg/magnetic-ring/store/mainchain/states"
    15  )
    16  
    17  func TestLevelManager(t *testing.T) {
    18  	dbDir := "./test/"
    19  	defer os.RemoveAll(dbDir)
    20  	lm, err := NewLevelManager(1, 1, dbDir)
    21  	if err != nil {
    22  		t.Fail()
    23  		t.Error(err)
    24  		return
    25  	}
    26  	asmap := getAccountStates()
    27  	destroy := big.NewInt(50000)
    28  	for i := uint64(1); i <= uint64(len(asmap)); i++ {
    29  		lm.ReceiveAccountStates(asmap[i][2:3], destroy, i)
    30  		destroy.Mul(destroy, big.NewInt(10))
    31  		destroy.Div(destroy, big.NewInt(9))
    32  		fmt.Printf("----------------------------------loop %d------------------------------\n", i)
    33  		for k, v := range lm.nextAccLvl {
    34  			im, l, r, cur := v.Lv.Decode()
    35  			fmt.Printf("account:%s amount:%d im:%v l:%d r:%d cur:%d\n", k.ToString(), v.Amount, im, l, r, cur)
    36  		}
    37  		for j := EasyLevel(1); j <= lv9; j++ {
    38  			fmt.Printf("lv %d count:%d amount:%d \n", j, lm.distribution[j].Count, lm.distribution[j].Amount.Uint64())
    39  		}
    40  		fmt.Println(lm.getLvAmountDistribution())
    41  		bs, err := lm.GetNextHeaderProperty(i + 1)
    42  		if err != nil {
    43  			t.Log(err)
    44  		}
    45  		t.Logf("b%d %v", i+1, bs)
    46  	}
    47  	bs, err := lm.GetNextHeaderProperty(5)
    48  	if err != nil {
    49  		t.Log(err)
    50  	}
    51  	t.Log("bs1:", bs)
    52  	bs, err = lm.GetNextHeaderProperty(11)
    53  	if err != nil {
    54  		t.Error(err)
    55  		return
    56  	}
    57  	t.Log("bs2:", bs)
    58  
    59  	b := big.NewInt(0).SetUint64(bs[3])
    60  	b.Mul(b, big.NewInt(4.5e11))
    61  	b.Div(b, big.NewInt(1e8))
    62  	fmt.Println(b)
    63  }
    64  
    65  func getAccountStates() map[uint64]states.AccountStates {
    66  
    67  	asmap := make(map[uint64]states.AccountStates, 10)
    68  
    69  	ass1 := states.AccountStates{}
    70  	ass1 = append(ass1,
    71  		&states.AccountState{
    72  			Address: mock.Address_1,
    73  			Data: &states.Account{
    74  				Balance: big.NewInt(1.5e8),
    75  			},
    76  		},
    77  		&states.AccountState{
    78  			Address: mock.Address_2,
    79  			Data: &states.Account{
    80  				Balance: big.NewInt(3.5e12),
    81  			},
    82  		},
    83  		&states.AccountState{
    84  			Address: mock.Address_3,
    85  			Data: &states.Account{
    86  				Balance: big.NewInt(3.5e11),
    87  			},
    88  		},
    89  		&states.AccountState{
    90  			Address: mock.Address_4,
    91  			Data: &states.Account{
    92  				Balance: big.NewInt(6.5e11),
    93  			},
    94  		},
    95  	)
    96  	asmap[1] = ass1
    97  
    98  	ass2 := states.AccountStates{}
    99  	ass2 = append(ass2,
   100  		&states.AccountState{
   101  			Address: mock.Address_1,
   102  			Data: &states.Account{
   103  				Balance: big.NewInt(0.5e8),
   104  			},
   105  		},
   106  		&states.AccountState{
   107  			Address: mock.Address_2,
   108  			Data: &states.Account{
   109  				Balance: big.NewInt(1.5e10),
   110  			},
   111  		},
   112  		&states.AccountState{
   113  			Address: mock.Address_3,
   114  			Data: &states.Account{
   115  				Balance: big.NewInt(6.5e11),
   116  			},
   117  		},
   118  		&states.AccountState{
   119  			Address: mock.Address_4,
   120  			Data: &states.Account{
   121  				Balance: big.NewInt(128.5e11),
   122  			},
   123  		},
   124  	)
   125  	asmap[2] = ass2
   126  
   127  	ass3 := states.AccountStates{}
   128  	ass3 = append(ass3,
   129  		&states.AccountState{
   130  			Address: mock.Address_1,
   131  			Data: &states.Account{
   132  				Balance: big.NewInt(30e11),
   133  			},
   134  		},
   135  		&states.AccountState{
   136  			Address: mock.Address_2,
   137  			Data: &states.Account{
   138  				Balance: big.NewInt(2.5e11),
   139  			},
   140  		},
   141  		&states.AccountState{
   142  			Address: mock.Address_3,
   143  			Data: &states.Account{
   144  				Balance: big.NewInt(8.5e11),
   145  			},
   146  		},
   147  		&states.AccountState{
   148  			Address: mock.Address_4,
   149  			Data: &states.Account{
   150  				Balance: big.NewInt(128.5e11),
   151  			},
   152  		},
   153  	)
   154  	asmap[3] = ass3
   155  
   156  	ass4 := states.AccountStates{}
   157  	ass4 = append(ass4,
   158  		&states.AccountState{
   159  			Address: mock.Address_1,
   160  			Data: &states.Account{
   161  				Balance: big.NewInt(1000),
   162  			},
   163  		},
   164  		&states.AccountState{
   165  			Address: mock.Address_2,
   166  			Data: &states.Account{
   167  				Balance: big.NewInt(1.e10),
   168  			},
   169  		},
   170  		&states.AccountState{
   171  			Address: mock.Address_3,
   172  			Data: &states.Account{
   173  				Balance: big.NewInt(9.5e11),
   174  			},
   175  		},
   176  		&states.AccountState{
   177  			Address: mock.Address_4,
   178  			Data: &states.Account{
   179  				Balance: big.NewInt(129e11),
   180  			},
   181  		},
   182  	)
   183  
   184  	asmap[4] = ass4
   185  	ass5 := states.AccountStates{}
   186  	ass5 = append(ass5,
   187  		&states.AccountState{
   188  			Address: mock.Address_1,
   189  			Data: &states.Account{
   190  				Balance: big.NewInt(500),
   191  			},
   192  		},
   193  		&states.AccountState{
   194  			Address: mock.Address_2,
   195  			Data: &states.Account{
   196  				Balance: big.NewInt(1.9e11),
   197  			},
   198  		},
   199  		&states.AccountState{
   200  			Address: mock.Address_3,
   201  			Data: &states.Account{
   202  				Balance: big.NewInt(4.5e11),
   203  			},
   204  		},
   205  		&states.AccountState{
   206  			Address: mock.Address_4,
   207  			Data: &states.Account{
   208  				Balance: big.NewInt(130e11),
   209  			},
   210  		},
   211  	)
   212  	asmap[5] = ass5
   213  	ass6 := states.AccountStates{}
   214  	ass6 = append(ass6,
   215  		&states.AccountState{
   216  			Address: mock.Address_1,
   217  			Data: &states.Account{
   218  				Balance: big.NewInt(500),
   219  			},
   220  		},
   221  		&states.AccountState{
   222  			Address: mock.Address_2,
   223  			Data: &states.Account{
   224  				Balance: big.NewInt(1.9e11),
   225  			},
   226  		},
   227  		&states.AccountState{
   228  			Address: mock.Address_3,
   229  			Data: &states.Account{
   230  				Balance: big.NewInt(4.5e11),
   231  			},
   232  		},
   233  		&states.AccountState{
   234  			Address: mock.Address_4,
   235  			Data: &states.Account{
   236  				Balance: big.NewInt(130e11),
   237  			},
   238  		},
   239  	)
   240  	asmap[6] = ass6
   241  	ass7 := states.AccountStates{}
   242  	ass7 = append(ass7,
   243  		&states.AccountState{
   244  			Address: mock.Address_1,
   245  			Data: &states.Account{
   246  				Balance: big.NewInt(500),
   247  			},
   248  		},
   249  		&states.AccountState{
   250  			Address: mock.Address_2,
   251  			Data: &states.Account{
   252  				Balance: big.NewInt(1.9e11),
   253  			},
   254  		},
   255  		&states.AccountState{
   256  			Address: mock.Address_3,
   257  			Data: &states.Account{
   258  				Balance: big.NewInt(4.5e11),
   259  			},
   260  		},
   261  		&states.AccountState{
   262  			Address: mock.Address_4,
   263  			Data: &states.Account{
   264  				Balance: big.NewInt(130e11),
   265  			},
   266  		},
   267  	)
   268  	asmap[7] = ass7
   269  	ass8 := states.AccountStates{}
   270  	ass8 = append(ass8,
   271  		&states.AccountState{
   272  			Address: mock.Address_1,
   273  			Data: &states.Account{
   274  				Balance: big.NewInt(500),
   275  			},
   276  		},
   277  		&states.AccountState{
   278  			Address: mock.Address_2,
   279  			Data: &states.Account{
   280  				Balance: big.NewInt(1.9e11),
   281  			},
   282  		},
   283  		&states.AccountState{
   284  			Address: mock.Address_3,
   285  			Data: &states.Account{
   286  				Balance: big.NewInt(4.5e11),
   287  			},
   288  		},
   289  		&states.AccountState{
   290  			Address: mock.Address_4,
   291  			Data: &states.Account{
   292  				Balance: big.NewInt(130e11),
   293  			},
   294  		},
   295  	)
   296  	asmap[8] = ass8
   297  	ass9 := states.AccountStates{}
   298  	ass9 = append(ass9,
   299  		&states.AccountState{
   300  			Address: mock.Address_1,
   301  			Data: &states.Account{
   302  				Balance: big.NewInt(500),
   303  			},
   304  		},
   305  		&states.AccountState{
   306  			Address: mock.Address_2,
   307  			Data: &states.Account{
   308  				Balance: big.NewInt(1.9e11),
   309  			},
   310  		},
   311  		&states.AccountState{
   312  			Address: mock.Address_3,
   313  			Data: &states.Account{
   314  				Balance: big.NewInt(4.5e11),
   315  			},
   316  		},
   317  		&states.AccountState{
   318  			Address: mock.Address_4,
   319  			Data: &states.Account{
   320  				Balance: big.NewInt(130e11),
   321  			},
   322  		},
   323  	)
   324  	asmap[9] = ass9
   325  	ass10 := states.AccountStates{}
   326  	ass10 = append(ass10,
   327  		&states.AccountState{
   328  			Address: mock.Address_1,
   329  			Data: &states.Account{
   330  				Balance: big.NewInt(500),
   331  			},
   332  		},
   333  		&states.AccountState{
   334  			Address: mock.Address_2,
   335  			Data: &states.Account{
   336  				Balance: big.NewInt(1.9e11),
   337  			},
   338  		},
   339  		&states.AccountState{
   340  			Address: mock.Address_3,
   341  			Data: &states.Account{
   342  				Balance: big.NewInt(4.5e11),
   343  			},
   344  		},
   345  		&states.AccountState{
   346  			Address: mock.Address_4,
   347  			Data: &states.Account{
   348  				Balance: big.NewInt(130e11),
   349  			},
   350  		},
   351  	)
   352  	asmap[10] = ass10
   353  	return asmap
   354  }
   355  
   356  func TestDecode(t *testing.T) {
   357  	sk := sink.NewZeroCopySink(nil)
   358  	sk.WriteUint8(uint8(10))
   359  	h := uint8(sk.Bytes()[0])
   360  	fmt.Println(h)
   361  	a := []uint64{1}
   362  	fmt.Println(len(a), cap(a))
   363  }