OSDN Git Service

fk
authorHAOYUatHZ <haoyu@protonmail.com>
Fri, 7 Jun 2019 11:18:28 +0000 (19:18 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Fri, 7 Jun 2019 11:18:28 +0000 (19:18 +0800)
docs/federation/federation.sql
federation/database/orm/cross_transaction_input.go
federation/synchron/attach_block_processor.go
federation/synchron/block_processor.go
federation/synchron/detach_block_processor.go

index 04d407f..bd28029 100644 (file)
@@ -89,11 +89,14 @@ CREATE TABLE `cross_transaction_inputs` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `tx_id` int(11) NOT NULL,
   `source_pos` int(11) NOT NULL,
+  `asset_id` int(11) NOT NULL,
+  `asset_amount` bigint(20) DEFAULT '0',
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`),
   UNIQUE KEY `input_id` (`tx_id`,`source_pos`),
-  CONSTRAINT `cross_transaction_inputs_ibfk_1` FOREIGN KEY (`tx_id`) REFERENCES `cross_transactions` (`id`)
+  CONSTRAINT `cross_transaction_inputs_ibfk_1` FOREIGN KEY (`tx_id`) REFERENCES `cross_transactions` (`id`),
+  CONSTRAINT `cross_transaction_inputs_ibfk_2` FOREIGN KEY (`asset_id`) REFERENCES `assets` (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 LOCK TABLES `cross_transaction_inputs` WRITE;
index b3fdd38..6ac771f 100644 (file)
@@ -5,11 +5,14 @@ import (
 )
 
 type CrossTransactionInput struct {
-       ID        uint64 `gorm:"primary_key"`
-       TxID      uint64
-       SourcePos uint64
-       CreatedAt types.Timestamp
-       UpdatedAt types.Timestamp
+       ID          uint64 `gorm:"primary_key"`
+       TxID        uint64
+       SourcePos   uint64
+       AssetID     uint64
+       AssetAmount uint64
+       CreatedAt   types.Timestamp
+       UpdatedAt   types.Timestamp
 
        CrossTransaction *CrossTransaction `gorm:"foreignkey:TxID"`
+       Asset            *Asset            `gorm:"foreignkey:AssetID"`
 }
index d9fdb2d..c6c624c 100644 (file)
@@ -28,6 +28,10 @@ type attachBlockProcessor struct {
        // txStatus *btmBc.TransactionStatus
 }
 
+func (p *attachBlockProcessor) getCfg() *config.Chain {
+       return p.cfg
+}
+
 func (p *attachBlockProcessor) processIssuing(db *gorm.DB, txs []*btmTypes.Tx) error {
        return addIssueAssets(db, txs)
 }
index b5f80c3..b07e931 100644 (file)
@@ -18,12 +18,14 @@ import (
        // "github.com/blockcenter/config"
        // "github.com/blockcenter/types"
        "github.com/vapor/errors"
+       "github.com/vapor/federation/config"
        "github.com/vapor/federation/database/orm"
 )
 
 var ErrInconsistentDB = errors.New("inconsistent db status")
 
 type blockProcessor interface {
+       getCfg() *config.Chain
        processIssuing(db *gorm.DB, txs []*btmTypes.Tx) error
        processChainInfo() error
        // getBlock() *btmTypes.Block
@@ -72,9 +74,11 @@ func addIssueAssets(db *gorm.DB, txs []*btmTypes.Tx) error {
 
 func updateBlock(db *gorm.DB, bp blockProcessor) error {
        // txs := bp.getBlock().Transactions
-       // if err := bp.processIssuing(db, txs, bp.getCoin().ID); err != nil {
-       //      return err
-       // }
+       if bp.getCfg().IsMainchain {
+               if err := bp.processIssuing(db, txs); err != nil {
+                       return err
+               }
+       }
 
        // addressTxMappings, err := GetAddressTxMappings(cfg, txs, bp.getTxStatus(), db)
        // if err != nil {
@@ -89,9 +93,15 @@ func updateBlock(db *gorm.DB, bp blockProcessor) error {
        //      return err
        // }
 
-       // if err := updateDeletedTransaction(db); err != nil {
-       //      return err
-       // }
+       if err := updateDeletedTransaction(db); err != nil {
+               return err
+       }
 
        return bp.processChainInfo()
 }
+
+// An expired unconfirmed transaction will be marked as deleted, but the latter transaction was packaged into block,
+// the deleted_at flag must be removed. In addition, the gorm can't support update deleted_at field directly, can only use raw sql.
+func updateDeletedTransaction(db *gorm.DB) error {
+       return db.Exec("UPDATE cross_transactions SET deleted_at = NULL WHERE block_height > 0 AND deleted_at IS NOT NULL").Error
+}
index ebd18a4..fd5e5f2 100644 (file)
@@ -19,6 +19,10 @@ type detachBlockProcessor struct {
        // txStatus *bc.TransactionStatus
 }
 
+func (p *detachBlockProcessor) getCfg() *config.Chain {
+       return p.cfg
+}
+
 func (p *detachBlockProcessor) processIssuing(db *gorm.DB, txs []*btmTypes.Tx) error {
        return nil
 }