github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/consensus/cbft/ppos_test.go (about)

     1  package cbft
     2  
     3  import (
     4  	"github.com/PlatONnetwork/PlatON-Go/core/ppos_storage"
     5  	"github.com/PlatONnetwork/PlatON-Go/core/types"
     6  	"github.com/PlatONnetwork/PlatON-Go/p2p/discover"
     7  	"github.com/PlatONnetwork/PlatON-Go/params"
     8  	"math/big"
     9  	"testing"
    10  	"github.com/PlatONnetwork/PlatON-Go/core/ppos"
    11  	"github.com/PlatONnetwork/PlatON-Go/ethdb"
    12  	"github.com/PlatONnetwork/PlatON-Go/core"
    13  	"fmt"
    14  	"github.com/PlatONnetwork/PlatON-Go/core/vm"
    15  	"encoding/json"
    16  	"net"
    17  	"strconv"
    18  	"github.com/PlatONnetwork/PlatON-Go/core/state"
    19  	"sync/atomic"
    20  	"github.com/PlatONnetwork/PlatON-Go/common"
    21  	"math/rand"
    22  	"time"
    23  	"github.com/PlatONnetwork/PlatON-Go/common/byteutil"
    24  	"github.com/PlatONnetwork/PlatON-Go/crypto"
    25  	"github.com/PlatONnetwork/PlatON-Go/core/ticketcache"
    26  )
    27  
    28  func newTesterAccountPool() ([]discover.NodeID, error) {
    29  	var accounts []discover.NodeID
    30  	for _, url := range params.MainnetBootnodes {
    31  		node, err := discover.ParseNode(url)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		accounts = append(accounts, node.ID)
    36  	}
    37  	return accounts, nil
    38  }
    39  
    40  func TestBlockCopy(t *testing.T) {
    41  	header := &types.Header{
    42  		Number: big.NewInt(1),
    43  	}
    44  	block1 := types.NewBlock(header, nil, nil, nil)
    45  
    46  	block2Obj := *block1
    47  
    48  	block2Obj.Header().Number = big.NewInt(2)
    49  
    50  	block2 := &block2Obj
    51  	block2.Header().Number = big.NewInt(3)
    52  
    53  	println(block1.Number().Uint64())
    54  	println(block2.Number().Uint64())
    55  
    56  }
    57  
    58  
    59  func printObject(title string, obj, logger interface{}){
    60  	objs, _ := json.Marshal(obj)
    61  	switch logger.(type) {
    62  	case *testing.T:
    63  		t := logger.(*testing.T)
    64  		t.Log(title, string(objs), "\n")
    65  	case *testing.B:
    66  		b := logger.(*testing.B)
    67  		b.Log(title, string(objs), "\n")
    68  	}
    69  }
    70  
    71  
    72  func buildPpos() (*ppos, *core.BlockChain) {
    73  	configs := params.PposConfig{
    74  		CandidateConfig: &params.CandidateConfig{
    75  			MaxChair: 1,
    76  			MaxCount: 3,
    77  			RefundBlockNumber: 	1,
    78  		},
    79  		TicketConfig: &params.TicketConfig {
    80  			MaxCount: 100,
    81  			ExpireBlockNumber: 2,
    82  		},
    83  	}
    84  	ppos := &ppos{
    85  		candidateContext:  pposm.NewCandidatePoolContext(&configs),
    86  		ticketContext: pposm.NewTicketPoolContext(&configs),
    87  	}
    88  
    89  	var (
    90  		db      = ethdb.NewMemDatabase()
    91  		genesis = new(core.Genesis).MustCommit(db)
    92  	)
    93  	fmt.Println("genesis", genesis)
    94  
    95  	// Initialize ppos storage
    96  	ppos_storage.NewPPosTemp(db)
    97  
    98  	// Initialize a fresh chain with only a genesis block
    99  	blockchain, _ := core.NewBlockChain(db, nil, params.AllEthashProtocolChanges, nil, vm.Config{}, nil)
   100  
   101  	ppos.SetCandidateContextOption(blockchain, buildInitialNodes())
   102  
   103  	return ppos, blockchain
   104  }
   105  
   106  func buildInitialNodes() []discover.Node {
   107  
   108  	nodes := make([]discover.Node, 0)
   109  	for i := 1; i <= 3; i++ {
   110  		ip := net.ParseIP("127.0.0.1")
   111  		// uint16
   112  		var port uint16
   113  		if portInt, err := strconv.Atoi("854" + fmt.Sprint(i)); nil != err {
   114  			return nil
   115  		} else {
   116  			port = uint16(portInt)
   117  		}
   118  		nodeId := discover.MustHexID("0x0123456789012134567890112345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234" + fmt.Sprint(i))
   119  		nodes = append(nodes, *(discover.NewNode(nodeId, ip, port, port)))
   120  	}
   121  	return nodes
   122  }
   123  
   124  func TestNewPpos (t *testing.T) {
   125  	ppos, _ := buildPpos()
   126  	printObject("ppos.candidatePoolText:", ppos.candidateContext, t)
   127  	printObject("ppos.ticketPool:", ppos.ticketContext, t)
   128  }
   129  
   130  // test BlockProducerIndex
   131  func ppos_BlockProducerIndex(logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   132  	ppos, bc := buildPpos()
   133  	curr := bc.CurrentBlock()
   134  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342")
   135  	num,_ := ppos.BlockProducerIndex(curr.Number(), curr.Hash(), curr.Number(), nodeId, 1)
   136  	logFn("BlockProducerIndex:", num)
   137  }
   138  func TestPpos_BlockProducerIndex(t *testing.T) {
   139  	ppos_BlockProducerIndex(t, t.Log, t.Error)
   140  }
   141  func BenchmarkPpos_BlockProducerIndex(b *testing.B) {
   142  	ppos_BlockProducerIndex(b, b.Log, b.Error)
   143  }
   144  
   145  /** about candidatepool */
   146  // test SetCandidate
   147  func ppos_SetCandidate (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   148  	ppos, bc := buildPpos()
   149  	var state *state.StateDB
   150  	if st, err := bc.State(); nil != err {
   151  		errFn("test SetCandidate getting state err", err)
   152  	}else {
   153  		state = st
   154  	}
   155  	logFn("test SetCandidate ...")
   156  
   157  
   158  	deposit, _ := new(big.Int).SetString("1000000000000000000000001", 10)
   159  	candidate := &types.Candidate{
   160  		Deposit:		deposit,
   161  		BlockNumber:    new(big.Int).SetUint64(7),
   162  		CandidateId:    discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   163  		TxIndex:  		6,
   164  		Host:  			"10.0.0.1",
   165  		Port:  			"8548",
   166  		Owner: 			common.HexToAddress("0x12"),
   167  
   168  	}
   169  	logFn("Set New Candidate ...")
   170  	/** test SetCandidate */
   171  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   172  		errFn("SetCandidate err:", err)
   173  	}else {
   174  		logFn("SetCandidate success ... ")
   175  	}
   176  }
   177  func TestPpos_SetCandidate(t *testing.T) {
   178  	ppos_SetCandidate(t, t.Log, t.Error)
   179  }
   180  func BenchmarkPpos_SetCandidate(b *testing.B) {
   181  	ppos_SetCandidate(b, b.Log, b.Error)
   182  }
   183  
   184  // test GetCandidate
   185  func ppos_GetCandidate (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   186  	ppos, bc := buildPpos()
   187  	var state *state.StateDB
   188  	if st, err := bc.State(); nil != err {
   189  		errFn("test GetCandidate getting state err", err)
   190  	}else {
   191  		state = st
   192  	}
   193  	logFn("test GetCandidate ...")
   194  
   195  
   196  	deposit, _ := new(big.Int).SetString("1000000000000000000000001", 10)
   197  	candidate := &types.Candidate{
   198  		Deposit: 		deposit,
   199  		BlockNumber:    new(big.Int).SetUint64(7),
   200  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   201  		TxIndex:  		6,
   202  		Host:  			"10.0.0.1",
   203  		Port:  			"8548",
   204  		Owner: 			common.HexToAddress("0x12"),
   205  
   206  	}
   207  	logFn("Set New Candidate ...")
   208  	/** test SetCandidate */
   209  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   210  		errFn("SetCandidate err:", err)
   211  	}else {
   212  		logFn("SetCandidate success ... ")
   213  	}
   214  
   215  	/** test GetCandidate ... */
   216  	if can := ppos.GetCandidate(state, candidate.CandidateId, big.NewInt(1)); nil == can {
   217  		errFn("GetCandidate err")
   218  	}else {
   219  		printObject("GetCandidate can:", can, logger)
   220  	}
   221  }
   222  func TestPpos_GetCandidate(t *testing.T) {
   223  	ppos_GetCandidate(t, t.Log, t.Error)
   224  }
   225  func BenchmarkPpos_GetCandidate(b *testing.B) {
   226  	ppos_GetCandidate(b, b.Log, b.Error)
   227  }
   228  
   229  // test GetCandidateArr
   230  func ppos_GetCandidateArr (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   231  	ppos, bc := buildPpos()
   232  	var state *state.StateDB
   233  	if st, err := bc.State(); nil != err {
   234  		errFn("test GetCandidateArr getting state err", err)
   235  	}else {
   236  		state = st
   237  	}
   238  	logFn("test GetCandidateArr ...")
   239  
   240  
   241  	deposit, _ := new(big.Int).SetString("1000000000000000000000001", 10)
   242  	candidate := &types.Candidate{
   243  		Deposit: 		deposit,
   244  		BlockNumber:    new(big.Int).SetUint64(7),
   245  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   246  		TxIndex:  		6,
   247  		Host:  			"10.0.0.1",
   248  		Port:  			"8548",
   249  		Owner: 			common.HexToAddress("0x12"),
   250  
   251  	}
   252  	logFn("Set New Candidate ...")
   253  	/** test SetCandidate */
   254  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   255  		errFn("SetCandidate err:", err)
   256  	}else {
   257  		logFn("SetCandidate success ... ")
   258  	}
   259  
   260  	logFn("Set New Candidate ...")
   261  	/** test SetCandidate */
   262  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   263  		errFn("SetCandidate err:", err)
   264  	}
   265  
   266  	deposit, _ = new(big.Int).SetString("1000000000000000000000001", 10)
   267  	candidate2 := &types.Candidate{
   268  		Deposit: 		deposit,
   269  		BlockNumber:    new(big.Int).SetUint64(7),
   270  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
   271  		TxIndex:  		5,
   272  		Host:  			"10.0.0.1",
   273  		Port:  			"8548",
   274  		Owner: 			common.HexToAddress("0x15"),
   275  
   276  	}
   277  	logFn("Set New Candidate ...")
   278  	/** test SetCandidate */
   279  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
   280  		errFn("SetCandidate err:", err)
   281  	}
   282  
   283  	/** test GetCandidate */
   284  	logFn("test GetCandidateArr ...")
   285  	canArr := ppos.GetCandidateArr(state, big.NewInt(1), []discover.NodeID{candidate.CandidateId, candidate2.CandidateId}...)
   286  	printObject("GetCandidateArr", canArr, logger)
   287  }
   288  func TestPpos_GetCandidateArr(t *testing.T) {
   289  	ppos_GetCandidateArr(t, t.Log, t.Error)
   290  }
   291  func BenchmarkPpos_GetCandidateArr(b *testing.B) {
   292  	ppos_GetCandidateArr(b, b.Log, b.Error)
   293  }
   294  
   295  // test Election
   296  func ppos_Election (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   297  	ppos, bc := buildPpos()
   298  	var state *state.StateDB
   299  	if st, err := bc.State(); nil != err {
   300  		errFn("test Election getting state err", err)
   301  	}else {
   302  		state = st
   303  	}
   304  	logFn("test Election ...")
   305  
   306  	// cache
   307  	cans := make([]*types.Candidate, 0)
   308  
   309  	deposit, _ := new(big.Int).SetString("1000000000000000000000001", 10)
   310  	candidate := &types.Candidate{
   311  		Deposit: 		deposit,
   312  		BlockNumber:    new(big.Int).SetUint64(7),
   313  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   314  		TxIndex:  		6,
   315  		Host:  			"10.0.0.1",
   316  		Port:  			"8548",
   317  		Owner: 			common.HexToAddress("0x12"),
   318  
   319  	}
   320  	logFn("Set New Candidate ...")
   321  	/** test SetCandidate */
   322  	cans = append(cans, candidate)
   323  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   324  		errFn("SetCandidate err:", err)
   325  	}
   326  
   327  	deposit, _ = new(big.Int).SetString("1000000000000000000000000", 10)
   328  	candidate2 := &types.Candidate{
   329  		Deposit: 		deposit,
   330  		BlockNumber:    new(big.Int).SetUint64(7),
   331  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
   332  		TxIndex:  		5,
   333  		Host:  			"10.0.0.1",
   334  		Port:  			"8548",
   335  		Owner: 			common.HexToAddress("0x15"),
   336  
   337  	}
   338  	logFn("Set New Candidate ...")
   339  	/** test SetCandidate */
   340  	cans = append(cans, candidate2)
   341  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
   342  		errFn("SetCandidate err:", err)
   343  	}
   344  
   345  	deposit, _ = new(big.Int).SetString("1000000000000000000000002", 10)
   346  	candidate3 := &types.Candidate{
   347  		Deposit: 		deposit,
   348  		BlockNumber:    new(big.Int).SetUint64(6),
   349  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
   350  		TxIndex:  		5,
   351  		Host:  			"10.0.0.1",
   352  		Port:  			"8548",
   353  		Owner: 			common.HexToAddress("0x15"),
   354  
   355  	}
   356  	logFn("Set New Candidate ...")
   357  	/** test SetCandidate */
   358  	cans = append(cans, candidate3)
   359  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
   360  		errFn("SetCandidate err:", err)
   361  	}
   362  
   363  	deposit, _ = new(big.Int).SetString("1000000000000000000000000", 10)
   364  	candidate4 := &types.Candidate{
   365  		Deposit: 		deposit, // 99
   366  		BlockNumber:    new(big.Int).SetUint64(6),
   367  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
   368  		TxIndex:  		4,
   369  		Host:  			"10.0.0.1",
   370  		Port:  			"8548",
   371  		Owner: 			common.HexToAddress("0x15"),
   372  
   373  	}
   374  	logFn("Set New Candidate ...")
   375  	/** test SetCandidate */
   376  	cans = append(cans, candidate4)
   377  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
   378  		errFn("SetCandidate err:", err)
   379  	}
   380  
   381  
   382  	/** vote ticket */
   383  	var count uint32 = 0
   384  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
   385  	var blockNumber = new(big.Int).SetUint64(10)
   386  	voteNum := 13
   387  	timeMap := make(map[uint32]int64)
   388  	fmt.Println("VOTING START .............................................................")
   389  	for i := 0; i < voteNum ; i++ {
   390  		can := cans[rand.Intn(4)]
   391  
   392  		startTime := time.Now().UnixNano() / 1e6
   393  		voteOwner := ownerList[rand.Intn(2)]
   394  		deposit := new(big.Int).SetUint64(10)
   395  		state.SubBalance(voteOwner, deposit)
   396  		state.AddBalance(common.TicketPoolAddr, deposit)
   397  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
   398  		if i < 2 {
   399  			tempBlockNumber.SetUint64(6)
   400  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
   401  		}
   402  
   403  		if i == 2 {
   404  			fmt.Println("release ticket,start ############################################################")
   405  			var tempBlockNumber uint64 = 6
   406  			for i := 0; i < 4; i++ {
   407  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
   408  				tempBlockNumber++
   409  			}
   410  			fmt.Println("release ticket,end ############################################################")
   411  		}
   412  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
   413  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
   414  		if nil != err {
   415  			fmt.Println("vote ticket error:", err)
   416  		}
   417  		atomic.AddUint32(&count, 1)
   418  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
   419  
   420  	}
   421  	fmt.Println("VOTING END .............................................................")
   422  
   423  	/** test Election */
   424  	logFn("test Election ...")
   425  	_, err := ppos.Election(state, common.Hash{}, big.NewInt(20))
   426  	logFn("Whether election was successful err", err)
   427  }
   428  /*
   429  func TestPpos_Election(t *testing.T) {
   430  	ppos_Election(t, t.Log, t.Error)
   431  }
   432  func BenchmarkPpos_Election(b *testing.B) {
   433  	ppos_Election(b, b.Log, b.Error)
   434  }*/
   435  
   436  // test Switch
   437  func ppos_Switch (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   438  	ppos, bc := buildPpos()
   439  	var state *state.StateDB
   440  	if st, err := bc.State(); nil != err {
   441  		errFn("test Switch getting state err", err)
   442  	}else {
   443  		state = st
   444  	}
   445  	logFn("test Switch ...")
   446  
   447  	// cache
   448  	cans := make([]*types.Candidate, 0)
   449  
   450  	deposit, _ := new(big.Int).SetString("1000000000000000000000000", 10)
   451  	candidate := &types.Candidate{
   452  		Deposit: 		deposit,
   453  		BlockNumber:    new(big.Int).SetUint64(7),
   454  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   455  		TxIndex:  		6,
   456  		Host:  			"10.0.0.1",
   457  		Port:  			"8548",
   458  		Owner: 			common.HexToAddress("0x12"),
   459  
   460  	}
   461  	logFn("Set New Candidate ...")
   462  	/** test SetCandidate */
   463  	cans = append(cans, candidate)
   464  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   465  		errFn("SetCandidate err:", err)
   466  	}
   467  
   468  	deposit, _ = new(big.Int).SetString("1000000000000000000000000", 10)
   469  	candidate2 := &types.Candidate{
   470  		Deposit: 		new(big.Int).SetUint64(99),
   471  		BlockNumber:    new(big.Int).SetUint64(7),
   472  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
   473  		TxIndex:  		5,
   474  		Host:  			"10.0.0.1",
   475  		Port:  			"8548",
   476  		Owner: 			common.HexToAddress("0x15"),
   477  
   478  	}
   479  	logFn("Set New Candidate ...")
   480  	/** test SetCandidate */
   481  	cans = append(cans, candidate2)
   482  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
   483  		errFn("SetCandidate err:", err)
   484  	}
   485  
   486  	candidate3 := &types.Candidate{
   487  		Deposit: 		new(big.Int).SetUint64(99),
   488  		BlockNumber:    new(big.Int).SetUint64(6),
   489  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
   490  		TxIndex:  		5,
   491  		Host:  			"10.0.0.1",
   492  		Port:  			"8548",
   493  		Owner: 			common.HexToAddress("0x15"),
   494  
   495  	}
   496  	logFn("Set New Candidate ...")
   497  	/** test SetCandidate */
   498  	cans = append(cans, candidate3)
   499  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
   500  		errFn("SetCandidate err:", err)
   501  	}
   502  
   503  	candidate4 := &types.Candidate{
   504  		Deposit: 		new(big.Int).SetUint64(120), // 99
   505  		BlockNumber:    new(big.Int).SetUint64(6),
   506  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
   507  		TxIndex:  		4,
   508  		Host:  			"10.0.0.1",
   509  		Port:  			"8548",
   510  		Owner: 			common.HexToAddress("0x15"),
   511  
   512  	}
   513  	logFn("Set New Candidate ...")
   514  	/** test SetCandidate */
   515  	cans = append(cans, candidate4)
   516  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
   517  		errFn("SetCandidate err:", err)
   518  	}
   519  
   520  
   521  	/** vote ticket */
   522  	var count uint32 = 0
   523  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
   524  	var blockNumber = new(big.Int).SetUint64(10)
   525  	voteNum := 13
   526  	timeMap := make(map[uint32]int64)
   527  	fmt.Println("VOTING START .............................................................")
   528  	for i := 0; i < voteNum ; i++ {
   529  		can := cans[rand.Intn(4)]
   530  
   531  		startTime := time.Now().UnixNano() / 1e6
   532  		voteOwner := ownerList[rand.Intn(2)]
   533  		deposit := new(big.Int).SetUint64(10)
   534  		state.SubBalance(voteOwner, deposit)
   535  		state.AddBalance(common.TicketPoolAddr, deposit)
   536  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
   537  		if i < 2 {
   538  			tempBlockNumber.SetUint64(6)
   539  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
   540  		}
   541  
   542  		if i == 2 {
   543  			fmt.Println("release ticket,start ############################################################")
   544  			var tempBlockNumber uint64 = 6
   545  			for i := 0; i < 4; i++ {
   546  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
   547  				tempBlockNumber++
   548  			}
   549  			fmt.Println("release ticket,end ############################################################")
   550  		}
   551  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
   552  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
   553  		if nil != err {
   554  			fmt.Println("vote ticket error:", err)
   555  		}
   556  		atomic.AddUint32(&count, 1)
   557  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
   558  
   559  	}
   560  	fmt.Println("VOTING END .............................................................")
   561  
   562  	/** test Election */
   563  	logFn("test Election ...")
   564  	_, err := ppos.Election(state, common.Hash{}, big.NewInt(20))
   565  	logFn("Whether election was successful err", err)
   566  
   567  	/** test GetWitness */
   568  	logFn("test GetWitness ...")
   569  	canArr, _ := ppos.GetWitness(state, 1, big.NewInt(1))
   570  	printObject("next Witnesses", canArr, logger)
   571  
   572  	/** test switch */
   573  	logFn("test Switch ...")
   574  	flag := ppos.Switch(state, big.NewInt(1))
   575  	logFn("Switch was success ", flag)
   576  }
   577  /*func TestPpos_Switch(t *testing.T) {
   578  	ppos_Switch(t, t.Log, t.Error)
   579  }
   580  func BenchmarkPpos_Switch(b *testing.B) {
   581  	ppos_Switch(b, b.Log, b.Error)
   582  }*/
   583  
   584  // test GetWitness
   585  func ppos_GetWitness (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   586  	ppos, bc := buildPpos()
   587  	var state *state.StateDB
   588  	if st, err := bc.State(); nil != err {
   589  		errFn("test GetWitness getting state err", err)
   590  	}else {
   591  		state = st
   592  	}
   593  	logFn("test GetWitness ...")
   594  
   595  	// cache
   596  	cans := make([]*types.Candidate, 0)
   597  
   598  	candidate := &types.Candidate{
   599  		Deposit: 		new(big.Int).SetUint64(100),
   600  		BlockNumber:    new(big.Int).SetUint64(7),
   601  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   602  		TxIndex:  		6,
   603  		Host:  			"10.0.0.1",
   604  		Port:  			"8548",
   605  		Owner: 			common.HexToAddress("0x12"),
   606  
   607  	}
   608  	logFn("Set New Candidate ...")
   609  	/** test SetCandidate */
   610  	cans = append(cans, candidate)
   611  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   612  		errFn("SetCandidate err:", err)
   613  	}
   614  
   615  	candidate2 := &types.Candidate{
   616  		Deposit: 		new(big.Int).SetUint64(99),
   617  		BlockNumber:    new(big.Int).SetUint64(7),
   618  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
   619  		TxIndex:  		5,
   620  		Host:  			"10.0.0.1",
   621  		Port:  			"8548",
   622  		Owner: 			common.HexToAddress("0x15"),
   623  
   624  	}
   625  	logFn("Set New Candidate ...")
   626  	/** test SetCandidate */
   627  	cans = append(cans, candidate2)
   628  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
   629  		errFn("SetCandidate err:", err)
   630  	}
   631  
   632  	candidate3 := &types.Candidate{
   633  		Deposit: 		new(big.Int).SetUint64(99),
   634  		BlockNumber:    new(big.Int).SetUint64(6),
   635  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
   636  		TxIndex:  		5,
   637  		Host:  			"10.0.0.1",
   638  		Port:  			"8548",
   639  		Owner: 			common.HexToAddress("0x15"),
   640  
   641  	}
   642  	logFn("Set New Candidate ...")
   643  	/** test SetCandidate */
   644  	cans = append(cans, candidate3)
   645  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
   646  		errFn("SetCandidate err:", err)
   647  	}
   648  
   649  	candidate4 := &types.Candidate{
   650  		Deposit: 		new(big.Int).SetUint64(120), // 99
   651  		BlockNumber:    new(big.Int).SetUint64(6),
   652  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
   653  		TxIndex:  		4,
   654  		Host:  			"10.0.0.1",
   655  		Port:  			"8548",
   656  		Owner: 			common.HexToAddress("0x15"),
   657  
   658  	}
   659  	logFn("Set New Candidate ...")
   660  	/** test SetCandidate */
   661  	cans = append(cans, candidate4)
   662  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
   663  		errFn("SetCandidate err:", err)
   664  	}
   665  
   666  
   667  	/** vote ticket */
   668  	var count uint32 = 0
   669  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
   670  	var blockNumber = new(big.Int).SetUint64(10)
   671  	voteNum := 13
   672  	timeMap := make(map[uint32]int64)
   673  	fmt.Println("VOTING START .............................................................")
   674  	for i := 0; i < voteNum ; i++ {
   675  		can := cans[rand.Intn(4)]
   676  
   677  		startTime := time.Now().UnixNano() / 1e6
   678  		voteOwner := ownerList[rand.Intn(2)]
   679  		deposit := new(big.Int).SetUint64(10)
   680  		state.SubBalance(voteOwner, deposit)
   681  		state.AddBalance(common.TicketPoolAddr, deposit)
   682  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
   683  		if i < 2 {
   684  			tempBlockNumber.SetUint64(6)
   685  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
   686  		}
   687  
   688  		if i == 2 {
   689  			fmt.Println("release ticket,start ############################################################")
   690  			var tempBlockNumber uint64 = 6
   691  			for i := 0; i < 4; i++ {
   692  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
   693  				tempBlockNumber++
   694  			}
   695  			fmt.Println("release ticket,end ############################################################")
   696  		}
   697  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
   698  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
   699  		if nil != err {
   700  			fmt.Println("vote ticket error:", err)
   701  		}
   702  		atomic.AddUint32(&count, 1)
   703  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
   704  
   705  	}
   706  	fmt.Println("VOTING END .............................................................")
   707  
   708  	/** test Election */
   709  	logFn("test Election ...")
   710  	_, err := ppos.Election(state, common.Hash{}, big.NewInt(20))
   711  	logFn("Whether election was successful err", err)
   712  
   713  	/** test GetWitness */
   714  	logFn("test GetWitness ...")
   715  	canArr, _ := ppos.GetWitness(state, 1, big.NewInt(1))
   716  	printObject("next Witnesses", canArr, logger)
   717  
   718  	/** test switch */
   719  	logFn("test Switch ...")
   720  	flag := ppos.Switch(state, big.NewInt(1))
   721  	logFn("Switch was success ", flag)
   722  
   723  	/** test GetWitness */
   724  	logFn("test GetWitness ...")
   725  	canArr, _ = ppos.GetWitness(state, 0, big.NewInt(1))
   726  	printObject(" current Witnesses", canArr, logger)
   727  }
   728  /*func TestPpos_GetWitness(t *testing.T) {
   729  	ppos_GetWitness(t, t.Log, t.Error)
   730  }
   731  func BenchmarkPpos_GetWitness(b *testing.B) {
   732  	ppos_GetWitness(b, b.Log, b.Error)
   733  }*/
   734  
   735  // test GetAllWitness
   736  func ppos_GetAllWitness (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   737  	ppos, bc := buildPpos()
   738  	var state *state.StateDB
   739  	if st, err := bc.State(); nil != err {
   740  		errFn("test GetAllWitness getting state err", err)
   741  	}else {
   742  		state = st
   743  	}
   744  	logFn("test GetAllWitness ...")
   745  
   746  	// cache
   747  	cans := make([]*types.Candidate, 0)
   748  
   749  	candidate := &types.Candidate{
   750  		Deposit: 		new(big.Int).SetUint64(100),
   751  		BlockNumber:    new(big.Int).SetUint64(7),
   752  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   753  		TxIndex:  		6,
   754  		Host:  			"10.0.0.1",
   755  		Port:  			"8548",
   756  		Owner: 			common.HexToAddress("0x12"),
   757  
   758  	}
   759  	logFn("Set New Candidate ...")
   760  	/** test SetCandidate */
   761  	cans = append(cans, candidate)
   762  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   763  		errFn("SetCandidate err:", err)
   764  	}
   765  
   766  	candidate2 := &types.Candidate{
   767  		Deposit: 		new(big.Int).SetUint64(99),
   768  		BlockNumber:    new(big.Int).SetUint64(7),
   769  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
   770  		TxIndex:  		5,
   771  		Host:  			"10.0.0.1",
   772  		Port:  			"8548",
   773  		Owner: 			common.HexToAddress("0x15"),
   774  
   775  	}
   776  	logFn("Set New Candidate ...")
   777  	/** test SetCandidate */
   778  	cans = append(cans, candidate2)
   779  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
   780  		errFn("SetCandidate err:", err)
   781  	}
   782  
   783  	candidate3 := &types.Candidate{
   784  		Deposit: 		new(big.Int).SetUint64(99),
   785  		BlockNumber:    new(big.Int).SetUint64(6),
   786  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
   787  		TxIndex:  		5,
   788  		Host:  			"10.0.0.1",
   789  		Port:  			"8548",
   790  		Owner: 			common.HexToAddress("0x15"),
   791  
   792  	}
   793  	logFn("Set New Candidate ...")
   794  	/** test SetCandidate */
   795  	cans = append(cans, candidate3)
   796  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
   797  		errFn("SetCandidate err:", err)
   798  	}
   799  
   800  	candidate4 := &types.Candidate{
   801  		Deposit: 		new(big.Int).SetUint64(120), // 99
   802  		BlockNumber:    new(big.Int).SetUint64(6),
   803  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
   804  		TxIndex:  		4,
   805  		Host:  			"10.0.0.1",
   806  		Port:  			"8548",
   807  		Owner: 			common.HexToAddress("0x15"),
   808  
   809  	}
   810  	logFn("Set New Candidate ...")
   811  	/** test SetCandidate */
   812  	cans = append(cans, candidate4)
   813  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
   814  		errFn("SetCandidate err:", err)
   815  	}
   816  
   817  
   818  	/** vote ticket */
   819  	var count uint32 = 0
   820  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
   821  	var blockNumber = new(big.Int).SetUint64(10)
   822  	voteNum := 13
   823  	timeMap := make(map[uint32]int64)
   824  	fmt.Println("VOTING START .............................................................")
   825  	for i := 0; i < voteNum ; i++ {
   826  		can := cans[rand.Intn(4)]
   827  
   828  		startTime := time.Now().UnixNano() / 1e6
   829  		voteOwner := ownerList[rand.Intn(2)]
   830  		deposit := new(big.Int).SetUint64(10)
   831  		state.SubBalance(voteOwner, deposit)
   832  		state.AddBalance(common.TicketPoolAddr, deposit)
   833  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
   834  		if i < 2 {
   835  			tempBlockNumber.SetUint64(6)
   836  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
   837  		}
   838  
   839  		if i == 2 {
   840  			fmt.Println("release ticket,start ############################################################")
   841  			var tempBlockNumber uint64 = 6
   842  			for i := 0; i < 4; i++ {
   843  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
   844  				tempBlockNumber++
   845  			}
   846  			fmt.Println("release ticket,end ############################################################")
   847  		}
   848  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
   849  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
   850  		if nil != err {
   851  			fmt.Println("vote ticket error:", err)
   852  		}
   853  		atomic.AddUint32(&count, 1)
   854  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
   855  
   856  	}
   857  	fmt.Println("VOTING END .............................................................")
   858  
   859  	/** test Election */
   860  	logFn("test Election ...")
   861  	_, err := ppos.Election(state, common.Hash{}, big.NewInt(20))
   862  	logFn("Whether election was successful err", err)
   863  
   864  	/** test GetWitness */
   865  	logFn("test GetWitness ...")
   866  	canArr, _ := ppos.GetWitness(state, 1, big.NewInt(1))
   867  	printObject("next Witnesses", canArr, logger)
   868  
   869  	/** test switch */
   870  	logFn("test Switch ...")
   871  	flag := ppos.Switch(state, big.NewInt(1))
   872  	logFn("Switch was success ", flag)
   873  
   874  	/** test Election again */
   875  	logFn("test Election again ...")
   876  	_, err = ppos.Election(state, common.Hash{}, big.NewInt(20))
   877  	logFn("Whether election again was successful err", err)
   878  
   879  	/** test GetAllWitness */
   880  	logFn("test GetAllWitness ...")
   881  	preArr, canArr, nextArr, _ := ppos.GetAllWitness(state, big.NewInt(1))
   882  	printObject(" previous Witnesses", preArr, logger)
   883  	printObject(" current Witnesses", canArr, logger)
   884  	printObject(" next Witnesses", nextArr, logger)
   885  }
   886  /*func TestPpos_GetAllWitness(t *testing.T) {
   887  	ppos_GetAllWitness(t, t.Log, t.Error)
   888  }
   889  func BenchmarkPpos_GetAllWitness(b *testing.B) {
   890  	ppos_GetAllWitness(b, b.Log, b.Error)
   891  }
   892  */
   893  // test WithdrawCandidate
   894  func ppos_WithdrawCandidate (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   895  	ppos, bc := buildPpos()
   896  	var state *state.StateDB
   897  	if st, err := bc.State(); nil != err {
   898  		errFn("test WithdrawCandidate getting state err", err)
   899  	}else {
   900  		state = st
   901  	}
   902  	logFn("test WithdrawCandidate ...")
   903  
   904  
   905  	candidate := &types.Candidate{
   906  		Deposit: 		new(big.Int).SetUint64(100),
   907  		BlockNumber:    new(big.Int).SetUint64(7),
   908  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
   909  		TxIndex:  		6,
   910  		Host:  			"10.0.0.1",
   911  		Port:  			"8548",
   912  		Owner: 			common.HexToAddress("0x12"),
   913  
   914  	}
   915  	logFn("Set New Candidate ...")
   916  	/** test SetCandidate */
   917  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
   918  		errFn("SetCandidate err:", err)
   919  	}
   920  
   921  	candidate2 := &types.Candidate{
   922  		Deposit: 		new(big.Int).SetUint64(99),
   923  		BlockNumber:    new(big.Int).SetUint64(7),
   924  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
   925  		TxIndex:  		5,
   926  		Host:  			"10.0.0.1",
   927  		Port:  			"8548",
   928  		Owner: 			common.HexToAddress("0x15"),
   929  
   930  	}
   931  	logFn("Set New Candidate ...")
   932  	/** test SetCandidate */
   933  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
   934  		errFn("SetCandidate err:", err)
   935  	}
   936  
   937  	candidate3 := &types.Candidate{
   938  		Deposit: 		new(big.Int).SetUint64(99),
   939  		BlockNumber:    new(big.Int).SetUint64(6),
   940  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
   941  		TxIndex:  		5,
   942  		Host:  			"10.0.0.1",
   943  		Port:  			"8548",
   944  		Owner: 			common.HexToAddress("0x15"),
   945  
   946  	}
   947  	logFn("Set New Candidate ...")
   948  	/** test SetCandidate */
   949  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
   950  		errFn("SetCandidate err:", err)
   951  	}
   952  
   953  	candidate4 := &types.Candidate{
   954  		Deposit: 		new(big.Int).SetUint64(120), // 99
   955  		BlockNumber:    new(big.Int).SetUint64(6),
   956  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
   957  		TxIndex:  		4,
   958  		Host:  			"10.0.0.1",
   959  		Port:  			"8548",
   960  		Owner: 			common.HexToAddress("0x15"),
   961  
   962  	}
   963  	logFn("Set New Candidate ...")
   964  	/** test SetCandidate */
   965  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
   966  		errFn("SetCandidate err:", err)
   967  	}
   968  
   969  
   970  	/** test GetCandidate */
   971  	logFn("test GetCandidate ...")
   972  	can := ppos.GetCandidate(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), big.NewInt(1))
   973  	printObject("GetCandidate", can, logger)
   974  
   975  	/** test WithdrawCandidate */
   976  	logFn("test WithdrawCandidate ...")
   977  	ok1 := ppos.WithdrawCandidate(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), new(big.Int).SetUint64(uint64(99)), new(big.Int).SetUint64(uint64(10)))
   978  	logFn("error", ok1)
   979  
   980  	/** test GetCandidate */
   981  	logFn("test GetCandidate ...")
   982  	can2 := ppos.GetCandidate(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), big.NewInt(1))
   983  	printObject("GetCandidate", can2, logger)
   984  
   985  }
   986  /*func TestPpos_WithdrawCandidate(t *testing.T) {
   987  	ppos_WithdrawCandidate(t, t.Log, t.Error)
   988  }
   989  func BenchmarkPpos_WithdrawCandidate(b *testing.B) {
   990  	ppos_WithdrawCandidate(b, b.Log, b.Error)
   991  }*/
   992  
   993  // test GetChosens
   994  func ppos_GetChosens (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
   995  	ppos, bc := buildPpos()
   996  	var state *state.StateDB
   997  	if st, err := bc.State(); nil != err {
   998  		errFn("test GetChosens getting state err", err)
   999  	}else {
  1000  		state = st
  1001  	}
  1002  	logFn("test GetChosens ...")
  1003  
  1004  	candidate := &types.Candidate{
  1005  		Deposit: 		new(big.Int).SetUint64(100),
  1006  		BlockNumber:    new(big.Int).SetUint64(7),
  1007  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1008  		TxIndex:  		6,
  1009  		Host:  			"10.0.0.1",
  1010  		Port:  			"8548",
  1011  		Owner: 			common.HexToAddress("0x12"),
  1012  
  1013  	}
  1014  	logFn("Set New Candidate ...")
  1015  	/** test SetCandidate */
  1016  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1017  		errFn("SetCandidate err:", err)
  1018  	}
  1019  
  1020  	candidate2 := &types.Candidate{
  1021  		Deposit: 		new(big.Int).SetUint64(99),
  1022  		BlockNumber:    new(big.Int).SetUint64(7),
  1023  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
  1024  		TxIndex:  		5,
  1025  		Host:  			"10.0.0.1",
  1026  		Port:  			"8548",
  1027  		Owner: 			common.HexToAddress("0x15"),
  1028  
  1029  	}
  1030  	logFn("Set New Candidate ...")
  1031  	/** test SetCandidate */
  1032  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
  1033  		errFn("SetCandidate err:", err)
  1034  	}
  1035  
  1036  	/** test GetChosens */
  1037  	logFn("test GetChosens ...")
  1038  	canArr := ppos.GetChosens(state, 0, big.NewInt(1))
  1039  	printObject("elected candidates", canArr, logger)
  1040  }
  1041  /*func TestPpos_GetChosens(t *testing.T) {
  1042  	ppos_GetChosens(t, t.Log, t.Error)
  1043  }
  1044  func BenchmarkPpos_GetChosens(b *testing.B) {
  1045  	ppos_GetChosens(b, b.Log, b.Error)
  1046  }*/
  1047  
  1048  // test GetChairpersons
  1049  func ppos_GetChairpersons (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  1050  	ppos, bc := buildPpos()
  1051  	var state *state.StateDB
  1052  	if st, err := bc.State(); nil != err {
  1053  		errFn("test GetChairpersons getting state err", err)
  1054  	}else {
  1055  		state = st
  1056  	}
  1057  	logFn("test GetChairpersons ...")
  1058  
  1059  	// cache
  1060  	cans := make([]*types.Candidate, 0)
  1061  
  1062  	candidate := &types.Candidate{
  1063  		Deposit: 		new(big.Int).SetUint64(100),
  1064  		BlockNumber:    new(big.Int).SetUint64(7),
  1065  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1066  		TxIndex:  		6,
  1067  		Host:  			"10.0.0.1",
  1068  		Port:  			"8548",
  1069  		Owner: 			common.HexToAddress("0x12"),
  1070  
  1071  	}
  1072  	logFn("Set New Candidate ...")
  1073  	/** test SetCandidate */
  1074  	cans = append(cans, candidate)
  1075  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1076  		errFn("SetCandidate err:", err)
  1077  	}
  1078  
  1079  	candidate2 := &types.Candidate{
  1080  		Deposit: 		new(big.Int).SetUint64(99),
  1081  		BlockNumber:    new(big.Int).SetUint64(7),
  1082  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
  1083  		TxIndex:  		5,
  1084  		Host:  			"10.0.0.1",
  1085  		Port:  			"8548",
  1086  		Owner: 			common.HexToAddress("0x15"),
  1087  
  1088  	}
  1089  	logFn("Set New Candidate ...")
  1090  	/** test SetCandidate */
  1091  	cans = append(cans, candidate2)
  1092  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
  1093  		errFn("SetCandidate err:", err)
  1094  	}
  1095  
  1096  	candidate3 := &types.Candidate{
  1097  		Deposit: 		new(big.Int).SetUint64(99),
  1098  		BlockNumber:    new(big.Int).SetUint64(6),
  1099  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
  1100  		TxIndex:  		5,
  1101  		Host:  			"10.0.0.1",
  1102  		Port:  			"8548",
  1103  		Owner: 			common.HexToAddress("0x15"),
  1104  
  1105  	}
  1106  	logFn("Set New Candidate ...")
  1107  	/** test SetCandidate */
  1108  	cans = append(cans, candidate3)
  1109  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
  1110  		errFn("SetCandidate err:", err)
  1111  	}
  1112  
  1113  	candidate4 := &types.Candidate{
  1114  		Deposit: 		new(big.Int).SetUint64(120), // 99
  1115  		BlockNumber:    new(big.Int).SetUint64(6),
  1116  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
  1117  		TxIndex:  		4,
  1118  		Host:  			"10.0.0.1",
  1119  		Port:  			"8548",
  1120  		Owner: 			common.HexToAddress("0x15"),
  1121  
  1122  	}
  1123  	logFn("Set New Candidate ...")
  1124  	/** test SetCandidate */
  1125  	cans = append(cans, candidate4)
  1126  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
  1127  		errFn("SetCandidate err:", err)
  1128  	}
  1129  
  1130  
  1131  	/** vote ticket */
  1132  	var count uint32 = 0
  1133  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
  1134  	var blockNumber = new(big.Int).SetUint64(10)
  1135  	voteNum := 13
  1136  	timeMap := make(map[uint32]int64)
  1137  	fmt.Println("VOTING START .............................................................")
  1138  	for i := 0; i < voteNum ; i++ {
  1139  		can := cans[rand.Intn(4)]
  1140  
  1141  		startTime := time.Now().UnixNano() / 1e6
  1142  		voteOwner := ownerList[rand.Intn(2)]
  1143  		deposit := new(big.Int).SetUint64(10)
  1144  		state.SubBalance(voteOwner, deposit)
  1145  		state.AddBalance(common.TicketPoolAddr, deposit)
  1146  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1147  		if i < 2 {
  1148  			tempBlockNumber.SetUint64(6)
  1149  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
  1150  		}
  1151  
  1152  		if i == 2 {
  1153  			fmt.Println("release ticket,start ############################################################")
  1154  			var tempBlockNumber uint64 = 6
  1155  			for i := 0; i < 4; i++ {
  1156  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
  1157  				tempBlockNumber++
  1158  			}
  1159  			fmt.Println("release ticket,end ############################################################")
  1160  		}
  1161  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1162  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
  1163  		if nil != err {
  1164  			fmt.Println("vote ticket error:", err)
  1165  		}
  1166  		atomic.AddUint32(&count, 1)
  1167  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1168  
  1169  	}
  1170  	fmt.Println("VOTING END .............................................................")
  1171  
  1172  	/** test Election */
  1173  	logFn("test Election ...")
  1174  	_, err := ppos.Election(state, common.Hash{}, big.NewInt(20))
  1175  	logFn("Whether election was successful err", err)
  1176  
  1177  	/** test GetWitness */
  1178  	logFn("test GetWitness ...")
  1179  	nodeIdArr, _ := ppos.GetWitness(state, 1, big.NewInt(1))
  1180  	printObject("next Witnesses", nodeIdArr, logger)
  1181  
  1182  	/** test switch */
  1183  	logFn("test Switch ...")
  1184  	flag := ppos.Switch(state, big.NewInt(1))
  1185  	logFn("Switch was success ", flag)
  1186  
  1187  	/** test GetChairpersons */
  1188  	logFn("test GetChairpersons ...")
  1189  	canArr := ppos.GetChairpersons(state, big.NewInt(1))
  1190  	printObject("GetChairpersons canArr:", canArr, logger)
  1191  }
  1192  /*func TestPpos_GetChairpersons(t *testing.T) {
  1193  	ppos_GetChairpersons(t, t.Log, t.Error)
  1194  }
  1195  func BenchmarkPpos_GetChairpersons(b *testing.B) {
  1196  	ppos_GetChairpersons(b, b.Log, b.Error)
  1197  }*/
  1198  
  1199  // test GetDefeat
  1200  func ppos_GetDefeat (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  1201  	ppos, bc := buildPpos()
  1202  	var state *state.StateDB
  1203  	if st, err := bc.State(); nil != err {
  1204  		errFn("test GetDefeat getting state err", err)
  1205  	}else {
  1206  		state = st
  1207  	}
  1208  	logFn("test GetDefeat ...")
  1209  
  1210  	// cache
  1211  	cans := make([]*types.Candidate, 0)
  1212  
  1213  	candidate := &types.Candidate{
  1214  		Deposit: 		new(big.Int).SetUint64(100),
  1215  		BlockNumber:    new(big.Int).SetUint64(7),
  1216  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1217  		TxIndex:  		6,
  1218  		Host:  			"10.0.0.1",
  1219  		Port:  			"8548",
  1220  		Owner: 			common.HexToAddress("0x12"),
  1221  
  1222  	}
  1223  	logFn("Set New Candidate ...")
  1224  	/** test SetCandidate */
  1225  	cans = append(cans, candidate)
  1226  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1227  		errFn("SetCandidate err:", err)
  1228  	}
  1229  
  1230  	candidate2 := &types.Candidate{
  1231  		Deposit: 		new(big.Int).SetUint64(99),
  1232  		BlockNumber:    new(big.Int).SetUint64(7),
  1233  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
  1234  		TxIndex:  		5,
  1235  		Host:  			"10.0.0.1",
  1236  		Port:  			"8548",
  1237  		Owner: 			common.HexToAddress("0x15"),
  1238  
  1239  	}
  1240  	logFn("Set New Candidate ...")
  1241  	/** test SetCandidate */
  1242  	cans = append(cans, candidate2)
  1243  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
  1244  		errFn("SetCandidate err:", err)
  1245  	}
  1246  
  1247  	candidate3 := &types.Candidate{
  1248  		Deposit: 		new(big.Int).SetUint64(99),
  1249  		BlockNumber:    new(big.Int).SetUint64(6),
  1250  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
  1251  		TxIndex:  		5,
  1252  		Host:  			"10.0.0.1",
  1253  		Port:  			"8548",
  1254  		Owner: 			common.HexToAddress("0x15"),
  1255  
  1256  	}
  1257  	logFn("Set New Candidate ...")
  1258  	/** test SetCandidate */
  1259  	cans = append(cans, candidate3)
  1260  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
  1261  		errFn("SetCandidate err:", err)
  1262  	}
  1263  
  1264  	candidate4 := &types.Candidate{
  1265  		Deposit: 		new(big.Int).SetUint64(120), // 99
  1266  		BlockNumber:    new(big.Int).SetUint64(6),
  1267  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
  1268  		TxIndex:  		4,
  1269  		Host:  			"10.0.0.1",
  1270  		Port:  			"8548",
  1271  		Owner: 			common.HexToAddress("0x15"),
  1272  
  1273  	}
  1274  	logFn("Set New Candidate ...")
  1275  	/** test SetCandidate */
  1276  	cans = append(cans, candidate4)
  1277  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
  1278  		errFn("SetCandidate err:", err)
  1279  	}
  1280  
  1281  
  1282  	/** vote ticket */
  1283  	var count uint32 = 0
  1284  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
  1285  	var blockNumber = new(big.Int).SetUint64(10)
  1286  	voteNum := 13
  1287  	timeMap := make(map[uint32]int64)
  1288  	fmt.Println("VOTING START .............................................................")
  1289  	for i := 0; i < voteNum ; i++ {
  1290  		can := cans[rand.Intn(4)]
  1291  
  1292  		startTime := time.Now().UnixNano() / 1e6
  1293  		voteOwner := ownerList[rand.Intn(2)]
  1294  		deposit := new(big.Int).SetUint64(10)
  1295  		state.SubBalance(voteOwner, deposit)
  1296  		state.AddBalance(common.TicketPoolAddr, deposit)
  1297  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1298  		if i < 2 {
  1299  			tempBlockNumber.SetUint64(6)
  1300  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
  1301  		}
  1302  
  1303  		if i == 2 {
  1304  			fmt.Println("release ticket,start ############################################################")
  1305  			var tempBlockNumber uint64 = 6
  1306  			for i := 0; i < 4; i++ {
  1307  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
  1308  				tempBlockNumber++
  1309  			}
  1310  			fmt.Println("release ticket,end ############################################################")
  1311  		}
  1312  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1313  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
  1314  		if nil != err {
  1315  			fmt.Println("vote ticket error:", err)
  1316  		}
  1317  		atomic.AddUint32(&count, 1)
  1318  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1319  
  1320  	}
  1321  	fmt.Println("VOTING END .............................................................")
  1322  
  1323  
  1324  
  1325  	/** test WithdrawCandidate */
  1326  	logFn("test WithdrawCandidate ...")
  1327  	ok1 := ppos.WithdrawCandidate(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), new(big.Int).SetUint64(uint64(99)), new(big.Int).SetUint64(uint64(10)))
  1328  	logFn("error", ok1)
  1329  
  1330  
  1331  	/** test IsDefeat */
  1332  	logFn("test IsDefeat ...")
  1333  	flag := ppos.IsDefeat(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), big.NewInt(1))
  1334  	logFn("isdefeat", flag)
  1335  
  1336  	/** test GetDefeat */
  1337  	logFn("test GetDefeat ...")
  1338  	defeatArr := ppos.GetDefeat(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), big.NewInt(1))
  1339  	printObject("can be refund defeats", defeatArr, logger)
  1340  }
  1341  /*func TestPpos_GetDefeat(t *testing.T) {
  1342  	ppos_GetDefeat(t, t.Log, t.Error)
  1343  }
  1344  func BenchmarkPpos_GetDefeat(b *testing.B) {
  1345  	ppos_GetDefeat(b, b.Log, b.Error)
  1346  }
  1347  */
  1348  // test IsDefeat
  1349  func ppos_IsDefeat (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  1350  	ppos, bc := buildPpos()
  1351  	var state *state.StateDB
  1352  	if st, err := bc.State(); nil != err {
  1353  		errFn("test IsDefeat getting state err", err)
  1354  	}else {
  1355  		state = st
  1356  	}
  1357  	logFn("test IsDefeat ...")
  1358  
  1359  	// cache
  1360  	cans := make([]*types.Candidate, 0)
  1361  
  1362  	candidate := &types.Candidate{
  1363  		Deposit: 		new(big.Int).SetUint64(100),
  1364  		BlockNumber:    new(big.Int).SetUint64(7),
  1365  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1366  		TxIndex:  		6,
  1367  		Host:  			"10.0.0.1",
  1368  		Port:  			"8548",
  1369  		Owner: 			common.HexToAddress("0x12"),
  1370  
  1371  	}
  1372  	logFn("Set New Candidate ...")
  1373  	/** test SetCandidate */
  1374  	cans = append(cans, candidate)
  1375  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1376  		errFn("SetCandidate err:", err)
  1377  	}
  1378  
  1379  	candidate2 := &types.Candidate{
  1380  		Deposit: 		new(big.Int).SetUint64(99),
  1381  		BlockNumber:    new(big.Int).SetUint64(7),
  1382  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
  1383  		TxIndex:  		5,
  1384  		Host:  			"10.0.0.1",
  1385  		Port:  			"8548",
  1386  		Owner: 			common.HexToAddress("0x15"),
  1387  
  1388  	}
  1389  	logFn("Set New Candidate ...")
  1390  	/** test SetCandidate */
  1391  	cans = append(cans, candidate2)
  1392  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
  1393  		errFn("SetCandidate err:", err)
  1394  	}
  1395  
  1396  	candidate3 := &types.Candidate{
  1397  		Deposit: 		new(big.Int).SetUint64(99),
  1398  		BlockNumber:    new(big.Int).SetUint64(6),
  1399  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
  1400  		TxIndex:  		5,
  1401  		Host:  			"10.0.0.1",
  1402  		Port:  			"8548",
  1403  		Owner: 			common.HexToAddress("0x15"),
  1404  
  1405  	}
  1406  	logFn("Set New Candidate ...")
  1407  	/** test SetCandidate */
  1408  	cans = append(cans, candidate3)
  1409  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
  1410  		errFn("SetCandidate err:", err)
  1411  	}
  1412  
  1413  	candidate4 := &types.Candidate{
  1414  		Deposit: 		new(big.Int).SetUint64(120), // 99
  1415  		BlockNumber:    new(big.Int).SetUint64(6),
  1416  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
  1417  		TxIndex:  		4,
  1418  		Host:  			"10.0.0.1",
  1419  		Port:  			"8548",
  1420  		Owner: 			common.HexToAddress("0x15"),
  1421  
  1422  	}
  1423  	logFn("Set New Candidate ...")
  1424  	/** test SetCandidate */
  1425  	cans = append(cans, candidate4)
  1426  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
  1427  		errFn("SetCandidate err:", err)
  1428  	}
  1429  
  1430  
  1431  	/** vote ticket */
  1432  	var count uint32 = 0
  1433  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
  1434  	var blockNumber = new(big.Int).SetUint64(10)
  1435  	voteNum := 13
  1436  	timeMap := make(map[uint32]int64)
  1437  	fmt.Println("VOTING START .............................................................")
  1438  	for i := 0; i < voteNum ; i++ {
  1439  		can := cans[rand.Intn(4)]
  1440  
  1441  		startTime := time.Now().UnixNano() / 1e6
  1442  		voteOwner := ownerList[rand.Intn(2)]
  1443  		deposit := new(big.Int).SetUint64(10)
  1444  		state.SubBalance(voteOwner, deposit)
  1445  		state.AddBalance(common.TicketPoolAddr, deposit)
  1446  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1447  		if i < 2 {
  1448  			tempBlockNumber.SetUint64(6)
  1449  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
  1450  		}
  1451  
  1452  		if i == 2 {
  1453  			fmt.Println("release ticket,start ############################################################")
  1454  			var tempBlockNumber uint64 = 6
  1455  			for i := 0; i < 4; i++ {
  1456  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
  1457  				tempBlockNumber++
  1458  			}
  1459  			fmt.Println("release ticket,end ############################################################")
  1460  		}
  1461  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1462  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
  1463  		if nil != err {
  1464  			fmt.Println("vote ticket error:", err)
  1465  		}
  1466  		atomic.AddUint32(&count, 1)
  1467  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1468  
  1469  	}
  1470  	fmt.Println("VOTING END .............................................................")
  1471  
  1472  
  1473  
  1474  	/** test WithdrawCandidate */
  1475  	logFn("test WithdrawCandidate ...")
  1476  	ok1 := ppos.WithdrawCandidate(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), new(big.Int).SetUint64(uint64(99)), new(big.Int).SetUint64(uint64(10)))
  1477  	logFn("error", ok1)
  1478  
  1479  
  1480  	/** test IsDefeat */
  1481  	logFn("test IsDefeat ...")
  1482  	flag := ppos.IsDefeat(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), big.NewInt(1))
  1483  	logFn("isdefeat", flag)
  1484  }
  1485  /*func TestPpos_IsDefeat(t *testing.T) {
  1486  	ppos_IsDefeat(t, t.Log, t.Error)
  1487  }
  1488  func BenchmarkPpos_IsDefeat(b *testing.B) {
  1489  	ppos_IsDefeat(b, b.Log, b.Error)
  1490  }*/
  1491  
  1492  // test RefundBalance
  1493  func ppos_RefundBalance (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  1494  	ppos, bc := buildPpos()
  1495  	var state *state.StateDB
  1496  	if st, err := bc.State(); nil != err {
  1497  		errFn("test RefundBalance getting state err", err)
  1498  	}else {
  1499  		state = st
  1500  	}
  1501  	logFn("test RefundBalance ...")
  1502  
  1503  	// cache
  1504  	cans := make([]*types.Candidate, 0)
  1505  
  1506  	candidate := &types.Candidate{
  1507  		Deposit: 		new(big.Int).SetUint64(100),
  1508  		BlockNumber:    new(big.Int).SetUint64(7),
  1509  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1510  		TxIndex:  		6,
  1511  		Host:  			"10.0.0.1",
  1512  		Port:  			"8548",
  1513  		Owner: 			common.HexToAddress("0x12"),
  1514  
  1515  	}
  1516  	logFn("Set New Candidate ...")
  1517  	/** test SetCandidate */
  1518  	cans = append(cans, candidate)
  1519  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1520  		errFn("SetCandidate err:", err)
  1521  	}
  1522  
  1523  	candidate2 := &types.Candidate{
  1524  		Deposit: 		new(big.Int).SetUint64(99),
  1525  		BlockNumber:    new(big.Int).SetUint64(7),
  1526  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012341"),
  1527  		TxIndex:  		5,
  1528  		Host:  			"10.0.0.1",
  1529  		Port:  			"8548",
  1530  		Owner: 			common.HexToAddress("0x15"),
  1531  
  1532  	}
  1533  	logFn("Set New Candidate ...")
  1534  	/** test SetCandidate */
  1535  	cans = append(cans, candidate2)
  1536  	if err := ppos.SetCandidate(state, candidate2.CandidateId, candidate2); nil != err {
  1537  		errFn("SetCandidate err:", err)
  1538  	}
  1539  
  1540  	candidate3 := &types.Candidate{
  1541  		Deposit: 		new(big.Int).SetUint64(99),
  1542  		BlockNumber:    new(big.Int).SetUint64(6),
  1543  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012342"),
  1544  		TxIndex:  		5,
  1545  		Host:  			"10.0.0.1",
  1546  		Port:  			"8548",
  1547  		Owner: 			common.HexToAddress("0x15"),
  1548  
  1549  	}
  1550  	logFn("Set New Candidate ...")
  1551  	/** test SetCandidate */
  1552  	cans = append(cans, candidate3)
  1553  	if err := ppos.SetCandidate(state, candidate3.CandidateId, candidate3); nil != err {
  1554  		errFn("SetCandidate err:", err)
  1555  	}
  1556  
  1557  	candidate4 := &types.Candidate{
  1558  		Deposit: 		new(big.Int).SetUint64(120), // 99
  1559  		BlockNumber:    new(big.Int).SetUint64(6),
  1560  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012343"),
  1561  		TxIndex:  		4,
  1562  		Host:  			"10.0.0.1",
  1563  		Port:  			"8548",
  1564  		Owner: 			common.HexToAddress("0x15"),
  1565  
  1566  	}
  1567  	logFn("Set New Candidate ...")
  1568  	/** test SetCandidate */
  1569  	cans = append(cans, candidate4)
  1570  	if err := ppos.SetCandidate(state, candidate4.CandidateId, candidate4); nil != err {
  1571  		errFn("SetCandidate err:", err)
  1572  	}
  1573  
  1574  
  1575  	/** vote ticket */
  1576  	var count uint32 = 0
  1577  	ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")}
  1578  	var blockNumber = new(big.Int).SetUint64(10)
  1579  	voteNum := 13
  1580  	timeMap := make(map[uint32]int64)
  1581  	fmt.Println("VOTING START .............................................................")
  1582  	for i := 0; i < voteNum ; i++ {
  1583  		can := cans[rand.Intn(4)]
  1584  
  1585  		startTime := time.Now().UnixNano() / 1e6
  1586  		voteOwner := ownerList[rand.Intn(2)]
  1587  		deposit := new(big.Int).SetUint64(10)
  1588  		state.SubBalance(voteOwner, deposit)
  1589  		state.AddBalance(common.TicketPoolAddr, deposit)
  1590  		tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1591  		if i < 2 {
  1592  			tempBlockNumber.SetUint64(6)
  1593  			logFn("vote blockNumber:", tempBlockNumber.Uint64())
  1594  		}
  1595  
  1596  		if i == 2 {
  1597  			fmt.Println("release ticket,start ############################################################")
  1598  			var tempBlockNumber uint64 = 6
  1599  			for i := 0; i < 4; i++ {
  1600  				ppos.Notify(state, new(big.Int).SetUint64(tempBlockNumber))
  1601  				tempBlockNumber++
  1602  			}
  1603  			fmt.Println("release ticket,end ############################################################")
  1604  		}
  1605  		fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1606  		_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, can.CandidateId, tempBlockNumber)
  1607  		if nil != err {
  1608  			fmt.Println("vote ticket error:", err)
  1609  		}
  1610  		atomic.AddUint32(&count, 1)
  1611  		timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1612  
  1613  	}
  1614  	fmt.Println("VOTING END .............................................................")
  1615  
  1616  
  1617  
  1618  	/** test WithdrawCandidate */
  1619  	logFn("test WithdrawCandidate ...")
  1620  	ok1 := ppos.WithdrawCandidate(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), new(big.Int).SetUint64(uint64(99)), new(big.Int).SetUint64(uint64(10)))
  1621  	logFn("error", ok1)
  1622  
  1623  	/** test  RefundBalance*/
  1624  	if err := ppos.RefundBalance(state, candidate.CandidateId, big.NewInt(11)); nil != err {
  1625  		errFn("RefundBalance err", err)
  1626  	}else {
  1627  		logFn("RefundBalance success ...")
  1628  	}
  1629  }
  1630  /*func TestPpos_RefundBalance(t *testing.T) {
  1631  	ppos_RefundBalance(t, t.Log, t.Error)
  1632  }
  1633  func BenchmarkPpos_RefundBalance(b *testing.B) {
  1634  	ppos_RefundBalance(b, b.Log, b.Error)
  1635  }*/
  1636  
  1637  // test GetOwner
  1638  func ppos_GetOwner (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  1639  	ppos, bc := buildPpos()
  1640  	var state *state.StateDB
  1641  	if st, err := bc.State(); nil != err {
  1642  		errFn("test GetOwner getting state err", err)
  1643  	}else {
  1644  		state = st
  1645  	}
  1646  	logFn("test GetOwner ...")
  1647  
  1648  	// cache
  1649  	cans := make([]*types.Candidate, 0)
  1650  
  1651  	candidate := &types.Candidate{
  1652  		Deposit: 		new(big.Int).SetUint64(100),
  1653  		BlockNumber:    new(big.Int).SetUint64(7),
  1654  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1655  		TxIndex:  		6,
  1656  		Host:  			"10.0.0.1",
  1657  		Port:  			"8548",
  1658  		Owner: 			common.HexToAddress("0x12"),
  1659  
  1660  	}
  1661  	logFn("Set New Candidate ...")
  1662  	/** test SetCandidate */
  1663  	cans = append(cans, candidate)
  1664  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1665  		errFn("SetCandidate err:", err)
  1666  	}
  1667  
  1668  	/** test GetOwner */
  1669  	ownerAddr := ppos.GetOwner(state, candidate.CandidateId, big.NewInt(1))
  1670  	logFn("Getting Onwer's Address:", ownerAddr.String())
  1671  }
  1672  /*func TestPpos_GetOwner(t *testing.T) {
  1673  	ppos_GetOwner(t, t.Log, t.Error)
  1674  }
  1675  func BenchmarkPpos_GetOwner(b *testing.B) {
  1676  	ppos_GetOwner(b, b.Log, b.Error)
  1677  }*/
  1678  
  1679  // test GetRefundInterval
  1680  func ppos_GetRefundInterval (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  1681  	ppos, _ := buildPpos()
  1682  
  1683  	logFn("test GetRefundInterval ...")
  1684  
  1685  	/** test  GetRefundInterval*/
  1686  	num := ppos.GetRefundInterval(big.NewInt(1))
  1687  	logFn("RefundInterval:", num)
  1688  }
  1689  /*func TestPpos_GetRefundInterval(t *testing.T) {
  1690  	ppos_GetRefundInterval(t, t.Log, t.Error)
  1691  }
  1692  func BenchmarkPpos_GetRefundInterval(b *testing.B) {
  1693  	ppos_GetRefundInterval(b, b.Log, b.Error)
  1694  }*/
  1695  
  1696  /** about tickpool */
  1697  // test GetPoolNumber
  1698  func ppos_GetPoolNumber (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  1699  	ppos, bc := buildPpos()
  1700  	var state *state.StateDB
  1701  	if st, err := bc.State(); nil != err {
  1702  		errFn("test GetPoolNumber getting state err", err)
  1703  	}else {
  1704  		state = st
  1705  	}
  1706  	logFn("test GetPoolNumber ...")
  1707  
  1708  	/** test  GetPoolNumber */
  1709  	logFn("test GetPoolNumber ...")
  1710  	num := ppos.GetPoolNumber(state)
  1711  	logFn("GetPoolNumber:", num)
  1712  }
  1713  /*func TestPpos_GetPoolNumber(t *testing.T) {
  1714  	ppos_GetPoolNumber(t, t.Log, t.Error)
  1715  }
  1716  func BenchmarkPpos_GetPoolNumber(b *testing.B) {
  1717  	ppos_GetPoolNumber(b, b.Log, b.Error)
  1718  }*/
  1719  
  1720  // test VoteTicket
  1721  func ppos_VoteTicket (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  1722  	ppos, bc := buildPpos()
  1723  	var state *state.StateDB
  1724  	if st, err := bc.State(); nil != err {
  1725  		errFn("test VoteTicket getting state err", err)
  1726  	}else {
  1727  		state = st
  1728  	}
  1729  	logFn("test VoteTicket ...")
  1730  
  1731  
  1732  	candidate := &types.Candidate{
  1733  		Deposit: 		new(big.Int).SetUint64(100),
  1734  		BlockNumber:    new(big.Int).SetUint64(7),
  1735  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1736  		TxIndex:  		6,
  1737  		Host:  			"10.0.0.1",
  1738  		Port:  			"8548",
  1739  		Owner: 			common.HexToAddress("0x12"),
  1740  
  1741  	}
  1742  	logFn("Set New Candidate ...")
  1743  	/** test SetCandidate */
  1744  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1745  		errFn("SetCandidate err:", err)
  1746  	}
  1747  
  1748  
  1749  	/** vote ticket */
  1750  	var count uint32 = 0
  1751  	var blockNumber = new(big.Int).SetUint64(10)
  1752  
  1753  	timeMap := make(map[uint32]int64)
  1754  	logFn("VOTING START .............................................................")
  1755  
  1756  	startTime := time.Now().UnixNano() / 1e6
  1757  	voteOwner := common.HexToAddress("0x20")
  1758  	deposit := new(big.Int).SetUint64(10)
  1759  	state.SubBalance(voteOwner, deposit)
  1760  	state.AddBalance(common.TicketPoolAddr, deposit)
  1761  	tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1762  	fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1763  	_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, candidate.CandidateId, tempBlockNumber)
  1764  	if nil != err {
  1765  		errFn("vote ticket error:", err)
  1766  	}
  1767  	atomic.AddUint32(&count, 1)
  1768  	timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1769  	logFn("VOTING END .............................................................")
  1770  }
  1771  /*func TestPpos_VoteTicket(t *testing.T) {
  1772  	ppos_VoteTicket(t, t.Log, t.Error)
  1773  }
  1774  func BenchmarkPpos_VoteTicket(b *testing.B) {
  1775  	ppos_VoteTicket(b, b.Log, b.Error)
  1776  }*/
  1777  
  1778  // test GetTicket
  1779  func ppos_GetTicket (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  1780  	ppos, bc := buildPpos()
  1781  	var state *state.StateDB
  1782  	if st, err := bc.State(); nil != err {
  1783  		errFn("test GetTicket getting state err", err)
  1784  	}else {
  1785  		state = st
  1786  	}
  1787  	logFn("test GetTicket ...")
  1788  
  1789  
  1790  	candidate := &types.Candidate{
  1791  		Deposit: 		new(big.Int).SetUint64(100),
  1792  		BlockNumber:    new(big.Int).SetUint64(7),
  1793  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1794  		TxIndex:  		6,
  1795  		Host:  			"10.0.0.1",
  1796  		Port:  			"8548",
  1797  		Owner: 			common.HexToAddress("0x12"),
  1798  
  1799  	}
  1800  	logFn("Set New Candidate ...")
  1801  	/** test SetCandidate */
  1802  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1803  		errFn("SetCandidate err:", err)
  1804  	}
  1805  
  1806  
  1807  	/** vote ticket */
  1808  	var count uint32 = 0
  1809  	var blockNumber = new(big.Int).SetUint64(10)
  1810  
  1811  	timeMap := make(map[uint32]int64)
  1812  	logFn("VOTING START .............................................................")
  1813  
  1814  	startTime := time.Now().UnixNano() / 1e6
  1815  	voteOwner := common.HexToAddress("0x20")
  1816  	deposit := new(big.Int).SetUint64(10)
  1817  	state.SubBalance(voteOwner, deposit)
  1818  	state.AddBalance(common.TicketPoolAddr, deposit)
  1819  	tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1820  	fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1821  	num, err := ppos.VoteTicket(state, voteOwner, 1, deposit, candidate.CandidateId, tempBlockNumber)
  1822  	if nil != err {
  1823  		errFn("vote ticket error:", err)
  1824  	}else{
  1825  		logFn("ticket success num", num)
  1826  	}
  1827  	atomic.AddUint32(&count, 1)
  1828  	timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1829  	logFn("VOTING END .............................................................")
  1830  
  1831  	/** GetTicket */
  1832  	if ticket := ppos.GetTicket(state, state.TxHash()); nil == ticket {
  1833  		errFn("GetTicket err")
  1834  	}else {
  1835  		printObject("GetTicket ticketInfo:", ticket, logger)
  1836  	}
  1837  }
  1838  /*func TestPpos_GetTicket(t *testing.T) {
  1839  	ppos_GetTicket(t, t.Log, t.Error)
  1840  }
  1841  func BenchmarkPpos_GetTicket(b *testing.B) {
  1842  	ppos_GetTicket(b, b.Log, b.Error)
  1843  }*/
  1844  
  1845  // test GetTicketList
  1846  func ppos_GetTicketList (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  1847  	ppos, bc := buildPpos()
  1848  	var state *state.StateDB
  1849  	if st, err := bc.State(); nil != err {
  1850  		errFn("test GetTicketList getting state err", err)
  1851  	}else {
  1852  		state = st
  1853  	}
  1854  	logFn("test GetTicketList ...")
  1855  
  1856  
  1857  	candidate := &types.Candidate{
  1858  		Deposit: 		new(big.Int).SetUint64(100),
  1859  		BlockNumber:    new(big.Int).SetUint64(7),
  1860  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1861  		TxIndex:  		6,
  1862  		Host:  			"10.0.0.1",
  1863  		Port:  			"8548",
  1864  		Owner: 			common.HexToAddress("0x12"),
  1865  
  1866  	}
  1867  	logFn("Set New Candidate ...")
  1868  	/** test SetCandidate */
  1869  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1870  		errFn("SetCandidate err:", err)
  1871  	}
  1872  
  1873  
  1874  	/** vote ticket */
  1875  	var count uint32 = 0
  1876  	var blockNumber = new(big.Int).SetUint64(10)
  1877  
  1878  	timeMap := make(map[uint32]int64)
  1879  	logFn("VOTING START .............................................................")
  1880  
  1881  	startTime := time.Now().UnixNano() / 1e6
  1882  	voteOwner := common.HexToAddress("0x20")
  1883  	deposit := new(big.Int).SetUint64(10)
  1884  	state.SubBalance(voteOwner, deposit)
  1885  	state.AddBalance(common.TicketPoolAddr, deposit)
  1886  	tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1887  	fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1888  	num, err := ppos.VoteTicket(state, voteOwner, 1, deposit, candidate.CandidateId, tempBlockNumber)
  1889  	if nil != err {
  1890  		errFn("vote ticket error:", err)
  1891  	}else{
  1892  		logFn("ticket success num", num)
  1893  	}
  1894  	atomic.AddUint32(&count, 1)
  1895  	timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1896  	logFn("VOTING END .............................................................")
  1897  
  1898  	/** GetTicketList */
  1899  	if tickets := ppos.GetTicketList(state, []common.Hash{state.TxHash()}); len(tickets) == 0 {
  1900  		errFn("GetTicketList err")
  1901  	}else {
  1902  		printObject("GetTicketList ticketArr:", tickets, logger)
  1903  	}
  1904  }
  1905  /*func TestPpos_GetTicketList(t *testing.T) {
  1906  	ppos_GetTicketList(t, t.Log, t.Error)
  1907  }
  1908  func BenchmarkPpos_GetTicketList(b *testing.B) {
  1909  	ppos_GetTicketList(b, b.Log, b.Error)
  1910  }
  1911  */
  1912  // test GetCandidateTicketIds
  1913  func ppos_GetCandidateTicketIds (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  1914  	ppos, bc := buildPpos()
  1915  	var state *state.StateDB
  1916  	if st, err := bc.State(); nil != err {
  1917  		errFn("test GetCandidateTicketIds getting state err", err)
  1918  	}else {
  1919  		state = st
  1920  	}
  1921  	logFn("test GetCandidateTicketIds ...")
  1922  
  1923  
  1924  	candidate := &types.Candidate{
  1925  		Deposit: 		new(big.Int).SetUint64(100),
  1926  		BlockNumber:    new(big.Int).SetUint64(7),
  1927  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1928  		TxIndex:  		6,
  1929  		Host:  			"10.0.0.1",
  1930  		Port:  			"8548",
  1931  		Owner: 			common.HexToAddress("0x12"),
  1932  
  1933  	}
  1934  	logFn("Set New Candidate ...")
  1935  	/** test SetCandidate */
  1936  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1937  		errFn("SetCandidate err:", err)
  1938  	}
  1939  
  1940  
  1941  	/** vote ticket */
  1942  	var count uint32 = 0
  1943  	var blockNumber = new(big.Int).SetUint64(10)
  1944  
  1945  	timeMap := make(map[uint32]int64)
  1946  	logFn("VOTING START .............................................................")
  1947  
  1948  	startTime := time.Now().UnixNano() / 1e6
  1949  	voteOwner := common.HexToAddress("0x20")
  1950  	deposit := new(big.Int).SetUint64(10)
  1951  	state.SubBalance(voteOwner, deposit)
  1952  	state.AddBalance(common.TicketPoolAddr, deposit)
  1953  	tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  1954  	fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  1955  	_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, candidate.CandidateId, tempBlockNumber)
  1956  	if nil != err {
  1957  		errFn("vote ticket error:", err)
  1958  	}
  1959  	atomic.AddUint32(&count, 1)
  1960  	timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  1961  	logFn("VOTING END .............................................................")
  1962  
  1963  	tickIds := ppos.GetCandidateTicketIds(state, candidate.CandidateId)
  1964  	printObject("GetCandidateTicketIds:", tickIds, logger)
  1965  }
  1966  /*func TestPpos_GetCandidateTicketIds(t *testing.T) {
  1967  	ppos_GetCandidateTicketIds(t, t.Log, t.Error)
  1968  }
  1969  func BenchmarkPpos_GetCandidateTicketIds(b *testing.B) {
  1970  	ppos_GetCandidateTicketIds(b, b.Log, b.Error)
  1971  }*/
  1972  
  1973  // test GetCandidateEpoch
  1974  func ppos_GetCandidateEpoch  (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  1975  	ppos, bc := buildPpos()
  1976  	var state *state.StateDB
  1977  	if st, err := bc.State(); nil != err {
  1978  		errFn("test GetCandidateEpoch getting state err", err)
  1979  	}else {
  1980  		state = st
  1981  	}
  1982  	logFn("test GetCandidateEpoch ...")
  1983  
  1984  
  1985  	candidate := &types.Candidate{
  1986  		Deposit: 		new(big.Int).SetUint64(100),
  1987  		BlockNumber:    new(big.Int).SetUint64(7),
  1988  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  1989  		TxIndex:  		6,
  1990  		Host:  			"10.0.0.1",
  1991  		Port:  			"8548",
  1992  		Owner: 			common.HexToAddress("0x12"),
  1993  
  1994  	}
  1995  	logFn("Set New Candidate ...")
  1996  	/** test SetCandidate */
  1997  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  1998  		errFn("SetCandidate err:", err)
  1999  	}
  2000  
  2001  
  2002  	/** vote ticket */
  2003  	var count uint32 = 0
  2004  	var blockNumber = new(big.Int).SetUint64(10)
  2005  
  2006  	timeMap := make(map[uint32]int64)
  2007  	logFn("VOTING START .............................................................")
  2008  
  2009  	startTime := time.Now().UnixNano() / 1e6
  2010  	voteOwner := common.HexToAddress("0x20")
  2011  	deposit := new(big.Int).SetUint64(10)
  2012  	state.SubBalance(voteOwner, deposit)
  2013  	state.AddBalance(common.TicketPoolAddr, deposit)
  2014  	tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  2015  	fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  2016  	_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, candidate.CandidateId, tempBlockNumber)
  2017  	if nil != err {
  2018  		errFn("vote ticket error:", err)
  2019  	}
  2020  	atomic.AddUint32(&count, 1)
  2021  	timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  2022  	logFn("VOTING END .............................................................")
  2023  
  2024  	epoch := ppos.GetCandidateEpoch(state, candidate.CandidateId)
  2025  	logFn("GetCandidateEpoch:", epoch)
  2026  }
  2027  /*func TestPpos_GetCandidateEpoch(t *testing.T) {
  2028  	ppos_GetCandidateEpoch(t, t.Log, t.Error)
  2029  }
  2030  func BenchmarkPpos_GetCandidateEpoch(b *testing.B) {
  2031  	ppos_GetCandidateEpoch(b, b.Log, b.Error)
  2032  }*/
  2033  
  2034  // test GetTicketPrice
  2035  func ppos_GetTicketPrice (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  2036  	ppos, bc := buildPpos()
  2037  	var state *state.StateDB
  2038  	if st, err := bc.State(); nil != err {
  2039  		errFn("test GetTicketPrice getting state err", err)
  2040  	}else {
  2041  		state = st
  2042  	}
  2043  	logFn("test GetTicketPrice ...")
  2044  
  2045  	/** test  GetTicketPrice */
  2046  	logFn("test GetTicketPrice ...")
  2047  	num := ppos.GetTicketPrice(state)
  2048  	logFn("GetTicketPrice:", num)
  2049  }
  2050  /*func TestPpos_GetTicketPrice(t *testing.T) {
  2051  	ppos_GetTicketPrice(t, t.Log, t.Error)
  2052  }
  2053  func BenchmarkPpos_GetTicketPrice(b *testing.B) {
  2054  	ppos_GetTicketPrice(b, b.Log, b.Error)
  2055  }*/
  2056  
  2057  
  2058  // test Notify
  2059  func ppos_Notify (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  2060  	ppos, bc := buildPpos()
  2061  	var state *state.StateDB
  2062  	if st, err := bc.State(); nil != err {
  2063  		errFn("test Notify getting state err", err)
  2064  	}else {
  2065  		state = st
  2066  	}
  2067  	logFn("test Notify ...")
  2068  
  2069  
  2070  	candidate := &types.Candidate{
  2071  		Deposit: 		new(big.Int).SetUint64(100),
  2072  		BlockNumber:    new(big.Int).SetUint64(7),
  2073  		CandidateId:   discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"),
  2074  		TxIndex:  		6,
  2075  		Host:  			"10.0.0.1",
  2076  		Port:  			"8548",
  2077  		Owner: 			common.HexToAddress("0x12"),
  2078  
  2079  	}
  2080  	logFn("Set New Candidate ...")
  2081  	/** test SetCandidate */
  2082  	if err := ppos.SetCandidate(state, candidate.CandidateId, candidate); nil != err {
  2083  		errFn("SetCandidate err:", err)
  2084  	}
  2085  
  2086  
  2087  	/** vote ticket */
  2088  	var count uint32 = 0
  2089  	var blockNumber = new(big.Int).SetUint64(10)
  2090  
  2091  	timeMap := make(map[uint32]int64)
  2092  	logFn("VOTING START .............................................................")
  2093  
  2094  	startTime := time.Now().UnixNano() / 1e6
  2095  	voteOwner := common.HexToAddress("0x20")
  2096  	deposit := new(big.Int).SetUint64(10)
  2097  	state.SubBalance(voteOwner, deposit)
  2098  	state.AddBalance(common.TicketPoolAddr, deposit)
  2099  	tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64())
  2100  	fmt.Println("Voting Current Candidate:", "voter is:", voteOwner.String(), " ,ticket num:", candidate.CandidateId.String(), " ,blocknumber when voted:", tempBlockNumber.String())
  2101  	_, err := ppos.VoteTicket(state, voteOwner, 1, deposit, candidate.CandidateId, tempBlockNumber)
  2102  	if nil != err {
  2103  		errFn("vote ticket error:", err)
  2104  	}
  2105  	atomic.AddUint32(&count, 1)
  2106  	timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime
  2107  	logFn("VOTING END .............................................................")
  2108  
  2109  	if err := ppos.Notify(state, new(big.Int).SetUint64(10)); nil != err {
  2110  		errFn("Notify err", err)
  2111  	}else {
  2112  		logFn("Notify success ... ")
  2113  	}
  2114  }
  2115  /*func TestPpos_Notify(t *testing.T) {
  2116  	ppos_Notify(t, t.Log, t.Error)
  2117  }
  2118  func BenchmarkPpos_Notify(b *testing.B) {
  2119  	ppos_Notify(b, b.Log, b.Error)
  2120  }*/
  2121  
  2122  /** about other */
  2123  
  2124  // test UpdateNodeList
  2125  func ppos_UpdateNodeList (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  2126  	ppos, bc := buildPpos()
  2127  	logFn("test UpdateNodeList ...")
  2128  	genesis := bc.Genesis()
  2129  	ppos.UpdateNodeList(bc, genesis.Number(), genesis.Hash())
  2130  }
  2131  func TestPpos_UpdateNodeList(t *testing.T) {
  2132  	ppos_UpdateNodeList(t, t.Log, t.Error)
  2133  }
  2134  func BenchmarkPpos_UpdateNodeList(b *testing.B) {
  2135  	ppos_UpdateNodeList(b, b.Log, b.Error)
  2136  }
  2137  
  2138  // test Submit2Cache
  2139  func getBlockMaxData() (ticketcache.TicketCache, error) {
  2140  	//every nodeid has 256 ticket total has 200 nodeid
  2141  	ret := ticketcache.NewTicketCache()
  2142  	for n:=0; n<200; n++ {
  2143  		nodeid := make([]byte, 0, 64)
  2144  		nodeid = append(nodeid, crypto.Keccak256Hash([]byte("nodeid"), byteutil.IntToBytes(n)).Bytes()...)
  2145  		nodeid = append(nodeid, crypto.Keccak256Hash([]byte("nodeid"), byteutil.IntToBytes(n*10)).Bytes()...)
  2146  		NodeId, err := discover.BytesID(nodeid)
  2147  		if err!=nil {
  2148  			return ret, err
  2149  		}
  2150  		tids := make([]common.Hash, 0)
  2151  		for i:=0; i< 51200/200 ; i++ {
  2152  			tids = append(tids, crypto.Keccak256Hash([]byte("tid"), byteutil.IntToBytes(i)))
  2153  		}
  2154  		ret[NodeId] = tids
  2155  	}
  2156  	return ret, nil
  2157  }
  2158  func ppos_Submit2Cache (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  2159  	ldb, err := ethdb.NewLDBDatabase("./data/platon/chaindata", 0, 0)
  2160  	if err!=nil {
  2161  		errFn("NewLDBDatabase faile")
  2162  	}
  2163  	tc := ticketcache.NewTicketIdsCache(ldb)
  2164  	for i:=0; i< 20; i++  {
  2165  		number := big.NewInt(int64(i))
  2166  		bkhash := crypto.Keccak256Hash(byteutil.IntToBytes(i))
  2167  		mapCache ,err := getBlockMaxData()
  2168  		if err!=nil {
  2169  			errFn("getMaxtickets faile", "err: ", err)
  2170  		}
  2171  		tc.Submit2Cache(number, big.NewInt(int64(20)), bkhash, mapCache)
  2172  
  2173  	}
  2174  	ldb.Close()
  2175  }
  2176  /*func TestPpos_Submit2Cache(t *testing.T) {
  2177  	ppos_Submit2Cache(t, t.Log, t.Error)
  2178  }
  2179  func BenchmarkPpos_Submit2Cache(b *testing.B) {
  2180  	ppos_Submit2Cache(b, b.Log, b.Error)
  2181  }*/
  2182  
  2183  // TODO Hash
  2184  
  2185  // test GetFormerRound
  2186  func ppos_GetFormerRound (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  2187  	ppos, bc := buildPpos()
  2188  	logFn("test GetFormerRound ...")
  2189  	genesis := bc.Genesis()
  2190  	round := ppos.GetFormerRound(genesis.Number(), genesis.Hash())
  2191  	printObject("GetFormerRound", round.nodes, logger)
  2192  }
  2193  func TestPpos_GetFormerRound(t *testing.T) {
  2194  	ppos_GetFormerRound(t, t.Log, t.Error)
  2195  }
  2196  func BenchmarkPpos_GetFormerRound(b *testing.B) {
  2197  	ppos_GetFormerRound(b, b.Log, b.Error)
  2198  }
  2199  
  2200  // test GetCurrentRound
  2201  func ppos_GetCurrentRound (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  2202  	ppos, bc := buildPpos()
  2203  	logFn("test GetCurrentRound ...")
  2204  	genesis := bc.Genesis()
  2205  	round := ppos.GetCurrentRound(genesis.Number(), genesis.Hash())
  2206  	printObject("GetCurrentRound", round.nodes, logger)
  2207  }
  2208  /*func TestPpos_GetCurrentRound(t *testing.T) {
  2209  	ppos_GetCurrentRound(t, t.Log, t.Error)
  2210  }
  2211  func BenchmarkPpos_GetCurrentRound(b *testing.B) {
  2212  	ppos_GetCurrentRound(b, b.Log, b.Error)
  2213  }*/
  2214  
  2215  
  2216  // test GetNextRound
  2217  func ppos_GetNextRound (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})) {
  2218  	ppos, bc := buildPpos()
  2219  	logFn("test GetNextRound ...")
  2220  	genesis := bc.Genesis()
  2221  	round := ppos.GetNextRound(genesis.Number(), genesis.Hash())
  2222  	printObject("GetNextRound", round.nodes, logger)
  2223  }
  2224  /*func TestPpos_GetNextRound(t *testing.T) {
  2225  	ppos_GetNextRound(t, t.Log, t.Error)
  2226  }
  2227  func BenchmarkPpos_GetNextRound(b *testing.B) {
  2228  	ppos_GetNextRound(b, b.Log, b.Error)
  2229  }*/
  2230  
  2231  // test SetNodeCache
  2232  func ppos_SetNodeCache (logger interface{}, logFn func (args ... interface{}), errFn func (args ... interface{})){
  2233  	ppos, bc := buildPpos()
  2234  	var state *state.StateDB
  2235  	if st, err := bc.State(); nil != err {
  2236  		errFn("test SetNodeCache getting state err", err)
  2237  	}else {
  2238  		state = st
  2239  	}
  2240  	logFn("test SetNodeCache ...")
  2241  	genesis := bc.Genesis()
  2242  	if err := ppos.SetNodeCache(state, genesis.Number(), big.NewInt(0), big.NewInt(1), genesis.Hash(), common.HexToHash("0xa1d63b9e5f36c9b12e6aed34612bc1f6e846d1e94a53f52673f2433a30e9ac51"), common.HexToHash("0xa1d63b9e5f36c9b12e6aed34612bc1f6e846d1e94a53f52673f2433a30e9bd62")); nil != err {
  2243  		errFn("SetNodeCache err", err)
  2244  	}else {
  2245  		logFn("SetNodeCache success ... ")
  2246  	}
  2247  }
  2248  /*func TestPpos_SetNodeCache(t *testing.T) {
  2249  	ppos_SetNodeCache(t, t.Log, t.Error)
  2250  }
  2251  func BenchmarkPpos_SetNodeCache(b *testing.B) {
  2252  	ppos_SetNodeCache(b, b.Log, b.Error)
  2253  }*/
  2254  
  2255