github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/http/router/orgchain.go (about) 1 package router 2 3 import ( 4 "github.com/gin-gonic/gin" 5 "github.com/sixexorg/magnetic-ring/central/entity" 6 "github.com/sixexorg/magnetic-ring/central/mongo/service/impl" 7 "github.com/sixexorg/magnetic-ring/common" 8 "github.com/sixexorg/magnetic-ring/common/sink" 9 "github.com/sixexorg/magnetic-ring/core/orgchain/types" 10 httpactor "github.com/sixexorg/magnetic-ring/http/actor" 11 "github.com/sixexorg/magnetic-ring/http/req" 12 "github.com/sixexorg/magnetic-ring/log" 13 "github.com/sixexorg/magnetic-ring/orgcontainer" 14 "github.com/sixexorg/magnetic-ring/store/orgchain/validation" 15 "gopkg.in/mgo.v2/bson" 16 "net/http" 17 ) 18 19 var ( 20 leagueGroup *gin.RouterGroup 21 ) 22 23 func StartLeagueRouter() { 24 25 router := GetRouter() 26 leagueGroup = router.Group("orgapi", func(c *gin.Context) {}) 27 28 InitOrgRouters() 29 } 30 31 func InitOrgRouters() { 32 leagueGroup.POST("getleaguenonce", getLeagueNonce) 33 leagueGroup.POST("sendsubtx", sendSubTx) 34 leagueGroup.POST("getorgbalance", getSubBalance) 35 leagueGroup.POST("getorgheight", getSubCurrentBlockHeight) 36 leagueGroup.POST("getorgblock", getSubBlock) 37 leagueGroup.POST("getorgtxbyhash", getSubTxByHash) 38 leagueGroup.POST("getorginfo", getLeague) 39 leagueGroup.POST("getorgvotes", getVoteList) 40 leagueGroup.POST("getorgmembers", getLeagueMemberList) 41 leagueGroup.POST("getorgdetail", getLeagueDetail) 42 leagueGroup.POST("ungetbonus", unGetBonus) 43 leagueGroup.POST("leagueasset", leagueAsset) 44 leagueGroup.POST("getorgvotedetail", getVoteDetail) 45 leagueGroup.POST("getchatlist", getChatRecordList) 46 leagueGroup.POST("getransferlist", getTransferList) 47 leagueGroup.POST("getvoteunitlist", getVoteUnitList) 48 } 49 50 func sendSubTx(c *gin.Context) { 51 cl := new(req.ReqSendTx) 52 err := c.BindJSON(cl) 53 bsm := bson.M{} 54 if err != nil { 55 log.Info("an error occured when bindjson", "err", err) 56 bsm["errormsg"] = err.Error() 57 c.JSON(http.StatusOK, bsm) 58 return 59 } 60 61 log.Info("sendtx", "ReqSendTx", cl) 62 63 tx := &types.Transaction{} 64 source := sink.NewZeroCopySource(cl.Raw) 65 err = tx.Deserialization(source) 66 if err != nil { 67 log.Info("transaction Deserialization error", "err", err) 68 bsm["errormsg"] = err.Error() 69 c.JSON(http.StatusOK, bsm) 70 return 71 } 72 73 log.Info("magnetic receive transaction", "tx", tx) 74 75 leagueAddress, err := common.ToAddress(cl.LeagueId) 76 if err != nil { 77 log.Error("toaddress errorrrr", "error", err) 78 bsm["errormsg"] = err.Error() 79 c.JSON(http.StatusOK, bsm) 80 return 81 } 82 83 hx, desc := httpactor.AppendTxToSubPool(tx, leagueAddress) 84 desc = "success" 85 86 log.Info("magnetic append transaction to pool", "tx", tx) 87 88 log.Info("watch the hash", "hash", hx) 89 90 bsm["errormsg"] = "" 91 bsm["hash"] = hx 92 bsm["desc"] = desc 93 94 c.JSON(http.StatusOK, bsm) 95 } 96 97 func RegistGetSubBalance() { 98 leagueGroup.POST("getorgbalance") 99 } 100 101 func getSubBalance(c *gin.Context) { 102 cl := new(req.ReqGetBalance) 103 err := c.BindJSON(cl) 104 if err != nil { 105 log.Info("an error occured when bindjson", "error", err) 106 return 107 } 108 109 address, err := common.ToAddress(cl.OrgId) 110 if err != nil { 111 mp := make(map[string]interface{}) 112 mp["ut"] = 0 113 mp["energy"] = 0 114 c.JSON(http.StatusOK, mp) 115 return 116 } 117 118 ctner, err := orgcontainer.GetContainers().GetContainer(address) 119 if err != nil { 120 panic(err) 121 } 122 123 ldgr := ctner.Ledger() 124 125 mp := make(map[string]interface{}) 126 account, err := ldgr.GetPrevAccount4V(ldgr.GetCurrentBlockHeight(), address, common.Address{}) 127 if err != nil { 128 mp["error"] = err.Error() 129 c.JSON(http.StatusOK, mp) 130 return 131 } 132 133 if account == nil { 134 mp["ut"] = 0 135 mp["energy"] = 0 136 c.JSON(http.StatusOK, mp) 137 return 138 } 139 140 ut := account.Balance() 141 energy := account.Energy() 142 143 mp["ut"] = ut 144 mp["energy"] = energy 145 146 c.JSON(http.StatusOK, mp) 147 } 148 149 func getSubCurrentBlockHeight(c *gin.Context) { 150 cl := new(req.ReqGetBalance) 151 err := c.BindJSON(cl) 152 if err != nil { 153 log.Info("an error occured when bindjson", "error", err) 154 return 155 } 156 157 address, err := common.ToAddress(cl.Addr) 158 if err != nil { 159 mp := make(map[string]interface{}) 160 mp["error"] = err.Error() 161 c.JSON(http.StatusOK, mp) 162 return 163 } 164 165 ctner, err := orgcontainer.GetContainers().GetContainer(address) 166 if err != nil { 167 panic(err) 168 } 169 170 ldgr := ctner.Ledger() 171 172 mp := make(map[string]interface{}) 173 height := ldgr.GetCurrentBlockHeight() 174 175 mp["height"] = height 176 177 c.JSON(http.StatusOK, mp) 178 } 179 180 func getSubBlock(c *gin.Context) { 181 cl := new(req.ReqGetBlockByHeight) 182 err := c.BindJSON(cl) 183 if err != nil { 184 log.Info("an error occured when bindjson", "error", err) 185 return 186 } 187 188 address, err := common.ToAddress(cl.OrgId) 189 if err != nil { 190 mp := make(map[string]interface{}) 191 mp["error"] = err.Error() 192 c.JSON(http.StatusOK, mp) 193 return 194 } 195 196 ctner, err := orgcontainer.GetContainers().GetContainer(address) 197 if err != nil { 198 panic(err) 199 } 200 201 ldgr := ctner.Ledger() 202 203 mp := make(map[string]interface{}) 204 block, err := ldgr.GetBlockByHeight(cl.Height) 205 if err != nil { 206 mp["error"] = err.Error() 207 c.JSON(http.StatusOK, mp) 208 return 209 } 210 211 mp["block"] = block 212 213 c.JSON(http.StatusOK, mp) 214 } 215 216 func getSubTxByHash(c *gin.Context) { 217 cl := new(req.ReqGetTxByHash) 218 err := c.BindJSON(cl) 219 if err != nil { 220 log.Info("an error occured when bindjson", "error", err) 221 return 222 } 223 224 address, err := common.ToAddress(cl.LeagueId) 225 if err != nil { 226 mp := make(map[string]interface{}) 227 mp["error"] = err.Error() 228 c.JSON(http.StatusOK, mp) 229 return 230 } 231 232 ctner, err := orgcontainer.GetContainers().GetContainer(address) 233 if err != nil { 234 panic(err) 235 } 236 237 ldgr := ctner.Ledger() 238 239 hash, err := common.StringToHash(cl.TxHash) 240 if err != nil { 241 log.Info("an error occured when convert string 2 hash", "error", err) 242 return 243 } 244 245 mp := make(map[string]interface{}) 246 block, height, err := ldgr.GetTransaction(hash, address) 247 if err != nil { 248 mp["error"] = err.Error() 249 c.JSON(http.StatusOK, mp) 250 return 251 } 252 253 mp["tx"] = block 254 mp["height"] = height 255 256 c.JSON(http.StatusOK, mp) 257 } 258 259 func getLeague(c *gin.Context) { 260 cl := new(req.ReqOrgInfo) 261 err := c.BindJSON(cl) 262 mp := make(map[string]interface{}) 263 if err != nil { 264 mp["error"] = err.Error() 265 c.JSON(http.StatusInternalServerError, mp) 266 return 267 } 268 269 address, err := common.ToAddress(cl.OrgId) 270 if err != nil { 271 mp["error"] = err.Error() 272 c.JSON(http.StatusInternalServerError, mp) 273 return 274 } 275 276 ctner, err := orgcontainer.GetContainers().GetContainer(address) 277 if err != nil { 278 mp := make(map[string]interface{}) 279 mp["error"] = err.Error() 280 c.JSON(http.StatusInternalServerError, mp) 281 return 282 } 283 284 mp["orgid"] = ctner.LeagueId().ToString() 285 c.JSON(http.StatusOK, mp) 286 } 287 288 /*****************************************************************/ 289 290 func getLeagueDetail(c *gin.Context) { 291 cl := new(req.ReqOrgInfo) 292 err := c.BindJSON(cl) 293 mp := make(map[string]interface{}) 294 if err != nil { 295 mp["error"] = err.Error() 296 c.JSON(http.StatusInternalServerError, mp) 297 return 298 } 299 300 mp["ut"] = 300000 301 mp["totalboxes"] = 1000 302 mp["orgSymbol"] = "CHINA" 303 mp["utrate"] = 1000 304 mp["userauth"] = true 305 mp["orgaddress"] = "ct4x6b5nK4JVTFFHRW947RAECX2xT4E4Fk4" 306 307 c.JSON(http.StatusOK, mp) 308 } 309 310 func getLeagueMemberList(c *gin.Context) { 311 cl := new(req.ReqGetMemberList) 312 err := c.BindJSON(cl) 313 mp := make(map[string]interface{}) 314 if err != nil { 315 log.Error("impl.NewMemberImpl(nil,cl.OrgId).GetMebers()", "error", err) 316 mp["error"] = err.Error() 317 c.JSON(http.StatusInternalServerError, mp) 318 return 319 } 320 321 total, mbs, err := impl.NewMemberImpl(nil, cl.OrgId).GetMebers(cl.Page, cl.PageSize) 322 if err != nil { 323 log.Error("impl.NewMemberImpl(nil,cl.OrgId).GetMebers()", "error", err) 324 mp["error"] = err.Error() 325 c.JSON(http.StatusInternalServerError, mp) 326 return 327 } 328 329 mbslit := mbs.([]*entity.Member) 330 331 memberlist := make([]string, 0) //{"ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk0", "ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk1", "ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk2", "ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk4", "ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk4", "ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk5"} 332 333 for _, tmp := range mbslit { 334 //tmp := mem.(*entity.Member) 335 memberlist = append(memberlist, tmp.Addr) 336 } 337 338 mp["total"] = total 339 mp["memberlist"] = memberlist 340 341 c.JSON(http.StatusOK, mp) 342 } 343 344 func getVoteList(c *gin.Context) { 345 cl := new(req.ReqGetVoteList) 346 err := c.BindJSON(cl) 347 mp := make(map[string]interface{}) 348 if err != nil { 349 mp["error"] = err.Error() 350 c.JSON(http.StatusInternalServerError, mp) 351 return 352 } 353 354 address, err := common.ToAddress(cl.OrgId) 355 if err != nil { 356 mp["error"] = err.Error() 357 c.JSON(http.StatusInternalServerError, mp) 358 return 359 } 360 361 ctner, err := orgcontainer.GetContainers().GetContainer(address) 362 if err != nil { 363 mp["error"] = err.Error() 364 c.JSON(http.StatusInternalServerError, mp) 365 return 366 } 367 368 ldgr := ctner.Ledger() 369 370 total, list, err := impl.NewVoteDetailImpl(nil, cl.OrgId).GetVoteList(cl.Page, cl.PageSize, ldgr.GetCurrentBlockHeight(), cl.VoteState) 371 if err != nil { 372 log.Error("impl.NewMemberImpl(nil,cl.OrgId).GetVoteList()", "error", err) 373 mp["error"] = err.Error() 374 c.JSON(http.StatusInternalServerError, mp) 375 return 376 } 377 378 mp["votelist"] = list 379 mp["total"] = total 380 381 c.JSON(http.StatusOK, mp) 382 } 383 384 func getChatRecordList(c *gin.Context) { 385 cl := new(req.ReqGetVoteList) 386 err := c.BindJSON(cl) 387 mp := make(map[string]interface{}) 388 if err != nil { 389 mp["error"] = err.Error() 390 c.JSON(http.StatusInternalServerError, mp) 391 return 392 } 393 394 //addr, _ := common.ToAddress("ct1x6b5nK4JVTFFHRW947RAECX2xT4E4Fk0") 395 //hs, _ := common.StringToHash("53ee84c855b17595543cd26d41ff40504f2cbdd5296c537aac8b94f716053331") 396 397 //vlist := []*entity.VoteUnit{ 398 // entity.NewVoteUnit(types.Join, addr, 8, hs), 399 // entity.NewVoteUnit(types.Leave, addr, 1608, hs), 400 // entity.NewVoteUnit(types.RaiseUT, addr, 3608, hs), 401 //} 402 403 total, list, err := impl.NewOrgChatMsgImpl(nil, cl.OrgId).GetChatList(cl.Page, cl.PageSize) 404 405 mp["chatrecordlist"] = list 406 mp["total"] = total 407 408 c.JSON(http.StatusOK, mp) 409 } 410 411 func unGetBonus(c *gin.Context) { 412 cl := new(req.ReqUngetBonus) 413 err := c.BindJSON(cl) 414 mp := make(map[string]interface{}) 415 if err != nil { 416 mp["error"] = err.Error() 417 c.JSON(http.StatusInternalServerError, mp) 418 return 419 } 420 421 address, err := common.ToAddress(cl.OrgId) 422 if err != nil { 423 mp["error"] = err.Error() 424 c.JSON(http.StatusInternalServerError, mp) 425 return 426 } 427 428 ctner, err := orgcontainer.GetContainers().GetContainer(address) 429 if err != nil { 430 mp := make(map[string]interface{}) 431 mp["error"] = err.Error() 432 c.JSON(http.StatusInternalServerError, mp) 433 return 434 } 435 436 accountAddress, err := common.ToAddress(cl.Account) 437 if err != nil { 438 mp["error"] = err.Error() 439 c.JSON(http.StatusInternalServerError, mp) 440 return 441 } 442 443 energy, _, err := validation.CalculateBonus(ctner.Ledger(), accountAddress, address) 444 if err != nil { 445 mp["error"] = err.Error() 446 c.JSON(http.StatusInternalServerError, mp) 447 return 448 } 449 450 mp["energy"] = energy.Uint64() 451 452 c.JSON(http.StatusOK, mp) 453 } 454 455 func leagueAsset(c *gin.Context) { 456 cl := new(req.ReqGetOrgAsset) 457 err := c.BindJSON(cl) 458 mp := make(map[string]interface{}) 459 if err != nil { 460 mp["error"] = err.Error() 461 c.JSON(http.StatusInternalServerError, mp) 462 return 463 } 464 465 address, err := common.ToAddress(cl.OrgId) 466 if err != nil { 467 mp["error"] = err.Error() 468 c.JSON(http.StatusInternalServerError, mp) 469 return 470 } 471 472 ctner, err := orgcontainer.GetContainers().GetContainer(address) 473 if err != nil { 474 mp := make(map[string]interface{}) 475 mp["error"] = err.Error() 476 c.JSON(http.StatusInternalServerError, mp) 477 return 478 } 479 480 acctaddress, err := common.ToAddress(cl.Account) 481 if err != nil { 482 mp["error"] = err.Error() 483 c.JSON(http.StatusInternalServerError, mp) 484 return 485 } 486 487 stt, err := ctner.Ledger().GetPrevAccount4V(cl.Height, acctaddress, address) 488 if err != nil { 489 mp["error"] = err.Error() 490 c.JSON(http.StatusInternalServerError, mp) 491 return 492 } 493 494 mp["ut"] = stt.Balance() 495 mp["energy"] = stt.Energy() 496 497 c.JSON(http.StatusOK, mp) 498 } 499 500 func getVoteDetail(c *gin.Context) { 501 cl := new(req.ReqVoteDetail) 502 err := c.BindJSON(cl) 503 mp := make(map[string]interface{}) 504 if err != nil { 505 mp["error"] = err.Error() 506 c.JSON(http.StatusInternalServerError, mp) 507 return 508 } 509 510 address, err := common.ToAddress(cl.OrgId) 511 if err != nil { 512 mp["error"] = err.Error() 513 c.JSON(http.StatusInternalServerError, mp) 514 return 515 } 516 517 ctner, err := orgcontainer.GetContainers().GetContainer(address) 518 if err != nil { 519 mp := make(map[string]interface{}) 520 mp["error"] = err.Error() 521 c.JSON(http.StatusInternalServerError, mp) 522 return 523 } 524 525 hsh, err := common.StringToHash(cl.VoteId) 526 if err != nil { 527 mp := make(map[string]interface{}) 528 mp["error"] = err.Error() 529 c.JSON(http.StatusInternalServerError, mp) 530 return 531 } 532 533 detail, err := impl.NewVoteDetailImpl(nil, cl.OrgId).GetLeagueVoteDetail(cl.OrgId, cl.VoteId) 534 if err != nil { 535 mp := make(map[string]interface{}) 536 mp["error"] = err.Error() 537 c.JSON(http.StatusInternalServerError, mp) 538 return 539 } 540 541 vstt, err := ctner.Ledger().GetVoteState(hsh, cl.Height) 542 if err != nil { 543 mp := make(map[string]interface{}) 544 mp["error"] = err.Error() 545 c.JSON(http.StatusInternalServerError, mp) 546 return 547 } 548 549 uttotal := ctner.Ledger().GetUTByHeight(cl.Height, common.Address{}) 550 551 agreeper, _ := common.CalcPer(vstt.Agree, uttotal) 552 agaitper, _ := common.CalcPer(vstt.Against, uttotal) 553 giveuper, _ := common.CalcPer(vstt.Abstention, uttotal) 554 555 result := vstt.GetResult(uttotal, 67) 556 557 acctaddress, err := common.ToAddress(cl.Account) 558 if err != nil { 559 mp["error"] = err.Error() 560 c.JSON(http.StatusInternalServerError, mp) 561 return 562 } 563 564 accountState, err := ctner.Ledger().GetPrevAccount4V(cl.Height, acctaddress, address) 565 if err != nil { 566 mp["error"] = err.Error() 567 c.JSON(http.StatusInternalServerError, mp) 568 return 569 } 570 571 myut := accountState.Balance() 572 573 mystat := impl.NewVoteStatisticsImpl(nil, cl.OrgId).IsAccountVoted(cl.Account) 574 575 vlist := entity.VoteDetail{ 576 MetaBox: detail.MetaBox, 577 Hoster: detail.Hoster, 578 AgreePer: agreeper, 579 AgainstPer: agaitper, 580 GiveUpPer: giveuper, 581 UtBefore: detail.UtBefore, 582 MyUt: myut.Uint64(), 583 Msg: detail.Msg, 584 State: result, 585 EndHeight: detail.EndHeight, 586 IfIVoted: mystat, 587 } 588 589 mp["votedetail"] = vlist 590 591 c.JSON(http.StatusOK, mp) 592 } 593 594 func getTransferList(c *gin.Context) { 595 cl := new(req.ReqGetVoteList) 596 err := c.BindJSON(cl) 597 mp := make(map[string]interface{}) 598 if err != nil { 599 mp["error"] = err.Error() 600 c.JSON(http.StatusInternalServerError, mp) 601 return 602 } 603 604 total, list, err := impl.NewOrgTxnsImpl(nil, cl.OrgId).GetTransferTxList(cl.Account, cl.Page, cl.PageSize) 605 606 mp["transferlist"] = list 607 mp["total"] = total 608 609 c.JSON(http.StatusOK, mp) 610 } 611 612 func getLeagueNonce(c *gin.Context) { 613 cl := new(req.ReqUngetBonus) 614 err := c.BindJSON(cl) 615 if err != nil { 616 log.Info("an error occured when bindjson", "error", err) 617 return 618 } 619 620 leagueAddress, err := common.ToAddress(cl.OrgId) 621 if err != nil { 622 mp := make(map[string]interface{}) 623 mp["error"] = err.Error() 624 c.JSON(http.StatusOK, mp) 625 return 626 } 627 628 accountAddress, err := common.ToAddress(cl.Account) 629 if err != nil { 630 mp := make(map[string]interface{}) 631 mp["error"] = err.Error() 632 c.JSON(http.StatusOK, mp) 633 return 634 } 635 636 empty := common.Address{} 637 if leagueAddress == empty { 638 mp := make(map[string]interface{}) 639 mp["error"] = err.Error() 640 c.JSON(http.StatusOK, mp) 641 return 642 } 643 644 ctner, err := orgcontainer.GetContainers().GetContainer(leagueAddress) 645 if err != nil { 646 panic(err) 647 } 648 649 ldgr := ctner.Ledger() 650 651 acctstate, err := ldgr.GetPrevAccount4V(ldgr.GetCurrentBlockHeight(), accountAddress, leagueAddress) 652 if err != nil { 653 mp := make(map[string]interface{}) 654 mp["error"] = err.Error() 655 c.JSON(http.StatusOK, mp) 656 return 657 } 658 659 mp := make(map[string]interface{}) 660 mp["nonce"] = acctstate.Nonce() 661 662 c.JSON(http.StatusOK, mp) 663 } 664 665 func getVoteUnitList(c *gin.Context) { 666 cl := new(req.ReqGetVoteUnitList) 667 err := c.BindJSON(cl) 668 mp := make(map[string]interface{}) 669 if err != nil { 670 mp["error"] = err.Error() 671 c.JSON(http.StatusInternalServerError, mp) 672 return 673 } 674 675 total, list, err := impl.NewVoteStatisticsImpl(nil, cl.OrgId).FindVoteUnitList(cl.OrgId, cl.VoteId, cl.Page, cl.PageSize) 676 677 mp["voteunitlist"] = list 678 mp["total"] = total 679 680 c.JSON(http.StatusOK, mp) 681 }