OSDN Git Service

rename (#465)
[bytom/vapor.git] / toolbar / federation / database / orm / cross_transaction.go
1 package orm
2
3 import (
4         "database/sql"
5         "encoding/json"
6
7         "github.com/bytom/vapor/errors"
8         "github.com/bytom/vapor/toolbar/common"
9         fedCommon "github.com/bytom/vapor/toolbar/federation/common"
10 )
11
12 type CrossTransaction struct {
13         ID                   uint64 `gorm:"primary_key"`
14         ChainID              uint64
15         SourceBlockHeight    uint64
16         SourceBlockTimestamp uint64
17         SourceBlockHash      string
18         SourceTxIndex        uint64
19         SourceMuxID          string
20         SourceTxHash         string
21         SourceRawTransaction string
22         DestBlockHeight      sql.NullInt64  `sql:"default:null"`
23         DestBlockTimestamp   sql.NullInt64  `sql:"default:null"`
24         DestBlockHash        sql.NullString `sql:"default:null"`
25         DestTxIndex          sql.NullInt64  `sql:"default:null"`
26         DestTxHash           sql.NullString `sql:"default:null"`
27         Status               uint8
28         CreatedAt            common.Timestamp
29         UpdatedAt            common.Timestamp
30
31         Chain *Chain `gorm:"foreignkey:ChainID"`
32         Reqs  []*CrossTransactionReq
33 }
34
35 func (c *CrossTransaction) MarshalJSON() ([]byte, error) {
36         var status string
37         switch c.Status {
38         case fedCommon.CrossTxPendingStatus:
39                 status = fedCommon.CrossTxPendingStatusLabel
40         case fedCommon.CrossTxCompletedStatus:
41                 status = fedCommon.CrossTxCompletedStatusLabel
42         default:
43                 return nil, errors.New("unknown cross-chain tx status")
44         }
45
46         return json.Marshal(&struct {
47                 SourceChainName      string                 `json:"source_chain_name"`
48                 SourceBlockHeight    uint64                 `json:"source_block_height"`
49                 SourceBlockTimestamp uint64                 `json:"source_block_timestamp"`
50                 SourceBlockHash      string                 `json:"source_block_hash"`
51                 SourceTxIndex        uint64                 `json:"source_tx_index"`
52                 SourceTxHash         string                 `json:"source_tx_hash"`
53                 DestBlockHeight      uint64                 `json:"dest_block_height"`
54                 DestBlockTimestamp   uint64                 `json:"dest_block_timestamp"`
55                 DestBlockHash        string                 `json:"dest_block_hash"`
56                 DestTxIndex          uint64                 `json:"dest_tx_index"`
57                 DestTxHash           string                 `json:"dest_tx_hash"`
58                 Status               string                 `json:"status"`
59                 Reqs                 []*CrossTransactionReq `json:"crosschain_requests"`
60         }{
61                 SourceChainName:      c.Chain.Name,
62                 SourceBlockHeight:    c.SourceBlockHeight,
63                 SourceBlockTimestamp: c.SourceBlockTimestamp,
64                 SourceBlockHash:      c.SourceBlockHash,
65                 SourceTxIndex:        c.SourceTxIndex,
66                 SourceTxHash:         c.SourceTxHash,
67                 DestBlockHeight:      uint64(c.DestBlockHeight.Int64),
68                 DestBlockTimestamp:   uint64(c.DestBlockTimestamp.Int64),
69                 DestBlockHash:        c.DestBlockHash.String,
70                 DestTxIndex:          uint64(c.DestTxIndex.Int64),
71                 DestTxHash:           c.DestTxHash.String,
72                 Status:               status,
73                 Reqs:                 c.Reqs,
74         })
75 }