sharding/node: shardDB is a property not a service

Former-commit-id: 7d85080fc0d3b82a0bcf5b60546f22d0e78cd9e3 [formerly 7b659786136a9451a7980cb447128ad4f059efd1]
Former-commit-id: 9844823e588bf0d9127fb4019bd66812b6527411
This commit is contained in:
Terence Tsao 2018-06-08 13:15:18 -07:00
parent 86b349f137
commit 9969f7e9bf
4 changed files with 37 additions and 31 deletions

View File

@ -19,27 +19,26 @@ import (
"github.com/ethereum/go-ethereum/sharding/mainchain"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding/database"
"github.com/ethereum/go-ethereum/sharding/params"
"github.com/ethereum/go-ethereum/sharding/txpool"
cli "gopkg.in/urfave/cli.v1"
"gopkg.in/urfave/cli.v1"
)
const shardChainDbName = "shardchaindata"
// ShardEthereum is a service that is registered and started when geth is launched.
// it contains APIs and fields that handle the different components of the sharded
// Ethereum network.
type ShardEthereum struct {
shardConfig *params.ShardConfig // Holds necessary information to configure shards.
txPool *txpool.ShardTXPool // Defines the sharding-specific txpool. To be designed.
actor sharding.Actor // Either notary, proposer, or observer.
shardChainDb ethdb.Database // Access to the persistent db to store shard data.
eventFeed *event.Feed // Used to enable P2P related interactions via different sharding actors.
smcClient *mainchain.SMCClient // Provides bindings to the SMC deployed on the Ethereum mainchain.
shardConfig *params.ShardConfig // Holds necessary information to configure shards.
txPool *txpool.ShardTXPool // Defines the sharding-specific txpool. To be designed.
actor sharding.Actor // Either notary, proposer, or observer.
shardChainDb database.ShardBackend // Access to the persistent db to store shard data.
eventFeed *event.Feed // Used to enable P2P related interactions via different sharding actors.
smcClient *mainchain.SMCClient // Provides bindings to the SMC deployed on the Ethereum mainchain.
// Lifecycle and service stores.
services map[reflect.Type]sharding.Service // Service registry.
@ -76,9 +75,17 @@ func New(ctx *cli.Context) (*ShardEthereum, error) {
return nil, err
}
shardChainDb, err := database.NewShardDB(path, shardChainDbName)
if err != nil {
return nil, err
}
// Adds the initialized SMCClient to the ShardEthereum instance.
shardEthereum.smcClient = smcClient
// Adds the initialized shardChainDb to the ShardEthereum instance.
shardEthereum.shardChainDb = shardChainDb
if err := shardEthereum.registerP2P(); err != nil {
return nil, err
}
@ -184,20 +191,13 @@ func (s *ShardEthereum) registerActorService(actor string) error {
ctx.RetrieveService(&p2p)
if actor == "notary" {
return notary.NewNotary(s.smcClient, p2p)
return notary.NewNotary(s.smcClient, p2p, s.shardChainDb)
} else if actor == "proposer" {
var txPool *txpool.ShardTXPool
ctx.RetrieveService(&txPool)
return proposer.NewProposer(s.smcClient, p2p, txPool)
return proposer.NewProposer(s.smcClient, p2p, txPool, s.shardChainDb)
}
return observer.NewObserver(p2p)
return observer.NewObserver(p2p, s.shardChainDb)
})
}
// registerShardChainDb is relevant to all actors in the sharded system. To register
// shardChainDb grants actor access to a persistent db (either leveldb, badger, or others)
// the detail of which db to use is still yet to be figured out.
func (s *ShardEthereum) registerShardChainDb(dataDir string) error {
return nil
}

View File

@ -5,6 +5,7 @@ package notary
import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/database"
"github.com/ethereum/go-ethereum/sharding/mainchain"
)
@ -12,13 +13,14 @@ import (
// in a sharded system. Must satisfy the Service interface defined in
// sharding/service.go.
type Notary struct {
smcClient *mainchain.SMCClient
shardp2p sharding.ShardP2P
smcClient *mainchain.SMCClient
shardp2p sharding.ShardP2P
shardChainDb database.ShardBackend
}
// NewNotary creates a new notary instance.
func NewNotary(smcClient *mainchain.SMCClient, shardp2p sharding.ShardP2P) (*Notary, error) {
return &Notary{smcClient, shardp2p}, nil
func NewNotary(smcClient *mainchain.SMCClient, shardp2p sharding.ShardP2P, shardChainDb database.ShardBackend) (*Notary, error) {
return &Notary{smcClient, shardp2p, shardChainDb}, nil
}
// Start the main routine for a notary.

View File

@ -5,18 +5,20 @@ package observer
import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/database"
)
// Observer holds functionality required to run an observer service
// in a sharded system. Must satisfy the Service interface defined in
// sharding/service.go.
type Observer struct {
shardp2p sharding.ShardP2P
shardp2p sharding.ShardP2P
shardChainDb database.ShardBackend
}
// NewObserver creates a new observer instance.
func NewObserver(shardp2p sharding.ShardP2P) (*Observer, error) {
return &Observer{shardp2p}, nil
func NewObserver(shardp2p sharding.ShardP2P, shardChainDb database.ShardBackend) (*Observer, error) {
return &Observer{shardp2p, shardChainDb}, nil
}
// Start the main routine for an observer.

View File

@ -5,6 +5,7 @@ package proposer
import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/database"
"github.com/ethereum/go-ethereum/sharding/mainchain"
)
@ -12,17 +13,18 @@ import (
// in a sharded system. Must satisfy the Service interface defined in
// sharding/service.go.
type Proposer struct {
client mainchain.Client
shardp2p sharding.ShardP2P
txpool sharding.TXPool
client mainchain.Client
shardp2p sharding.ShardP2P
txpool sharding.TXPool
shardChainDb database.ShardBackend
}
// NewProposer creates a struct instance of a proposer service.
// It will have access to a mainchain client, a shardp2p network,
// and a shard transaction pool.
func NewProposer(client mainchain.Client, shardp2p sharding.ShardP2P, txpool sharding.TXPool) (*Proposer, error) {
func NewProposer(client mainchain.Client, shardp2p sharding.ShardP2P, txpool sharding.TXPool, shardChainDb database.ShardBackend) (*Proposer, error) {
// Initializes a directory persistent db.
return &Proposer{client, shardp2p, txpool}, nil
return &Proposer{client, shardp2p, txpool, shardChainDb}, nil
}
// Start the main loop for proposing collations.