forked from hyperledger/fabric
/
configtx_processor.go
58 lines (49 loc) · 1.78 KB
/
configtx_processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package peer
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protoutil"
)
const (
channelConfigKey = "CHANNEL_CONFIG_ENV_BYTES"
peerNamespace = ""
)
// ConfigTxProcessor implements the interface 'github.com/hyperledger/fabric/core/ledger/customtx/Processor'
type ConfigTxProcessor struct{}
// GenerateSimulationResults implements function in the interface 'github.com/hyperledger/fabric/core/ledger/customtx/Processor'
// This implementation processes CONFIG transactions which simply stores the config-envelope-bytes
func (tp *ConfigTxProcessor) GenerateSimulationResults(txEnv *common.Envelope, simulator ledger.TxSimulator, initializingLedger bool) error {
payload := protoutil.UnmarshalPayloadOrPanic(txEnv.Payload)
channelHdr := protoutil.UnmarshalChannelHeaderOrPanic(payload.Header.ChannelHeader)
txType := common.HeaderType(channelHdr.GetType())
switch txType {
case common.HeaderType_CONFIG:
peerLogger.Debugf("Processing CONFIG")
if payload.Data == nil {
return fmt.Errorf("channel config found nil")
}
return simulator.SetState(peerNamespace, channelConfigKey, payload.Data)
default:
return fmt.Errorf("tx type [%s] is not expected", txType)
}
}
func retrieveChannelConfig(queryExecuter ledger.QueryExecutor) (*common.Config, error) {
configBytes, err := queryExecuter.GetState(peerNamespace, channelConfigKey)
if err != nil {
return nil, err
}
if configBytes == nil {
return nil, nil
}
configEnvelope := &common.ConfigEnvelope{}
if err := proto.Unmarshal(configBytes, configEnvelope); err != nil {
return nil, err
}
return configEnvelope.Config, nil
}