github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/http/router/mnchain_test.go (about)

     1  package router_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"math/big"
     9  	"net/http"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/sixexorg/magnetic-ring/http/cli"
    14  	"github.com/sixexorg/magnetic-ring/signer/mainchain/signature"
    15  
    16  	"github.com/sixexorg/magnetic-ring/mock"
    17  
    18  	"github.com/sixexorg/magnetic-ring/common"
    19  	"github.com/sixexorg/magnetic-ring/common/sink"
    20  	"github.com/sixexorg/magnetic-ring/core/mainchain/types"
    21  	orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types"
    22  	"github.com/sixexorg/magnetic-ring/http/req"
    23  	txpool "github.com/sixexorg/magnetic-ring/txpool/mainchain"
    24  )
    25  
    26  var (
    27  	//baseurl = "http://192.168.10.166:10086"
    28  	baseurl = "http://127.0.0.1:20086"
    29  	//baseurl  = "http://192.168.9.123:10086"
    30  	leagueId           = getLeagueId(mock.AccountNormal_1.Addr, mock.AccountNormal_2.Addr, 2).ToString()
    31  	leagueIdAddress, _ = common.ToAddress(leagueId)
    32  )
    33  
    34  func TestAccount(t *testing.T) {
    35  	fmt.Println(mock.AccountNormal_1.Address().ToString())
    36  	fmt.Println(leagueId)
    37  }
    38  
    39  func TestTransferBox(t *testing.T) {
    40  	TransferBox(5,t)
    41  }
    42  
    43  func TestTransferEnergy(t *testing.T) {
    44  	TransferEnergy(t)
    45  }
    46  
    47  func TestCreateLeague(t *testing.T) {
    48  	CreateLeague(t)
    49  }
    50  
    51  func TestEnergyToLeague(t *testing.T) {
    52  	EnergyToLeague(t)
    53  }
    54  
    55  func TransferBox(nonce uint64,t *testing.T) {
    56  	var (
    57  		from = mock.AccountNormal_1.Addr.ToString()
    58  		to   = "ct1j95gw45wnmdazmgpux7wc25tTrT5ExXg"
    59  
    60  		//nonce, leagueNonce uint64 = 4, 2
    61  
    62  		tp types.TransactionType = types.TransferBox
    63  	)
    64  	/*TransferBox
    65  	TransferEnergy
    66  	CreateLeague
    67  	JoinLeague
    68  	EnergyToLeague */
    69  	//nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType)
    70  	tx := transactionBuild(nonce, 2, from, to, leagueId, tp)
    71  	err := tx.ToRaw()
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	req := req.ReqSendTx{
    76  		Raw: tx.Raw,
    77  	}
    78  	fullurl := fmt.Sprintf("%s/block/sendtx", baseurl)
    79  	resp, err := cli.HttpSend(fullurl, req)
    80  	if err != nil {
    81  		t.Log(err)
    82  		return
    83  	}
    84  	t.Logf("txhash=%s,\n", resp)
    85  }
    86  
    87  func TransferEnergy(t *testing.T) {
    88  	var (
    89  		from = mock.AccountNormal_1.Addr.ToString()
    90  		to   = "ct1j95gw45wnmdazmgpux7wc25tTrT5ExXg" //"ct2j9ywuyqz8VW7ZGBBd26V4VqAWd4r43Vs"
    91  
    92  		nonce, leagueNonce uint64 = 6, 2
    93  
    94  		tp types.TransactionType = types.TransferEnergy
    95  	)
    96  	/*TransferBox
    97  	TransferEnergy
    98  	CreateLeague
    99  	JoinLeague
   100  	EnergyToLeague */
   101  	//nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType)
   102  	tx := transactionBuild(nonce, leagueNonce, from, to, leagueId, tp)
   103  	err := tx.ToRaw()
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  	req := req.ReqSendTx{
   108  		Raw: tx.Raw,
   109  	}
   110  	fullurl := fmt.Sprintf("%s/block/sendtx", baseurl)
   111  	resp, err := cli.HttpSend(fullurl, req)
   112  	if err != nil {
   113  		t.Log(err)
   114  		return
   115  	}
   116  	t.Logf("txhash=%s,\n", resp)
   117  }
   118  
   119  func EnergyToLeague(t *testing.T) {
   120  	var (
   121  		from = mock.AccountNormal_1.Addr.ToString()
   122  		to   = "ct1j95gw45wnmdazmgpux7wc25tTrT5ExXg"
   123  
   124  		nonce, leagueNonce uint64 = 3, 2
   125  
   126  		tp types.TransactionType = types.TransferEnergy
   127  	)
   128  	/*TransferBox
   129  	TransferEnergy
   130  	CreateLeague
   131  	JoinLeague
   132  	EnergyToLeague */
   133  	//nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType)
   134  	tx := transactionBuild(nonce, leagueNonce, from, to, leagueId, tp)
   135  	err := tx.ToRaw()
   136  	if err != nil {
   137  		panic(err)
   138  	}
   139  	req := req.ReqSendTx{
   140  		Raw: tx.Raw,
   141  	}
   142  	fullurl := fmt.Sprintf("%s/block/sendtx", baseurl)
   143  	resp, err := cli.HttpSend(fullurl, req)
   144  	if err != nil {
   145  		t.Log(err)
   146  		return
   147  	}
   148  	t.Logf("txhash=%s,\n", resp)
   149  }
   150  
   151  func TestSendTx(t *testing.T) {
   152  	var (
   153  		to = "ct0fHcR19s2nDaHxR3CVdUyYxHgsjzp8wrt"
   154  
   155  		nonce, leagueNonce uint64 = 1, 2
   156  		from                      = mock.AccountNormal_1.Addr.ToString()
   157  
   158  		tp types.TransactionType = types.JoinLeague
   159  	)
   160  	/*TransferBox
   161  	TransferEnergy
   162  	CreateLeague
   163  	JoinLeague
   164  	EnergyToLeague */
   165  	//nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType)
   166  	tx := transactionBuild(nonce, leagueNonce, from, to, leagueId, tp)
   167  	tx.Sigs = new(common.SigPack)
   168  	tx.Templt = common.NewSigTmplt()
   169  
   170  	maue := make(common.Maus, 0)
   171  	mau := common.Mau{}
   172  	mau.M = 1
   173  	mau.Pubks = make([]common.PubHash, 0)
   174  
   175  	pubkstr := "04f52f8ccc9de7496af4460da2bfe8762fd64dfabada237542824d327b350f415c276c92c56656f2cfa30c93c852609ef7bf55234bca3106d5e588acb025929356"
   176  	puhash := common.PubHash{}
   177  
   178  	pkbuf, _ := common.Hex2Bytes(pubkstr)
   179  
   180  	t.Logf("pkbuflen=%d\n", len(pkbuf))
   181  
   182  	copy(puhash[:], pkbuf[:])
   183  
   184  	mau.Pubks = append(mau.Pubks, puhash)
   185  
   186  	maue = append(maue, mau)
   187  
   188  	tx.Templt.SigData = append(tx.Templt.SigData, &maue)
   189  
   190  	fromAddr, err := common.ToAddress(from)
   191  	if err != nil {
   192  		fmt.Printf("common to address error=%v\n", err)
   193  		return
   194  	}
   195  
   196  	fmt.Printf("👀 %s\n", fromAddr.ToString())
   197  
   198  	err = signature.SignTransaction(mock.Mgr, fromAddr, tx, "jjjkkk")
   199  	if err != nil {
   200  		fmt.Printf("sign transaction error=%v\n", err)
   201  		return
   202  	}
   203  
   204  	be, eb := tx.VerifySignature()
   205  	if eb != nil {
   206  		fmt.Printf("eb=%v\n", eb)
   207  		return
   208  	}
   209  	fmt.Printf("be-🎶 🎶 🎶 🎶 🎶 🎶 🎶->%t\n", be)
   210  
   211  	err = tx.ToRaw()
   212  	if err != nil {
   213  		panic(err)
   214  	}
   215  	////return
   216  	//ra := common.Hex2Bytes("0110600008f85ef85cf85af858954a3dcbc73c7f8a5a5b751078347790a2412d206300b84078d07af214b8cbad56f92fcd7d2ae5f82489678258f0c6ad1aa7bad4bea6d24dcd9305fa590584beafa42400431ec960d838981c3ac0901de0c911df3f34b2bf4e0009f84cf84af848f84601f843b84104f992c8418749fc5d4bbcaffc4ff120b20b5550f0b5744cff2dd55ad1bfa7f7db94891af2315443a35330a6527436037b7b3d6de5c41e16171fd461e73b5434500100036443d34df4118065f93c4bee56b5d61d4a3b3d6918011d0001dcdbda9543d34df4118065f93c4bee56b5d61d4a3b3d6918018203e80200000000000000000000000000000000000000000000000000000000000000001c0002dbdad9957417702301a3130edc4bdb35ef760fc47f53f2f9008203e8")
   217  	req := req.ReqSendTx{
   218  		Raw: tx.Raw,
   219  	}
   220  	fullurl := fmt.Sprintf("%s/block/sendtx", baseurl)
   221  	resp, err := cli.HttpSend(fullurl, req)
   222  	if err != nil {
   223  		t.Log(err)
   224  		return
   225  	}
   226  	t.Logf("txhash=%s,\n", resp)
   227  }
   228  
   229  func CreateLeague(t *testing.T) {
   230  	var (
   231  		//from                      = "ct0fHcR19s2nDaHxR3CVdUyYxHgsjzp8wrt"
   232  		to = mock.AccountNormal_1.Addr.ToString()
   233  
   234  		nonce, leagueNonce uint64 = 2, 2
   235  		from                      = mock.AccountNormal_1.Addr.ToString()
   236  
   237  		tp types.TransactionType = types.CreateLeague
   238  	)
   239  	/*TransferBox
   240  	TransferEnergy
   241  	CreateLeague
   242  	JoinLeague
   243  	EnergyToLeague */
   244  	//nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType)
   245  	tx := transactionBuild(nonce, leagueNonce, from, to, leagueId, tp)
   246  	err := tx.ToRaw()
   247  	if err != nil {
   248  		panic(err)
   249  	}
   250  	req := req.ReqSendTx{
   251  		Raw: tx.Raw,
   252  	}
   253  
   254  	leagueId := types.ToLeagueAddress(tx)
   255  	fmt.Printf("leagueId=%s\n", leagueId.ToString())
   256  
   257  	fullurl := fmt.Sprintf("%s/block/sendtx", baseurl)
   258  	resp, err := cli.HttpSend(fullurl, req)
   259  	if err != nil {
   260  		t.Log(err)
   261  		return
   262  	}
   263  	t.Logf("txhash=%s,\n", resp)
   264  }
   265  
   266  func TestSubDynamicTxSend(t *testing.T) {
   267  	var (
   268  		nonce     uint64                   = 2
   269  		from                               = mock.AccountNormal_1.Addr.ToString()
   270  		to                                 = "ct0fHcR19s2nDaHxR3CVdUyYxHgsjzp8wrt"
   271  		tp        orgtypes.TransactionType = orgtypes.TransferUT
   272  		voteReply                          = orgtypes.Agree
   273  		txhash                             = "52fcc88ab3521b1fbcc0cf2e7132cb67540c3826b390dcbc0fa6c7d744c7d149"
   274  	)
   275  	/*TransferUT
   276  	ReplyVote
   277  	VoteIncreaseUT
   278  	GetBonus
   279  	Join
   280  	Leave
   281  	EnergyFromMain
   282  	EnergyToMain   */
   283  	//nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType)
   284  	tx := orgTransactionBuild(nonce, from, to, tp, txhash, voteReply)
   285  	err := tx.ToRaw()
   286  
   287  	//fmt.Println("hash ", tx.Hash())
   288  	sk := sink.NewZeroCopySource(tx.Raw)
   289  	txTmp := &orgtypes.Transaction{}
   290  	err = txTmp.Deserialization(sk)
   291  	if err != nil {
   292  		fmt.Println(err)
   293  		return
   294  	}
   295  	if err != nil {
   296  		t.Errorf("toraw err-->%v\n", err)
   297  		return
   298  	}
   299  	//ct2aQyCjVfJ5vgh8x8xb2yRbTu6741Yg8u7
   300  	fmt.Printf("••••••••••••••••••••••••••••herhe-->%s\n", leagueId)
   301  
   302  	req := req.ReqSendTx{
   303  		LeagueId: leagueId,
   304  		Raw:      tx.Raw,
   305  	}
   306  	fullurl := fmt.Sprintf("%s/orgapi/sendsubtx", baseurl)
   307  	resp, err := cli.HttpSend(fullurl, req)
   308  	if err != nil {
   309  		t.Log(err)
   310  		return
   311  	}
   312  	t.Logf("txhash=%s,\n", resp)
   313  }
   314  
   315  func TestRegistGetTxs(t *testing.T) {
   316  
   317  	fullurl := fmt.Sprintf("%s/block/gettxs", baseurl)
   318  
   319  	official, _ := common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda")
   320  
   321  	req := req.ReqGetTxs{
   322  		Addr: official.ToString(),
   323  	}
   324  
   325  	reqbuf, err := json.Marshal(req)
   326  	if err != nil {
   327  		fmt.Println(err.Error())
   328  		return
   329  	}
   330  
   331  	buffer := bytes.NewBuffer(reqbuf)
   332  	request, err := http.NewRequest("GET", fullurl, buffer)
   333  	if err != nil {
   334  		fmt.Println(err.Error())
   335  		return
   336  	}
   337  	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
   338  	client := http.Client{}
   339  	resp, err := client.Do(request)
   340  
   341  	buf, err := ioutil.ReadAll(resp.Body)
   342  	if err != nil {
   343  		t.Logf("an error occured when ioutil.readall,err-->%v\n", err)
   344  		return
   345  	}
   346  
   347  	t.Logf("resp:%s\n", buf)
   348  }
   349  
   350  func TestAddSyncOrg(t *testing.T) {
   351  
   352  	fullurl := fmt.Sprintf("%s/block/syncorgdata", baseurl)
   353  
   354  	leagueId := "ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda"
   355  
   356  	req := req.ReqSyncOrgData{
   357  		LeagueId: leagueId,
   358  		BAdd:     true,
   359  	}
   360  
   361  	reqbuf, err := json.Marshal(req)
   362  	if err != nil {
   363  		fmt.Println(err.Error())
   364  		return
   365  	}
   366  
   367  	buffer := bytes.NewBuffer(reqbuf)
   368  	request, err := http.NewRequest("GET", fullurl, buffer)
   369  	if err != nil {
   370  		fmt.Println(err.Error())
   371  		return
   372  	}
   373  	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
   374  	client := http.Client{}
   375  	resp, err := client.Do(request)
   376  
   377  	buf, err := ioutil.ReadAll(resp.Body)
   378  	if err != nil {
   379  		t.Logf("an error occured when ioutil.readall,err-->%v\n", err)
   380  		return
   381  	}
   382  
   383  	t.Logf("resp:%s\n", buf)
   384  }
   385  
   386  func TestSendSubTx(t *testing.T) {
   387  
   388  	fullurl := fmt.Sprintf("%s/block/sendsubtx", baseurl)
   389  
   390  	official := mock.Mock_Address_1
   391  	txins := &common.TxIns{}
   392  	txin := common.TxIn{
   393  
   394  		Address: official,
   395  		Amount:  big.NewInt(100),
   396  		Nonce:   3,
   397  	}
   398  	txins.Tis = append(txins.Tis, &txin)
   399  
   400  	to, _ := common.ToAddress("ct04xYt91DuMWGB7W1FhGbF3UfY7akq4bf9")
   401  	totxins := &common.TxOuts{}
   402  	totxin := common.TxOut{
   403  		Address: to,
   404  		Amount:  big.NewInt(100),
   405  	}
   406  	totxins.Tos = append(totxins.Tos, &totxin)
   407  
   408  	msg, _ := common.StringToHash("hello")
   409  	txdata := orgtypes.TxData{
   410  
   411  		From:  official,
   412  		Froms: txins,
   413  		Tos:   totxins,
   414  		Msg:   msg,
   415  		Fee:   big.NewInt(1001),
   416  		Nonce: 1,
   417  	}
   418  
   419  	tx, err := orgtypes.NewTransactionRaw(orgtypes.TransferUT, types.TxVersion, &txdata)
   420  	err = tx.ToRaw()
   421  	if err != nil {
   422  		t.Errorf("toraw err-->%v\n", err)
   423  		return
   424  	}
   425  
   426  	req := req.ReqSendTx{
   427  		LeagueId: "ct2b5xc5uyaJrSqZ866gVvM5QtXybprb9sy",
   428  		Raw:      tx.Raw,
   429  	}
   430  
   431  	reqbuf, err := json.Marshal(req)
   432  	if err != nil {
   433  		fmt.Println(err.Error())
   434  		return
   435  	}
   436  
   437  	buffer := bytes.NewBuffer(reqbuf)
   438  	request, err := http.NewRequest("POST", fullurl, buffer)
   439  	if err != nil {
   440  		fmt.Println(err.Error())
   441  		return
   442  	}
   443  	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
   444  	client := http.Client{}
   445  	resp, err := client.Do(request)
   446  
   447  	buf, err := ioutil.ReadAll(resp.Body)
   448  	if err != nil {
   449  		t.Logf("an error occured when ioutil.readall,err-->%v\n", err)
   450  		return
   451  	}
   452  
   453  	respobj := new(txpool.TxResp)
   454  	err = json.Unmarshal(buf, respobj)
   455  
   456  	if err != nil {
   457  		t.Errorf("err=%v\n", err)
   458  		return
   459  	}
   460  
   461  	t.Logf("txhash=%s,error info=%s\n", respobj.Hash, respobj.Desc)
   462  }
   463  
   464  func TestTime(t *testing.T)  {
   465  
   466  	ok := Ok{Tm:time.Now()}
   467  
   468  	okbuf,_ := json.Marshal(ok)
   469  	fmt.Printf("okbuf=%s\n",okbuf)
   470  
   471  
   472  
   473  }
   474  
   475  type Ok struct {
   476  	Tm time.Time `json:"tm"`
   477  }