2021-06-08 21:30:23 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/blockchain/types"
|
|
|
|
"github.com/Secured-Finance/dione/cache"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BlockPool is pool for blocks that isn't not validated or committed yet
|
|
|
|
type BlockPool struct {
|
2021-06-11 11:40:32 +00:00
|
|
|
mempool *Mempool
|
2021-06-08 21:30:23 +00:00
|
|
|
knownBlocks cache.Cache
|
|
|
|
acceptedBlocks cache.Cache
|
|
|
|
}
|
|
|
|
|
2021-06-11 11:40:32 +00:00
|
|
|
func NewBlockPool(mp *Mempool) (*BlockPool, error) {
|
2021-06-08 21:30:23 +00:00
|
|
|
bp := &BlockPool{
|
|
|
|
acceptedBlocks: cache.NewInMemoryCache(), // here we need to use separate cache
|
2021-06-11 11:40:32 +00:00
|
|
|
knownBlocks: cache.NewInMemoryCache(),
|
|
|
|
mempool: mp,
|
2021-06-08 21:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *BlockPool) AddBlock(block *types.Block) error {
|
|
|
|
return bp.knownBlocks.Store(hex.EncodeToString(block.Header.Hash), block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *BlockPool) GetBlock(blockhash []byte) (*types.Block, error) {
|
|
|
|
var block *types.Block
|
|
|
|
return block, bp.knownBlocks.Get(hex.EncodeToString(blockhash), &block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneBlocks cleans known blocks list. It is called when new consensus round starts.
|
|
|
|
func (bp *BlockPool) PruneBlocks() {
|
|
|
|
for k := range bp.knownBlocks.Items() {
|
|
|
|
bp.knownBlocks.Delete(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *BlockPool) AddAcceptedBlock(block *types.Block) error {
|
|
|
|
return bp.acceptedBlocks.Store(hex.EncodeToString(block.Header.Hash), block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *BlockPool) GetAllAcceptedBlocks() []*types.Block {
|
|
|
|
var blocks []*types.Block
|
|
|
|
for _, v := range bp.acceptedBlocks.Items() {
|
|
|
|
blocks = append(blocks, v.(*types.Block))
|
|
|
|
}
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneAcceptedBlocks cleans accepted blocks list. It is called when new consensus round starts.
|
|
|
|
func (bp *BlockPool) PruneAcceptedBlocks() {
|
2021-06-11 11:40:32 +00:00
|
|
|
for k, v := range bp.acceptedBlocks.Items() {
|
|
|
|
block := v.(*types.Block)
|
|
|
|
for _, v := range block.Data {
|
|
|
|
v.MerkleProof = nil
|
|
|
|
bp.mempool.StoreTx(v) // return transactions back to mempool
|
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
bp.acceptedBlocks.Delete(k)
|
|
|
|
}
|
|
|
|
}
|