• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版7fcd3aa425118859690abf4bcd0ee75775296352 (tree)
时间2019-10-24 23:34:32
作者Paladz <yzhu101@uott...>
CommiterGitHub

Log Message

Merge branch 'mov' into mov_core

更改概述

差异

--- a/docs/federation/sql_dump/federation_shema.sql
+++ b/docs/federation/sql_dump/federation_shema.sql
@@ -96,6 +96,7 @@ CREATE TABLE `assets` (
9696 `issuance_program` varchar(128) NOT NULL,
9797 `vm_version` int(11) NOT NULL DEFAULT '1',
9898 `definition` text,
99+ `is_open_federation_issue` tinyint(1) DEFAULT '0',
99100 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
100101 `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
101102 PRIMARY KEY (`id`),
--- a/toolbar/federation/api/handler.go
+++ b/toolbar/federation/api/handler.go
@@ -34,7 +34,7 @@ func (s *Server) ListCrosschainTxs(c *gin.Context, listTxsReq *listCrosschainTxs
3434 txFilter.SourceTxHash = txHash
3535 }
3636 if txHash, err := listTxsReq.GetFilterString("dest_tx_hash"); err == nil && txHash != "" {
37- txFilter.DestTxHash = sql.NullString{txHash, true}
37+ txFilter.DestTxHash = sql.NullString{String: txHash, Valid: true}
3838 }
3939
4040 txQuery := s.db.Preload("Chain").Preload("Reqs").Preload("Reqs.Asset").Where(txFilter)
--- a/toolbar/federation/database/orm/asset.go
+++ b/toolbar/federation/database/orm/asset.go
@@ -5,11 +5,12 @@ import (
55 )
66
77 type Asset struct {
8- ID uint64 `gorm:"primary_key;foreignkey:ID" json:"-"`
9- AssetID string `json:"asset_id"`
10- IssuanceProgram string `json:"-"`
11- VMVersion uint64 `json:"-"`
12- Definition string `json:"-"`
13- CreatedAt common.Timestamp `json:"-"`
14- UpdatedAt common.Timestamp `json:"-"`
8+ ID uint64 `gorm:"primary_key;foreignkey:ID" json:"-"`
9+ AssetID string `json:"asset_id"`
10+ IssuanceProgram string `json:"-"`
11+ VMVersion uint64 `json:"-"`
12+ Definition string `json:"-"`
13+ IsOpenFederationIssue bool `json:"_"`
14+ CreatedAt common.Timestamp `json:"-"`
15+ UpdatedAt common.Timestamp `json:"-"`
1516 }
--- a/toolbar/federation/synchron/mainchain_keeper.go
+++ b/toolbar/federation/synchron/mainchain_keeper.go
@@ -14,6 +14,7 @@ import (
1414 "github.com/jinzhu/gorm"
1515 log "github.com/sirupsen/logrus"
1616
17+ vpCommon "github.com/vapor/common"
1718 "github.com/vapor/consensus"
1819 "github.com/vapor/errors"
1920 "github.com/vapor/protocol/bc"
@@ -53,6 +54,8 @@ func NewMainchainKeeper(db *gorm.DB, assetStore *database.AssetStore, cfg *confi
5354
5455 func (m *mainchainKeeper) Run() {
5556 ticker := time.NewTicker(time.Duration(m.cfg.SyncSeconds) * time.Second)
57+ defer ticker.Stop()
58+
5659 for ; true; <-ticker.C {
5760 for {
5861 isUpdate, err := m.syncBlock()
@@ -86,6 +89,10 @@ func (m *mainchainKeeper) createCrossChainReqs(db *gorm.DB, crossTransactionID u
8689 return err
8790 }
8891
92+ if asset.IsOpenFederationIssue {
93+ continue
94+ }
95+
8996 req := &orm.CrossTransactionReq{
9097 CrossTransactionID: crossTransactionID,
9198 SourcePos: uint64(i),
@@ -103,30 +110,42 @@ func (m *mainchainKeeper) createCrossChainReqs(db *gorm.DB, crossTransactionID u
103110 return nil
104111 }
105112
106-func (m *mainchainKeeper) isDepositTx(tx *types.Tx) bool {
113+func (m *mainchainKeeper) isDepositTx(tx *types.Tx) (bool, error) {
107114 for _, input := range tx.Inputs {
108115 if bytes.Equal(input.ControlProgram(), m.federationProg) {
109- return false
116+ return false, nil
110117 }
111118 }
112119
113120 for _, output := range tx.Outputs {
114- if bytes.Equal(output.OutputCommitment.ControlProgram, m.federationProg) {
115- return true
121+ if !bytes.Equal(output.OutputCommitment.ControlProgram, m.federationProg) {
122+ continue
123+ }
124+
125+ if isOFAsset, err := m.isOpenFederationAsset(output.AssetId); err != nil {
126+ return false, err
127+ } else if !isOFAsset {
128+ return true, nil
116129 }
117130 }
118- return false
131+ return false, nil
119132 }
120133
121-func (m *mainchainKeeper) isWithdrawalTx(tx *types.Tx) bool {
134+func (m *mainchainKeeper) isWithdrawalTx(tx *types.Tx) (bool, error) {
122135 for _, input := range tx.Inputs {
123136 if !bytes.Equal(input.ControlProgram(), m.federationProg) {
124- return false
137+ return false, nil
138+ }
139+
140+ if isOFAsset, err := m.isOpenFederationAsset(input.AssetAmount().AssetId); err != nil {
141+ return false, err
142+ } else if isOFAsset {
143+ return false, nil
125144 }
126145 }
127146
128147 sourceTxHash := locateSideChainTx(tx.Outputs[len(tx.Outputs)-1])
129- return sourceTxHash != ""
148+ return sourceTxHash != "", nil
130149 }
131150
132151 func locateSideChainTx(output *types.TxOutput) string {
@@ -156,13 +175,17 @@ func (m *mainchainKeeper) processBlock(db *gorm.DB, block *types.Block, txStatus
156175 return err
157176 }
158177
159- if m.isDepositTx(tx) {
178+ if isDeposit, err := m.isDepositTx(tx); err != nil {
179+ return err
180+ } else if isDeposit {
160181 if err := m.processDepositTx(db, block, txStatus, i); err != nil {
161182 return err
162183 }
163184 }
164185
165- if m.isWithdrawalTx(tx) {
186+ if isWithdrawal, err := m.isWithdrawalTx(tx); err != nil {
187+ return err
188+ } else if isWithdrawal {
166189 if err := m.processWithdrawalTx(db, block, i); err != nil {
167190 return err
168191 }
@@ -188,6 +211,15 @@ func (m *mainchainKeeper) processChainInfo(db *gorm.DB, block *types.Block) erro
188211 return nil
189212 }
190213
214+func (m *mainchainKeeper) isOpenFederationAsset(assetID *btmBc.AssetID) (bool, error) {
215+ asset, err := m.assetStore.GetByAssetID(assetID.String())
216+ if err != nil {
217+ return false, err
218+ }
219+
220+ return asset.IsOpenFederationIssue, nil
221+}
222+
191223 func (m *mainchainKeeper) processDepositTx(db *gorm.DB, block *types.Block, txStatus *bc.TransactionStatus, txIndex int) error {
192224 tx := block.Transactions[txIndex]
193225 var muxID btmBc.Hash
@@ -242,10 +274,11 @@ func (m *mainchainKeeper) processIssuance(tx *types.Tx) error {
242274 }
243275
244276 asset := &orm.Asset{
245- AssetID: assetID.String(),
246- IssuanceProgram: hex.EncodeToString(issuance.IssuanceProgram),
247- VMVersion: issuance.VMVersion,
248- Definition: string(issuance.AssetDefinition),
277+ AssetID: assetID.String(),
278+ IssuanceProgram: hex.EncodeToString(issuance.IssuanceProgram),
279+ VMVersion: issuance.VMVersion,
280+ Definition: string(issuance.AssetDefinition),
281+ IsOpenFederationIssue: vpCommon.IsOpenFederationIssueAsset(issuance.AssetDefinition),
249282 }
250283
251284 if err := m.db.Create(asset).Error; err != nil {
--- a/toolbar/federation/synchron/sidechain_keeper.go
+++ b/toolbar/federation/synchron/sidechain_keeper.go
@@ -78,6 +78,10 @@ func (s *sidechainKeeper) createCrossChainReqs(db *gorm.DB, crossTransactionID u
7878 return err
7979 }
8080
81+ if asset.IsOpenFederationIssue {
82+ continue
83+ }
84+
8185 prog := rawOutput.ControlProgram()
8286 req := &orm.CrossTransactionReq{
8387 CrossTransactionID: crossTransactionID,
@@ -96,33 +100,49 @@ func (s *sidechainKeeper) createCrossChainReqs(db *gorm.DB, crossTransactionID u
96100 return nil
97101 }
98102
99-func (s *sidechainKeeper) isDepositTx(tx *types.Tx) bool {
103+func (s *sidechainKeeper) isDepositTx(tx *types.Tx) (bool, error) {
100104 for _, input := range tx.Inputs {
101- if input.InputType() == types.CrossChainInputType {
102- return true
105+ if input.InputType() != types.CrossChainInputType {
106+ continue
107+ }
108+
109+ if isOFAsset, err := s.isOpenFederationAsset(input.AssetAmount().AssetId); err != nil {
110+ return false, err
111+ } else if !isOFAsset {
112+ return true, nil
103113 }
104114 }
105- return false
115+ return false, nil
106116 }
107117
108-func (s *sidechainKeeper) isWithdrawalTx(tx *types.Tx) bool {
118+func (s *sidechainKeeper) isWithdrawalTx(tx *types.Tx) (bool, error) {
109119 for _, output := range tx.Outputs {
110- if output.OutputType() == types.CrossChainOutputType {
111- return true
120+ if output.OutputType() != types.CrossChainOutputType {
121+ continue
122+ }
123+
124+ if isOFAsset, err := s.isOpenFederationAsset(output.AssetAmount().AssetId); err != nil {
125+ return false, err
126+ } else if !isOFAsset {
127+ return true, nil
112128 }
113129 }
114- return false
130+ return false, nil
115131 }
116132
117133 func (s *sidechainKeeper) processBlock(db *gorm.DB, block *types.Block, txStatus *bc.TransactionStatus) error {
118134 for i, tx := range block.Transactions {
119- if s.isDepositTx(tx) {
135+ if isDeposit, err := s.isDepositTx(tx); err != nil {
136+ return err
137+ } else if isDeposit {
120138 if err := s.processDepositTx(db, block, i); err != nil {
121139 return err
122140 }
123141 }
124142
125- if s.isWithdrawalTx(tx) {
143+ if isWithdrawal, err := s.isWithdrawalTx(tx); err != nil {
144+ return err
145+ } else if isWithdrawal {
126146 if err := s.processWithdrawalTx(db, block, txStatus, i); err != nil {
127147 return err
128148 }
@@ -177,6 +197,15 @@ func (s *sidechainKeeper) processDepositTx(db *gorm.DB, block *types.Block, txIn
177197 return nil
178198 }
179199
200+func (s *sidechainKeeper) isOpenFederationAsset(assetID *bc.AssetID) (bool, error) {
201+ asset, err := s.assetStore.GetByAssetID(assetID.String())
202+ if err != nil {
203+ return false, err
204+ }
205+
206+ return asset.IsOpenFederationIssue, nil
207+}
208+
180209 func (s *sidechainKeeper) processWithdrawalTx(db *gorm.DB, block *types.Block, txStatus *bc.TransactionStatus, txIndex int) error {
181210 tx := block.Transactions[txIndex]
182211 var muxID bc.Hash
Show on old repository browser