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

     1  package router_test
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/sixexorg/magnetic-ring/common"
     7  	maintypes "github.com/sixexorg/magnetic-ring/core/mainchain/types"
     8  
     9  	"testing"
    10  
    11  	"fmt"
    12  
    13  	orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types"
    14  	"github.com/sixexorg/magnetic-ring/mock"
    15  )
    16  
    17  var (
    18  	nodeId        = mock.AccountNormal_2.Addr
    19  	symbolStr, _  = common.String2Symbol("ABCD1")
    20  	ceateLeagueTx = &maintypes.Transaction{
    21  		Version: maintypes.TxVersion,
    22  		TxType:  maintypes.CreateLeague,
    23  		TxData: &maintypes.TxData{
    24  			From:    mock.AccountNormal_1.Addr,
    25  			Nonce:   1,
    26  			Fee:     big.NewInt(100),
    27  			Rate:    1000000000,
    28  			MinBox:  10,
    29  			MetaBox: big.NewInt(50),
    30  			NodeId:  nodeId,
    31  			Private: false,
    32  			Symbol:  symbolStr,
    33  		}}
    34  )
    35  
    36  func TestExecLeagueId(t *testing.T) {
    37  	nonce := uint64(3)
    38  	fromAddr, _ := common.ToAddress("ct0fHcR19s2nDaHxR3CVdUyYxHgsjzp8wrt")
    39  	tx := &maintypes.Transaction{
    40  		Version: maintypes.TxVersion,
    41  		TxType:  maintypes.CreateLeague,
    42  		TxData: &maintypes.TxData{
    43  			From:    fromAddr,
    44  			Nonce:   nonce,
    45  			Fee:     big.NewInt(100),
    46  			Rate:    1000000000,
    47  			MinBox:  10,
    48  			MetaBox: big.NewInt(50),
    49  			NodeId:  nodeId,
    50  			Private: true,
    51  		},
    52  	}
    53  	fmt.Println(maintypes.ToLeagueAddress(tx).ToString())
    54  }
    55  
    56  func transactionBuild(nonce, leagueNonce uint64, from, to, league string, tp maintypes.TransactionType) *maintypes.Transaction {
    57  	fromAddr, _ := common.ToAddress(from)
    58  	toAddr, _ := common.ToAddress(to)
    59  
    60  	leagueId, _ := common.ToAddress(league)
    61  	var transquantity int64 = 10e11
    62  	switch tp {
    63  	case maintypes.TransferBox, maintypes.TransferEnergy:
    64  		txins := &common.TxIns{}
    65  		txout := common.TxIn{
    66  			Address: fromAddr,
    67  			Amount:  big.NewInt(transquantity),
    68  			Nonce:   nonce,
    69  		}
    70  		txins.Tis = append(txins.Tis, &txout)
    71  
    72  		totxins := &common.TxOuts{}
    73  		totxin := common.TxOut{
    74  			Address: toAddr,
    75  			Amount:  big.NewInt(transquantity),
    76  		}
    77  		totxins.Tos = append(totxins.Tos, &totxin)
    78  		txdata := maintypes.TxData{
    79  			From:  fromAddr,
    80  			Froms: txins,
    81  			Tos:   totxins,
    82  			Fee:   big.NewInt(10000000000),
    83  		}
    84  		tx, err := maintypes.NewTransaction(tp, maintypes.TxVersion, &txdata)
    85  		if err != nil {
    86  			panic(err)
    87  		}
    88  		return tx
    89  	case maintypes.EnergyToLeague:
    90  		tx := &maintypes.Transaction{
    91  			Version: maintypes.TxVersion,
    92  			TxType:  tp,
    93  			TxData: &maintypes.TxData{
    94  				From:     fromAddr,
    95  				Nonce:    nonce,
    96  				Fee:      big.NewInt(10),
    97  				LeagueId: leagueId,
    98  				Energy:     big.NewInt(10000000000),
    99  			},
   100  		}
   101  		return tx
   102  	case maintypes.CreateLeague:
   103  		tx := ceateLeagueTx
   104  		fmt.Printf("mock1=%s\n", mock.AccountNormal_1.Addr.ToString())
   105  		fmt.Printf("nodeidsdafjsdajsdjsjsjsjd👠=%s\n", nodeId.ToString())
   106  
   107  		tx.TxData.From = fromAddr
   108  		tx.TxData.Nonce = nonce
   109  		return tx
   110  	case maintypes.JoinLeague:
   111  		tx := &maintypes.Transaction{
   112  			Version: maintypes.TxVersion,
   113  			TxType:  tp,
   114  			TxData: &maintypes.TxData{
   115  				From:        fromAddr,
   116  				Nonce:       nonce,
   117  				Account:     fromAddr,
   118  				LeagueNonce: leagueNonce,
   119  				Fee:         big.NewInt(100),
   120  				LeagueId:    leagueId,
   121  				MinBox:      10,
   122  			},
   123  		}
   124  		return tx
   125  	case maintypes.RecycleMinbox:
   126  		tx := &maintypes.Transaction{
   127  			Version: maintypes.TxVersion,
   128  			TxType:  tp,
   129  			TxData: &maintypes.TxData{
   130  				From:     fromAddr,
   131  				Nonce:    nonce,
   132  				Fee:      big.NewInt(100),
   133  				LeagueId: leagueId,
   134  				MinBox:   10,
   135  			},
   136  		}
   137  		return tx
   138  	case maintypes.GetBonus:
   139  		tx := &maintypes.Transaction{
   140  			Version: maintypes.TxVersion,
   141  			TxType:  tp,
   142  			TxData: &maintypes.TxData{
   143  				From:  fromAddr,
   144  				Nonce: nonce,
   145  				Fee:   big.NewInt(100),
   146  			},
   147  		}
   148  		return tx
   149  	}
   150  	return nil
   151  }
   152  func TestLeagueId(t *testing.T) {
   153  	fmt.Println(getLeagueId(mock.AccountNormal_1.Addr, mock.AccountNormal_2.Addr, 2).ToString())
   154  	fmt.Println(mock.AccountNormal_1.Addr.ToString())
   155  }
   156  
   157  func getLeagueId(account, node common.Address, nonce uint64) common.Address {
   158  	tx := ceateLeagueTx
   159  	tx.TxData.From = account
   160  	tx.TxData.NodeId = node
   161  	tx.TxData.Nonce = nonce
   162  	return maintypes.ToLeagueAddress(tx)
   163  }
   164  func buildLeagueRaw(tx *maintypes.Transaction) []byte {
   165  	var (
   166  		orgTp orgtypes.TransactionType
   167  		orgtx = &orgtypes.Transaction{
   168  			Version: maintypes.TxVersion,
   169  			TxType:  orgTp,
   170  			TxData: &orgtypes.TxData{
   171  				From:   tx.TxData.From,
   172  				TxHash: tx.Hash(),
   173  			},
   174  		}
   175  	)
   176  	switch tx.TxType {
   177  	case maintypes.EnergyToLeague:
   178  		orgTp = orgtypes.EnergyFromMain
   179  		orgtx.TxData.Energy = tx.TxData.Energy
   180  		break
   181  	case maintypes.JoinLeague:
   182  		orgTp = orgtypes.Join
   183  		break
   184  	}
   185  	orgtx.TxType = orgTp
   186  	err := orgtx.ToRaw()
   187  	if err != nil {
   188  		return nil
   189  	}
   190  	return orgtx.Raw
   191  }