Rename BlockPool to BlockChain
This commit is contained in:
parent
34c88a8d01
commit
012d4a68c3
@ -1,10 +1,12 @@
|
||||
package pool
|
||||
package blockchain
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
"github.com/Secured-Finance/dione/blockchain/utils"
|
||||
|
||||
types2 "github.com/Secured-Finance/dione/blockchain/types"
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
|
||||
@ -23,15 +25,15 @@ var (
|
||||
ErrLatestHeightNil = errors.New("latest block height is nil")
|
||||
)
|
||||
|
||||
type BlockPool struct {
|
||||
type BlockChain struct {
|
||||
dbEnv *lmdb.Env
|
||||
db lmdb.DBI
|
||||
metadataIndex *Index
|
||||
heightIndex *Index
|
||||
metadataIndex *utils.Index
|
||||
heightIndex *utils.Index
|
||||
}
|
||||
|
||||
func NewBlockPool(path string) (*BlockPool, error) {
|
||||
pool := &BlockPool{}
|
||||
func NewBlockChain(path string) (*BlockChain, error) {
|
||||
chain := &BlockChain{}
|
||||
|
||||
// configure lmdb env
|
||||
env, err := lmdb.NewEnv()
|
||||
@ -49,7 +51,7 @@ func NewBlockPool(path string) (*BlockPool, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pool.dbEnv = env
|
||||
chain.dbEnv = env
|
||||
|
||||
var dbi lmdb.DBI
|
||||
err = env.Update(func(txn *lmdb.Txn) error {
|
||||
@ -60,25 +62,25 @@ func NewBlockPool(path string) (*BlockPool, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pool.db = dbi
|
||||
chain.db = dbi
|
||||
|
||||
// create index instances
|
||||
metadataIndex := NewIndex(DefaultMetadataIndexName, env, dbi)
|
||||
heightIndex := NewIndex("height", env, dbi)
|
||||
pool.metadataIndex = metadataIndex
|
||||
pool.heightIndex = heightIndex
|
||||
metadataIndex := utils.NewIndex(DefaultMetadataIndexName, env, dbi)
|
||||
heightIndex := utils.NewIndex("height", env, dbi)
|
||||
chain.metadataIndex = metadataIndex
|
||||
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)
|
||||
}
|
||||
|
||||
func (bp *BlockPool) GetLatestBlockHeight() (uint64, error) {
|
||||
func (bp *BlockChain) GetLatestBlockHeight() (uint64, error) {
|
||||
height, err := bp.metadataIndex.GetUint64([]byte(LatestBlockHeightKey))
|
||||
if err != nil {
|
||||
if err == ErrIndexKeyNotFound {
|
||||
if err == utils.ErrIndexKeyNotFound {
|
||||
return 0, ErrLatestHeightNil
|
||||
}
|
||||
return 0, err
|
||||
@ -86,7 +88,7 @@ func (bp *BlockPool) GetLatestBlockHeight() (uint64, error) {
|
||||
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 {
|
||||
data, err := cbor.Marshal(block.Data)
|
||||
if err != nil {
|
||||
@ -136,7 +138,7 @@ func (bp *BlockPool) StoreBlock(block *types2.Block) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bp *BlockPool) HasBlock(blockHash []byte) (bool, error) {
|
||||
func (bp *BlockChain) HasBlock(blockHash []byte) (bool, error) {
|
||||
var blockExists bool
|
||||
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
|
||||
h := hex.EncodeToString(blockHash)
|
||||
@ -157,7 +159,7 @@ func (bp *BlockPool) HasBlock(blockHash []byte) (bool, error) {
|
||||
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
|
||||
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
|
||||
h := hex.EncodeToString(blockHash)
|
||||
@ -177,7 +179,7 @@ func (bp *BlockPool) FetchBlockData(blockHash []byte) ([]*types2.Transaction, er
|
||||
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
|
||||
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
|
||||
h := hex.EncodeToString(blockHash)
|
||||
@ -197,7 +199,7 @@ func (bp *BlockPool) FetchBlockHeader(blockHash []byte) (*types2.BlockHeader, er
|
||||
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
|
||||
header, err := bp.FetchBlockHeader(blockHash)
|
||||
if err != nil {
|
||||
@ -214,12 +216,12 @@ func (bp *BlockPool) FetchBlock(blockHash []byte) (*types2.Block, error) {
|
||||
return &block, nil
|
||||
}
|
||||
|
||||
func (bp *BlockPool) FetchBlockByHeight(height uint64) (*types2.Block, error) {
|
||||
func (bp *BlockChain) FetchBlockByHeight(height uint64) (*types2.Block, error) {
|
||||
var heightBytes []byte
|
||||
binary.LittleEndian.PutUint64(heightBytes, height)
|
||||
blockHash, err := bp.heightIndex.GetBytes(heightBytes)
|
||||
if err != nil {
|
||||
if err == ErrIndexKeyNotFound {
|
||||
if err == utils.ErrIndexKeyNotFound {
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
}
|
||||
@ -230,12 +232,12 @@ func (bp *BlockPool) FetchBlockByHeight(height uint64) (*types2.Block, error) {
|
||||
return block, nil
|
||||
}
|
||||
|
||||
func (bp *BlockPool) FetchBlockHeaderByHeight(height uint64) (*types2.BlockHeader, error) {
|
||||
func (bp *BlockChain) FetchBlockHeaderByHeight(height uint64) (*types2.BlockHeader, error) {
|
||||
var heightBytes []byte
|
||||
binary.LittleEndian.PutUint64(heightBytes, height)
|
||||
blockHash, err := bp.heightIndex.GetBytes(heightBytes)
|
||||
if err != nil {
|
||||
if err == ErrIndexKeyNotFound {
|
||||
if err == utils.ErrIndexKeyNotFound {
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
}
|
@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Secured-Finance/dione/blockchain"
|
||||
|
||||
"github.com/Secured-Finance/dione/pubsub"
|
||||
|
||||
"github.com/Secured-Finance/dione/consensus/policy"
|
||||
@ -28,13 +30,10 @@ import (
|
||||
gorpc "github.com/libp2p/go-libp2p-gorpc"
|
||||
)
|
||||
|
||||
type SyncManager interface {
|
||||
Start()
|
||||
Stop()
|
||||
}
|
||||
type SyncManager interface{}
|
||||
|
||||
type syncManager struct {
|
||||
blockpool *pool.BlockPool
|
||||
blockpool *blockchain.BlockChain
|
||||
mempool *pool.Mempool
|
||||
wg sync.WaitGroup
|
||||
ctx context.Context
|
||||
@ -45,7 +44,7 @@ type syncManager struct {
|
||||
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())
|
||||
sm := &syncManager{
|
||||
blockpool: bp,
|
||||
@ -63,34 +62,13 @@ func NewSyncManager(bp *pool.BlockPool, mp *pool.Mempool, p2pRPCClient *gorpc.Cl
|
||||
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 {
|
||||
if sm.initialSyncCompleted {
|
||||
return nil
|
||||
}
|
||||
|
||||
ourLastHeight, err := sm.blockpool.GetLatestBlockHeight()
|
||||
if err == pool.ErrLatestHeightNil {
|
||||
if err == blockchain.ErrLatestHeightNil {
|
||||
gBlock := types2.GenesisBlock()
|
||||
err = sm.blockpool.StoreBlock(gBlock) // commit genesis block
|
||||
if err != nil {
|
||||
@ -243,9 +221,6 @@ func (sm *syncManager) processReceivedBlock(block types2.Block) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sm *syncManager) syncLoop() {
|
||||
}
|
||||
|
||||
func (sm *syncManager) onNewTransaction(message *pubsub.GenericMessage) {
|
||||
tx, ok := message.Payload.(types2.Transaction)
|
||||
if !ok {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package pool
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/Secured-Finance/dione/blockchain"
|
||||
|
||||
gorpc "github.com/libp2p/go-libp2p-gorpc"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -16,12 +18,12 @@ import (
|
||||
)
|
||||
|
||||
type NetworkService struct {
|
||||
blockpool *pool.BlockPool
|
||||
blockpool *blockchain.BlockChain
|
||||
mempool *pool.Mempool
|
||||
rpcClient *gorpc.Client
|
||||
}
|
||||
|
||||
func NewNetworkService(bp *pool.BlockPool) *NetworkService {
|
||||
func NewNetworkService(bp *blockchain.BlockChain) *NetworkService {
|
||||
return &NetworkService{
|
||||
blockpool: bp,
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Secured-Finance/dione/blockchain"
|
||||
|
||||
types2 "github.com/Secured-Finance/dione/blockchain/types"
|
||||
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
@ -63,7 +65,7 @@ type Node struct {
|
||||
Wallet *wallet.LocalWallet
|
||||
Cache cache.Cache
|
||||
DisputeManager *consensus.DisputeManager
|
||||
BlockPool *pool.BlockPool
|
||||
BlockPool *blockchain.BlockChain
|
||||
MemPool *pool.Mempool
|
||||
SyncManager sync.SyncManager
|
||||
NetworkService *NetworkService
|
||||
@ -139,7 +141,7 @@ func NewNode(config *config.Config, prvKey crypto.PrivKey, pexDiscoveryUpdateTim
|
||||
// == initialize blockchain modules
|
||||
|
||||
// initialize blockpool database
|
||||
bp, err := provideBlockPool(n.Config)
|
||||
bp, err := provideBlockChain(n.Config)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Failed to initialize blockpool: %s", err.Error())
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Secured-Finance/dione/blockchain"
|
||||
|
||||
drand2 "github.com/Secured-Finance/dione/beacon/drand"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/protocol"
|
||||
@ -152,15 +154,15 @@ func providePeerDiscovery(baddrs []multiaddr.Multiaddr, h host.Host, pexDiscover
|
||||
return pexDiscovery, nil
|
||||
}
|
||||
|
||||
func provideBlockPool(config *config.Config) (*pool.BlockPool, error) {
|
||||
return pool.NewBlockPool(config.Blockchain.DatabasePath)
|
||||
func provideBlockChain(config *config.Config) (*blockchain.BlockChain, error) {
|
||||
return blockchain.NewBlockChain(config.Blockchain.DatabasePath)
|
||||
}
|
||||
|
||||
func provideMemPool() (*pool.Mempool, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -172,6 +174,6 @@ func provideP2PRPCClient(h host.Host) *gorpc.Client {
|
||||
return gorpc.NewClient(h, DioneProtocolID)
|
||||
}
|
||||
|
||||
func provideNetworkService(bp *pool.BlockPool) *NetworkService {
|
||||
func provideNetworkService(bp *blockchain.BlockChain) *NetworkService {
|
||||
return NewNetworkService(bp)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user