)
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
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)
}
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")
}
}
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 {
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()
}
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()
-}
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
}
}
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
}
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++ {
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())
}