OSDN Git Service

rename
[bytom/vapor.git] / federation / synchron / mainchain_keeper.go
index 6954160..1384cf3 100644 (file)
@@ -7,12 +7,13 @@ import (
        "fmt"
        "time"
 
-       btmConsensus "github.com/bytom/consensus"
+       "github.com/bytom/consensus"
        btmBc "github.com/bytom/protocol/bc"
-       btmTypes "github.com/bytom/protocol/bc/types"
+       "github.com/bytom/protocol/bc/types"
        "github.com/jinzhu/gorm"
        log "github.com/sirupsen/logrus"
 
+       vaporCfg "github.com/vapor/config"
        "github.com/vapor/errors"
        "github.com/vapor/federation/common"
        "github.com/vapor/federation/config"
@@ -22,6 +23,8 @@ import (
        "github.com/vapor/protocol/bc"
 )
 
+var fedProg = vaporCfg.FederationProgrom(vaporCfg.CommonConfig)
+
 type mainchainKeeper struct {
        cfg        *config.Chain
        db         *gorm.DB
@@ -77,7 +80,7 @@ func (m *mainchainKeeper) syncBlock() (bool, error) {
                return false, err
        }
 
-       nextBlock := &btmTypes.Block{}
+       nextBlock := &types.Block{}
        if err := nextBlock.UnmarshalText([]byte(nextBlockStr)); err != nil {
                return false, errors.New("Unmarshal nextBlock")
        }
@@ -97,7 +100,7 @@ func (m *mainchainKeeper) syncBlock() (bool, error) {
        return true, nil
 }
 
-func (m *mainchainKeeper) tryAttachBlock(chain *orm.Chain, block *btmTypes.Block, txStatus *bc.TransactionStatus) error {
+func (m *mainchainKeeper) tryAttachBlock(chain *orm.Chain, block *types.Block, txStatus *bc.TransactionStatus) error {
        blockHash := block.Hash()
        log.WithFields(log.Fields{"block_height": block.Height, "block_hash": blockHash.String()}).Info("start to attachBlock")
        m.db.Begin()
@@ -109,7 +112,7 @@ func (m *mainchainKeeper) tryAttachBlock(chain *orm.Chain, block *btmTypes.Block
        return m.db.Commit().Error
 }
 
-func (m *mainchainKeeper) processBlock(chain *orm.Chain, block *btmTypes.Block, txStatus *bc.TransactionStatus) error {
+func (m *mainchainKeeper) processBlock(chain *orm.Chain, block *types.Block, txStatus *bc.TransactionStatus) error {
        if err := m.processIssuing(block.Transactions); err != nil {
                return err
        }
@@ -131,7 +134,7 @@ func (m *mainchainKeeper) processBlock(chain *orm.Chain, block *btmTypes.Block,
        return m.processChainInfo(chain, block)
 }
 
-func (m *mainchainKeeper) isDepositTx(tx *btmTypes.Tx) bool {
+func (m *mainchainKeeper) isDepositTx(tx *types.Tx) bool {
        for _, output := range tx.Outputs {
                if bytes.Equal(output.OutputCommitment.ControlProgram, fedProg) {
                        return true
@@ -140,7 +143,7 @@ func (m *mainchainKeeper) isDepositTx(tx *btmTypes.Tx) bool {
        return false
 }
 
-func (m *mainchainKeeper) isWithdrawalTx(tx *btmTypes.Tx) bool {
+func (m *mainchainKeeper) isWithdrawalTx(tx *types.Tx) bool {
        for _, input := range tx.Inputs {
                if bytes.Equal(input.ControlProgram(), fedProg) {
                        return true
@@ -149,7 +152,7 @@ func (m *mainchainKeeper) isWithdrawalTx(tx *btmTypes.Tx) bool {
        return false
 }
 
-func (m *mainchainKeeper) processDepositTx(chain *orm.Chain, block *btmTypes.Block, txStatus *bc.TransactionStatus, txIndex uint64, tx *btmTypes.Tx) error {
+func (m *mainchainKeeper) processDepositTx(chain *orm.Chain, block *types.Block, txStatus *bc.TransactionStatus, txIndex uint64, tx *types.Tx) error {
        blockHash := block.Hash()
 
        var muxID btmBc.Hash
@@ -190,7 +193,7 @@ func (m *mainchainKeeper) processDepositTx(chain *orm.Chain, block *btmTypes.Blo
        }
 
        statusFail := txStatus.VerifyStatus[txIndex].StatusFail
-       crossChainInputs, err := m.getCrossChainInputs(ormTx.ID, tx, statusFail)
+       crossChainInputs, err := m.getCrossChainReqs(ormTx.ID, tx, statusFail)
        if err != nil {
                return err
        }
@@ -204,7 +207,7 @@ func (m *mainchainKeeper) processDepositTx(chain *orm.Chain, block *btmTypes.Blo
        return nil
 }
 
-func (m *mainchainKeeper) getCrossChainInputs(crossTransactionID uint64, tx *btmTypes.Tx, statusFail bool) ([]*orm.CrossTransactionReq, error) {
+func (m *mainchainKeeper) getCrossChainReqs(crossTransactionID uint64, tx *types.Tx, statusFail bool) ([]*orm.CrossTransactionReq, error) {
        // assume inputs are from an identical owner
        script := hex.EncodeToString(tx.Inputs[0].ControlProgram())
        inputs := []*orm.CrossTransactionReq{}
@@ -214,7 +217,7 @@ func (m *mainchainKeeper) getCrossChainInputs(crossTransactionID uint64, tx *btm
                        continue
                }
 
-               if statusFail && *rawOutput.OutputCommitment.AssetAmount.AssetId != *btmConsensus.BTMAssetID {
+               if statusFail && *rawOutput.OutputCommitment.AssetAmount.AssetId != *consensus.BTMAssetID {
                        continue
                }
 
@@ -235,7 +238,7 @@ func (m *mainchainKeeper) getCrossChainInputs(crossTransactionID uint64, tx *btm
        return inputs, nil
 }
 
-func (m *mainchainKeeper) processWithdrawalTx(chain *orm.Chain, block *btmTypes.Block, txIndex uint64, tx *btmTypes.Tx) error {
+func (m *mainchainKeeper) processWithdrawalTx(chain *orm.Chain, block *types.Block, txIndex uint64, tx *types.Tx) error {
        blockHash := block.Hash()
        return m.db.Model(&orm.CrossTransaction{}).Where("chain_id != ?", chain.ID).
                Where(&orm.CrossTransaction{
@@ -249,8 +252,7 @@ func (m *mainchainKeeper) processWithdrawalTx(chain *orm.Chain, block *btmTypes.
        }).Error
 }
 
-// TODO: maybe common
-func (m *mainchainKeeper) processChainInfo(chain *orm.Chain, block *btmTypes.Block) error {
+func (m *mainchainKeeper) processChainInfo(chain *orm.Chain, block *types.Block) error {
        blockHash := block.Hash()
        chain.BlockHash = blockHash.String()
        chain.BlockHeight = block.Height
@@ -266,11 +268,11 @@ func (m *mainchainKeeper) processChainInfo(chain *orm.Chain, block *btmTypes.Blo
        return nil
 }
 
-func (m *mainchainKeeper) processIssuing(txs []*btmTypes.Tx) error {
+func (m *mainchainKeeper) processIssuing(txs []*types.Tx) error {
        for _, tx := range txs {
                for _, input := range tx.Inputs {
                        switch inp := input.TypedInput.(type) {
-                       case *btmTypes.IssuanceInput:
+                       case *types.IssuanceInput:
                                assetID := inp.AssetID()
                                if _, err := m.getAsset(assetID.String()); err == nil {
                                        continue
@@ -294,7 +296,6 @@ func (m *mainchainKeeper) processIssuing(txs []*btmTypes.Tx) error {
        return nil
 }
 
-// TODO: maybe common
 func (m *mainchainKeeper) getAsset(assetID string) (*orm.Asset, error) {
        if asset := m.assetCache.Get(assetID); asset != nil {
                return asset, nil