OSDN Git Service

Modify interface function
authorYahtoo Ma <yahtoo.ma@gmail.com>
Mon, 21 Oct 2019 02:34:54 +0000 (10:34 +0800)
committerYahtoo Ma <yahtoo.ma@gmail.com>
Mon, 21 Oct 2019 02:34:54 +0000 (10:34 +0800)
proposal/blockproposer/blockproposer.go
protocol/block.go
protocol/mov.go
protocol/protocol.go

index c209741..314dc54 100644 (file)
@@ -18,11 +18,12 @@ import (
 )
 
 const (
-       logModule = "blockproposer"
+       logModule     = "blockproposer"
+       maxBlockTxNum = 3000
 )
 
 type Preprocessor interface {
-       BeforeProposalBlock(txs []*types.Tx) ([]*types.Tx, error)
+       BeforeProposalBlock(capacity int) ([]*types.Tx, error)
 }
 
 // BlockProposer propose several block in specified time range
@@ -87,14 +88,19 @@ func (b *BlockProposer) generateBlocks() {
                for _, txDesc := range txs {
                        packageTxs = append(packageTxs, txDesc.Tx)
                }
-
+               capacity := maxBlockTxNum - len(txs)
                for i, p := range b.preprocessors {
-                       txs, err := p.BeforeProposalBlock(packageTxs)
+                       if capacity <= 0 {
+                               break
+                       }
+
+                       txs, err := p.BeforeProposalBlock(capacity)
                        if err != nil {
                                log.WithFields(log.Fields{"module": logModule, "index": i, "error": err}).Error("failed on sub protocol txs package")
                                continue
                        }
                        packageTxs = append(packageTxs, txs...)
+                       capacity = capacity - len(txs)
                }
 
                block, err := proposal.NewBlockTemplate(b.chain, b.txPool, b.accountManager, packageTxs, nextBlockTime)
index 0571119..05eb702 100644 (file)
@@ -299,7 +299,7 @@ func (c *Chain) saveBlock(block *types.Block) error {
        }
 
        for _, p := range c.subProtocols {
-               if err := p.ValidateBlock(bcBlock); err != nil {
+               if err := p.ValidateBlock(block); err != nil {
                        return errors.Wrap(err, "sub protocol save block")
                }
        }
index 331a36a..03d02f8 100644 (file)
@@ -11,12 +11,12 @@ const (
 
 type matchEnginer interface {
        ApplyBlock(block *types.Block) error
-       BeforeProposalBlock(txs []*types.Tx) ([]*types.Tx, error)
-       ChainStatus() (uint64, *bc.Hash)
+       BeforeProposalBlock(capacity int) ([]*types.Tx, error)
+       ChainStatus() (uint64, *bc.Hash, error)
        DetachBlock(block *types.Block) error
        IsDust(tx *types.Tx) bool
-       ValidateBlock(block *bc.Block) error
-       ValidateTxs(txs []*bc.Tx) error
+       ValidateBlock(block *types.Block) error
+       ValidateTxs(txs []*types.Tx) error
 }
 
 type MOV struct {
@@ -31,11 +31,11 @@ func (m MOV) ApplyBlock(block *types.Block) error {
        return m.engine.ApplyBlock(block)
 }
 
-func (m MOV) BeforeProposalBlock(txs []*types.Tx) ([]*types.Tx, error) {
-       return m.engine.BeforeProposalBlock(txs)
+func (m MOV) BeforeProposalBlock(capacity int) ([]*types.Tx, error) {
+       return m.engine.BeforeProposalBlock(capacity)
 }
 
-func (m MOV) ChainStatus() (uint64, *bc.Hash) {
+func (m MOV) ChainStatus() (uint64, *bc.Hash, error) {
        return m.engine.ChainStatus()
 }
 
@@ -51,14 +51,10 @@ func (m MOV) Name() string {
        return protocolName
 }
 
-func (m MOV) ValidateBlock(block *bc.Block) error {
+func (m MOV) ValidateBlock(block *types.Block) error {
        return m.engine.ValidateBlock(block)
 }
 
-func (m MOV) ValidateTxs(txs []*bc.Tx) error {
+func (m MOV) ValidateTxs(txs []*types.Tx) error {
        return m.engine.ValidateTxs(txs)
 }
-
-func (m MOV) Status() (uint64, *bc.Hash) {
-       return m.engine.ChainStatus()
-}
index dc0b241..fa75c3b 100644 (file)
@@ -21,9 +21,9 @@ const (
 
 type Protocoler interface {
        Name() string
-       Status() (uint64, *bc.Hash)
-       ValidateBlock(block *bc.Block) error
-       ValidateTxs(txs []*bc.Tx) error
+       ChainStatus() (uint64, *bc.Hash, error)
+       ValidateBlock(block *types.Block) error
+       ValidateTxs(txs []*types.Tx) error
        ApplyBlock(block *types.Block) error
        DetachBlock(block *types.Block) error
 }
@@ -202,7 +202,11 @@ func (c *Chain) markTransactions(txs ...*types.Tx) {
 }
 
 func (c *Chain) syncProtocolStatus(subProtocol Protocoler) error {
-       protocolHeight, protocolHash := subProtocol.Status()
+       protocolHeight, protocolHash, err := subProtocol.ChainStatus()
+       if err != nil {
+               return errors.Wrap(err, "failed on get sub protocol status")
+       }
+
        if protocolHeight == c.BestBlockHeight() && protocolHash == c.BestBlockHash() {
                return nil
        }
@@ -217,7 +221,10 @@ func (c *Chain) syncProtocolStatus(subProtocol Protocoler) error {
                        return errors.Wrap(err, subProtocol.Name(), "sub protocol detach block err")
                }
 
-               protocolHeight, protocolHash = subProtocol.Status()
+               protocolHeight, protocolHash, err = subProtocol.ChainStatus()
+               if err != nil {
+                       return errors.Wrap(err, "failed on get sub protocol status")
+               }
        }
 
        for height := protocolHeight + 1; height <= c.BestBlockHeight(); height++ {
@@ -230,7 +237,11 @@ func (c *Chain) syncProtocolStatus(subProtocol Protocoler) error {
                        return errors.Wrap(err, subProtocol.Name(), "sub protocol apply block err")
                }
 
-               protocolHeight, protocolHash = subProtocol.Status()
+               protocolHeight, protocolHash, err = subProtocol.ChainStatus()
+               if err != nil {
+                       return errors.Wrap(err, "failed on get sub protocol status")
+               }
+
                if *protocolHash != block.Hash() {
                        return errors.Wrap(errors.New("sub protocol status sync err"), subProtocol.Name())
                }