OSDN Git Service

tx validation call the chain function
authorpaladz <453256728@qq.com>
Mon, 25 Sep 2017 07:45:30 +0000 (15:45 +0800)
committerpaladz <453256728@qq.com>
Mon, 25 Sep 2017 07:45:30 +0000 (15:45 +0800)
blockchain/reactor.go

index 991d5fa..d03bb79 100644 (file)
@@ -11,16 +11,15 @@ import (
        "github.com/bytom/blockchain/accesstoken"
        "github.com/bytom/blockchain/account"
        "github.com/bytom/blockchain/asset"
+       "github.com/bytom/blockchain/pseudohsm"
        "github.com/bytom/blockchain/txdb"
        "github.com/bytom/blockchain/txfeed"
-       "github.com/bytom/blockchain/pseudohsm"
        "github.com/bytom/encoding/json"
        "github.com/bytom/log"
        "github.com/bytom/mining/cpuminer"
        "github.com/bytom/p2p"
        "github.com/bytom/protocol"
        "github.com/bytom/protocol/bc/legacy"
-       "github.com/bytom/protocol/validation"
        "github.com/bytom/types"
        wire "github.com/tendermint/go-wire"
        cmn "github.com/tendermint/tmlibs/common"
@@ -52,27 +51,26 @@ const (
        crosscoreRPCPrefix               = "/rpc/"
 )
 
-
 // BlockchainReactor handles long-term catchup syncing.
 type BlockchainReactor struct {
        p2p.BaseReactor
 
-       chain      *protocol.Chain
-       store      *txdb.Store
-       accounts   *account.Manager
-       assets     *asset.Registry
+       chain       *protocol.Chain
+       store       *txdb.Store
+       accounts    *account.Manager
+       assets      *asset.Registry
        accesstoken *accesstoken.Token
-       txFeeds    *txfeed.TxFeed
-       pool       *BlockPool
-       txPool     *protocol.TxPool
-       hsm                *pseudohsm.HSM
-       mining     *cpuminer.CPUMiner
-       mux        *http.ServeMux
-       handler         http.Handler
-       fastSync        bool
-       requestsCh      chan BlockRequest
-       timeoutsCh      chan string
-       submitter       txbuilder.Submitter
+       txFeeds     *txfeed.TxFeed
+       pool        *BlockPool
+       txPool      *protocol.TxPool
+       hsm         *pseudohsm.HSM
+       mining      *cpuminer.CPUMiner
+       mux         *http.ServeMux
+       handler     http.Handler
+       fastSync    bool
+       requestsCh  chan BlockRequest
+       timeoutsCh  chan string
+       submitter   txbuilder.Submitter
        evsw        types.EventSwitch
 }
 
@@ -99,8 +97,6 @@ func batchRecover(ctx context.Context, v *interface{}) {
        }
 }
 
-
-
 func jsonHandler(f interface{}) http.Handler {
        h, err := httpjson.Handler(f, errorFormatter.Write)
        if err != nil {
@@ -113,7 +109,6 @@ func alwaysError(err error) http.Handler {
        return jsonHandler(func() error { return err })
 }
 
-
 func (bcr *BlockchainReactor) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
        bcr.handler.ServeHTTP(rw, req)
 }
@@ -193,7 +188,6 @@ func (bcr *BlockchainReactor) BuildHander() {
        //m.Handle("/hsm/reset-password", jsonHandler(bcr.pseudohsmResetPassword))
        //m.Handle("/hsm/update-alias", jsonHandler(bcr.pseudohsmUpdateAlias))
 
-
        latencyHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
                if l := latency(m, req); l != nil {
                        defer l.RecordSince(time.Now())
@@ -257,14 +251,13 @@ type page struct {
        LastPage bool         `json:"last_page"`
 }
 
-
 func NewBlockchainReactor(store *txdb.Store, chain *protocol.Chain, txPool *protocol.TxPool, accounts *account.Manager, assets *asset.Registry, hsm *pseudohsm.HSM, fastSync bool) *BlockchainReactor {
        requestsCh := make(chan BlockRequest, defaultChannelCapacity)
        timeoutsCh := make(chan string, defaultChannelCapacity)
        pool := NewBlockPool(
-                       store.Height()+1,
-                       requestsCh,
-                       timeoutsCh,
+               store.Height()+1,
+               requestsCh,
+               timeoutsCh,
        )
        mining := cpuminer.NewCPUMiner(chain, txPool)
        bcR := &BlockchainReactor{
@@ -276,7 +269,7 @@ func NewBlockchainReactor(store *txdb.Store, chain *protocol.Chain, txPool *prot
                txPool:     txPool,
                mining:     mining,
                mux:        http.NewServeMux(),
-               hsm:        hsm,
+               hsm:        hsm,
                fastSync:   fastSync,
                requestsCh: requestsCh,
                timeoutsCh: timeoutsCh,
@@ -285,7 +278,6 @@ func NewBlockchainReactor(store *txdb.Store, chain *protocol.Chain, txPool *prot
        return bcR
 }
 
-
 // OnStart implements BaseService
 func (bcR *BlockchainReactor) OnStart() error {
        bcR.BaseReactor.OnStart()
@@ -366,17 +358,12 @@ func (bcR *BlockchainReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte)
                //fmt.Printf("reveive peer high is %d \n", msg.Height)
                bcR.pool.SetPeerHeight(src.Key, msg.Height)
        case *bcTransactionMessage:
-               block, _ := bcR.chain.State()
                tx := msg.GetTransaction()
-               if bcR.txPool.HaveTransaction(&tx.Tx.ID) {
-                       return
-               }
 
-               gas, err := validation.ValidateTx(tx.Tx, legacy.MapBlock(block))
-               if err != nil {
+               if err := bcR.chain.ValidateTx(tx); err != nil {
                        return
                }
-               bcR.txPool.AddTransaction(tx, block.BlockHeader.Height, gas)
+
                go bcR.BroadcastTransaction(tx)
        default:
                bcR.Logger.Error(cmn.Fmt("Unknown message type %v", reflect.TypeOf(msg)))