Rename BlockPool to BlockChain

This commit is contained in:
ChronosX88 2021-06-05 00:18:06 +03:00
parent 34c88a8d01
commit 012d4a68c3
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
6 changed files with 48 additions and 65 deletions

View File

@ -1,10 +1,12 @@
package pool package blockchain
import ( import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"errors" "errors"
"github.com/Secured-Finance/dione/blockchain/utils"
types2 "github.com/Secured-Finance/dione/blockchain/types" types2 "github.com/Secured-Finance/dione/blockchain/types"
"github.com/fxamacker/cbor/v2" "github.com/fxamacker/cbor/v2"
@ -23,15 +25,15 @@ var (
ErrLatestHeightNil = errors.New("latest block height is nil") ErrLatestHeightNil = errors.New("latest block height is nil")
) )
type BlockPool struct { type BlockChain struct {
dbEnv *lmdb.Env dbEnv *lmdb.Env
db lmdb.DBI db lmdb.DBI
metadataIndex *Index metadataIndex *utils.Index
heightIndex *Index heightIndex *utils.Index
} }
func NewBlockPool(path string) (*BlockPool, error) { func NewBlockChain(path string) (*BlockChain, error) {
pool := &BlockPool{} chain := &BlockChain{}
// configure lmdb env // configure lmdb env
env, err := lmdb.NewEnv() env, err := lmdb.NewEnv()
@ -49,7 +51,7 @@ func NewBlockPool(path string) (*BlockPool, error) {
return nil, err return nil, err
} }
pool.dbEnv = env chain.dbEnv = env
var dbi lmdb.DBI var dbi lmdb.DBI
err = env.Update(func(txn *lmdb.Txn) error { err = env.Update(func(txn *lmdb.Txn) error {
@ -60,25 +62,25 @@ func NewBlockPool(path string) (*BlockPool, error) {
return nil, err return nil, err
} }
pool.db = dbi chain.db = dbi
// create index instances // create index instances
metadataIndex := NewIndex(DefaultMetadataIndexName, env, dbi) metadataIndex := utils.NewIndex(DefaultMetadataIndexName, env, dbi)
heightIndex := NewIndex("height", env, dbi) heightIndex := utils.NewIndex("height", env, dbi)
pool.metadataIndex = metadataIndex chain.metadataIndex = metadataIndex
pool.heightIndex = heightIndex chain.heightIndex = heightIndex
return pool, nil return chain, nil
} }
func (bp *BlockPool) setLatestBlockHeight(height uint64) error { func (bp *BlockChain) setLatestBlockHeight(height uint64) error {
return bp.metadataIndex.PutUint64([]byte(LatestBlockHeightKey), height) return bp.metadataIndex.PutUint64([]byte(LatestBlockHeightKey), height)
} }
func (bp *BlockPool) GetLatestBlockHeight() (uint64, error) { func (bp *BlockChain) GetLatestBlockHeight() (uint64, error) {
height, err := bp.metadataIndex.GetUint64([]byte(LatestBlockHeightKey)) height, err := bp.metadataIndex.GetUint64([]byte(LatestBlockHeightKey))
if err != nil { if err != nil {
if err == ErrIndexKeyNotFound { if err == utils.ErrIndexKeyNotFound {
return 0, ErrLatestHeightNil return 0, ErrLatestHeightNil
} }
return 0, err return 0, err
@ -86,7 +88,7 @@ func (bp *BlockPool) GetLatestBlockHeight() (uint64, error) {
return height, nil return height, nil
} }
func (bp *BlockPool) StoreBlock(block *types2.Block) error { func (bp *BlockChain) StoreBlock(block *types2.Block) error {
err := bp.dbEnv.Update(func(txn *lmdb.Txn) error { err := bp.dbEnv.Update(func(txn *lmdb.Txn) error {
data, err := cbor.Marshal(block.Data) data, err := cbor.Marshal(block.Data)
if err != nil { if err != nil {
@ -136,7 +138,7 @@ func (bp *BlockPool) StoreBlock(block *types2.Block) error {
return nil return nil
} }
func (bp *BlockPool) HasBlock(blockHash []byte) (bool, error) { func (bp *BlockChain) HasBlock(blockHash []byte) (bool, error) {
var blockExists bool var blockExists bool
err := bp.dbEnv.View(func(txn *lmdb.Txn) error { err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
h := hex.EncodeToString(blockHash) h := hex.EncodeToString(blockHash)
@ -157,7 +159,7 @@ func (bp *BlockPool) HasBlock(blockHash []byte) (bool, error) {
return blockExists, nil return blockExists, nil
} }
func (bp *BlockPool) FetchBlockData(blockHash []byte) ([]*types2.Transaction, error) { func (bp *BlockChain) FetchBlockData(blockHash []byte) ([]*types2.Transaction, error) {
var data []*types2.Transaction var data []*types2.Transaction
err := bp.dbEnv.View(func(txn *lmdb.Txn) error { err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
h := hex.EncodeToString(blockHash) h := hex.EncodeToString(blockHash)
@ -177,7 +179,7 @@ func (bp *BlockPool) FetchBlockData(blockHash []byte) ([]*types2.Transaction, er
return data, nil return data, nil
} }
func (bp *BlockPool) FetchBlockHeader(blockHash []byte) (*types2.BlockHeader, error) { func (bp *BlockChain) FetchBlockHeader(blockHash []byte) (*types2.BlockHeader, error) {
var blockHeader types2.BlockHeader var blockHeader types2.BlockHeader
err := bp.dbEnv.View(func(txn *lmdb.Txn) error { err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
h := hex.EncodeToString(blockHash) h := hex.EncodeToString(blockHash)
@ -197,7 +199,7 @@ func (bp *BlockPool) FetchBlockHeader(blockHash []byte) (*types2.BlockHeader, er
return &blockHeader, nil return &blockHeader, nil
} }
func (bp *BlockPool) FetchBlock(blockHash []byte) (*types2.Block, error) { func (bp *BlockChain) FetchBlock(blockHash []byte) (*types2.Block, error) {
var block types2.Block var block types2.Block
header, err := bp.FetchBlockHeader(blockHash) header, err := bp.FetchBlockHeader(blockHash)
if err != nil { if err != nil {
@ -214,12 +216,12 @@ func (bp *BlockPool) FetchBlock(blockHash []byte) (*types2.Block, error) {
return &block, nil return &block, nil
} }
func (bp *BlockPool) FetchBlockByHeight(height uint64) (*types2.Block, error) { func (bp *BlockChain) FetchBlockByHeight(height uint64) (*types2.Block, error) {
var heightBytes []byte var heightBytes []byte
binary.LittleEndian.PutUint64(heightBytes, height) binary.LittleEndian.PutUint64(heightBytes, height)
blockHash, err := bp.heightIndex.GetBytes(heightBytes) blockHash, err := bp.heightIndex.GetBytes(heightBytes)
if err != nil { if err != nil {
if err == ErrIndexKeyNotFound { if err == utils.ErrIndexKeyNotFound {
return nil, ErrBlockNotFound return nil, ErrBlockNotFound
} }
} }
@ -230,12 +232,12 @@ func (bp *BlockPool) FetchBlockByHeight(height uint64) (*types2.Block, error) {
return block, nil return block, nil
} }
func (bp *BlockPool) FetchBlockHeaderByHeight(height uint64) (*types2.BlockHeader, error) { func (bp *BlockChain) FetchBlockHeaderByHeight(height uint64) (*types2.BlockHeader, error) {
var heightBytes []byte var heightBytes []byte
binary.LittleEndian.PutUint64(heightBytes, height) binary.LittleEndian.PutUint64(heightBytes, height)
blockHash, err := bp.heightIndex.GetBytes(heightBytes) blockHash, err := bp.heightIndex.GetBytes(heightBytes)
if err != nil { if err != nil {
if err == ErrIndexKeyNotFound { if err == utils.ErrIndexKeyNotFound {
return nil, ErrBlockNotFound return nil, ErrBlockNotFound
} }
} }

View File

@ -8,6 +8,8 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/Secured-Finance/dione/blockchain"
"github.com/Secured-Finance/dione/pubsub" "github.com/Secured-Finance/dione/pubsub"
"github.com/Secured-Finance/dione/consensus/policy" "github.com/Secured-Finance/dione/consensus/policy"
@ -28,13 +30,10 @@ import (
gorpc "github.com/libp2p/go-libp2p-gorpc" gorpc "github.com/libp2p/go-libp2p-gorpc"
) )
type SyncManager interface { type SyncManager interface{}
Start()
Stop()
}
type syncManager struct { type syncManager struct {
blockpool *pool.BlockPool blockpool *blockchain.BlockChain
mempool *pool.Mempool mempool *pool.Mempool
wg sync.WaitGroup wg sync.WaitGroup
ctx context.Context ctx context.Context
@ -45,7 +44,7 @@ type syncManager struct {
psb *pubsub.PubSubRouter psb *pubsub.PubSubRouter
} }
func NewSyncManager(bp *pool.BlockPool, mp *pool.Mempool, p2pRPCClient *gorpc.Client, bootstrapPeer peer.ID, psb *pubsub.PubSubRouter) SyncManager { func NewSyncManager(bp *blockchain.BlockChain, mp *pool.Mempool, p2pRPCClient *gorpc.Client, bootstrapPeer peer.ID, psb *pubsub.PubSubRouter) SyncManager {
ctx, cancelFunc := context.WithCancel(context.Background()) ctx, cancelFunc := context.WithCancel(context.Background())
sm := &syncManager{ sm := &syncManager{
blockpool: bp, blockpool: bp,
@ -63,34 +62,13 @@ func NewSyncManager(bp *pool.BlockPool, mp *pool.Mempool, p2pRPCClient *gorpc.Cl
return sm return sm
} }
func (sm *syncManager) Start() {
sm.wg.Add(1)
err := sm.doInitialBlockPoolSync()
if err != nil {
logrus.Error(err)
}
err = sm.doInitialMempoolSync()
if err != nil {
logrus.Error(err)
}
go sm.syncLoop()
}
func (sm *syncManager) Stop() {
sm.ctxCancelFunc()
sm.wg.Wait()
}
func (sm *syncManager) doInitialBlockPoolSync() error { func (sm *syncManager) doInitialBlockPoolSync() error {
if sm.initialSyncCompleted { if sm.initialSyncCompleted {
return nil return nil
} }
ourLastHeight, err := sm.blockpool.GetLatestBlockHeight() ourLastHeight, err := sm.blockpool.GetLatestBlockHeight()
if err == pool.ErrLatestHeightNil { if err == blockchain.ErrLatestHeightNil {
gBlock := types2.GenesisBlock() gBlock := types2.GenesisBlock()
err = sm.blockpool.StoreBlock(gBlock) // commit genesis block err = sm.blockpool.StoreBlock(gBlock) // commit genesis block
if err != nil { if err != nil {
@ -243,9 +221,6 @@ func (sm *syncManager) processReceivedBlock(block types2.Block) error {
return nil return nil
} }
func (sm *syncManager) syncLoop() {
}
func (sm *syncManager) onNewTransaction(message *pubsub.GenericMessage) { func (sm *syncManager) onNewTransaction(message *pubsub.GenericMessage) {
tx, ok := message.Payload.(types2.Transaction) tx, ok := message.Payload.(types2.Transaction)
if !ok { if !ok {

View File

@ -1,4 +1,4 @@
package pool package utils
import ( import (
"encoding/binary" "encoding/binary"

View File

@ -5,6 +5,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/Secured-Finance/dione/blockchain"
gorpc "github.com/libp2p/go-libp2p-gorpc" gorpc "github.com/libp2p/go-libp2p-gorpc"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -16,12 +18,12 @@ import (
) )
type NetworkService struct { type NetworkService struct {
blockpool *pool.BlockPool blockpool *blockchain.BlockChain
mempool *pool.Mempool mempool *pool.Mempool
rpcClient *gorpc.Client rpcClient *gorpc.Client
} }
func NewNetworkService(bp *pool.BlockPool) *NetworkService { func NewNetworkService(bp *blockchain.BlockChain) *NetworkService {
return &NetworkService{ return &NetworkService{
blockpool: bp, blockpool: bp,
} }

View File

@ -9,6 +9,8 @@ import (
"os" "os"
"time" "time"
"github.com/Secured-Finance/dione/blockchain"
types2 "github.com/Secured-Finance/dione/blockchain/types" types2 "github.com/Secured-Finance/dione/blockchain/types"
"github.com/fxamacker/cbor/v2" "github.com/fxamacker/cbor/v2"
@ -63,7 +65,7 @@ type Node struct {
Wallet *wallet.LocalWallet Wallet *wallet.LocalWallet
Cache cache.Cache Cache cache.Cache
DisputeManager *consensus.DisputeManager DisputeManager *consensus.DisputeManager
BlockPool *pool.BlockPool BlockPool *blockchain.BlockChain
MemPool *pool.Mempool MemPool *pool.Mempool
SyncManager sync.SyncManager SyncManager sync.SyncManager
NetworkService *NetworkService NetworkService *NetworkService
@ -139,7 +141,7 @@ func NewNode(config *config.Config, prvKey crypto.PrivKey, pexDiscoveryUpdateTim
// == initialize blockchain modules // == initialize blockchain modules
// initialize blockpool database // initialize blockpool database
bp, err := provideBlockPool(n.Config) bp, err := provideBlockChain(n.Config)
if err != nil { if err != nil {
logrus.Fatalf("Failed to initialize blockpool: %s", err.Error()) logrus.Fatalf("Failed to initialize blockpool: %s", err.Error())
} }

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/Secured-Finance/dione/blockchain"
drand2 "github.com/Secured-Finance/dione/beacon/drand" drand2 "github.com/Secured-Finance/dione/beacon/drand"
"github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-libp2p-core/protocol"
@ -152,15 +154,15 @@ func providePeerDiscovery(baddrs []multiaddr.Multiaddr, h host.Host, pexDiscover
return pexDiscovery, nil return pexDiscovery, nil
} }
func provideBlockPool(config *config.Config) (*pool.BlockPool, error) { func provideBlockChain(config *config.Config) (*blockchain.BlockChain, error) {
return pool.NewBlockPool(config.Blockchain.DatabasePath) return blockchain.NewBlockChain(config.Blockchain.DatabasePath)
} }
func provideMemPool() (*pool.Mempool, error) { func provideMemPool() (*pool.Mempool, error) {
return pool.NewMempool() return pool.NewMempool()
} }
func provideSyncManager(bp *pool.BlockPool, mp *pool.Mempool, r *gorpc.Client, bootstrap multiaddr.Multiaddr, psb *pubsub.PubSubRouter) (sync.SyncManager, error) { func provideSyncManager(bp *blockchain.BlockChain, mp *pool.Mempool, r *gorpc.Client, bootstrap multiaddr.Multiaddr, psb *pubsub.PubSubRouter) (sync.SyncManager, error) {
addr, err := peer.AddrInfoFromP2pAddr(bootstrap) addr, err := peer.AddrInfoFromP2pAddr(bootstrap)
if err != nil { if err != nil {
return nil, err return nil, err
@ -172,6 +174,6 @@ func provideP2PRPCClient(h host.Host) *gorpc.Client {
return gorpc.NewClient(h, DioneProtocolID) return gorpc.NewClient(h, DioneProtocolID)
} }
func provideNetworkService(bp *pool.BlockPool) *NetworkService { func provideNetworkService(bp *blockchain.BlockChain) *NetworkService {
return NewNetworkService(bp) return NewNetworkService(bp)
} }