OSDN Git Service

feat(federation): add address and timestamp (#223)
authorHAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
Wed, 26 Jun 2019 08:26:17 +0000 (16:26 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 26 Jun 2019 08:26:17 +0000 (16:26 +0800)
* update req & resp

* init timestamp

* fix timestamp

* fix

* add asset

* init from_address to_address

docs/federation/README-en.md
docs/federation/federation.sql
federation/api/handler.go
federation/database/orm/cross_transaction.go
federation/database/orm/cross_transaction_req.go
federation/synchron/mainchain_keeper.go
federation/synchron/sidechain_keeper.go

index 907ee83..00eb5ac 100644 (file)
@@ -92,7 +92,7 @@ Optional:
 - `Object` - *filter*, transactions filter.
     + Optional
         * `String` - *status*, transactions status, which can be `pending` or `completed`.
-        * `String` - *from_chain*, transactions source chain, which can be `bytom` or `vapor`.
+        * `String` - *source_chain_name*, transactions source chain, which can be `bytom` or `vapor`.
         * `String` - *source_tx_hash*, souce transaction hash string.
         * `String` - *dest_tx_hash*, destination transaction hash string.
 - `Object` - *sort*, transactions sorter.
@@ -105,7 +105,7 @@ Optional:
 
 `Object`:
 
-- `String` - *from_chain*, source chain name of the cross-chain transaction.
+- `String` - *source_chain_name*, source chain name of the cross-chain transaction.
 - `Integer` - *source_block_height*, block height of the cross-chain transaction on the source chain.
 - `String` - *source_block_hash*, block hash of the cross-chain transaction on the source chain.
 - `Integer` - *source_tx_index*, transaction index in the source block.
@@ -136,7 +136,7 @@ curl -X POST 127.0.0.1:3000/api/v1/federation/list-crosschain-txs -d '{}'
     },
     "data":[
       {
-        "from_chain":"bytom",
+        "source_chain_name":"bytom",
         "source_block_height":174,
         "source_block_hash":"569a3a5a43910ea634a947fd092bb3085359db451235ae59c20daab4e4b0d274",
         "source_tx_index":1,
index fe5c668..dad8c5c 100644 (file)
@@ -47,12 +47,14 @@ CREATE TABLE `cross_transactions` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `chain_id` tinyint(1) NOT NULL,
   `source_block_height` int(11) NOT NULL,
+  `source_block_timestamp` int(11) NOT NULL,
   `source_block_hash` char(64) NOT NULL,
   `source_tx_index` int(11) NOT NULL,
   `source_mux_id` char(64) NOT NULL,
   `source_tx_hash` char(64) NOT NULL,
   `source_raw_transaction` mediumtext NOT NULL,
   `dest_block_height` int(11) DEFAULT NULL,
+  `dest_block_timestamp` int(11) DEFAULT NULL,
   `dest_block_hash` char(64) DEFAULT NULL,
   `dest_tx_index` int(11) DEFAULT NULL,
   `dest_tx_hash` char(64) DEFAULT NULL,
index afd4bfb..01c9c0d 100644 (file)
@@ -38,8 +38,8 @@ func (s *Server) ListCrosschainTxs(c *gin.Context, listTxsReq *listCrosschainTxs
 
        txQuery := s.db.Preload("Chain").Preload("Reqs").Preload("Reqs.Asset").Where(txFilter)
        // filter direction
-       if fromChainName, err := listTxsReq.GetFilterString("from_chain"); err == nil && fromChainName != "" {
-               txQuery = txQuery.Joins("join chains on chains.id = cross_transactions.chain_id").Where("chains.name = ?", fromChainName)
+       if sourceChainName, err := listTxsReq.GetFilterString("source_chain_name"); err == nil && sourceChainName != "" {
+               txQuery = txQuery.Joins("join chains on chains.id = cross_transactions.chain_id").Where("chains.name = ?", sourceChainName)
        }
        txQuery = txQuery.Order(fmt.Sprintf("cross_transactions.source_block_height %s", listTxsReq.Sorter.Order))
        txQuery = txQuery.Order(fmt.Sprintf("cross_transactions.source_tx_index %s", listTxsReq.Sorter.Order))
index 4a327b8..5da3f51 100644 (file)
@@ -13,12 +13,14 @@ type CrossTransaction struct {
        ID                   uint64 `gorm:"primary_key"`
        ChainID              uint64
        SourceBlockHeight    uint64
+       SourceBlockTimestamp uint64
        SourceBlockHash      string
        SourceTxIndex        uint64
        SourceMuxID          string
        SourceTxHash         string
        SourceRawTransaction string
        DestBlockHeight      sql.NullInt64  `sql:"default:null"`
+       DestBlockTimestamp   sql.NullInt64  `sql:"default:null"`
        DestBlockHash        sql.NullString `sql:"default:null"`
        DestTxIndex          sql.NullInt64  `sql:"default:null"`
        DestTxHash           sql.NullString `sql:"default:null"`
@@ -42,28 +44,32 @@ func (c *CrossTransaction) MarshalJSON() ([]byte, error) {
        }
 
        return json.Marshal(&struct {
-               FromChain         string                 `json:"from_chain"`
-               SourceBlockHeight uint64                 `json:"source_block_height"`
-               SourceBlockHash   string                 `json:"source_block_hash"`
-               SourceTxIndex     uint64                 `json:"source_tx_index"`
-               SourceTxHash      string                 `json:"source_tx_hash"`
-               DestBlockHeight   uint64                 `json:"dest_block_height"`
-               DestBlockHash     string                 `json:"dest_block_hash"`
-               DestTxIndex       uint64                 `json:"dest_tx_index"`
-               DestTxHash        string                 `json:"dest_tx_hash"`
-               Status            string                 `json:"status"`
-               Reqs              []*CrossTransactionReq `json:"crosschain_requests"`
+               SourceChainName      string                 `json:"source_chain_name"`
+               SourceBlockHeight    uint64                 `json:"source_block_height"`
+               SourceBlockTimestamp uint64                 `json:"source_block_timestamp"`
+               SourceBlockHash      string                 `json:"source_block_hash"`
+               SourceTxIndex        uint64                 `json:"source_tx_index"`
+               SourceTxHash         string                 `json:"source_tx_hash"`
+               DestBlockHeight      uint64                 `json:"dest_block_height"`
+               DestBlockTimestamp   uint64                 `json:"dest_block_timestamp"`
+               DestBlockHash        string                 `json:"dest_block_hash"`
+               DestTxIndex          uint64                 `json:"dest_tx_index"`
+               DestTxHash           string                 `json:"dest_tx_hash"`
+               Status               string                 `json:"status"`
+               Reqs                 []*CrossTransactionReq `json:"crosschain_requests"`
        }{
-               FromChain:         c.Chain.Name,
-               SourceBlockHeight: c.SourceBlockHeight,
-               SourceBlockHash:   c.SourceBlockHash,
-               SourceTxIndex:     c.SourceTxIndex,
-               SourceTxHash:      c.SourceTxHash,
-               DestBlockHeight:   uint64(c.DestBlockHeight.Int64),
-               DestBlockHash:     c.DestBlockHash.String,
-               DestTxIndex:       uint64(c.DestTxIndex.Int64),
-               DestTxHash:        c.DestTxHash.String,
-               Status:            status,
-               Reqs:              c.Reqs,
+               SourceChainName:      c.Chain.Name,
+               SourceBlockHeight:    c.SourceBlockHeight,
+               SourceBlockTimestamp: c.SourceBlockTimestamp,
+               SourceBlockHash:      c.SourceBlockHash,
+               SourceTxIndex:        c.SourceTxIndex,
+               SourceTxHash:         c.SourceTxHash,
+               DestBlockHeight:      uint64(c.DestBlockHeight.Int64),
+               DestBlockTimestamp:   uint64(c.DestBlockTimestamp.Int64),
+               DestBlockHash:        c.DestBlockHash.String,
+               DestTxIndex:          uint64(c.DestTxIndex.Int64),
+               DestTxHash:           c.DestTxHash.String,
+               Status:               status,
+               Reqs:                 c.Reqs,
        })
 }
index 39a487b..d5c9ec5 100644 (file)
@@ -11,6 +11,8 @@ type CrossTransactionReq struct {
        AssetID            uint64          `json:"-"`
        AssetAmount        uint64          `json:"amount"`
        Script             string          `json:"-"`
+       FromAddress        string          `json:"from_address"`
+       ToAddress          string          `json:"to_address"`
        CreatedAt          types.Timestamp `json:"-"`
        UpdatedAt          types.Timestamp `json:"-"`
 
index 0884449..b777037 100644 (file)
@@ -174,12 +174,14 @@ func (m *mainchainKeeper) processDepositTx(chain *orm.Chain, block *types.Block,
        ormTx := &orm.CrossTransaction{
                ChainID:              chain.ID,
                SourceBlockHeight:    block.Height,
+               SourceBlockTimestamp: block.Timestamp,
                SourceBlockHash:      blockHash.String(),
                SourceTxIndex:        txIndex,
                SourceMuxID:          muxID.String(),
                SourceTxHash:         tx.ID.String(),
                SourceRawTransaction: string(rawTx),
                DestBlockHeight:      sql.NullInt64{Valid: false},
+               DestBlockTimestamp:   sql.NullInt64{Valid: false},
                DestBlockHash:        sql.NullString{Valid: false},
                DestTxIndex:          sql.NullInt64{Valid: false},
                DestTxHash:           sql.NullString{Valid: false},
@@ -242,10 +244,11 @@ func (m *mainchainKeeper) processWithdrawalTx(chain *orm.Chain, block *types.Blo
                        DestTxHash: sql.NullString{tx.ID.String(), true},
                        Status:     common.CrossTxPendingStatus,
                }).UpdateColumn(&orm.CrossTransaction{
-               DestBlockHeight: sql.NullInt64{int64(block.Height), true},
-               DestBlockHash:   sql.NullString{blockHash.String(), true},
-               DestTxIndex:     sql.NullInt64{int64(txIndex), true},
-               Status:          common.CrossTxCompletedStatus,
+               DestBlockHeight:    sql.NullInt64{int64(block.Height), true},
+               DestBlockTimestamp: sql.NullInt64{int64(block.Timestamp), true},
+               DestBlockHash:      sql.NullString{blockHash.String(), true},
+               DestTxIndex:        sql.NullInt64{int64(txIndex), true},
+               Status:             common.CrossTxCompletedStatus,
        })
        if stmt.Error != nil {
                return stmt.Error
index 91058c5..0e28ec1 100644 (file)
@@ -150,10 +150,11 @@ func (s *sidechainKeeper) processDepositTx(chain *orm.Chain, block *types.Block,
                        DestTxHash: sql.NullString{tx.ID.String(), true},
                        Status:     common.CrossTxPendingStatus,
                }).UpdateColumn(&orm.CrossTransaction{
-               DestBlockHeight: sql.NullInt64{int64(block.Height), true},
-               DestBlockHash:   sql.NullString{blockHash.String(), true},
-               DestTxIndex:     sql.NullInt64{int64(txIndex), true},
-               Status:          common.CrossTxCompletedStatus,
+               DestBlockHeight:    sql.NullInt64{int64(block.Height), true},
+               DestBlockTimestamp: sql.NullInt64{int64(block.Timestamp), true},
+               DestBlockHash:      sql.NullString{blockHash.String(), true},
+               DestTxIndex:        sql.NullInt64{int64(txIndex), true},
+               Status:             common.CrossTxCompletedStatus,
        })
        if stmt.Error != nil {
                return stmt.Error
@@ -189,12 +190,14 @@ func (s *sidechainKeeper) processWithdrawalTx(chain *orm.Chain, block *types.Blo
        ormTx := &orm.CrossTransaction{
                ChainID:              chain.ID,
                SourceBlockHeight:    block.Height,
+               SourceBlockTimestamp: block.Timestamp,
                SourceBlockHash:      blockHash.String(),
                SourceTxIndex:        txIndex,
                SourceMuxID:          muxID.String(),
                SourceTxHash:         tx.ID.String(),
                SourceRawTransaction: string(rawTx),
                DestBlockHeight:      sql.NullInt64{Valid: false},
+               DestBlockTimestamp:   sql.NullInt64{Valid: false},
                DestBlockHash:        sql.NullString{Valid: false},
                DestTxIndex:          sql.NullInt64{Valid: false},
                DestTxHash:           sql.NullString{Valid: false},