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

     1  package router
     2  
     3  import (
     4  	"github.com/sixexorg/magnetic-ring/http/resp"
     5  	"net/http"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/sixexorg/magnetic-ring/common"
     9  	"github.com/sixexorg/magnetic-ring/common/sink"
    10  	"github.com/sixexorg/magnetic-ring/core/mainchain/types"
    11  	"github.com/sixexorg/magnetic-ring/http/actor"
    12  	"github.com/sixexorg/magnetic-ring/http/req"
    13  	"github.com/sixexorg/magnetic-ring/log"
    14  	"github.com/sixexorg/magnetic-ring/store/mainchain/storages"
    15  	txpool "github.com/sixexorg/magnetic-ring/txpool/mainchain"
    16  )
    17  
    18  var (
    19  	blkrg *gin.RouterGroup
    20  
    21  	accountTxMap map[string]types.Transactions
    22  )
    23  
    24  func TempPutTransaction(tx *types.Transaction) {
    25  	if accountTxMap[tx.TxData.From.ToString()] == nil {
    26  		accountTxMap[tx.TxData.From.ToString()] = make(types.Transactions, 0)
    27  	}
    28  	txns := accountTxMap[tx.TxData.From.ToString()]
    29  
    30  	txns = append(txns, tx)
    31  
    32  	accountTxMap[tx.TxData.From.ToString()] = txns
    33  }
    34  
    35  func TempPutTransactions(txs types.Transactions) {
    36  	for _, tx := range txs {
    37  		TempPutTransaction(tx)
    38  	}
    39  }
    40  
    41  func StartRouter() {
    42  	accountTxMap = make(map[string]types.Transactions)
    43  
    44  	router := GetRouter()
    45  	blkrg = router.Group("block", func(c *gin.Context) {})
    46  
    47  	InitMnBlockChainRouters()
    48  
    49  	StartLeagueRouter()
    50  }
    51  
    52  func InitMnBlockChainRouters() {
    53  	blkrg.POST("sendtx", sendTx)
    54  	blkrg.POST("gettxs", getTxs)
    55  	blkrg.POST("getbalance", getBalance)
    56  	blkrg.POST("getaccountnonce", getAccountNonce)
    57  	blkrg.POST("getcurrentblockheight", getCurrentBlockHeight)
    58  	blkrg.POST("getcurrentblock", getCurrentBlock)
    59  	blkrg.POST("gettxbytxhash", getTxByHash)
    60  	blkrg.POST("syncorgdata", syncOrgData)
    61  	blkrg.POST("getblockbyheight", getBlockByHeight)
    62  	blkrg.POST("getrefblock", getAccountBlocks)                
    63  }
    64  
    65  func sendTx(c *gin.Context) {
    66  	cl := new(req.ReqSendTx)
    67  	err := c.BindJSON(cl)
    68  	if err != nil {
    69  		log.Info("an error occured when bindjson", "err", err)
    70  		return
    71  	}
    72  
    73  	log.Info("sendtx", "ReqSendTx", cl)
    74  
    75  	tx := &types.Transaction{}
    76  	source := sink.NewZeroCopySource(cl.Raw)
    77  	err = tx.Deserialization(source)
    78  	if err != nil {
    79  		log.Info("transaction Deserialization error", "err", err)
    80  		return
    81  	}
    82  
    83  	log.Info("magnetic receive transaction", "tx", tx)
    84  
    85  	hx, desc := httpactor.AppendTxToPool(tx)
    86  	desc = "success"
    87  
    88  	log.Info("magnetic append transaction to pool", "tx", tx)
    89  
    90  	log.Info("watch the hash", "hash", hx)
    91  	resp := txpool.TxResp{
    92  		Hash: hx.String(),
    93  		Desc: desc,
    94  	}
    95  
    96  	c.JSON(http.StatusOK, resp)
    97  }
    98  
    99  func getTxs(c *gin.Context) {
   100  	cl := new(req.ReqGetTxs)
   101  	err := c.BindJSON(cl)
   102  	if err != nil {
   103  		log.Info("an error occured when bindjson", "error", err)
   104  		return
   105  	}
   106  
   107  	log.Info("gettx", "GetTxs", cl)
   108  
   109  	txs := accountTxMap[cl.Addr]
   110  
   111  	mp := make(map[string]interface{})
   112  	mp["txs"] = txs
   113  
   114  	c.JSON(http.StatusOK, mp)
   115  }
   116  
   117  func getAccountNonce(c *gin.Context) {
   118  	cl := new(req.ReqAccountNounce)
   119  	err := c.BindJSON(cl)
   120  	if err != nil {
   121  		log.Info("an error occured when bindjson", "error", err)
   122  		return
   123  	}
   124  	mp := make(map[string]interface{})
   125  
   126  	accountAddress, err := common.ToAddress(cl.Addr)
   127  	account, err := storages.GetLedgerStore().GetAccount(accountAddress)
   128  	if err != nil {
   129  		c.JSON(http.StatusOK, mp)
   130  		return
   131  	}
   132  
   133  	nonce := account.Data.Nonce
   134  
   135  	mp["nonce"] = nonce
   136  
   137  	c.JSON(http.StatusOK, mp)
   138  }
   139  
   140  func getBalance(c *gin.Context) {
   141  	cl := new(req.ReqGetBalance)
   142  	err := c.BindJSON(cl)
   143  	if err != nil {
   144  		log.Info("an error occured when bindjson", "error", err)
   145  		return
   146  	}
   147  
   148  	address, err := common.ToAddress(cl.Addr)
   149  	if err != nil {
   150  		mp := make(map[string]interface{})
   151  		mp["crystal"] = 0
   152  		mp["energy"] = 0
   153  		c.JSON(http.StatusOK, mp)
   154  		return
   155  	}
   156  
   157  	account, err := storages.GetLedgerStore().GetAccount(address)
   158  	if err != nil {
   159  		panic(err)
   160  	}
   161  	if account == nil {
   162  		mp := make(map[string]interface{})
   163  		mp["crystal"] = 0
   164  		mp["energy"] = 0
   165  		c.JSON(http.StatusOK, mp)
   166  		return
   167  	}
   168  
   169  	crystal := account.Data.Balance
   170  	energy := account.Data.EnergyBalance
   171  
   172  	mp := make(map[string]interface{})
   173  	mp["crystal"] = crystal
   174  	mp["energy"] = energy
   175  
   176  	c.JSON(http.StatusOK, mp)
   177  }
   178  
   179  func getCurrentBlockHeight(c *gin.Context) {
   180  
   181  	mp := make(map[string]interface{})
   182  	mp["height"] = storages.GetLedgerStore().GetCurrentBlockHeight()
   183  
   184  	c.JSON(http.StatusOK, mp)
   185  }
   186  
   187  func getCurrentBlock(c *gin.Context) {
   188  
   189  	mp := make(map[string]interface{})
   190  	block, _ := storages.GetLedgerStore().GetCurrentBlock()
   191  	mp["block"] = block
   192  
   193  	c.JSON(http.StatusOK, mp)
   194  }
   195  
   196  func getTxByHash(c *gin.Context) {
   197  	req := new(req.ReqGetTxByHash)
   198  	err := c.BindJSON(req)
   199  
   200  	if err != nil {
   201  		log.Info("an error occured when bindjson", "error", err)
   202  		return
   203  	}
   204  
   205  	log.Info("get current block height")
   206  
   207  	hash, err := common.StringToHash(req.TxHash)
   208  	if err != nil {
   209  		log.Info("an error occured when convert string 2 hash", "error", err)
   210  		return
   211  	}
   212  
   213  	tx, _, err := storages.GetLedgerStore().GetTxByHash(hash)
   214  
   215  	if err != nil {
   216  		log.Info("an error occured when get tx by hash", "error", err)
   217  		return
   218  	}
   219  
   220  	respobj := resp.RespGetTxByHash{
   221  		tx,
   222  	}
   223  
   224  	c.JSON(http.StatusOK, respobj)
   225  }
   226  
   227  func syncOrgData(c *gin.Context) {
   228  	req := new(req.ReqSyncOrgData)
   229  	err := c.BindJSON(req)
   230  
   231  	if err != nil {
   232  		log.Info("an error occured when bindjson", "error", err)
   233  		return
   234  	}
   235  
   236  	leagueAddr, err := common.ToAddress(req.LeagueId)
   237  	if err != nil {
   238  		log.Error("convert string to league address error", "err", err)
   239  		return
   240  	}
   241  
   242  	httpactor.TellP2pSync(leagueAddr, req.BAdd)
   243  
   244  	mp := make(map[string]interface{})
   245  	mp["result"] = "success"
   246  
   247  	c.JSON(http.StatusOK, mp)
   248  }
   249  
   250  func getBlockByHeight(c *gin.Context) {
   251  	req := new(req.ReqGetBlockByHeight)
   252  	err := c.BindJSON(req)
   253  
   254  	if err != nil {
   255  		log.Info("an error occured when bindjson", "error", err)
   256  		return
   257  	}
   258  
   259  	mp := make(map[string]interface{})
   260  	block, _ := storages.GetLedgerStore().GetBlockByHeight(req.Height)
   261  	mp["block"] = block
   262  
   263  	c.JSON(http.StatusOK, mp)
   264  }
   265  
   266  func getAccountBlocks(c *gin.Context) {
   267  	req := new(req.ReqGetRefBlock)
   268  	err := c.BindJSON(req)
   269  
   270  	if err != nil {
   271  		log.Info("an error occured when bindjson", "error", err)
   272  		return
   273  	}
   274  
   275  	mp := make(map[string]interface{})
   276  
   277  	leagueAddr, err := common.ToAddress(req.Account)
   278  	if err != nil {
   279  		log.Error("convert string to league address error", "err", err)
   280  		return
   281  	}
   282  
   283  	tres, err := storages.GetLedgerStore().GetAccountRange(req.Start, req.End, leagueAddr)
   284  	if err != nil {
   285  		mp["error"] = err
   286  		c.JSON(http.StatusOK, mp)
   287  
   288  		return
   289  	}
   290  	mp["accounts"] = tres
   291  	c.JSON(http.StatusOK, mp)
   292  }
   293