Commit dd8961c9 authored by Gari Singh's avatar Gari Singh
Browse files

FAB-15182 Use idiomatic self-reference



Use short idiomatic reference for
provider. Really an aesthetic change.

FAB-15182 #done

Change-Id: Id64e6f0ee88b44b3f722b937349aac7cb63c722f
Signed-off-by: default avatarGari Singh <gari.r.singh@gmail.com>
parent d468a176
...@@ -62,13 +62,13 @@ func NewProvider() (ledger.PeerLedgerProvider, error) { ...@@ -62,13 +62,13 @@ func NewProvider() (ledger.PeerLedgerProvider, error) {
// Initialize the history database (index for history of values by key) // Initialize the history database (index for history of values by key)
historydbProvider := historyleveldb.NewHistoryDBProvider() historydbProvider := historyleveldb.NewHistoryDBProvider()
logger.Info("ledger provider Initialized") logger.Info("ledger provider Initialized")
provider := &Provider{idStore, ledgerStoreProvider, p := &Provider{idStore, ledgerStoreProvider,
nil, historydbProvider, nil, nil, nil, nil, nil, nil} nil, historydbProvider, nil, nil, nil, nil, nil, nil}
return provider, nil return p, nil
} }
// Initialize implements the corresponding method from interface ledger.PeerLedgerProvider // Initialize implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Initialize(initializer *ledger.Initializer) error { func (p *Provider) Initialize(initializer *ledger.Initializer) error {
var err error var err error
configHistoryMgr := confighistory.NewMgr(initializer.DeployedChaincodeInfoProvider) configHistoryMgr := confighistory.NewMgr(initializer.DeployedChaincodeInfoProvider)
collElgNotifier := &collElgNotifier{ collElgNotifier := &collElgNotifier{
...@@ -80,13 +80,13 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error { ...@@ -80,13 +80,13 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
stateListeners = append(stateListeners, collElgNotifier) stateListeners = append(stateListeners, collElgNotifier)
stateListeners = append(stateListeners, configHistoryMgr) stateListeners = append(stateListeners, configHistoryMgr)
provider.initializer = initializer p.initializer = initializer
provider.configHistoryMgr = configHistoryMgr p.configHistoryMgr = configHistoryMgr
provider.stateListeners = stateListeners p.stateListeners = stateListeners
provider.collElgNotifier = collElgNotifier p.collElgNotifier = collElgNotifier
provider.bookkeepingProvider = bookkeeping.NewProvider() p.bookkeepingProvider = bookkeeping.NewProvider()
provider.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider( p.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider(
provider.bookkeepingProvider, p.bookkeepingProvider,
initializer.MetricsProvider, initializer.MetricsProvider,
initializer.HealthCheckRegistry, initializer.HealthCheckRegistry,
initializer.Config.StateDB, initializer.Config.StateDB,
...@@ -94,8 +94,8 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error { ...@@ -94,8 +94,8 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
if err != nil { if err != nil {
return err return err
} }
provider.stats = newStats(initializer.MetricsProvider) p.stats = newStats(initializer.MetricsProvider)
provider.recoverUnderConstructionLedger() p.recoverUnderConstructionLedger()
return nil return nil
} }
...@@ -104,26 +104,26 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error { ...@@ -104,26 +104,26 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
// upon a successful ledger creation with the committed genesis block, removes the flag and add entry into // upon a successful ledger creation with the committed genesis block, removes the flag and add entry into
// created ledgers list (atomically). If a crash happens in between, the 'recoverUnderConstructionLedger' // created ledgers list (atomically). If a crash happens in between, the 'recoverUnderConstructionLedger'
// function is invoked before declaring the provider to be usable // function is invoked before declaring the provider to be usable
func (provider *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger, error) { func (p *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger, error) {
ledgerID, err := protoutil.GetChainIDFromBlock(genesisBlock) ledgerID, err := protoutil.GetChainIDFromBlock(genesisBlock)
if err != nil { if err != nil {
return nil, err return nil, err
} }
exists, err := provider.idStore.ledgerIDExists(ledgerID) exists, err := p.idStore.ledgerIDExists(ledgerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if exists { if exists {
return nil, ErrLedgerIDExists return nil, ErrLedgerIDExists
} }
if err = provider.idStore.setUnderConstructionFlag(ledgerID); err != nil { if err = p.idStore.setUnderConstructionFlag(ledgerID); err != nil {
return nil, err return nil, err
} }
lgr, err := provider.openInternal(ledgerID) lgr, err := p.openInternal(ledgerID)
if err != nil { if err != nil {
logger.Errorf("Error opening a new empty ledger. Unsetting under construction flag. Error: %+v", err) logger.Errorf("Error opening a new empty ledger. Unsetting under construction flag. Error: %+v", err)
panicOnErr(provider.runCleanup(ledgerID), "Error running cleanup for ledger id [%s]", ledgerID) panicOnErr(p.runCleanup(ledgerID), "Error running cleanup for ledger id [%s]", ledgerID)
panicOnErr(provider.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag") panicOnErr(p.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag")
return nil, err return nil, err
} }
if err := lgr.CommitWithPvtData(&ledger.BlockAndPvtData{ if err := lgr.CommitWithPvtData(&ledger.BlockAndPvtData{
...@@ -132,40 +132,40 @@ func (provider *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger, ...@@ -132,40 +132,40 @@ func (provider *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger,
lgr.Close() lgr.Close()
return nil, err return nil, err
} }
panicOnErr(provider.idStore.createLedgerID(ledgerID, genesisBlock), "Error while marking ledger as created") panicOnErr(p.idStore.createLedgerID(ledgerID, genesisBlock), "Error while marking ledger as created")
return lgr, nil return lgr, nil
} }
// Open implements the corresponding method from interface ledger.PeerLedgerProvider // Open implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Open(ledgerID string) (ledger.PeerLedger, error) { func (p *Provider) Open(ledgerID string) (ledger.PeerLedger, error) {
logger.Debugf("Open() opening kvledger: %s", ledgerID) logger.Debugf("Open() opening kvledger: %s", ledgerID)
// Check the ID store to ensure that the chainId/ledgerId exists // Check the ID store to ensure that the chainId/ledgerId exists
exists, err := provider.idStore.ledgerIDExists(ledgerID) exists, err := p.idStore.ledgerIDExists(ledgerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !exists { if !exists {
return nil, ErrNonExistingLedgerID return nil, ErrNonExistingLedgerID
} }
return provider.openInternal(ledgerID) return p.openInternal(ledgerID)
} }
func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, error) { func (p *Provider) openInternal(ledgerID string) (ledger.PeerLedger, error) {
// Get the block store for a chain/ledger // Get the block store for a chain/ledger
blockStore, err := provider.ledgerStoreProvider.Open(ledgerID) blockStore, err := p.ledgerStoreProvider.Open(ledgerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
provider.collElgNotifier.registerListener(ledgerID, blockStore) p.collElgNotifier.registerListener(ledgerID, blockStore)
// Get the versioned database (state database) for a chain/ledger // Get the versioned database (state database) for a chain/ledger
vDB, err := provider.vdbProvider.GetDBHandle(ledgerID) vDB, err := p.vdbProvider.GetDBHandle(ledgerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Get the history database (index for history of values by key) for a chain/ledger // Get the history database (index for history of values by key) for a chain/ledger
historyDB, err := provider.historydbProvider.GetDBHandle(ledgerID) historyDB, err := p.historydbProvider.GetDBHandle(ledgerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -173,10 +173,10 @@ func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, erro ...@@ -173,10 +173,10 @@ func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, erro
// Create a kvLedger for this chain/ledger, which encasulates the underlying data stores // Create a kvLedger for this chain/ledger, which encasulates the underlying data stores
// (id store, blockstore, state database, history database) // (id store, blockstore, state database, history database)
l, err := newKVLedger( l, err := newKVLedger(
ledgerID, blockStore, vDB, historyDB, provider.configHistoryMgr, ledgerID, blockStore, vDB, historyDB, p.configHistoryMgr,
provider.stateListeners, provider.bookkeepingProvider, p.stateListeners, p.bookkeepingProvider,
provider.initializer.DeployedChaincodeInfoProvider, p.initializer.DeployedChaincodeInfoProvider,
provider.stats.ledgerStats(ledgerID), p.stats.ledgerStats(ledgerID),
) )
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -185,39 +185,39 @@ func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, erro ...@@ -185,39 +185,39 @@ func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, erro
} }
// Exists implements the corresponding method from interface ledger.PeerLedgerProvider // Exists implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Exists(ledgerID string) (bool, error) { func (p *Provider) Exists(ledgerID string) (bool, error) {
return provider.idStore.ledgerIDExists(ledgerID) return p.idStore.ledgerIDExists(ledgerID)
} }
// List implements the corresponding method from interface ledger.PeerLedgerProvider // List implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) List() ([]string, error) { func (p *Provider) List() ([]string, error) {
return provider.idStore.getAllLedgerIds() return p.idStore.getAllLedgerIds()
} }
// Close implements the corresponding method from interface ledger.PeerLedgerProvider // Close implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Close() { func (p *Provider) Close() {
provider.idStore.close() p.idStore.close()
provider.ledgerStoreProvider.Close() p.ledgerStoreProvider.Close()
provider.vdbProvider.Close() p.vdbProvider.Close()
provider.historydbProvider.Close() p.historydbProvider.Close()
provider.bookkeepingProvider.Close() p.bookkeepingProvider.Close()
provider.configHistoryMgr.Close() p.configHistoryMgr.Close()
} }
// recoverUnderConstructionLedger checks whether the under construction flag is set - this would be the case // recoverUnderConstructionLedger checks whether the under construction flag is set - this would be the case
// if a crash had happened during creation of ledger and the ledger creation could have been left in intermediate // if a crash had happened during creation of ledger and the ledger creation could have been left in intermediate
// state. Recovery checks if the ledger was created and the genesis block was committed successfully then it completes // state. Recovery checks if the ledger was created and the genesis block was committed successfully then it completes
// the last step of adding the ledger id to the list of created ledgers. Else, it clears the under construction flag // the last step of adding the ledger id to the list of created ledgers. Else, it clears the under construction flag
func (provider *Provider) recoverUnderConstructionLedger() { func (p *Provider) recoverUnderConstructionLedger() {
logger.Debugf("Recovering under construction ledger") logger.Debugf("Recovering under construction ledger")
ledgerID, err := provider.idStore.getUnderConstructionFlag() ledgerID, err := p.idStore.getUnderConstructionFlag()
panicOnErr(err, "Error while checking whether the under construction flag is set") panicOnErr(err, "Error while checking whether the under construction flag is set")
if ledgerID == "" { if ledgerID == "" {
logger.Debugf("No under construction ledger found. Quitting recovery") logger.Debugf("No under construction ledger found. Quitting recovery")
return return
} }
logger.Infof("ledger [%s] found as under construction", ledgerID) logger.Infof("ledger [%s] found as under construction", ledgerID)
ledger, err := provider.openInternal(ledgerID) ledger, err := p.openInternal(ledgerID)
panicOnErr(err, "Error while opening under construction ledger [%s]", ledgerID) panicOnErr(err, "Error while opening under construction ledger [%s]", ledgerID)
bcInfo, err := ledger.GetBlockchainInfo() bcInfo, err := ledger.GetBlockchainInfo()
panicOnErr(err, "Error while getting blockchain info for the under construction ledger [%s]", ledgerID) panicOnErr(err, "Error while getting blockchain info for the under construction ledger [%s]", ledgerID)
...@@ -226,13 +226,13 @@ func (provider *Provider) recoverUnderConstructionLedger() { ...@@ -226,13 +226,13 @@ func (provider *Provider) recoverUnderConstructionLedger() {
switch bcInfo.Height { switch bcInfo.Height {
case 0: case 0:
logger.Infof("Genesis block was not committed. Hence, the peer ledger not created. unsetting the under construction flag") logger.Infof("Genesis block was not committed. Hence, the peer ledger not created. unsetting the under construction flag")
panicOnErr(provider.runCleanup(ledgerID), "Error while running cleanup for ledger id [%s]", ledgerID) panicOnErr(p.runCleanup(ledgerID), "Error while running cleanup for ledger id [%s]", ledgerID)
panicOnErr(provider.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag") panicOnErr(p.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag")
case 1: case 1:
logger.Infof("Genesis block was committed. Hence, marking the peer ledger as created") logger.Infof("Genesis block was committed. Hence, marking the peer ledger as created")
genesisBlock, err := ledger.GetBlockByNumber(0) genesisBlock, err := ledger.GetBlockByNumber(0)
panicOnErr(err, "Error while retrieving genesis block from blockchain for ledger [%s]", ledgerID) panicOnErr(err, "Error while retrieving genesis block from blockchain for ledger [%s]", ledgerID)
panicOnErr(provider.idStore.createLedgerID(ledgerID, genesisBlock), "Error while adding ledgerID [%s] to created list", ledgerID) panicOnErr(p.idStore.createLedgerID(ledgerID, genesisBlock), "Error while adding ledgerID [%s] to created list", ledgerID)
default: default:
panic(errors.Errorf( panic(errors.Errorf(
"data inconsistency: under construction flag is set for ledger [%s] while the height of the blockchain is [%d]", "data inconsistency: under construction flag is set for ledger [%s] while the height of the blockchain is [%d]",
...@@ -243,7 +243,7 @@ func (provider *Provider) recoverUnderConstructionLedger() { ...@@ -243,7 +243,7 @@ func (provider *Provider) recoverUnderConstructionLedger() {
// runCleanup cleans up blockstorage, statedb, and historydb for what // runCleanup cleans up blockstorage, statedb, and historydb for what
// may have got created during in-complete ledger creation // may have got created during in-complete ledger creation
func (provider *Provider) runCleanup(ledgerID string) error { func (p *Provider) runCleanup(ledgerID string) error {
// TODO - though, not having this is harmless for kv ledger. // TODO - though, not having this is harmless for kv ledger.
// If we want, following could be done: // If we want, following could be done:
// - blockstorage could remove empty folders // - blockstorage could remove empty folders
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment