• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisiond0bb5bd3f9bc71530bc6448251bd182d22461392 (tree)
Time2019-06-26 15:39:31
AuthorHAOYUatHZ <haoyu@prot...>
CommiterHAOYUatHZ

Log Message

refactor reponse

Change Summary

Incremental Difference

--- a/federation/api/handler.go
+++ b/federation/api/handler.go
@@ -21,9 +21,9 @@ func (s *Server) ListCrosschainTxs(c *gin.Context, listTxsReq *listCrosschainTxs
2121 // filter tx status
2222 if status, err := listTxsReq.GetFilterString("status"); err == nil && status != "" {
2323 switch strings.ToLower(status) {
24- case "pending":
24+ case common.CrossTxPendingStatusLabel:
2525 txFilter.Status = common.CrossTxPendingStatus
26- case "completed":
26+ case common.CrossTxCompletedStatusLabel:
2727 txFilter.Status = common.CrossTxCompletedStatus
2828 }
2929 }
--- a/federation/common/const.go
+++ b/federation/common/const.go
@@ -5,3 +5,8 @@ const (
55 CrossTxPendingStatus
66 CrossTxCompletedStatus
77 )
8+
9+const (
10+ CrossTxPendingStatusLabel = "pending"
11+ CrossTxCompletedStatusLabel = "completed"
12+)
--- a/federation/database/orm/cross_transaction.go
+++ b/federation/database/orm/cross_transaction.go
@@ -2,27 +2,68 @@ package orm
22
33 import (
44 "database/sql"
5+ "encoding/json"
56
7+ "github.com/vapor/errors"
8+ "github.com/vapor/federation/common"
69 "github.com/vapor/federation/types"
710 )
811
912 type CrossTransaction struct {
10- ID uint64 `gorm:"primary_key" json:"-"`
11- ChainID uint64 `json:"-"`
12- SourceBlockHeight uint64 `json:"source_block_height"`
13- SourceBlockHash string `json:"source_block_hash"`
14- SourceTxIndex uint64 `json:"source_tx_index"`
15- SourceMuxID string `json:"-"`
16- SourceTxHash string `json:"source_tx_hash"`
17- SourceRawTransaction string `json:"-"`
18- DestBlockHeight sql.NullInt64 `sql:"default:null" json:"dest_block_height"`
19- DestBlockHash sql.NullString `sql:"default:null" json:"dest_block_hash"`
20- DestTxIndex sql.NullInt64 `sql:"default:null" json:"dest_tx_index"`
21- DestTxHash sql.NullString `sql:"default:null" json:"dest_tx_hash"`
22- Status uint8 `json:"status"`
23- CreatedAt types.Timestamp `json:"-"`
24- UpdatedAt types.Timestamp `json:"-"`
13+ ID uint64 `gorm:"primary_key"`
14+ ChainID uint64
15+ SourceBlockHeight uint64
16+ SourceBlockHash string
17+ SourceTxIndex uint64
18+ SourceMuxID string
19+ SourceTxHash string
20+ SourceRawTransaction string
21+ DestBlockHeight sql.NullInt64 `sql:"default:null"`
22+ DestBlockHash sql.NullString `sql:"default:null"`
23+ DestTxIndex sql.NullInt64 `sql:"default:null"`
24+ DestTxHash sql.NullString `sql:"default:null"`
25+ Status uint8
26+ CreatedAt types.Timestamp
27+ UpdatedAt types.Timestamp
2528
26- Chain *Chain `gorm:"foreignkey:ChainID" json:"-"`
27- Reqs []*CrossTransactionReq `json:"crosschain_requests"`
29+ Chain *Chain `gorm:"foreignkey:ChainID"`
30+ Reqs []*CrossTransactionReq
31+}
32+
33+func (c *CrossTransaction) MarshalJSON() ([]byte, error) {
34+ var status string
35+ switch c.Status {
36+ case common.CrossTxPendingStatus:
37+ status = common.CrossTxPendingStatusLabel
38+ case common.CrossTxCompletedStatus:
39+ status = common.CrossTxCompletedStatusLabel
40+ default:
41+ return nil, errors.New("unknown cross-chain tx status")
42+ }
43+
44+ return json.Marshal(&struct {
45+ FromChain string `json:"from_chain"`
46+ SourceBlockHeight uint64 `json:"source_block_height"`
47+ SourceBlockHash string `json:"source_block_hash"`
48+ SourceTxIndex uint64 `json:"source_tx_index"`
49+ SourceTxHash string `json:"source_tx_hash"`
50+ DestBlockHeight uint64 `json:"dest_block_height"`
51+ DestBlockHash string `json:"dest_block_hash"`
52+ DestTxIndex uint64 `json:"dest_tx_index"`
53+ DestTxHash string `json:"dest_tx_hash"`
54+ Status string `json:"status"`
55+ Reqs []*CrossTransactionReq `json:"crosschain_requests"`
56+ }{
57+ FromChain: c.Chain.Name,
58+ SourceBlockHeight: c.SourceBlockHeight,
59+ SourceBlockHash: c.SourceBlockHash,
60+ SourceTxIndex: c.SourceTxIndex,
61+ SourceTxHash: c.SourceTxHash,
62+ DestBlockHeight: uint64(c.DestBlockHeight.Int64),
63+ DestBlockHash: c.DestBlockHash.String,
64+ DestTxIndex: uint64(c.DestTxIndex.Int64),
65+ DestTxHash: c.DestTxHash.String,
66+ Status: status,
67+ Reqs: c.Reqs,
68+ })
2869 }
Show on old repository browser