github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/core/vm/candidate_pool_test.go (about)

     1  package vm_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"errors"
     8  	"fmt"
     9  	"github.com/PlatONnetwork/PlatON-Go/common"
    10  	"github.com/PlatONnetwork/PlatON-Go/common/byteutil"
    11  	"github.com/PlatONnetwork/PlatON-Go/common/hexutil"
    12  	"github.com/PlatONnetwork/PlatON-Go/core"
    13  	"github.com/PlatONnetwork/PlatON-Go/core/ppos"
    14  	"github.com/PlatONnetwork/PlatON-Go/core/ppos_storage"
    15  	"github.com/PlatONnetwork/PlatON-Go/core/state"
    16  	"github.com/PlatONnetwork/PlatON-Go/core/vm"
    17  	"github.com/PlatONnetwork/PlatON-Go/ethdb"
    18  	"github.com/PlatONnetwork/PlatON-Go/p2p/discover"
    19  	"github.com/PlatONnetwork/PlatON-Go/params"
    20  	"github.com/PlatONnetwork/PlatON-Go/rlp"
    21  	"math/big"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  /*func TestCandidatePoolOverAll(t *testing.T) {
    27  
    28  	candidateContract := vm.CandidateContract{
    29  		newContract(),
    30  		newEvm(),
    31  	}
    32  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
    33  	owner := common.HexToAddress("0x12")
    34  	fee := uint32(7000)
    35  	host := "192.168.9.184"
    36  	port := "16789"
    37  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
    38  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
    39  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
    40  	if nil != err {
    41  		fmt.Println("CandidateDeposit fail", "err", err)
    42  	}
    43  	fmt.Println("CandidateDeposit success")
    44  
    45  	// GetCandidateDetails(nodeIds []discover.NodeID) ([]byte, error)
    46  	fmt.Println("GetCandidateDetails input==>", "nodeIds: ", nodeId.String())
    47  	var nodeIds []discover.NodeID
    48  	nodeIds = append(nodeIds, nodeId)
    49  	resByte, err := candidateContract.GetCandidateDetails(nodeIds)
    50  	if nil != err {
    51  		fmt.Println("GetCandidateDetails fail", "err", err)
    52  		return
    53  	}
    54  	if nil == resByte {
    55  		fmt.Println("The candidate info is null")
    56  		return
    57  	}
    58  	fmt.Println("The candidate info is: ", vm.ResultByte2Json(resByte))
    59  
    60  	// CandidateApplyWithdraw(nodeId discover.NodeID, withdraw *big.Int) ([]byte, error)
    61  	withdraw := big.NewInt(100)
    62  	fmt.Println("CandidateApplyWithdraw input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
    63  	_, err = candidateContract.CandidateApplyWithdraw(nodeId, withdraw)
    64  	if nil != err {
    65  		fmt.Println("CandidateApplyWithdraw fail", "err", err)
    66  	}
    67  	fmt.Println("CandidateApplyWithdraw success")
    68  
    69  	// CandidateWithdraw(nodeId discover.NodeID) ([]byte, error)
    70  	fmt.Println("CandidateWithdraw input==>", "nodeId: ", nodeId.String())
    71  	_, err = candidateContract.CandidateWithdraw(nodeId)
    72  	if nil != err {
    73  		fmt.Println("CandidateWithdraw fail", "err", err)
    74  	}
    75  	fmt.Println("CandidateWithdraw success")
    76  
    77  	// GetCandidateWithdrawInfos(nodeId discover.NodeID) ([]byte, error)
    78  	fmt.Println("GetCandidateWithdrawInfos input==>", "nodeId: ", nodeId.String())
    79  	resByte, err = candidateContract.GetCandidateWithdrawInfos(nodeId)
    80  	if nil != err {
    81  		fmt.Println("GetCandidateWithdrawInfos fail", "err", err)
    82  		return
    83  	}
    84  	if nil == resByte {
    85  		fmt.Println("The GetCandidateWithdrawInfos is null")
    86  		return
    87  	}
    88  	fmt.Println("The GetCandidateWithdrawInfos is: ", vm.ResultByte2Json(resByte))
    89  
    90  	// GetCandidateList() ([]byte, error)
    91  	resByte, err = candidateContract.GetCandidateList()
    92  	if nil != err {
    93  		fmt.Println("GetCandidateList fail", "err", err)
    94  	}
    95  	if nil == resByte {
    96  		fmt.Println("The candidate list is null")
    97  		return
    98  	}
    99  	fmt.Println("The candidate list is: ", vm.ResultByte2Json(resByte))
   100  }*/
   101  
   102  func newChainState() (*state.StateDB, error) {
   103  	var (
   104  		db      = ethdb.NewMemDatabase()
   105  		genesis = new(core.Genesis).MustCommit(db)
   106  	)
   107  	fmt.Println("genesis", genesis)
   108  	ppos_storage.NewPPosTemp(db)
   109  	// Initialize a fresh chain with only a genesis block
   110  	blockchain, _ := core.NewBlockChain(db, nil, params.AllEthashProtocolChanges, nil, vm.Config{}, nil)
   111  
   112  	var state *state.StateDB
   113  	if statedb, err := blockchain.State(); nil != err {
   114  		return nil, errors.New("reference statedb failed" + err.Error())
   115  	} else {
   116  		state = statedb
   117  	}
   118  	return state, nil
   119  }
   120  
   121  func newPool() (*pposm.CandidatePoolContext, *pposm.TicketPoolContext) {
   122  	configs := &params.PposConfig{
   123  		CandidateConfig: &params.CandidateConfig{
   124  			Threshold:         "100",
   125  			DepositLimit:      10,
   126  			MaxChair:          1,
   127  			MaxCount:          3,
   128  			RefundBlockNumber: 1,
   129  		},
   130  		TicketConfig: &params.TicketConfig{
   131  			TicketPrice:       "1",
   132  			MaxCount:          10000,
   133  			ExpireBlockNumber: 100,
   134  		},
   135  	}
   136  	return pposm.NewCandidatePoolContext(configs), pposm.NewTicketPoolContext(configs)
   137  }
   138  
   139  func newEvm() *vm.EVM {
   140  	state, _ := newChainState()
   141  	candidatePoolContext, ticketPoolContext := newPool()
   142  	evm := &vm.EVM{
   143  		StateDB:              state,
   144  		CandidatePoolContext: candidatePoolContext,
   145  		TicketPoolContext:    ticketPoolContext,
   146  	}
   147  	context := vm.Context{
   148  		BlockNumber: big.NewInt(7),
   149  	}
   150  	evm.Context = context
   151  	return evm
   152  }
   153  
   154  func newContract() *vm.Contract {
   155  	callerAddress := vm.AccountRef(common.HexToAddress("0x12"))
   156  	contract := vm.NewContract(callerAddress, callerAddress, big.NewInt(1000), uint64(1))
   157  	return contract
   158  }
   159  
   160  func TestCandidateDeposit(t *testing.T) {
   161  	candidateContract := vm.CandidateContract{
   162  		newContract(),
   163  		newEvm(),
   164  	}
   165  
   166  	// CandidateDeposit(nodeId discover.NodeID, owner common.Address, fee uint32, host, port, extra string) ([]byte, error)
   167  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   168  	owner := common.HexToAddress("0x12")
   169  	fee := uint32(7000)
   170  	host := "192.168.9.184"
   171  	port := "16789"
   172  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   173  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   174  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   175  	if nil != err {
   176  		fmt.Println("CandidateDeposit fail", "err", err)
   177  	}
   178  	fmt.Println("CandidateDeposit success")
   179  }
   180  
   181  func TestCandidateDetails(t *testing.T) {
   182  	candidateContract := vm.CandidateContract{
   183  		newContract(),
   184  		newEvm(),
   185  	}
   186  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   187  	owner := common.HexToAddress("0x12")
   188  	fee := uint32(7000)
   189  	host := "192.168.9.184"
   190  	port := "16789"
   191  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   192  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   193  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   194  	if nil != err {
   195  		fmt.Println("CandidateDeposit fail", "err", err)
   196  	}
   197  	fmt.Println("CandidateDeposit success")
   198  
   199  	// GetCandidateDetails(nodeIds []discover.NodeID) ([]byte, error)
   200  	// nodeId = discover.MustHexID("0x11234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   201  	var nodeIds []discover.NodeID
   202  	nodeIds = append(nodeIds, nodeId)
   203  	fmt.Println("GetCandidateDetails input==>", "nodeIds: ", nodeId.String())
   204  	resByte, err := candidateContract.GetCandidateDetails(nodeIds)
   205  	if nil != err {
   206  		fmt.Println("GetCandidateDetails fail", "err", err)
   207  		return
   208  	}
   209  	if nil == resByte {
   210  		fmt.Println("The candidate info is null")
   211  		return
   212  	}
   213  	fmt.Println("The candidate info is: ", vm.ResultByte2Json(resByte))
   214  }
   215  
   216  func TestGetBatchCandidateDetail(t *testing.T) {
   217  	candidateContract := vm.CandidateContract{
   218  		newContract(),
   219  		newEvm(),
   220  	}
   221  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   222  	owner := common.HexToAddress("0x12")
   223  	fee := uint32(7000)
   224  	host := "192.168.9.184"
   225  	port := "16789"
   226  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   227  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   228  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   229  	if nil != err {
   230  		fmt.Println("CandidateDeposit fail", "err", err)
   231  	}
   232  	fmt.Println("CandidateDeposit1 success")
   233  
   234  	nodeId = discover.MustHexID("0x11234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   235  	owner = common.HexToAddress("0x12")
   236  	fee = uint32(8000)
   237  	host = "192.168.9.185"
   238  	port = "16789"
   239  	extra = "{\"nodeName\": \"Platon-Shenzhen\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Cosmic wave\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/sz\",\"time\":1546503651190}"
   240  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   241  	_, err = candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   242  	if nil != err {
   243  		fmt.Println("CandidateDeposit fail", "err", err)
   244  	}
   245  	fmt.Println("CandidateDeposit2 success")
   246  
   247  	// GetCandidateDetails(nodeIds []discover.NodeID) ([]byte, error)
   248  	nodeIds := []discover.NodeID{discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), discover.MustHexID("0x11234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")}
   249  	input, _ := json.Marshal(nodeIds)
   250  	fmt.Println("GetBatchCandidateDetail input==>", "nodeIds: ", string(input))
   251  	resByte, err := candidateContract.GetCandidateDetails(nodeIds)
   252  	if nil != err {
   253  		fmt.Println("GetCandidateDetails fail", "err", err)
   254  	}
   255  	if nil == resByte {
   256  		fmt.Println("The candidate info is null")
   257  		return
   258  	}
   259  	fmt.Println("The batch candidate info is: ", vm.ResultByte2Json(resByte))
   260  }
   261  
   262  func TestCandidateList(t *testing.T) {
   263  	candidateContract := vm.CandidateContract{
   264  		newContract(),
   265  		newEvm(),
   266  	}
   267  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   268  	owner := common.HexToAddress("0x12")
   269  	fee := uint32(7000)
   270  	host := "192.168.9.184"
   271  	port := "16789"
   272  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   273  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   274  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   275  	if nil != err {
   276  		fmt.Println("CandidateDeposit fail", "err", err)
   277  	}
   278  	fmt.Println("CandidateDeposit1 success")
   279  
   280  	nodeId = discover.MustHexID("0x11234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   281  	owner = common.HexToAddress("0x12")
   282  	fee = uint32(8000)
   283  	host = "192.168.9.185"
   284  	port = "16789"
   285  	extra = "{\"nodeName\": \"Platon-Shenzhen\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Cosmic wave\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/sz\",\"time\":1546503651190}"
   286  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   287  	_, err = candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   288  	if nil != err {
   289  		fmt.Println("CandidateDeposit fail", "err", err)
   290  	}
   291  	fmt.Println("CandidateDeposit2 success")
   292  
   293  	// GetCandidateList() ([]byte, error)
   294  	resByte, err := candidateContract.GetCandidateList()
   295  	if nil != err {
   296  		fmt.Println("GetCandidateList fail", "err", err)
   297  	}
   298  	if nil == resByte {
   299  		fmt.Println("The candidate list is null")
   300  		return
   301  	}
   302  	fmt.Println("The candidate list is: ", vm.ResultByte2Json(resByte))
   303  }
   304  
   305  func TestSetCandidateExtra(t *testing.T) {
   306  	candidateContract := vm.CandidateContract{
   307  		newContract(),
   308  		newEvm(),
   309  	}
   310  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   311  	owner := common.HexToAddress("0x12")
   312  	fee := uint32(7000)
   313  	host := "192.168.9.184"
   314  	port := "16789"
   315  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   316  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   317  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   318  	if nil != err {
   319  		fmt.Println("CandidateDeposit fail", "err", err)
   320  	}
   321  	fmt.Println("CandidateDeposit success")
   322  
   323  	// SetCandidateExtra(nodeId discover.NodeID, extra string) ([]byte, error)
   324  	extra = "this node is powerful!!"
   325  	fmt.Println("SetCandidateExtra input=>", "nodeId: ", nodeId.String(), "extra: ", extra)
   326  	_, err = candidateContract.SetCandidateExtra(nodeId, extra)
   327  	if nil != err {
   328  		fmt.Println("SetCandidateExtra fail", "err", err)
   329  	}
   330  	fmt.Println("SetCandidateExtra success")
   331  
   332  	// GetCandidateDetails(nodeIds []discover.NodeID) ([]byte, error)
   333  	fmt.Println("GetCandidateDetails input==>", "nodeIds: ", nodeId.String())
   334  	var nodeIds []discover.NodeID
   335  	nodeIds = append(nodeIds, nodeId)
   336  	resByte, err := candidateContract.GetCandidateDetails(nodeIds)
   337  	if nil != err {
   338  		fmt.Println("GetCandidateDetails fail", "err", err)
   339  		return
   340  	}
   341  	if nil == resByte {
   342  		fmt.Println("The candidate info is null")
   343  		return
   344  	}
   345  	fmt.Println("The candidate info is: ", vm.ResultByte2Json(resByte))
   346  }
   347  
   348  func TestCandidateApplyWithdraw(t *testing.T) {
   349  	candidateContract := vm.CandidateContract{
   350  		newContract(),
   351  		newEvm(),
   352  	}
   353  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   354  	owner := common.HexToAddress("0x12")
   355  	fee := uint32(7000)
   356  	host := "192.168.9.184"
   357  	port := "16789"
   358  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   359  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   360  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   361  	if nil != err {
   362  		fmt.Println("CandidateDeposit fail", "err", err)
   363  	}
   364  	fmt.Println("CandidateDeposit success")
   365  
   366  	// CandidateApplyWithdraw(nodeId discover.NodeID, withdraw *big.Int) ([]byte, error)
   367  	withdraw := big.NewInt(100)
   368  	fmt.Println("CandidateApplyWithdraw input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   369  	_, err = candidateContract.CandidateApplyWithdraw(nodeId, withdraw)
   370  	if nil != err {
   371  		fmt.Println("CandidateApplyWithdraw fail", "err", err)
   372  	}
   373  	fmt.Println("CandidateApplyWithdraw success")
   374  }
   375  
   376  func TestCandidateWithdraw(t *testing.T) {
   377  	candidateContract := vm.CandidateContract{
   378  		newContract(),
   379  		newEvm(),
   380  	}
   381  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   382  	owner := common.HexToAddress("0x12")
   383  	fee := uint32(7000)
   384  	host := "192.168.9.184"
   385  	port := "16789"
   386  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   387  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   388  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   389  	if nil != err {
   390  		fmt.Println("CandidateDeposit fail", "err", err)
   391  	}
   392  	fmt.Println("CandidateDeposit success")
   393  
   394  	// CandidateApplyWithdraw(nodeId discover.NodeID, withdraw *big.Int) ([]byte, error)
   395  	withdraw := big.NewInt(100)
   396  	fmt.Println("CandidateApplyWithdraw input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   397  	_, err = candidateContract.CandidateApplyWithdraw(nodeId, withdraw)
   398  	if nil != err {
   399  		fmt.Println("CandidateApplyWithdraw fail", "err", err)
   400  	}
   401  	fmt.Println("CandidateApplyWithdraw success")
   402  
   403  	// CandidateWithdraw(nodeId discover.NodeID) ([]byte, error)
   404  	fmt.Println("CandidateWithdraw input==>", "nodeId: ", nodeId.String())
   405  	_, err = candidateContract.CandidateWithdraw(nodeId)
   406  	if nil != err {
   407  		fmt.Println("CandidateWithdraw fail", "err", err)
   408  	}
   409  	fmt.Println("CandidateWithdraw success")
   410  
   411  	// GetCandidateDetails(nodeId discover.NodeID) ([]byte, error)
   412  	fmt.Println("CandidateDetails input==>", "nodeId: ", nodeId.String())
   413  	var nodeIds []discover.NodeID
   414  	nodeIds = append(nodeIds, nodeId)
   415  	resByte, err := candidateContract.GetCandidateDetails(nodeIds)
   416  	if nil != err {
   417  		fmt.Println("GetCandidateDetails fail", "err", err)
   418  		return
   419  	}
   420  	if nil == resByte {
   421  		fmt.Println("The candidate info is null")
   422  		return
   423  	}
   424  	fmt.Println("The candidate info is: ", vm.ResultByte2Json(resByte))
   425  }
   426  
   427  func TestCandidateWithdrawInfos(t *testing.T) {
   428  	candidateContract := vm.CandidateContract{
   429  		newContract(),
   430  		newEvm(),
   431  	}
   432  	nodeId := discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
   433  	owner := common.HexToAddress("0x12")
   434  	fee := uint32(7000)
   435  	host := "192.168.9.184"
   436  	port := "16789"
   437  	extra := "{\"nodeName\": \"Platon-Beijing\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-Gravitational area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/\",\"time\":1546503651190}"
   438  	fmt.Println("CandidateDeposit input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   439  	_, err := candidateContract.CandidateDeposit(nodeId, owner, fee, host, port, extra)
   440  	if nil != err {
   441  		fmt.Println("CandidateDeposit fail", "err", err)
   442  	}
   443  	fmt.Println("CandidateDeposit success")
   444  
   445  	// CandidateApplyWithdraw(nodeId discover.NodeID, withdraw *big.Int) ([]byte, error)
   446  	withdraw := big.NewInt(100)
   447  	fmt.Println("CandidateApplyWithdraw input==>", "nodeId: ", nodeId.String(), "owner: ", owner.Hex(), "fee: ", fee, "host: ", host, "port: ", port, "extra: ", extra)
   448  	_, err = candidateContract.CandidateApplyWithdraw(nodeId, withdraw)
   449  	if nil != err {
   450  		fmt.Println("CandidateApplyWithdraw fail", "err", err)
   451  	}
   452  	fmt.Println("CandidateApplyWithdraw success")
   453  
   454  	// CandidateWithdraw(nodeId discover.NodeID) ([]byte, error)
   455  	fmt.Println("CandidateWithdraw input==>", "nodeId: ", nodeId.String())
   456  	_, err = candidateContract.CandidateWithdraw(nodeId)
   457  	if nil != err {
   458  		fmt.Println("CandidateWithdraw fail", "err", err)
   459  	}
   460  	fmt.Println("CandidateWithdraw success")
   461  
   462  	// GetCandidateWithdrawInfos(nodeId discover.NodeID) ([]byte, error)
   463  	fmt.Println("GetCandidateWithdrawInfos input==>", "nodeId: ", nodeId.String())
   464  	resByte, err := candidateContract.GetCandidateWithdrawInfos(nodeId)
   465  	if nil != err {
   466  		fmt.Println("GetCandidateWithdrawInfos fail", "err", err)
   467  		return
   468  	}
   469  	if nil == resByte {
   470  		fmt.Println("The GetCandidateWithdrawInfos is null")
   471  		return
   472  	}
   473  	fmt.Println("The GetCandidateWithdrawInfos is: ", vm.ResultByte2Json(resByte))
   474  }
   475  
   476  func TestTime(t *testing.T) {
   477  	fmt.Printf("Timestamp (ms):%v;\n", time.Now().UnixNano()/1e6)
   478  }
   479  
   480  func TestCandidatePoolEncode(t *testing.T) {
   481  	//"enode://1f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429@192.168.9.181:16789",
   482  	//"enode://751f4f62fccee84fc290d0c68d673e4b0cc6975a5747d2baccb20f954d59ba3315d7bfb6d831523624d003c8c2d33451129e67c3eef3098f711ef3b3e268fd3c@192.168.9.182:16789",
   483  	//"enode://b6c8c9f99bfebfa4fb174df720b9385dbd398de699ec36750af3f38f8e310d4f0b90447acbef64bdf924c4b59280f3d42bb256e6123b53e9a7e99e4c432549d6@192.168.9.183:16789",
   484  	//"enode://97e424be5e58bfd4533303f8f515211599fd4ffe208646f7bfdf27885e50b6dd85d957587180988e76ae77b4b6563820a27b16885419e5ba6f575f19f6cb36b0@192.168.9.184:16789"
   485  	nodeId := []byte("1f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429")
   486  	owner := []byte("0x740ce31b3fac20dac379db243021a51e80ad00d7")
   487  	extra := "{\"nodeName\": \"Platon-Shanghai\", \"nodePortrait\": \"\",\"nodeDiscription\": \"PlatON-eastern area\",\"nodeDepartment\": \"JUZIX\",\"officialWebsite\": \"https://www.platon.network/sz\",\"time\":1546503651100}"
   488  	// CandidateDeposit(nodeId discover.NodeID, owner common.Address, fee uint32, host, port, extra string)
   489  	var CandidateDeposit [][]byte
   490  	CandidateDeposit = make([][]byte, 0)
   491  	CandidateDeposit = append(CandidateDeposit, byteutil.Uint64ToBytes(1001))
   492  	CandidateDeposit = append(CandidateDeposit, []byte("CandidateDeposit"))
   493  	CandidateDeposit = append(CandidateDeposit, nodeId)
   494  	CandidateDeposit = append(CandidateDeposit, owner)
   495  	CandidateDeposit = append(CandidateDeposit, byteutil.Uint32ToBytes(7900))
   496  	//CandidateDeposit = append(CandidateDeposit, bigIntStrToBytes("130000000000000000000"))
   497  	CandidateDeposit = append(CandidateDeposit, []byte("0.0.0.0"))
   498  	CandidateDeposit = append(CandidateDeposit, []byte("30303"))
   499  	CandidateDeposit = append(CandidateDeposit, []byte(extra))
   500  	bufDeposit := new(bytes.Buffer)
   501  	err := rlp.Encode(bufDeposit, CandidateDeposit)
   502  	if err != nil {
   503  		fmt.Println(err)
   504  		t.Errorf("CandidateDeposit encode rlp data fail")
   505  	} else {
   506  		fmt.Println("CandidateDeposit data rlp: ", hexutil.Encode(bufDeposit.Bytes()))
   507  	}
   508  
   509  	// CandidateApplyWithdraw(nodeId discover.NodeID, withdraw *big.Int)
   510  	var CandidateApplyWithdraw [][]byte
   511  	CandidateApplyWithdraw = make([][]byte, 0)
   512  	CandidateApplyWithdraw = append(CandidateApplyWithdraw, byteutil.Uint64ToBytes(1002))
   513  	CandidateApplyWithdraw = append(CandidateApplyWithdraw, []byte("CandidateApplyWithdraw"))
   514  	CandidateApplyWithdraw = append(CandidateApplyWithdraw, nodeId)
   515  	withdraw, ok := new(big.Int).SetString("14d1120d7b160000", 16)
   516  	if !ok {
   517  		t.Errorf("big int setstring fail")
   518  	}
   519  	CandidateApplyWithdraw = append(CandidateApplyWithdraw, withdraw.Bytes())
   520  	bufApply := new(bytes.Buffer)
   521  	err = rlp.Encode(bufApply, CandidateApplyWithdraw)
   522  	if err != nil {
   523  		fmt.Println(err)
   524  		t.Errorf("CandidateApplyWithdraw encode rlp data fail")
   525  	} else {
   526  		fmt.Println("CandidateApplyWithdraw data rlp: ", hexutil.Encode(bufApply.Bytes()))
   527  	}
   528  
   529  	// CandidateWithdraw(nodeId discover.NodeID)
   530  	var CandidateWithdraw [][]byte
   531  	CandidateWithdraw = make([][]byte, 0)
   532  	CandidateWithdraw = append(CandidateWithdraw, byteutil.Uint64ToBytes(1003))
   533  	CandidateWithdraw = append(CandidateWithdraw, []byte("CandidateWithdraw"))
   534  	CandidateWithdraw = append(CandidateWithdraw, nodeId)
   535  	bufWith := new(bytes.Buffer)
   536  	err = rlp.Encode(bufWith, CandidateWithdraw)
   537  	if err != nil {
   538  		fmt.Println(err)
   539  		t.Errorf("CandidateWithdraw encode rlp data fail")
   540  	} else {
   541  		fmt.Println("CandidateWithdraw data rlp: ", hexutil.Encode(bufWith.Bytes()))
   542  	}
   543  
   544  	// GetCandidateWithdrawInfos(nodeId discover.NodeID)
   545  	var GetCandidateWithdrawInfos [][]byte
   546  	GetCandidateWithdrawInfos = make([][]byte, 0)
   547  	GetCandidateWithdrawInfos = append(GetCandidateWithdrawInfos, byteutil.Uint64ToBytes(0xf1))
   548  	GetCandidateWithdrawInfos = append(GetCandidateWithdrawInfos, []byte("GetCandidateWithdrawInfos"))
   549  	GetCandidateWithdrawInfos = append(GetCandidateWithdrawInfos, nodeId)
   550  	bufGetCandidateWithdrawInfos := new(bytes.Buffer)
   551  	err = rlp.Encode(bufGetCandidateWithdrawInfos, GetCandidateWithdrawInfos)
   552  	if err != nil {
   553  		fmt.Println(err)
   554  		t.Errorf("GetCandidateWithdrawInfos encode rlp data fail")
   555  	} else {
   556  		fmt.Println("GetCandidateWithdrawInfos data rlp: ", hexutil.Encode(bufGetCandidateWithdrawInfos.Bytes()))
   557  	}
   558  
   559  	// GetCandidateDetails(nodeIds []discover.NodeID)
   560  	var GetCandidateDetails [][]byte
   561  	nodeId1 := "0x1f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429"
   562  	nodeId2 := "0x2f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429"
   563  	nodeId3 := "0x3f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429"
   564  	nodeIds := nodeId1 + ":" + nodeId2 + ":" + nodeId3
   565  	GetCandidateDetails = make([][]byte, 0)
   566  	GetCandidateDetails = append(GetCandidateDetails, byteutil.Uint64ToBytes(0xf1))
   567  	GetCandidateDetails = append(GetCandidateDetails, []byte("GetCandidateDetails"))
   568  	GetCandidateDetails = append(GetCandidateDetails, []byte(nodeIds))
   569  	bufGetCandidateDetails := new(bytes.Buffer)
   570  	err = rlp.Encode(bufGetCandidateDetails, GetCandidateDetails)
   571  	if err != nil {
   572  		fmt.Println(err)
   573  		t.Errorf("GetCandidateDetails encode rlp data fail")
   574  	} else {
   575  		fmt.Println("GetCandidateDetails data rlp: ", hexutil.Encode(bufGetCandidateDetails.Bytes()))
   576  	}
   577  
   578  	// GetCandidateList()
   579  	var GetCandidateList [][]byte
   580  	GetCandidateList = make([][]byte, 0)
   581  	GetCandidateList = append(GetCandidateList, byteutil.Uint64ToBytes(0xf1))
   582  	GetCandidateList = append(GetCandidateList, []byte("GetCandidateList"))
   583  	bufGetCandidateList := new(bytes.Buffer)
   584  	err = rlp.Encode(bufGetCandidateList, GetCandidateList)
   585  	if err != nil {
   586  		fmt.Println(err)
   587  		t.Errorf("GetCandidateList encode rlp data fail")
   588  	} else {
   589  		fmt.Println("GetCandidateList data rlp: ", hexutil.Encode(bufGetCandidateList.Bytes()))
   590  	}
   591  
   592  	// GetVerifiersList()
   593  	var GetVerifiersList [][]byte
   594  	GetVerifiersList = make([][]byte, 0)
   595  	GetVerifiersList = append(GetVerifiersList, byteutil.Uint64ToBytes(0xf1))
   596  	GetVerifiersList = append(GetVerifiersList, []byte("GetVerifiersList"))
   597  	bufGetVerifiersList := new(bytes.Buffer)
   598  	err = rlp.Encode(bufGetVerifiersList, GetVerifiersList)
   599  	if err != nil {
   600  		fmt.Println(err)
   601  		t.Errorf("GetVerifiersList encode rlp data fail")
   602  	} else {
   603  		fmt.Println("GetVerifiersList data rlp: ", hexutil.Encode(bufGetVerifiersList.Bytes()))
   604  	}
   605  
   606  }
   607  
   608  func TestCandidatePoolDecode(t *testing.T) {
   609  
   610  	//HexString -> []byte
   611  	rlpcode, _ := hex.DecodeString("f8a28800000000000003e88a566f74655469636b6574820008880de0b6b3a7640000b8803131346534386632316434643833656339616333396136323036326138303461303536363734326438306231393164653562613233613464633235663762656461306537386464313639333532613761643362313135383464303661303161303963653034376164383864653962646362363338383565383164653030613464")
   612  	var source [][]byte
   613  	if err := rlp.Decode(bytes.NewReader(rlpcode), &source); err != nil {
   614  		fmt.Println(err)
   615  		t.Errorf("TestRlpDecode decode rlp data fail")
   616  	}
   617  
   618  	for i, v := range source {
   619  		switch i {
   620  		case 0:
   621  			fmt.Println("i: ", i, " v: ", byteutil.BytesTouint64(v))
   622  		case 2:
   623  			fmt.Println("i: ", i, " v: ", byteutil.BytesTouint32(v))
   624  		default:
   625  			fmt.Println("i: ", i, " v: ", string(v))
   626  		}
   627  
   628  	}
   629  }