Commit 41da3349 authored by yacovm's avatar yacovm Committed by Yacov Manevich
Browse files

[FAB-12508] Add Block(seq) to consenter support



This change set adds a method that retrieves a block from
the ledger in the consenter support.

Change-Id: Id088ec688e169d306c242352b485a8e47809b7db
Signed-off-by: default avataryacovm <yacovm@il.ibm.com>
parent 319ca364
......@@ -48,6 +48,8 @@ type Writer interface {
Append(block *cb.Block) error
}
//go:generate mockery -dir . -name ReadWriter -case underscore -output mocks/
// ReadWriter encapsulates the read/write functions of the ledger
type ReadWriter interface {
Reader
......
// Code generated by mockery v1.0.0. DO NOT EDIT.
package mocks
import blockledger "github.com/hyperledger/fabric/common/ledger/blockledger"
import common "github.com/hyperledger/fabric/protos/common"
import mock "github.com/stretchr/testify/mock"
import orderer "github.com/hyperledger/fabric/protos/orderer"
// ReadWriter is an autogenerated mock type for the ReadWriter type
type ReadWriter struct {
mock.Mock
}
// Append provides a mock function with given fields: block
func (_m *ReadWriter) Append(block *common.Block) error {
ret := _m.Called(block)
var r0 error
if rf, ok := ret.Get(0).(func(*common.Block) error); ok {
r0 = rf(block)
} else {
r0 = ret.Error(0)
}
return r0
}
// Height provides a mock function with given fields:
func (_m *ReadWriter) Height() uint64 {
ret := _m.Called()
var r0 uint64
if rf, ok := ret.Get(0).(func() uint64); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(uint64)
}
return r0
}
// Iterator provides a mock function with given fields: startType
func (_m *ReadWriter) Iterator(startType *orderer.SeekPosition) (blockledger.Iterator, uint64) {
ret := _m.Called(startType)
var r0 blockledger.Iterator
if rf, ok := ret.Get(0).(func(*orderer.SeekPosition) blockledger.Iterator); ok {
r0 = rf(startType)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(blockledger.Iterator)
}
}
var r1 uint64
if rf, ok := ret.Get(1).(func(*orderer.SeekPosition) uint64); ok {
r1 = rf(startType)
} else {
r1 = ret.Get(1).(uint64)
}
return r0, r1
}
......@@ -73,6 +73,15 @@ func newChainSupport(
return cs
}
// Block returns a block with the following number,
// or nil if such a block doesn't exist.
func (cs *ChainSupport) Block(number uint64) *cb.Block {
if cs.Height() <= number {
return nil
}
return blockledger.GetBlock(cs.Reader(), number)
}
func (cs *ChainSupport) Reader() blockledger.Reader {
return cs
}
......
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package multichannel
import (
"testing"
"github.com/hyperledger/fabric/common/deliver/mock"
"github.com/hyperledger/fabric/common/ledger/blockledger/mocks"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/orderer"
"github.com/stretchr/testify/assert"
)
func TestChainSupportBlock(t *testing.T) {
ledger := &mocks.ReadWriter{}
ledger.On("Height").Return(uint64(100))
iterator := &mock.BlockIterator{}
iterator.NextReturns(&common.Block{Header: &common.BlockHeader{Number: 99}}, common.Status_SUCCESS)
ledger.On("Iterator", &orderer.SeekPosition{
Type: &orderer.SeekPosition_Specified{
Specified: &orderer.SeekSpecified{Number: 99},
},
}).Return(iterator, uint64(99))
cs := &ChainSupport{ledgerResources: &ledgerResources{ReadWriter: ledger}}
assert.Nil(t, cs.Block(100))
assert.Equal(t, uint64(99), cs.Block(99).Header.Number)
}
......@@ -84,6 +84,10 @@ type ConsenterSupport interface {
// Note that either WriteBlock or WriteConfigBlock must be called before invoking this method a second time.
CreateNextBlock(messages []*cb.Envelope) *cb.Block
// Block returns a block with the following number,
// or nil if such a block doesn't exist.
Block(number uint64) *cb.Block
// WriteBlock commits a block to the ledger.
WriteBlock(block *cb.Block, encodedMetadataValue []byte)
......
......@@ -3634,6 +3634,11 @@ type mockConsenterSupport struct {
mock.Mock
}
func (c *mockConsenterSupport) Block(seq uint64) *cb.Block {
// TODO: implement this
return nil
}
func (c *mockConsenterSupport) NewSignatureHeader() (*cb.SignatureHeader, error) {
args := c.Called()
return args.Get(0).(*cb.SignatureHeader), args.Error(1)
......
......@@ -119,6 +119,17 @@ type FakeConsenterSupport struct {
createNextBlockReturnsOnCall map[int]struct {
result1 *cb.Block
}
BlockStub func(seq uint64) *cb.Block
blockMutex sync.RWMutex
blockArgsForCall []struct {
seq uint64
}
blockReturns struct {
result1 *cb.Block
}
blockReturnsOnCall map[int]struct {
result1 *cb.Block
}
WriteBlockStub func(block *cb.Block, encodedMetadataValue []byte)
writeBlockMutex sync.RWMutex
writeBlockArgsForCall []struct {
......@@ -601,6 +612,54 @@ func (fake *FakeConsenterSupport) CreateNextBlockReturnsOnCall(i int, result1 *c
}{result1}
}
func (fake *FakeConsenterSupport) Block(seq uint64) *cb.Block {
fake.blockMutex.Lock()
ret, specificReturn := fake.blockReturnsOnCall[len(fake.blockArgsForCall)]
fake.blockArgsForCall = append(fake.blockArgsForCall, struct {
seq uint64
}{seq})
fake.recordInvocation("Block", []interface{}{seq})
fake.blockMutex.Unlock()
if fake.BlockStub != nil {
return fake.BlockStub(seq)
}
if specificReturn {
return ret.result1
}
return fake.blockReturns.result1
}
func (fake *FakeConsenterSupport) BlockCallCount() int {
fake.blockMutex.RLock()
defer fake.blockMutex.RUnlock()
return len(fake.blockArgsForCall)
}
func (fake *FakeConsenterSupport) BlockArgsForCall(i int) uint64 {
fake.blockMutex.RLock()
defer fake.blockMutex.RUnlock()
return fake.blockArgsForCall[i].seq
}
func (fake *FakeConsenterSupport) BlockReturns(result1 *cb.Block) {
fake.BlockStub = nil
fake.blockReturns = struct {
result1 *cb.Block
}{result1}
}
func (fake *FakeConsenterSupport) BlockReturnsOnCall(i int, result1 *cb.Block) {
fake.BlockStub = nil
if fake.blockReturnsOnCall == nil {
fake.blockReturnsOnCall = make(map[int]struct {
result1 *cb.Block
})
}
fake.blockReturnsOnCall[i] = struct {
result1 *cb.Block
}{result1}
}
func (fake *FakeConsenterSupport) WriteBlock(block *cb.Block, encodedMetadataValue []byte) {
var encodedMetadataValueCopy []byte
if encodedMetadataValue != nil {
......@@ -802,6 +861,8 @@ func (fake *FakeConsenterSupport) Invocations() map[string][][]interface{} {
defer fake.sharedConfigMutex.RUnlock()
fake.createNextBlockMutex.RLock()
defer fake.createNextBlockMutex.RUnlock()
fake.blockMutex.RLock()
defer fake.blockMutex.RUnlock()
fake.writeBlockMutex.RLock()
defer fake.writeBlockMutex.RUnlock()
fake.writeConfigBlockMutex.RLock()
......
......@@ -62,6 +62,11 @@ type ConsenterSupport struct {
SequenceVal uint64
}
func (mcs *ConsenterSupport) Block(seq uint64) *cb.Block {
// TODO: implement this
return nil
}
// BlockCutter returns BlockCutterVal
func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver {
return mcs.BlockCutterVal
......
Supports Markdown
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