consensus: apply policy during verifyBlock

To follow C# implementation we should also check proposed block on
policy matching.
This commit is contained in:
Anna Shaleva 2020-08-03 19:27:32 +03:00
parent 4a1c8464f9
commit db65ed04d9
4 changed files with 40 additions and 0 deletions

View file

@ -341,7 +341,19 @@ func (s *service) getTx(h util.Uint256) block.Transaction {
func (s *service) verifyBlock(b block.Block) bool {
coreb := &b.(*neoBlock).Block
maxBlockSize := int(s.Chain.GetMaxBlockSize())
size := io.GetVarSize(coreb)
if size > maxBlockSize {
s.log.Warn("proposed block size exceeds policy max block size",
zap.Int("max size allowed", maxBlockSize),
zap.Int("block size", size))
return false
}
var fee int64
for _, tx := range coreb.Transactions {
fee += tx.SystemFee
if err := s.Chain.VerifyTx(tx, coreb); err != nil {
s.log.Warn("invalid transaction in proposed block",
zap.Stringer("hash", tx.Hash()),
@ -350,6 +362,14 @@ func (s *service) verifyBlock(b block.Block) bool {
}
}
maxBlockSysFee := s.Chain.GetMaxBlockSystemFee()
if fee > maxBlockSysFee {
s.log.Warn("proposed block system fee exceeds policy max block system fee",
zap.Int("max system fee allowed", int(maxBlockSysFee)),
zap.Int("block system fee", int(fee)))
return false
}
return true
}

View file

@ -1147,6 +1147,16 @@ func (bc *Blockchain) FeePerByte() int64 {
return bc.contracts.Policy.GetFeePerByteInternal(bc.dao)
}
// GetMaxBlockSize returns maximum allowed block size from native Policy contract.
func (bc *Blockchain) GetMaxBlockSize() uint32 {
return bc.contracts.Policy.GetMaxBlockSizeInternal(bc.dao)
}
// GetMaxBlockSystemFee returns maximum block system fee from native Policy contract.
func (bc *Blockchain) GetMaxBlockSystemFee() int64 {
return bc.contracts.Policy.GetMaxBlockSystemFeeInternal(bc.dao)
}
// GetMemPool returns the memory pool of the blockchain.
func (bc *Blockchain) GetMemPool() *mempool.Pool {
return &bc.memPool

View file

@ -50,6 +50,8 @@ type Blockchainer interface {
GetTestVM(tx *transaction.Transaction) *vm.VM
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
mempool.Feer // fee interface
GetMaxBlockSize() uint32
GetMaxBlockSystemFee() int64
PoolTx(*transaction.Transaction) error
SubscribeForBlocks(ch chan<- *block.Block)
SubscribeForExecutions(ch chan<- *state.AppExecResult)

View file

@ -40,6 +40,14 @@ func (chain testChain) FeePerByte() int64 {
panic("TODO")
}
func (chain testChain) GetMaxBlockSystemFee() int64 {
panic("TODO")
}
func (chain testChain) GetMaxBlockSize() uint32 {
panic("TODO")
}
func (chain testChain) AddHeaders(...*block.Header) error {
panic("TODO")
}