committer_impl.go 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package committer

import (
20
21
	"fmt"

22
	"github.com/hyperledger/fabric/core/committer/txvalidator"
23
	"github.com/hyperledger/fabric/core/ledger"
24
	"github.com/hyperledger/fabric/events/producer"
25
	"github.com/hyperledger/fabric/protos/common"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	"github.com/op/go-logging"
)

//--------!!!IMPORTANT!!-!!IMPORTANT!!-!!IMPORTANT!!---------
// This is used merely to complete the loop for the "skeleton"
// path so we can reason about and  modify committer component
// more effectively using code.

var logger *logging.Logger // package-level logger

func init() {
	logger = logging.MustGetLogger("committer")
}

40
41
42
// LedgerCommitter is the implementation of  Committer interface
// it keeps the reference to the ledger to commit blocks and retreive
// chain information
43
type LedgerCommitter struct {
44
	ledger    ledger.PeerLedger
45
	validator txvalidator.Validator
46
47
48
}

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

53
// Commit commits block to into the ledger
54
// Note, it is important that this always be called serially
55
func (lc *LedgerCommitter) Commit(block *common.Block) error {
56
57
	// Validate and mark invalid transactions
	logger.Debug("Validating block")
58
59
60
	if err := lc.validator.Validate(block); err != nil {
		return err
	}
61

62
	if err := lc.ledger.Commit(block); err != nil {
63
64
		return err
	}
65
66
67
68
69
70
71

	// send block event *after* the block has been committed
	if err := producer.SendProducerBlockEvent(block); err != nil {
		logger.Errorf("Error sending block event %s", err)
		return fmt.Errorf("Error sending block event %s", err)
	}

72
73
74
75
76
	return nil
}

// LedgerHeight returns recently committed block sequence number
func (lc *LedgerCommitter) LedgerHeight() (uint64, error) {
77
	var info *common.BlockchainInfo
78
79
80
81
82
83
84
85
86
87
	var err error
	if info, err = lc.ledger.GetBlockchainInfo(); err != nil {
		logger.Errorf("Cannot get blockchain info, %s\n", info)
		return uint64(0), err
	}

	return info.Height, nil
}

// GetBlocks used to retrieve blocks with sequence numbers provided in the slice
88
89
func (lc *LedgerCommitter) GetBlocks(blockSeqs []uint64) []*common.Block {
	var blocks []*common.Block
90
91

	for _, seqNum := range blockSeqs {
92
93
		if blck, err := lc.ledger.GetBlockByNumber(seqNum); err != nil {
			logger.Errorf("Not able to acquire block num %d, from the ledger skipping...\n", seqNum)
94
			continue
95
		} else {
96
			logger.Debug("Appending next block with seqNum = ", seqNum, " to the resulting set")
97
			blocks = append(blocks, blck)
98
99
100
101
102
		}
	}

	return blocks
}
103
104
105
106
107

// Close the ledger
func (lc *LedgerCommitter) Close() {
	lc.ledger.Close()
}