Skip to content

Commit

Permalink
Merge "[FAB-1390] Refactor ledger interface names"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Jan 17, 2017
2 parents 04f2b51 + d58d51b commit 1b9bb80
Show file tree
Hide file tree
Showing 44 changed files with 248 additions and 238 deletions.
2 changes: 1 addition & 1 deletion core/chaincode/ccproviderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ccProviderContextImpl struct {
}

// GetContext returns a context for the supplied ledger, with the appropriate tx simulator
func (c *ccProviderImpl) GetContext(ledger ledger.ValidatedLedger) (context.Context, error) {
func (c *ccProviderImpl) GetContext(ledger ledger.PeerLedger) (context.Context, error) {
var err error
// get context for the chaincode execution
c.txsim, err = ledger.NewTxSimulator()
Expand Down
10 changes: 5 additions & 5 deletions core/chaincode/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (e *LedgerQuerier) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error)
}

// Execute the specified query string
func getQueryResult(vledger ledger.ValidatedLedger, query []byte) (ret []byte, err error) {
func getQueryResult(vledger ledger.PeerLedger, query []byte) (ret []byte, err error) {
if query == nil {
return nil, fmt.Errorf("Query string must not be nil.")
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func collectRecord(buffer *bytes.Buffer, rec *ledger.QueryRecord) {
buffer.WriteString("}")
}

func getTransactionByID(vledger ledger.ValidatedLedger, tid []byte) ([]byte, error) {
func getTransactionByID(vledger ledger.PeerLedger, tid []byte) ([]byte, error) {
if tid == nil {
return nil, fmt.Errorf("Transaction ID must not be nil.")
}
Expand All @@ -199,7 +199,7 @@ func getTransactionByID(vledger ledger.ValidatedLedger, tid []byte) ([]byte, err
return utils.Marshal(tx)
}

func getBlockByNumber(vledger ledger.ValidatedLedger, number []byte) ([]byte, error) {
func getBlockByNumber(vledger ledger.PeerLedger, number []byte) ([]byte, error) {
if number == nil {
return nil, fmt.Errorf("Block number must not be nil.")
}
Expand All @@ -216,7 +216,7 @@ func getBlockByNumber(vledger ledger.ValidatedLedger, number []byte) ([]byte, er
return utils.Marshal(block)
}

func getBlockByHash(vledger ledger.ValidatedLedger, hash []byte) ([]byte, error) {
func getBlockByHash(vledger ledger.PeerLedger, hash []byte) ([]byte, error) {
if hash == nil {
return nil, fmt.Errorf("Block hash must not be nil.")
}
Expand All @@ -229,7 +229,7 @@ func getBlockByHash(vledger ledger.ValidatedLedger, hash []byte) ([]byte, error)
return utils.Marshal(block)
}

func getChainInfo(vledger ledger.ValidatedLedger) ([]byte, error) {
func getChainInfo(vledger ledger.PeerLedger) ([]byte, error) {
binfo, err := vledger.GetBlockchainInfo()
if err != nil {
return nil, fmt.Errorf("Failed to get block info with error %s", err)
Expand Down
4 changes: 2 additions & 2 deletions core/committer/committer_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func init() {
// it keeps the reference to the ledger to commit blocks and retreive
// chain information
type LedgerCommitter struct {
ledger ledger.ValidatedLedger
ledger ledger.PeerLedger
validator txvalidator.Validator
}

// NewLedgerCommitter is a factory function to create an instance of the committer
func NewLedgerCommitter(ledger ledger.ValidatedLedger, validator txvalidator.Validator) *LedgerCommitter {
func NewLedgerCommitter(ledger ledger.PeerLedger, validator txvalidator.Validator) *LedgerCommitter {
return &LedgerCommitter{ledger: ledger, validator: validator}
}

Expand Down
6 changes: 3 additions & 3 deletions core/committer/txvalidator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ type vsccValidator interface {
// vsccValidator implementation which used to call
// vscc chaincode and validate block transactions
type vsccValidatorImpl struct {
ledger ledger.ValidatedLedger
ledger ledger.PeerLedger
ccprovider ccprovider.ChaincodeProvider
}

// implementation of Validator interface, keeps
// reference to the ledger to enable tx simulation
// and execution of vscc
type txValidator struct {
ledger ledger.ValidatedLedger
ledger ledger.PeerLedger
vscc vsccValidator
}

Expand All @@ -71,7 +71,7 @@ func init() {
}

// NewTxValidator creates new transactions validator
func NewTxValidator(ledger ledger.ValidatedLedger) Validator {
func NewTxValidator(ledger ledger.PeerLedger) Validator {
// Encapsulates interface implementation
return &txValidator{ledger, &vsccValidatorImpl{ledger: ledger, ccprovider: ccprovider.GetChaincodeProvider()}}
}
Expand Down
2 changes: 1 addition & 1 deletion core/common/ccprovider/ccprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// should be added below if necessary
type ChaincodeProvider interface {
// GetContext returns a ledger context
GetContext(ledger ledger.ValidatedLedger) (context.Context, error)
GetContext(ledger ledger.PeerLedger) (context.Context, error)
// GetCCContext returns an opaque chaincode context
GetCCContext(cid, name, version, txid string, syscc bool, prop *peer.Proposal) interface{}
// GetCCValidationInfoFromLCCC returns the VSCC and the policy listed by LCCC for the supplied chaincode
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/kvledger/example/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
// App - a sample fund transfer app
type App struct {
name string
ledger ledger.ValidatedLedger
ledger ledger.PeerLedger
}

// ConstructAppInstance constructs an instance of an app
func ConstructAppInstance(ledger ledger.ValidatedLedger) *App {
func ConstructAppInstance(ledger ledger.PeerLedger) *App {
return &App{"PaymentApp", ledger}
}

Expand Down
4 changes: 2 additions & 2 deletions core/ledger/kvledger/example/committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (

// Committer a toy committer
type Committer struct {
ledger ledger.ValidatedLedger
ledger ledger.PeerLedger
}

// ConstructCommitter constructs a committer for the example
func ConstructCommitter(ledger ledger.ValidatedLedger) *Committer {
func ConstructCommitter(ledger ledger.PeerLedger) *Committer {
return &Committer{ledger}
}

Expand Down
8 changes: 4 additions & 4 deletions core/ledger/kvledger/example/main/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
ledgerID = "Default"
)

var finalLedger ledger.ValidatedLedger
var peerLedger ledger.PeerLedger
var app *example.App
var committer *example.Committer
var consenter *example.Consenter
Expand All @@ -54,12 +54,12 @@ func init() {
cleanup()
ledgermgmt.Initialize()
var err error
finalLedger, err = ledgermgmt.CreateLedger(ledgerID)
peerLedger, err = ledgermgmt.CreateLedger(ledgerID)
if err != nil {
panic(fmt.Errorf("Error in NewKVLedger(): %s", err))
}
app = example.ConstructAppInstance(finalLedger)
committer = example.ConstructCommitter(finalLedger)
app = example.ConstructAppInstance(peerLedger)
committer = example.ConstructCommitter(peerLedger)
consenter = example.ConstructConsenter()
}

Expand Down
4 changes: 2 additions & 2 deletions core/ledger/kvledger/example/marble_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ var logger = logging.MustGetLogger("example")
// App - a sample fund transfer app
type MarbleApp struct {
name string
ledger ledger.ValidatedLedger
ledger ledger.PeerLedger
}

// ConstructAppInstance constructs an instance of an app
func ConstructMarbleAppInstance(ledger ledger.ValidatedLedger) *MarbleApp {
func ConstructMarbleAppInstance(ledger ledger.PeerLedger) *MarbleApp {
return &MarbleApp{"marbles_app", ledger}
}

Expand Down
2 changes: 1 addition & 1 deletion core/ledger/kvledger/kv_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

var logger = logging.MustGetLogger("kvledger")

// KVLedger provides an implementation of `ledger.ValidatedLedger`.
// KVLedger provides an implementation of `ledger.PeerLedger`.
// This implementation provides a key-value based data model
type KVLedger struct {
ledgerID string
Expand Down
18 changes: 9 additions & 9 deletions core/ledger/kvledger/kv_ledger_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ var (
ErrLedgerNotOpened = errors.New("Ledger is not opened yet")
)

// Provider implements interface ledger.ValidatedLedgerProvider
// Provider implements interface ledger.PeerLedgerProvider
type Provider struct {
idStore *idStore
vdbProvider statedb.VersionedDBProvider
}

// NewProvider instantiates a new Provider.
// This is not thread-safe and assumed to be synchronized be the caller
func NewProvider() (ledger.ValidatedLedgerProvider, error) {
func NewProvider() (ledger.PeerLedgerProvider, error) {
logger.Info("Initializing ledger provider")
var vdbProvider statedb.VersionedDBProvider
if !ledgerconfig.IsCouchDBEnabled() {
Expand All @@ -64,8 +64,8 @@ func NewProvider() (ledger.ValidatedLedgerProvider, error) {
return &Provider{idStore, vdbProvider}, nil
}

// Create implements the corresponding method from interface ledger.ValidatedLedgerProvider
func (provider *Provider) Create(ledgerID string) (ledger.ValidatedLedger, error) {
// Create implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Create(ledgerID string) (ledger.PeerLedger, error) {
exists, err := provider.idStore.ledgerIDExists(ledgerID)
if err != nil {
return nil, err
Expand All @@ -81,8 +81,8 @@ func (provider *Provider) Create(ledgerID string) (ledger.ValidatedLedger, error
return l, nil
}

// Open implements the corresponding method from interface ledger.ValidatedLedgerProvider
func (provider *Provider) Open(ledgerID string) (ledger.ValidatedLedger, error) {
// Open implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Open(ledgerID string) (ledger.PeerLedger, error) {
exists, err := provider.idStore.ledgerIDExists(ledgerID)
if err != nil {
return nil, err
Expand All @@ -97,17 +97,17 @@ func (provider *Provider) Open(ledgerID string) (ledger.ValidatedLedger, error)
return l, nil
}

// Exists implements the corresponding method from interface ledger.ValidatedLedgerProvider
// Exists implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Exists(ledgerID string) (bool, error) {
return provider.idStore.ledgerIDExists(ledgerID)
}

// List implements the corresponding method from interface ledger.ValidatedLedgerProvider
// List implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) List() ([]string, error) {
return provider.idStore.getAllLedgerIds()
}

// Close implements the corresponding method from interface ledger.ValidatedLedgerProvider
// Close implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Close() {
provider.vdbProvider.Close()
provider.idStore.close()
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/kvledger/kv_ledger_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestMultipleLedgerBasicRW(t *testing.T) {
defer env.cleanup()
numLedgers := 10
provider, _ := NewProvider()
ledgers := make([]ledger.ValidatedLedger, numLedgers)
ledgers := make([]ledger.PeerLedger, numLedgers)
for i := 0; i < numLedgers; i++ {
l, err := provider.Create(constructTestLedgerID(i))
testutil.AssertNoError(t, err, "")
Expand All @@ -85,7 +85,7 @@ func TestMultipleLedgerBasicRW(t *testing.T) {

provider, _ = NewProvider()
defer provider.Close()
ledgers = make([]ledger.ValidatedLedger, numLedgers)
ledgers = make([]ledger.PeerLedger, numLedgers)
for i := 0; i < numLedgers; i++ {
l, err := provider.Open(constructTestLedgerID(i))
testutil.AssertNoError(t, err, "")
Expand Down
8 changes: 4 additions & 4 deletions core/ledger/kvledger/marble_example/main/marble_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (

var logger = logging.MustGetLogger("main")

var finalLedger ledger.ValidatedLedger
var peerLedger ledger.PeerLedger
var marbleApp *example.MarbleApp
var committer *example.Committer
var consenter *example.Consenter
Expand All @@ -55,12 +55,12 @@ func init() {
cleanup()
ledgermgmt.Initialize()
var err error
finalLedger, err = ledgermgmt.CreateLedger(ledgerID)
peerLedger, err = ledgermgmt.CreateLedger(ledgerID)
if err != nil {
panic(fmt.Errorf("Error in NewKVLedger(): %s", err))
}
marbleApp = example.ConstructMarbleAppInstance(finalLedger)
committer = example.ConstructCommitter(finalLedger)
marbleApp = example.ConstructMarbleAppInstance(peerLedger)
committer = example.ConstructCommitter(peerLedger)
consenter = example.ConstructConsenter()
}

Expand Down
32 changes: 19 additions & 13 deletions core/ledger/ledger_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
)

// Ledger captures the methods that are common across the 'raw ledger' and the 'final ledger'
// Ledger captures the methods that are common across the 'PeerLedger', 'OrdererLedger', and 'ValidatedLedger'
type Ledger interface {
// GetBlockchainInfo returns basic info about blockchain
GetBlockchainInfo() (*pb.BlockchainInfo, error)
Expand All @@ -32,36 +32,34 @@ type Ledger interface {
// The iterator is a blocking iterator i.e., it blocks till the next block gets available in the ledger
// ResultsIterator contains type BlockHolder
GetBlocksIterator(startBlockNumber uint64) (ResultsIterator, error)
//Prune prunes the blocks/transactions that satisfy the given policy
Prune(policy PrunePolicy) error
// Close closes the ledger
Close()
}

// RawLedger implements methods required by 'raw ledger'
type RawLedger interface {
// OrdererLedger implements methods required by 'orderer ledger'
type OrdererLedger interface {
Ledger
// CommitBlock adds a new block
CommitBlock(block *common.Block) error
}

// ValidatedLedgerProvider provides handle to ledger instances
type ValidatedLedgerProvider interface {
// PeerLedgerProvider provides handle to ledger instances
type PeerLedgerProvider interface {
// CreateLedger creates a new ledger with a given unique id
Create(ledgerID string) (ValidatedLedger, error)
Create(ledgerID string) (PeerLedger, error)
// OpenLedger opens an already created ledger
Open(ledgerID string) (ValidatedLedger, error)
Open(ledgerID string) (PeerLedger, error)
// Exists tells whether the ledger with given id exits
Exists(ledgerID string) (bool, error)
// List lists the ids of the existing ledgers
List() ([]string, error)
// Close closes the ValidatedLedgerProvider
// Close closes the PeerLedgerProvider
Close()
}

// ValidatedLedger represents the 'final ledger'. In addition to implement the methods inherited from the Ledger,
// it provides the handle to objects for querying the state and executing transactions.
type ValidatedLedger interface {
// PeerLedger differs from the OrdererLedger in that PeerLedger locally maintain a bitmask
// that tells apart valid transactions from invalid ones
type PeerLedger interface {
Ledger
// GetTransactionByID retrieves a transaction by id
GetTransactionByID(txID string) (*pb.Transaction, error)
Expand All @@ -81,6 +79,14 @@ type ValidatedLedger interface {
NewHistoryQueryExecutor() (HistoryQueryExecutor, error)
// Commits block into the ledger
Commit(block *common.Block) error
//Prune prunes the blocks/transactions that satisfy the given policy
Prune(policy PrunePolicy) error
}

// ValidatedLedger represents the 'final ledger' after filtering out invalid transactions from PeerLedger.
// Post-v1
type ValidatedLedger interface {
Ledger
}

// QueryExecutor executes the queries
Expand Down
Loading

0 comments on commit 1b9bb80

Please sign in to comment.