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

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"encoding/gob"
     9  
    10  	"github.com/sixexorg/magnetic-ring/common"
    11  	"github.com/sixexorg/magnetic-ring/core/mainchain/types"
    12  	"github.com/sixexorg/magnetic-ring/store/orgchain/states"
    13  )
    14  
    15  func TestAggregateBonus(t *testing.T) {
    16  	var width, start, end uint64 = 5, 5, 80
    17  
    18  	gob.Register(&big.Int{})
    19  
    20  	var ed [16]struct {
    21  		aHeight uint64
    22  		balance *big.Int
    23  	}
    24  
    25  	assFTree := make([]common.FTreer, 0)
    26  	assFTree = append(assFTree, &states.AccountState{Height: 1, Data: &states.Account{Balance: big.NewInt(1)}})
    27  	assFTree = append(assFTree, &states.AccountState{Height: 8, Data: &states.Account{Balance: big.NewInt(5000)}})
    28  	assFTree = append(assFTree, &states.AccountState{Height: 11, Data: &states.Account{Balance: big.NewInt(20)}})
    29  	assFTree = append(assFTree, &states.AccountState{Height: 20, Data: &states.Account{Balance: big.NewInt(60000)}})
    30  	assFTree = append(assFTree, &states.AccountState{Height: 50, Data: &states.Account{Balance: big.NewInt(3000)}})
    31  	assFTree = append(assFTree, &states.AccountState{Height: 80, Data: &states.Account{Balance: big.NewInt(2222)}})
    32  
    33  	aft := common.NewForwardTree(width, start, end, assFTree)
    34  
    35  	for i := 0; !aft.Next(); i++ {
    36  		ed[i].aHeight = aft.Key()
    37  		ed[i].balance = aft.Val().(*big.Int)
    38  	}
    39  	bonus := make(map[uint64]uint64)
    40  	bonus[5] = 1
    41  	bonus[10] = 11
    42  	bonus[15] = 111
    43  	bonus[20] = 1111
    44  	bonus[25] = 11111
    45  	bonus[30] = 111111
    46  	bonus[35] = 1111111
    47  	bonus[40] = 111111
    48  	bonus[45] = 11111
    49  	bonus[50] = 1111
    50  	bonus[55] = 111
    51  	bonus[60] = 11
    52  	bonus[65] = 1
    53  	bonus[70] = 11
    54  	bonus[75] = 111
    55  	bonus[80] = 1111
    56  
    57  	sum := big.NewInt(0)
    58  	for k, v := range ed {
    59  		b := big.NewInt(0).SetUint64(bonus[start])
    60  		b.Mul(b, v.balance)
    61  		sum.Add(sum, b)
    62  		start += width
    63  		fmt.Printf("No.%d offset:%d height:%d balance:%d rate:%d amount:%d\n", k+1, start, v.aHeight, v.balance.Uint64(), bonus[start], sum.Uint64())
    64  	}
    65  
    66  	amount, left := aggregateBonus(assFTree, bonus, 5, 80, 5)
    67  	fmt.Println(amount, left)
    68  }
    69  
    70  func TestAppend(t *testing.T) {
    71  	txs := make([]*types.Transaction, 0, 2)
    72  	var tx *types.Transaction
    73  	txs = append(txs, tx)
    74  	fmt.Println(len(txs), tx)
    75  	fmt.Println(txs)
    76  }