github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/core/deliverservice/requester.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package deliverclient 18 19 import ( 20 "math" 21 22 "github.com/hyperledger/fabric/common/localmsp" 23 "github.com/hyperledger/fabric/core/deliverservice/blocksprovider" 24 "github.com/hyperledger/fabric/protos/common" 25 "github.com/hyperledger/fabric/protos/orderer" 26 "github.com/hyperledger/fabric/protos/utils" 27 ) 28 29 type blocksRequester struct { 30 chainID string 31 client blocksprovider.BlocksDeliverer 32 } 33 34 func (b *blocksRequester) RequestBlocks(ledgerInfoProvider blocksprovider.LedgerInfo) error { 35 height, err := ledgerInfoProvider.LedgerHeight() 36 if err != nil { 37 logger.Errorf("Can't get legder height for channel %s from committer [%s]", b.chainID, err) 38 return err 39 } 40 41 if height > 0 { 42 logger.Debugf("Starting deliver with block [%d] for channel %s", height, b.chainID) 43 if err := b.seekLatestFromCommitter(height); err != nil { 44 return err 45 } 46 } else { 47 logger.Debugf("Starting deliver with olders block for channel %s", b.chainID) 48 if err := b.seekOldest(); err != nil { 49 return err 50 } 51 } 52 53 return nil 54 } 55 56 func (b *blocksRequester) seekOldest() error { 57 seekInfo := &orderer.SeekInfo{ 58 Start: &orderer.SeekPosition{Type: &orderer.SeekPosition_Oldest{Oldest: &orderer.SeekOldest{}}}, 59 Stop: &orderer.SeekPosition{Type: &orderer.SeekPosition_Specified{Specified: &orderer.SeekSpecified{Number: math.MaxUint64}}}, 60 Behavior: orderer.SeekInfo_BLOCK_UNTIL_READY, 61 } 62 63 //TODO- epoch and msgVersion may need to be obtained for nowfollowing usage in orderer/configupdate/configupdate.go 64 msgVersion := int32(0) 65 epoch := uint64(0) 66 env, err := utils.CreateSignedEnvelope(common.HeaderType_CONFIG_UPDATE, b.chainID, localmsp.NewSigner(), seekInfo, msgVersion, epoch) 67 if err != nil { 68 return err 69 } 70 return b.client.Send(env) 71 } 72 73 func (b *blocksRequester) seekLatestFromCommitter(height uint64) error { 74 seekInfo := &orderer.SeekInfo{ 75 Start: &orderer.SeekPosition{Type: &orderer.SeekPosition_Specified{Specified: &orderer.SeekSpecified{Number: height}}}, 76 Stop: &orderer.SeekPosition{Type: &orderer.SeekPosition_Specified{Specified: &orderer.SeekSpecified{Number: math.MaxUint64}}}, 77 Behavior: orderer.SeekInfo_BLOCK_UNTIL_READY, 78 } 79 80 //TODO- epoch and msgVersion may need to be obtained for nowfollowing usage in orderer/configupdate/configupdate.go 81 msgVersion := int32(0) 82 epoch := uint64(0) 83 env, err := utils.CreateSignedEnvelope(common.HeaderType_CONFIG_UPDATE, b.chainID, localmsp.NewSigner(), seekInfo, msgVersion, epoch) 84 if err != nil { 85 return err 86 } 87 return b.client.Send(env) 88 }