diff --git a/orderer/common/deliver/deliver.go b/common/deliver/deliver.go similarity index 96% rename from orderer/common/deliver/deliver.go rename to common/deliver/deliver.go index c2e901c68f7..6baa9eb8d2d 100644 --- a/orderer/common/deliver/deliver.go +++ b/common/deliver/deliver.go @@ -19,19 +19,19 @@ package deliver import ( "io" - "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/common/ledger/blockledger" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/common/util" - "github.com/hyperledger/fabric/orderer/common/ledger" - "github.com/hyperledger/fabric/orderer/common/msgprocessor" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" "github.com/hyperledger/fabric/protos/utils" + + "github.com/golang/protobuf/proto" "github.com/op/go-logging" ) -const pkgLogID = "orderer/common/deliver" +const pkgLogID = "common/deliver" var logger *logging.Logger @@ -58,7 +58,7 @@ type Support interface { PolicyManager() policies.Manager // Reader returns the chain Reader for the chain - Reader() ledger.Reader + Reader() blockledger.Reader // Errored returns a channel which closes when the backing consenter has errored Errored() <-chan struct{} @@ -137,7 +137,7 @@ func (ds *deliverServer) deliverBlocks(srv ab.AtomicBroadcast_DeliverServer, env lastConfigSequence := chain.Sequence() - sf := msgprocessor.NewSigFilter(policies.ChannelReaders, chain) + sf := NewSigFilter(policies.ChannelReaders, chain) if err := sf.Apply(envelope); err != nil { logger.Warningf("[channel: %s] Received unauthorized deliver request from %s: %s", chdr.ChannelId, addr, err) return sendStatusReply(srv, cb.Status_FORBIDDEN) diff --git a/orderer/common/deliver/deliver_test.go b/common/deliver/deliver_test.go similarity index 94% rename from orderer/common/deliver/deliver_test.go rename to common/deliver/deliver_test.go index 6de4e8dd088..abaa66e094e 100644 --- a/orderer/common/deliver/deliver_test.go +++ b/common/deliver/deliver_test.go @@ -23,11 +23,11 @@ import ( "time" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/common/ledger/blockledger" + ramledger "github.com/hyperledger/fabric/common/ledger/blockledger/ram" mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" "github.com/hyperledger/fabric/common/policies" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - "github.com/hyperledger/fabric/orderer/common/ledger" - ramledger "github.com/hyperledger/fabric/orderer/common/ledger/ram" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" "github.com/hyperledger/fabric/protos/utils" @@ -120,7 +120,7 @@ func (mm *mockSupportManager) GetChain(chainID string) (Support, bool) { } type mockSupport struct { - ledger ledger.ReadWriter + ledger blockledger.ReadWriter policyManager *mockpolicies.Manager erroredChan chan struct{} configSeq uint64 @@ -138,11 +138,11 @@ func (mcs *mockSupport) PolicyManager() policies.Manager { return mcs.policyManager } -func (mcs *mockSupport) Reader() ledger.Reader { +func (mcs *mockSupport) Reader() blockledger.Reader { return mcs.ledger } -func NewRAMLedger() ledger.ReadWriter { +func NewRAMLedger() blockledger.ReadWriter { rlf := ramledger.New(ledgerSize + 1) rl, _ := rlf.GetOrCreate(genesisconfig.TestChainID) rl.Append(genesisBlock) @@ -153,7 +153,7 @@ func initializeDeliverHandler() Handler { mm := newMockMultichainManager() for i := 1; i < ledgerSize; i++ { l := mm.chains[systemChainID].ledger - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) } return NewHandlerImpl(mm) @@ -282,7 +282,7 @@ func TestUnauthorizedSeek(t *testing.T) { mm := newMockMultichainManager() for i := 1; i < ledgerSize; i++ { l := mm.chains[systemChainID].ledger - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) } mm.chains[systemChainID].policyManager.Policy.Err = fmt.Errorf("Fail to evaluate policy") @@ -308,7 +308,7 @@ func TestRevokedAuthorizationSeek(t *testing.T) { mm := newMockMultichainManager() for i := 1; i < ledgerSize; i++ { l := mm.chains[systemChainID].ledger - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) } m := newMockD() @@ -329,7 +329,7 @@ func TestRevokedAuthorizationSeek(t *testing.T) { mm.chains[systemChainID].policyManager.Policy.Err = fmt.Errorf("Fail to evaluate policy") mm.chains[systemChainID].configSeq++ l := mm.chains[systemChainID].ledger - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", ledgerSize+1))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", ledgerSize+1))}})) select { case deliverReply := <-m.sendChan: @@ -391,7 +391,7 @@ func TestBlockingSeek(t *testing.T) { mm := newMockMultichainManager() for i := 1; i < ledgerSize; i++ { l := mm.chains[systemChainID].ledger - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) } m := newMockD() @@ -418,7 +418,7 @@ func TestBlockingSeek(t *testing.T) { } l := mm.chains[systemChainID].ledger - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", ledgerSize+1))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", ledgerSize+1))}})) select { case deliverReply := <-m.sendChan: @@ -445,7 +445,7 @@ func TestErroredSeek(t *testing.T) { l := ms.ledger close(ms.erroredChan) for i := 1; i < ledgerSize; i++ { - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) } m := newMockD() @@ -469,7 +469,7 @@ func TestErroredBlockingSeek(t *testing.T) { ms := mm.chains[systemChainID] l := ms.ledger for i := 1; i < ledgerSize; i++ { - l.Append(ledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) + l.Append(blockledger.CreateNextBlock(l, []*cb.Envelope{{Payload: []byte(fmt.Sprintf("%d", i))}})) } m := newMockD() diff --git a/orderer/common/msgprocessor/sigfilter.go b/common/deliver/sigfilter.go similarity index 70% rename from orderer/common/msgprocessor/sigfilter.go rename to common/deliver/sigfilter.go index 31338059032..6b07232cf8a 100644 --- a/orderer/common/msgprocessor/sigfilter.go +++ b/common/deliver/sigfilter.go @@ -4,7 +4,7 @@ Copyright IBM Corp. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package msgprocessor +package deliver import ( "fmt" @@ -15,28 +15,34 @@ import ( "github.com/pkg/errors" ) +// ErrPermissionDenied is returned by errors which are caused by transactions +// which are not permitted due to an authorization failure. +var ErrPermissionDenied = errors.New("permission denied") + // SigFilterSupport provides the resources required for the signature filter type SigFilterSupport interface { // PolicyManager returns a reference to the current policy manager PolicyManager() policies.Manager } -type sigFilter struct { +// SigFilter stores the name of the policy to apply to deliver requests to +// determine whether a client is authorized +type SigFilter struct { policyName string support SigFilterSupport } // NewSigFilter creates a new signature filter, at every evaluation, the policy manager is called // to retrieve the latest version of the policy -func NewSigFilter(policyName string, support SigFilterSupport) Rule { - return &sigFilter{ +func NewSigFilter(policyName string, support SigFilterSupport) *SigFilter { + return &SigFilter{ policyName: policyName, support: support, } } // Apply applies the policy given, resulting in Reject or Forward, never Accept -func (sf *sigFilter) Apply(message *cb.Envelope) error { +func (sf *SigFilter) Apply(message *cb.Envelope) error { signedData, err := message.AsSignedData() if err != nil { diff --git a/orderer/common/msgprocessor/sigfilter_test.go b/common/deliver/sigfilter_test.go similarity index 98% rename from orderer/common/msgprocessor/sigfilter_test.go rename to common/deliver/sigfilter_test.go index e37ea5c419d..9573e5bbf3b 100644 --- a/orderer/common/msgprocessor/sigfilter_test.go +++ b/common/deliver/sigfilter_test.go @@ -4,7 +4,7 @@ Copyright IBM Corp. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package msgprocessor +package deliver import ( "fmt" diff --git a/orderer/common/ledger/blackbox_test.go b/common/ledger/blockledger/blackbox_test.go similarity index 84% rename from orderer/common/ledger/blackbox_test.go rename to common/ledger/blockledger/blackbox_test.go index e69a2852f55..fc7b63c7b0a 100644 --- a/orderer/common/ledger/blackbox_test.go +++ b/common/ledger/blockledger/blackbox_test.go @@ -14,14 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ledger_test +package blockledger_test import ( "bytes" "reflect" "testing" - . "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" + cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" ) @@ -32,7 +33,7 @@ type ledgerTestable interface { } type ledgerTestFactory interface { - New() (Factory, ReadWriter) + New() (blockledger.Factory, blockledger.ReadWriter) Destroy() error Persistent() bool } @@ -69,7 +70,7 @@ func testInitialization(lf ledgerTestFactory, t *testing.T) { if li.Height() != 1 { t.Fatalf("Block height should be 1") } - block := GetBlock(li, 0) + block := blockledger.GetBlock(li, 0) if block == nil { t.Fatalf("Error retrieving genesis block") } @@ -86,7 +87,7 @@ func testReinitialization(lf ledgerTestFactory, t *testing.T) { return } olf, oli := lf.New() - aBlock := CreateNextBlock(oli, []*cb.Envelope{{Payload: []byte("My Data")}}) + aBlock := blockledger.CreateNextBlock(oli, []*cb.Envelope{{Payload: []byte("My Data")}}) err := oli.Append(aBlock) if err != nil { t.Fatalf("Error appending block: %s", err) @@ -97,7 +98,7 @@ func testReinitialization(lf ledgerTestFactory, t *testing.T) { if li.Height() != 2 { t.Fatalf("Block height should be 2") } - block := GetBlock(li, 1) + block := blockledger.GetBlock(li, 1) if block == nil { t.Fatalf("Error retrieving block 1") } @@ -112,17 +113,17 @@ func TestAddition(t *testing.T) { func testAddition(lf ledgerTestFactory, t *testing.T) { _, li := lf.New() - genesis := GetBlock(li, 0) + genesis := blockledger.GetBlock(li, 0) if genesis == nil { t.Fatalf("Could not retrieve genesis block") } prevHash := genesis.Header.Hash() - li.Append(CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}})) + li.Append(blockledger.CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}})) if li.Height() != 2 { t.Fatalf("Block height should be 2") } - block := GetBlock(li, 1) + block := blockledger.GetBlock(li, 1) if block == nil { t.Fatalf("Error retrieving genesis block") } @@ -137,7 +138,7 @@ func TestRetrieval(t *testing.T) { func testRetrieval(lf ledgerTestFactory, t *testing.T) { _, li := lf.New() - li.Append(CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}})) + li.Append(blockledger.CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}})) it, num := li.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Oldest{}}) defer it.Close() if num != 0 { @@ -188,7 +189,7 @@ func testBlockedRetrieval(lf ledgerTestFactory, t *testing.T) { t.Fatalf("Should not be ready for block read") default: } - li.Append(CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}})) + li.Append(blockledger.CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}})) select { case <-signal: default: @@ -222,8 +223,8 @@ func testMultichain(lf ledgerTestFactory, t *testing.T) { t.Fatalf("Error creating chain1: %s", err) } - c1.Append(CreateNextBlock(c1, []*cb.Envelope{{Payload: c1p1}})) - c1b1 := CreateNextBlock(c1, []*cb.Envelope{{Payload: c1p2}}) + c1.Append(blockledger.CreateNextBlock(c1, []*cb.Envelope{{Payload: c1p1}})) + c1b1 := blockledger.CreateNextBlock(c1, []*cb.Envelope{{Payload: c1p2}}) c1.Append(c1b1) if c1.Height() != 2 { @@ -234,7 +235,7 @@ func testMultichain(lf ledgerTestFactory, t *testing.T) { if err != nil { t.Fatalf("Error creating chain2: %s", err) } - c2b0 := c2.Append(CreateNextBlock(c2, []*cb.Envelope{{Payload: c2p1}})) + c2b0 := c2.Append(blockledger.CreateNextBlock(c2, []*cb.Envelope{{Payload: c2p1}})) if c2.Height() != 1 { t.Fatalf("Block height for c2 should be 1") @@ -245,7 +246,7 @@ func testMultichain(lf ledgerTestFactory, t *testing.T) { t.Fatalf("Error retrieving chain1: %s", err) } - if b := GetBlock(c1, 1); !reflect.DeepEqual(c1b1, b) { + if b := blockledger.GetBlock(c1, 1); !reflect.DeepEqual(c1b1, b) { t.Fatalf("Did not properly store block 1 on chain 1:") } @@ -254,7 +255,7 @@ func testMultichain(lf ledgerTestFactory, t *testing.T) { t.Fatalf("Error retrieving chain2: %s", err) } - if b := GetBlock(c2, 0); reflect.DeepEqual(c2b0, b) { + if b := blockledger.GetBlock(c2, 0); reflect.DeepEqual(c2b0, b) { t.Fatalf("Did not properly store block 1 on chain 1") } } diff --git a/orderer/common/ledger/file/factory.go b/common/ledger/blockledger/file/factory.go similarity index 86% rename from orderer/common/ledger/file/factory.go rename to common/ledger/blockledger/file/factory.go index dbb57ffe83e..8068c3f425f 100644 --- a/orderer/common/ledger/file/factory.go +++ b/common/ledger/blockledger/file/factory.go @@ -21,17 +21,17 @@ import ( "github.com/hyperledger/fabric/common/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage" - "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" ) type fileLedgerFactory struct { blkstorageProvider blkstorage.BlockStoreProvider - ledgers map[string]ledger.ReadWriter + ledgers map[string]blockledger.ReadWriter mutex sync.Mutex } // GetOrCreate gets an existing ledger (if it exists) or creates it if it does not -func (flf *fileLedgerFactory) GetOrCreate(chainID string) (ledger.ReadWriter, error) { +func (flf *fileLedgerFactory) GetOrCreate(chainID string) (blockledger.ReadWriter, error) { flf.mutex.Lock() defer flf.mutex.Unlock() @@ -66,13 +66,13 @@ func (flf *fileLedgerFactory) Close() { } // New creates a new ledger factory -func New(directory string) ledger.Factory { +func New(directory string) blockledger.Factory { return &fileLedgerFactory{ blkstorageProvider: fsblkstorage.NewProvider( fsblkstorage.NewConf(directory, -1), &blkstorage.IndexConfig{ AttrsToIndex: []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockNum}}, ), - ledgers: make(map[string]ledger.ReadWriter), + ledgers: make(map[string]blockledger.ReadWriter), } } diff --git a/orderer/common/ledger/file/factory_test.go b/common/ledger/blockledger/file/factory_test.go similarity index 95% rename from orderer/common/ledger/file/factory_test.go rename to common/ledger/blockledger/file/factory_test.go index 0f210520e35..9949df17c86 100644 --- a/orderer/common/ledger/file/factory_test.go +++ b/common/ledger/blockledger/file/factory_test.go @@ -22,8 +22,8 @@ import ( "testing" "github.com/hyperledger/fabric/common/ledger/blkstorage" + "github.com/hyperledger/fabric/common/ledger/blockledger" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - "github.com/hyperledger/fabric/orderer/common/ledger" "github.com/stretchr/testify/assert" ) @@ -56,7 +56,7 @@ func (mbsp *mockBlockStoreProvider) Close() { func TestBlockstoreProviderError(t *testing.T) { flf := &fileLedgerFactory{ blkstorageProvider: &mockBlockStoreProvider{error: fmt.Errorf("blockstorage provider error")}, - ledgers: make(map[string]ledger.ReadWriter), + ledgers: make(map[string]blockledger.ReadWriter), } assert.Panics( t, diff --git a/orderer/common/ledger/file/impl.go b/common/ledger/blockledger/file/impl.go similarity index 87% rename from orderer/common/ledger/file/impl.go rename to common/ledger/blockledger/file/impl.go index 7a0828b6469..b1ea5edcd9a 100644 --- a/orderer/common/ledger/file/impl.go +++ b/common/ledger/blockledger/file/impl.go @@ -18,11 +18,12 @@ package fileledger import ( "github.com/hyperledger/fabric/common/flogging" - cl "github.com/hyperledger/fabric/common/ledger" + "github.com/hyperledger/fabric/common/ledger" "github.com/hyperledger/fabric/common/ledger/blkstorage" - ledger "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" + "github.com/op/go-logging" ) @@ -38,6 +39,7 @@ func init() { close(closedChan) } +// FileLedger is a struct used to interact with a node's ledger type fileLedger struct { blockStore blkstorage.BlockStore signal chan struct{} @@ -46,7 +48,7 @@ type fileLedger struct { type fileLedgerIterator struct { ledger *fileLedger blockNumber uint64 - commonIterator cl.ResultsIterator + commonIterator ledger.ResultsIterator } // Next blocks until there is a new block available, or returns an error if the @@ -79,9 +81,9 @@ func (i *fileLedgerIterator) Close() { i.commonIterator.Close() } -// Iterator returns an Iterator, as specified by a cb.SeekInfo message, and its +// Iterator returns an Iterator, as specified by an ab.SeekInfo message, and its // starting block number -func (fl *fileLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, uint64) { +func (fl *fileLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Iterator, uint64) { var startingBlockNumber uint64 switch start := startPosition.Type.(type) { case *ab.SeekPosition_Oldest: @@ -97,15 +99,15 @@ func (fl *fileLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, startingBlockNumber = start.Specified.Number height := fl.Height() if startingBlockNumber > height { - return &ledger.NotFoundErrorIterator{}, 0 + return &blockledger.NotFoundErrorIterator{}, 0 } default: - return &ledger.NotFoundErrorIterator{}, 0 + return &blockledger.NotFoundErrorIterator{}, 0 } iterator, err := fl.blockStore.RetrieveBlocks(startingBlockNumber) if err != nil { - return &ledger.NotFoundErrorIterator{}, 0 + return &blockledger.NotFoundErrorIterator{}, 0 } return &fileLedgerIterator{ledger: fl, blockNumber: startingBlockNumber, commonIterator: iterator}, startingBlockNumber diff --git a/orderer/common/ledger/file/impl_test.go b/common/ledger/blockledger/file/impl_test.go similarity index 93% rename from orderer/common/ledger/file/impl_test.go rename to common/ledger/blockledger/file/impl_test.go index a63205d8da7..17ec2c492c1 100644 --- a/orderer/common/ledger/file/impl_test.go +++ b/common/ledger/blockledger/file/impl_test.go @@ -25,8 +25,8 @@ import ( "github.com/hyperledger/fabric/common/flogging" cl "github.com/hyperledger/fabric/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - "github.com/hyperledger/fabric/orderer/common/ledger" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" "github.com/hyperledger/fabric/protos/peer" @@ -43,7 +43,7 @@ func init() { type testEnv struct { t *testing.T location string - flf ledger.Factory + flf blockledger.Factory } func initialize(t *testing.T) (*testEnv, *fileLedger) { @@ -139,7 +139,7 @@ func TestInitialization(t *testing.T) { assert.Equal(t, uint64(1), fl.Height(), "Block height should be 1") - block := ledger.GetBlock(fl, 0) + block := blockledger.GetBlock(fl, 0) assert.NotNil(t, block, "Error retrieving genesis block") assert.Equal(t, genesisBlock.Header.Hash(), block.Header.Hash(), "Block hashes did no match") } @@ -149,7 +149,7 @@ func TestReinitialization(t *testing.T) { defer tev.tearDown() // create a block to add to the ledger - b1 := ledger.CreateNextBlock(ledger1, []*cb.Envelope{{Payload: []byte("My Data")}}) + b1 := blockledger.CreateNextBlock(ledger1, []*cb.Envelope{{Payload: []byte("My Data")}}) // add the block to the ledger ledger1.Append(b1) @@ -180,7 +180,7 @@ func TestReinitialization(t *testing.T) { fl = ledger2.(*fileLedger) assert.Equal(t, uint64(2), fl.Height(), "Block height should be 2. Got %v", fl.Height()) - block := ledger.GetBlock(fl, 1) + block := blockledger.GetBlock(fl, 1) assert.NotNil(t, block, "Error retrieving block 1") assert.Equal(t, b1.Header.Hash(), block.Header.Hash(), "Block hashes did no match") } @@ -190,10 +190,10 @@ func TestAddition(t *testing.T) { defer tev.tearDown() info, _ := fl.blockStore.GetBlockchainInfo() prevHash := info.CurrentBlockHash - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) assert.Equal(t, uint64(2), fl.Height(), "Block height should be 2") - block := ledger.GetBlock(fl, 1) + block := blockledger.GetBlock(fl, 1) assert.NotNil(t, block, "Error retrieving genesis block") assert.Equal(t, prevHash, block.Header.PreviousHash, "Block hashes did no match") } @@ -201,7 +201,7 @@ func TestAddition(t *testing.T) { func TestRetrieval(t *testing.T) { tev, fl := initialize(t) defer tev.tearDown() - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) it, num := fl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Oldest{}}) defer it.Close() assert.Zero(t, num, "Expected genesis block iterator, but got %d", num) @@ -249,8 +249,7 @@ func TestBlockedRetrieval(t *testing.T) { t.Fatalf("Should not be ready for block read") default: } - - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) select { case <-signal: default: @@ -266,7 +265,7 @@ func TestBlockedRetrieval(t *testing.T) { "Expected to successfully retrieve the second block but got block number %d", block.Header.Number) go func() { - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) }() select { case <-it.ReadyChan(): @@ -316,7 +315,7 @@ func TestBlockstoreError(t *testing.T) { defer it.Close() assert.IsType( t, - &ledger.NotFoundErrorIterator{}, + &blockledger.NotFoundErrorIterator{}, it, "Expected Not Found Error if seek number is greater than ledger height") } diff --git a/orderer/common/ledger/file_test.go b/common/ledger/blockledger/file_test.go similarity index 91% rename from orderer/common/ledger/file_test.go rename to common/ledger/blockledger/file_test.go index 761f524d8bb..0f3af2bde49 100644 --- a/orderer/common/ledger/file_test.go +++ b/common/ledger/blockledger/file_test.go @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ledger_test +package blockledger_test import ( "io/ioutil" "os" + . "github.com/hyperledger/fabric/common/ledger/blockledger" + fileledger "github.com/hyperledger/fabric/common/ledger/blockledger/file" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - . "github.com/hyperledger/fabric/orderer/common/ledger" - fileledger "github.com/hyperledger/fabric/orderer/common/ledger/file" ) func init() { diff --git a/orderer/common/ledger/json/factory.go b/common/ledger/blockledger/json/factory.go similarity index 91% rename from orderer/common/ledger/json/factory.go rename to common/ledger/blockledger/json/factory.go index 9576effe65f..993f36dc440 100644 --- a/orderer/common/ledger/json/factory.go +++ b/common/ledger/blockledger/json/factory.go @@ -24,17 +24,17 @@ import ( "sync" "github.com/golang/protobuf/jsonpb" - "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" ) type jsonLedgerFactory struct { directory string - ledgers map[string]ledger.ReadWriter + ledgers map[string]blockledger.ReadWriter mutex sync.Mutex } // GetOrCreate gets an existing ledger (if it exists) or creates it if it does not -func (jlf *jsonLedgerFactory) GetOrCreate(chainID string) (ledger.ReadWriter, error) { +func (jlf *jsonLedgerFactory) GetOrCreate(chainID string) (blockledger.ReadWriter, error) { jlf.mutex.Lock() defer jlf.mutex.Unlock() @@ -60,7 +60,7 @@ func (jlf *jsonLedgerFactory) GetOrCreate(chainID string) (ledger.ReadWriter, er } // newChain creates a new chain backed by a JSON ledger -func newChain(directory string) ledger.ReadWriter { +func newChain(directory string) blockledger.ReadWriter { jl := &jsonLedger{ directory: directory, signal: make(chan struct{}), @@ -128,7 +128,7 @@ func (jlf *jsonLedgerFactory) Close() { } // New creates a new ledger factory -func New(directory string) ledger.Factory { +func New(directory string) blockledger.Factory { logger.Debugf("Initializing ledger at: %s", directory) if err := os.MkdirAll(directory, 0700); err != nil { logger.Panicf("Could not create directory %s: %s", directory, err) @@ -136,7 +136,7 @@ func New(directory string) ledger.Factory { jlf := &jsonLedgerFactory{ directory: directory, - ledgers: make(map[string]ledger.ReadWriter), + ledgers: make(map[string]blockledger.ReadWriter), } infos, err := ioutil.ReadDir(jlf.directory) diff --git a/orderer/common/ledger/json/factory_test.go b/common/ledger/blockledger/json/factory_test.go similarity index 100% rename from orderer/common/ledger/json/factory_test.go rename to common/ledger/blockledger/json/factory_test.go diff --git a/orderer/common/ledger/json/impl.go b/common/ledger/blockledger/json/impl.go similarity index 94% rename from orderer/common/ledger/json/impl.go rename to common/ledger/blockledger/json/impl.go index d9e3123f36b..47ad32fe011 100644 --- a/orderer/common/ledger/json/impl.go +++ b/common/ledger/blockledger/json/impl.go @@ -24,7 +24,7 @@ import ( "sync" "github.com/hyperledger/fabric/common/flogging" - ledger "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" "github.com/op/go-logging" @@ -114,9 +114,9 @@ func (cu *cursor) ReadyChan() <-chan struct{} { func (cu *cursor) Close() {} -// Iterator returns an Iterator, as specified by a cb.SeekInfo message, and its +// Iterator returns an Iterator, as specified by a ab.SeekInfo message, and its // starting block number -func (jl *jsonLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, uint64) { +func (jl *jsonLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Iterator, uint64) { switch start := startPosition.Type.(type) { case *ab.SeekPosition_Oldest: return &cursor{jl: jl, blockNumber: 0}, 0 @@ -125,11 +125,11 @@ func (jl *jsonLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, return &cursor{jl: jl, blockNumber: high}, high case *ab.SeekPosition_Specified: if start.Specified.Number > jl.height { - return &ledger.NotFoundErrorIterator{}, 0 + return &blockledger.NotFoundErrorIterator{}, 0 } return &cursor{jl: jl, blockNumber: start.Specified.Number}, start.Specified.Number default: - return &ledger.NotFoundErrorIterator{}, 0 + return &blockledger.NotFoundErrorIterator{}, 0 } } diff --git a/orderer/common/ledger/json/impl_test.go b/common/ledger/blockledger/json/impl_test.go similarity index 91% rename from orderer/common/ledger/json/impl_test.go rename to common/ledger/blockledger/json/impl_test.go index b3fad7d3ef1..6b896d398b8 100644 --- a/orderer/common/ledger/json/impl_test.go +++ b/common/ledger/blockledger/json/impl_test.go @@ -23,8 +23,8 @@ import ( "time" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/common/ledger/blockledger" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - "github.com/hyperledger/fabric/orderer/common/ledger" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" @@ -79,7 +79,7 @@ func TestInitialization(t *testing.T) { func TestReinitialization(t *testing.T) { tev, ofl := initialize(t) defer tev.tearDown() - ofl.Append(ledger.CreateNextBlock(ofl, []*cb.Envelope{{Payload: []byte("My Data")}})) + ofl.Append(blockledger.CreateNextBlock(ofl, []*cb.Envelope{{Payload: []byte("My Data")}})) flf := New(tev.location) chains := flf.ChainIDs() assert.Len(t, chains, 1, "Should have recovered the chain") @@ -115,7 +115,7 @@ func TestAddition(t *testing.T) { tev, fl := initialize(t) defer tev.tearDown() prevHash := fl.lastHash - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) assert.Equal(t, uint64(2), fl.height, "Block height should be 2") block, found := fl.readBlock(1) @@ -127,7 +127,7 @@ func TestAddition(t *testing.T) { func TestRetrieval(t *testing.T) { tev, fl := initialize(t) defer tev.tearDown() - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) it, num := fl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Oldest{}}) defer it.Close() assert.Equal(t, uint64(0), num, "Expected genesis block iterator, but got %d", num) @@ -174,7 +174,7 @@ func TestRaceCondition(t *testing.T) { close(complete) }() - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) <-complete assert.Equal(t, cb.Status_SUCCESS, status, "Expected to successfully read the block") @@ -194,7 +194,7 @@ func TestBlockedRetrieval(t *testing.T) { default: } - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}})) select { case <-signal: default: @@ -212,7 +212,7 @@ func TestBlockedRetrieval(t *testing.T) { // we desire, due to I/O operation in the middle. Consider making the // implementation more testable so we don't need to sleep here. time.Sleep(100 * time.Millisecond) - fl.Append(ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("Another Data")}})) + fl.Append(blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("Another Data")}})) }() block, status = it.Next() @@ -259,14 +259,14 @@ func TestInvalidAddition(t *testing.T) { // Append block with invalid number { - block := ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}}) + block := blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}}) block.Header.Number++ assert.Error(t, fl.Append(block), "Addition of block with invalid number should fail") } // Append block with invalid previousHash { - block := ledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}}) + block := blockledger.CreateNextBlock(fl, []*cb.Envelope{{Payload: []byte("My Data")}}) block.Header.PreviousHash = nil assert.Error(t, fl.Append(block), "Addition of block with invalid previousHash should fail") } diff --git a/orderer/common/ledger/json_test.go b/common/ledger/blockledger/json_test.go similarity index 91% rename from orderer/common/ledger/json_test.go rename to common/ledger/blockledger/json_test.go index 5d49965dd8e..67df92b86c8 100644 --- a/orderer/common/ledger/json_test.go +++ b/common/ledger/blockledger/json_test.go @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ledger_test +package blockledger_test import ( "io/ioutil" "os" + . "github.com/hyperledger/fabric/common/ledger/blockledger" + jsonledger "github.com/hyperledger/fabric/common/ledger/blockledger/json" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - . "github.com/hyperledger/fabric/orderer/common/ledger" - jsonledger "github.com/hyperledger/fabric/orderer/common/ledger/json" cb "github.com/hyperledger/fabric/protos/common" ) diff --git a/orderer/common/ledger/ledger.go b/common/ledger/blockledger/ledger.go similarity index 95% rename from orderer/common/ledger/ledger.go rename to common/ledger/blockledger/ledger.go index 6384cb1cdce..bcb1fceae0d 100644 --- a/orderer/common/ledger/ledger.go +++ b/common/ledger/blockledger/ledger.go @@ -1,12 +1,9 @@ /* Copyright IBM Corp. 2016 All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -14,7 +11,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ledger +package blockledger import ( cb "github.com/hyperledger/fabric/protos/common" @@ -47,7 +44,7 @@ type Iterator interface { // Reader allows the caller to inspect the ledger type Reader interface { - // Iterator returns an Iterator, as specified by a cb.SeekInfo message, and + // Iterator returns an Iterator, as specified by an ab.SeekInfo message, and // its starting block number Iterator(startType *ab.SeekPosition) (Iterator, uint64) // Height returns the number of blocks on the ledger diff --git a/orderer/common/ledger/ram/factory.go b/common/ledger/blockledger/ram/factory.go similarity index 84% rename from orderer/common/ledger/ram/factory.go rename to common/ledger/blockledger/ram/factory.go index 0fb8ec5250a..adc48cc353d 100644 --- a/orderer/common/ledger/ram/factory.go +++ b/common/ledger/blockledger/ram/factory.go @@ -19,18 +19,18 @@ package ramledger import ( "sync" - "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" cb "github.com/hyperledger/fabric/protos/common" ) type ramLedgerFactory struct { maxSize int - ledgers map[string]ledger.ReadWriter + ledgers map[string]blockledger.ReadWriter mutex sync.Mutex } // GetOrCreate gets an existing ledger (if it exists) or creates it if it does not -func (rlf *ramLedgerFactory) GetOrCreate(chainID string) (ledger.ReadWriter, error) { +func (rlf *ramLedgerFactory) GetOrCreate(chainID string) (blockledger.ReadWriter, error) { rlf.mutex.Lock() defer rlf.mutex.Unlock() @@ -47,7 +47,7 @@ func (rlf *ramLedgerFactory) GetOrCreate(chainID string) (ledger.ReadWriter, err } // newChain creates a new chain backed by a RAM ledger -func newChain(maxSize int) ledger.ReadWriter { +func newChain(maxSize int) blockledger.ReadWriter { preGenesis := &cb.Block{ Header: &cb.BlockHeader{ Number: ^uint64(0), @@ -87,10 +87,10 @@ func (rlf *ramLedgerFactory) Close() { } // New creates a new ledger factory -func New(maxSize int) ledger.Factory { +func New(maxSize int) blockledger.Factory { rlf := &ramLedgerFactory{ maxSize: maxSize, - ledgers: make(map[string]ledger.ReadWriter), + ledgers: make(map[string]blockledger.ReadWriter), } return rlf diff --git a/orderer/common/ledger/ram/factory_test.go b/common/ledger/blockledger/ram/factory_test.go similarity index 100% rename from orderer/common/ledger/ram/factory_test.go rename to common/ledger/blockledger/ram/factory_test.go diff --git a/orderer/common/ledger/ram/impl.go b/common/ledger/blockledger/ram/impl.go similarity index 94% rename from orderer/common/ledger/ram/impl.go rename to common/ledger/blockledger/ram/impl.go index ed664a4bb91..be8f440d9bd 100644 --- a/orderer/common/ledger/ram/impl.go +++ b/common/ledger/blockledger/ram/impl.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/hyperledger/fabric/common/flogging" - "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" "github.com/op/go-logging" @@ -73,9 +73,9 @@ func (cu *cursor) ReadyChan() <-chan struct{} { // Close does nothing func (cu *cursor) Close() {} -// Iterator returns an Iterator, as specified by a cb.SeekInfo message, and its +// Iterator returns an Iterator, as specified by a ab.SeekInfo message, and its // starting block number -func (rl *ramLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, uint64) { +func (rl *ramLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Iterator, uint64) { var list *simpleList switch start := startPosition.Type.(type) { case *ab.SeekPosition_Oldest: @@ -103,7 +103,7 @@ func (rl *ramLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, if specified+1 < oldest.block.Header.Number+1 || specified > rl.newest.block.Header.Number+1 { logger.Debugf("Returning error iterator because specified seek was %d with oldest %d and newest %d", specified, rl.oldest.block.Header.Number, rl.newest.block.Header.Number) - return &ledger.NotFoundErrorIterator{}, 0 + return &blockledger.NotFoundErrorIterator{}, 0 } if specified == oldest.block.Header.Number { diff --git a/orderer/common/ledger/ram/impl_test.go b/common/ledger/blockledger/ram/impl_test.go similarity index 89% rename from orderer/common/ledger/ram/impl_test.go rename to common/ledger/blockledger/ram/impl_test.go index 32df9b7bc3c..127acba398c 100644 --- a/orderer/common/ledger/ram/impl_test.go +++ b/common/ledger/blockledger/ram/impl_test.go @@ -20,8 +20,8 @@ import ( "testing" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/common/ledger/blockledger" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - "github.com/hyperledger/fabric/orderer/common/ledger" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" ) @@ -110,7 +110,7 @@ func TestTruncationSafety(t *testing.T) { func TestRetrieval(t *testing.T) { rl := newTestChain(3) - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) it, num := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Oldest{}}) defer it.Close() if num != 0 { @@ -157,7 +157,7 @@ func TestBlockedRetrieval(t *testing.T) { t.Fatalf("Should not be ready for block read") default: } - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) select { case <-signal: default: @@ -184,9 +184,9 @@ func TestIteratorPastEnd(t *testing.T) { func TestIteratorOldest(t *testing.T) { rl := newTestChain(3) // add enough block to roll off the genesis block - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}})) it, num := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: 1}}}) defer it.Close() if num != 1 { @@ -197,14 +197,14 @@ func TestIteratorOldest(t *testing.T) { func TestAppendBadBLock(t *testing.T) { rl := newTestChain(3) t.Run("BadBlockNumber", func(t *testing.T) { - nextBlock := ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}}) + nextBlock := blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}}) nextBlock.Header.Number = nextBlock.Header.Number + 1 if err := rl.Append(nextBlock); err == nil { t.Fatalf("Expected Append to fail.") } }) t.Run("BadPreviousHash", func(t *testing.T) { - nextBlock := ledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}}) + nextBlock := blockledger.CreateNextBlock(rl, []*cb.Envelope{{Payload: []byte("My Data")}}) nextBlock.Header.PreviousHash = []byte("bad hash") if err := rl.Append(nextBlock); err == nil { t.Fatalf("Expected Append to fail.") diff --git a/orderer/common/ledger/ram_test.go b/common/ledger/blockledger/ram_test.go similarity index 89% rename from orderer/common/ledger/ram_test.go rename to common/ledger/blockledger/ram_test.go index adcce64056e..053d5a281fd 100644 --- a/orderer/common/ledger/ram_test.go +++ b/common/ledger/blockledger/ram_test.go @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ledger_test +package blockledger_test import ( + . "github.com/hyperledger/fabric/common/ledger/blockledger" + ramledger "github.com/hyperledger/fabric/common/ledger/blockledger/ram" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - . "github.com/hyperledger/fabric/orderer/common/ledger" - ramledger "github.com/hyperledger/fabric/orderer/common/ledger/ram" ) func init() { diff --git a/orderer/common/ledger/util.go b/common/ledger/blockledger/util.go similarity index 99% rename from orderer/common/ledger/util.go rename to common/ledger/blockledger/util.go index 3e3195c2a45..b283b112952 100644 --- a/orderer/common/ledger/util.go +++ b/common/ledger/blockledger/util.go @@ -1,12 +1,9 @@ /* Copyright IBM Corp. 2016 All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -14,7 +11,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ledger +package blockledger import ( "github.com/golang/protobuf/proto" diff --git a/orderer/common/msgprocessor/standardchannel.go b/orderer/common/msgprocessor/standardchannel.go index 5bdb39ec8eb..e6ce011f2d9 100644 --- a/orderer/common/msgprocessor/standardchannel.go +++ b/orderer/common/msgprocessor/standardchannel.go @@ -9,6 +9,7 @@ package msgprocessor import ( "github.com/hyperledger/fabric/common/channelconfig" "github.com/hyperledger/fabric/common/crypto" + "github.com/hyperledger/fabric/common/deliver" "github.com/hyperledger/fabric/common/policies" cb "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/utils" @@ -53,7 +54,7 @@ func CreateStandardChannelFilters(filterSupport channelconfig.Resources) *RuleSe return NewRuleSet([]Rule{ EmptyRejectRule, NewSizeFilter(ordererConfig), - NewSigFilter(policies.ChannelWriters, filterSupport), + deliver.NewSigFilter(policies.ChannelWriters, filterSupport), }) } diff --git a/orderer/common/msgprocessor/systemchannel.go b/orderer/common/msgprocessor/systemchannel.go index 72cf042abe0..74a8f2d1eb6 100644 --- a/orderer/common/msgprocessor/systemchannel.go +++ b/orderer/common/msgprocessor/systemchannel.go @@ -12,6 +12,7 @@ import ( "github.com/hyperledger/fabric/common/channelconfig" "github.com/hyperledger/fabric/common/configtx" "github.com/hyperledger/fabric/common/crypto" + "github.com/hyperledger/fabric/common/deliver" "github.com/hyperledger/fabric/common/policies" cb "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/utils" @@ -49,7 +50,7 @@ func CreateSystemChannelFilters(chainCreator ChainCreator, ledgerResources chann return NewRuleSet([]Rule{ EmptyRejectRule, NewSizeFilter(ordererConfig), - NewSigFilter(policies.ChannelWriters, ledgerResources), + deliver.NewSigFilter(policies.ChannelWriters, ledgerResources), NewSystemChannelFilter(ledgerResources, chainCreator), }) } diff --git a/orderer/common/multichannel/blockwriter.go b/orderer/common/multichannel/blockwriter.go index de6a17b6428..9c4cbb93aa6 100644 --- a/orderer/common/multichannel/blockwriter.go +++ b/orderer/common/multichannel/blockwriter.go @@ -12,8 +12,8 @@ import ( newchannelconfig "github.com/hyperledger/fabric/common/channelconfig" "github.com/hyperledger/fabric/common/configtx" "github.com/hyperledger/fabric/common/crypto" + "github.com/hyperledger/fabric/common/ledger/blockledger" "github.com/hyperledger/fabric/common/util" - "github.com/hyperledger/fabric/orderer/common/ledger" cb "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/utils" @@ -22,7 +22,7 @@ import ( type blockWriterSupport interface { crypto.LocalSigner - ledger.ReadWriter + blockledger.ReadWriter configtx.Validator Update(*newchannelconfig.Bundle) CreateBundle(channelID string, config *cb.Config) (*newchannelconfig.Bundle, error) diff --git a/orderer/common/multichannel/blockwriter_test.go b/orderer/common/multichannel/blockwriter_test.go index 17e303c1004..7c6078218db 100644 --- a/orderer/common/multichannel/blockwriter_test.go +++ b/orderer/common/multichannel/blockwriter_test.go @@ -21,9 +21,9 @@ import ( newchannelconfig "github.com/hyperledger/fabric/common/channelconfig" "github.com/hyperledger/fabric/common/crypto" + "github.com/hyperledger/fabric/common/ledger/blockledger" mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - "github.com/hyperledger/fabric/orderer/common/ledger" cb "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/utils" "github.com/stretchr/testify/assert" @@ -32,7 +32,7 @@ import ( type mockBlockWriterSupport struct { *mockconfigtx.Validator crypto.LocalSigner - ledger.ReadWriter + blockledger.ReadWriter } func (mbws mockBlockWriterSupport) Update(bundle *newchannelconfig.Bundle) {} @@ -185,7 +185,7 @@ func TestGoodWriteConfig(t *testing.T) { bw.committingBlock.Lock() bw.committingBlock.Unlock() - cBlock := ledger.GetBlock(l, block.Header.Number) + cBlock := blockledger.GetBlock(l, block.Header.Number) assert.Equal(t, block.Header, cBlock.Header) assert.Equal(t, block.Data, cBlock.Data) diff --git a/orderer/common/multichannel/chainsupport.go b/orderer/common/multichannel/chainsupport.go index c582d754c07..07fb2015d16 100644 --- a/orderer/common/multichannel/chainsupport.go +++ b/orderer/common/multichannel/chainsupport.go @@ -8,8 +8,8 @@ package multichannel import ( "github.com/hyperledger/fabric/common/crypto" + "github.com/hyperledger/fabric/common/ledger/blockledger" "github.com/hyperledger/fabric/orderer/common/blockcutter" - "github.com/hyperledger/fabric/orderer/common/ledger" "github.com/hyperledger/fabric/orderer/common/msgprocessor" "github.com/hyperledger/fabric/orderer/consensus" cb "github.com/hyperledger/fabric/protos/common" @@ -35,7 +35,7 @@ func newChainSupport( signer crypto.LocalSigner, ) *ChainSupport { // Read in the last block and metadata for the channel - lastBlock := ledger.GetBlock(ledgerResources, ledgerResources.Height()-1) + lastBlock := blockledger.GetBlock(ledgerResources, ledgerResources.Height()-1) metadata, err := utils.GetMetadataFromBlock(lastBlock, cb.BlockMetadataIndex_ORDERER) // Assuming a block created with cb.NewBlock(), this should not @@ -74,7 +74,7 @@ func newChainSupport( return cs } -func (cs *ChainSupport) Reader() ledger.Reader { +func (cs *ChainSupport) Reader() blockledger.Reader { return cs } diff --git a/orderer/common/multichannel/registrar.go b/orderer/common/multichannel/registrar.go index 389292233d1..bc668984c9c 100644 --- a/orderer/common/multichannel/registrar.go +++ b/orderer/common/multichannel/registrar.go @@ -16,7 +16,7 @@ import ( "github.com/hyperledger/fabric/common/configtx" "github.com/hyperledger/fabric/common/crypto" "github.com/hyperledger/fabric/common/flogging" - "github.com/hyperledger/fabric/orderer/common/ledger" + "github.com/hyperledger/fabric/common/ledger/blockledger" "github.com/hyperledger/fabric/orderer/common/msgprocessor" "github.com/hyperledger/fabric/orderer/consensus" cb "github.com/hyperledger/fabric/protos/common" @@ -91,14 +91,14 @@ func (cr *configResources) SharedConfig() channelconfig.Orderer { type ledgerResources struct { *configResources - ledger.ReadWriter + blockledger.ReadWriter } // Registrar serves as a point of access and control for the individual channel resources. type Registrar struct { chains map[string]*ChainSupport consenters map[string]consensus.Consenter - ledgerFactory ledger.Factory + ledgerFactory blockledger.Factory signer crypto.LocalSigner systemChannelID string systemChannel *ChainSupport @@ -106,13 +106,13 @@ type Registrar struct { callbacks []func(bundle *channelconfig.Bundle) } -func getConfigTx(reader ledger.Reader) *cb.Envelope { - lastBlock := ledger.GetBlock(reader, reader.Height()-1) +func getConfigTx(reader blockledger.Reader) *cb.Envelope { + lastBlock := blockledger.GetBlock(reader, reader.Height()-1) index, err := utils.GetLastConfigIndexFromBlock(lastBlock) if err != nil { logger.Panicf("Chain did not have appropriately encoded last config in its latest block: %s", err) } - configBlock := ledger.GetBlock(reader, index) + configBlock := blockledger.GetBlock(reader, index) if configBlock == nil { logger.Panicf("Config block does not exist") } @@ -121,7 +121,7 @@ func getConfigTx(reader ledger.Reader) *cb.Envelope { } // NewRegistrar produces an instance of a *Registrar. -func NewRegistrar(ledgerFactory ledger.Factory, consenters map[string]consensus.Consenter, +func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]consensus.Consenter, signer crypto.LocalSigner, callbacks ...func(bundle *channelconfig.Bundle)) *Registrar { r := &Registrar{ chains: make(map[string]*ChainSupport), @@ -270,7 +270,7 @@ func (r *Registrar) newLedgerResources(configTx *cb.Envelope) *ledgerResources { func (r *Registrar) newChain(configtx *cb.Envelope) { ledgerResources := r.newLedgerResources(configtx) - ledgerResources.Append(ledger.CreateNextBlock(ledgerResources, []*cb.Envelope{configtx})) + ledgerResources.Append(blockledger.CreateNextBlock(ledgerResources, []*cb.Envelope{configtx})) // Copy the map to allow concurrent reads from broadcast/deliver while the new chainSupport is newChains := make(map[string]*ChainSupport) diff --git a/orderer/common/multichannel/registrar_test.go b/orderer/common/multichannel/registrar_test.go index 6bf50fdb156..69517beb198 100644 --- a/orderer/common/multichannel/registrar_test.go +++ b/orderer/common/multichannel/registrar_test.go @@ -14,14 +14,14 @@ import ( "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric/common/crypto" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/common/ledger/blockledger" + ramledger "github.com/hyperledger/fabric/common/ledger/blockledger/ram" mockchannelconfig "github.com/hyperledger/fabric/common/mocks/config" mockcrypto "github.com/hyperledger/fabric/common/mocks/crypto" mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" "github.com/hyperledger/fabric/common/tools/configtxgen/encoder" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" "github.com/hyperledger/fabric/msp" - "github.com/hyperledger/fabric/orderer/common/ledger" - ramledger "github.com/hyperledger/fabric/orderer/common/ledger/ram" "github.com/hyperledger/fabric/orderer/consensus" cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" @@ -50,7 +50,7 @@ func mockCrypto() crypto.LocalSigner { return mockcrypto.FakeLocalSigner } -func NewRAMLedgerAndFactory(maxSize int) (ledger.Factory, ledger.ReadWriter) { +func NewRAMLedgerAndFactory(maxSize int) (blockledger.Factory, blockledger.ReadWriter) { rlf := ramledger.New(10) rl, err := rlf.GetOrCreate(genesisconfig.TestChainID) if err != nil { @@ -63,7 +63,7 @@ func NewRAMLedgerAndFactory(maxSize int) (ledger.Factory, ledger.ReadWriter) { return rlf, rl } -func NewRAMLedger(maxSize int) ledger.ReadWriter { +func NewRAMLedger(maxSize int) blockledger.ReadWriter { _, rl := NewRAMLedgerAndFactory(maxSize) return rl } @@ -72,13 +72,13 @@ func NewRAMLedger(maxSize int) ledger.ReadWriter { func TestGetConfigTx(t *testing.T) { rl := NewRAMLedger(10) for i := 0; i < 5; i++ { - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, i)})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, i)})) } - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{makeConfigTx(genesisconfig.TestChainID, 5)})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{makeConfigTx(genesisconfig.TestChainID, 5)})) ctx := makeConfigTx(genesisconfig.TestChainID, 6) - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{ctx})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{ctx})) - block := ledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, 7)}) + block := blockledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, 7)}) block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = utils.MarshalOrPanic(&cb.Metadata{Value: utils.MarshalOrPanic(&cb.LastConfig{Index: 7})}) rl.Append(block) @@ -90,15 +90,15 @@ func TestGetConfigTx(t *testing.T) { func TestGetConfigTxFailure(t *testing.T) { rl := NewRAMLedger(10) for i := 0; i < 10; i++ { - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{ + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{ makeNormalTx(genesisconfig.TestChainID, i), makeConfigTx(genesisconfig.TestChainID, i), })) } - rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, 11)})) + rl.Append(blockledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, 11)})) assert.Panics(t, func() { getConfigTx(rl) }, "Should have panicked because there was no config tx") - block := ledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, 12)}) + block := blockledger.CreateNextBlock(rl, []*cb.Envelope{makeNormalTx(genesisconfig.TestChainID, 12)}) block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = []byte("bad metadata") assert.Panics(t, func() { getConfigTx(rl) }, "Should have panicked because of bad last config metadata") } diff --git a/orderer/common/server/main.go b/orderer/common/server/main.go index c3782276af6..37c5f2f6674 100644 --- a/orderer/common/server/main.go +++ b/orderer/common/server/main.go @@ -18,12 +18,12 @@ import ( "github.com/hyperledger/fabric/common/channelconfig" "github.com/hyperledger/fabric/common/crypto" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/common/ledger/blockledger" "github.com/hyperledger/fabric/common/tools/configtxgen/encoder" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" "github.com/hyperledger/fabric/core/comm" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/orderer/common/bootstrap/file" - "github.com/hyperledger/fabric/orderer/common/ledger" "github.com/hyperledger/fabric/orderer/common/localconfig" "github.com/hyperledger/fabric/orderer/common/metadata" "github.com/hyperledger/fabric/orderer/common/multichannel" @@ -185,7 +185,7 @@ func initializeServerConfig(conf *config.TopLevel) comm.ServerConfig { return comm.ServerConfig{SecOpts: secureOpts, KaOpts: kaOpts} } -func initializeBootstrapChannel(conf *config.TopLevel, lf ledger.Factory) { +func initializeBootstrapChannel(conf *config.TopLevel, lf blockledger.Factory) { var genesisBlock *cb.Block // Select the bootstrapping mechanism diff --git a/orderer/common/server/server.go b/orderer/common/server/server.go index 85c5d7e7915..b1e74eece90 100644 --- a/orderer/common/server/server.go +++ b/orderer/common/server/server.go @@ -15,8 +15,8 @@ import ( "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric/common/crypto" + "github.com/hyperledger/fabric/common/deliver" "github.com/hyperledger/fabric/orderer/common/broadcast" - "github.com/hyperledger/fabric/orderer/common/deliver" localconfig "github.com/hyperledger/fabric/orderer/common/localconfig" "github.com/hyperledger/fabric/orderer/common/multichannel" cb "github.com/hyperledger/fabric/protos/common" @@ -45,7 +45,7 @@ type server struct { debug *localconfig.Debug } -// NewServer creates an ab.AtomicBroadcastServer based on the broadcast target and ledger Reader +// NewServer creates a ab.AtomicBroadcastServer based on the broadcast target and ledger Reader func NewServer(r *multichannel.Registrar, _ crypto.LocalSigner, debug *localconfig.Debug) ab.AtomicBroadcastServer { s := &server{ dh: deliver.NewHandlerImpl(deliverSupport{Registrar: r}), diff --git a/orderer/common/server/util.go b/orderer/common/server/util.go index 520afed97ca..fc19dacfc87 100644 --- a/orderer/common/server/util.go +++ b/orderer/common/server/util.go @@ -12,15 +12,15 @@ import ( "path/filepath" "github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage" - "github.com/hyperledger/fabric/orderer/common/ledger" - fileledger "github.com/hyperledger/fabric/orderer/common/ledger/file" - jsonledger "github.com/hyperledger/fabric/orderer/common/ledger/json" - ramledger "github.com/hyperledger/fabric/orderer/common/ledger/ram" + "github.com/hyperledger/fabric/common/ledger/blockledger" + fileledger "github.com/hyperledger/fabric/common/ledger/blockledger/file" + jsonledger "github.com/hyperledger/fabric/common/ledger/blockledger/json" + ramledger "github.com/hyperledger/fabric/common/ledger/blockledger/ram" config "github.com/hyperledger/fabric/orderer/common/localconfig" ) -func createLedgerFactory(conf *config.TopLevel) (ledger.Factory, string) { - var lf ledger.Factory +func createLedgerFactory(conf *config.TopLevel) (blockledger.Factory, string) { + var lf blockledger.Factory var ld string switch conf.General.LedgerType { case "file":