-
Notifications
You must be signed in to change notification settings - Fork 1
/
configtx_processor.go
91 lines (75 loc) · 3.06 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package peer
import (
"fmt"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/customtx"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
const (
channelConfigKey = "resourcesconfigtx.CHANNEL_CONFIG_KEY"
peerNamespace = ""
)
// txProcessor implements the interface 'github.com/hyperledger/fabric/core/ledger/customtx/Processor'
type configtxProcessor struct {
}
// newTxProcessor constructs a new instance of txProcessor
func newConfigTxProcessor() customtx.Processor {
return &configtxProcessor{}
}
// GenerateSimulationResults implements function in the interface 'github.com/hyperledger/fabric/core/ledger/customtx/Processor'
// This implemantation processes following two types of transactions.
// CONFIG - simply stores the config in the statedb. Additionally, stores the resource config seed if the transaction is from the genesis block.
// PEER_RESOURCE_UPDATE - In a normal course, this validates the transaction against the current resource bundle,
// computes the full configuration, and stores the full configuration if the transaction is found valid.
// However, if 'initializingLedger' is true (i.e., either the ledger is being created from the genesis block
// or the ledger is synching the state with the blockchain, during start up), the full config is computed using
// the most recent configs from statedb
func (tp *configtxProcessor) GenerateSimulationResults(txEnv *common.Envelope, simulator ledger.TxSimulator, initializingLedger bool) error {
payload := utils.UnmarshalPayloadOrPanic(txEnv.Payload)
channelHdr := utils.UnmarshalChannelHeaderOrPanic(payload.Header.ChannelHeader)
txType := common.HeaderType(channelHdr.GetType())
switch txType {
case common.HeaderType_CONFIG:
peerLogger.Debugf("Processing CONFIG")
return processChannelConfigTx(txEnv, simulator)
default:
return fmt.Errorf("tx type [%s] is not expected", txType)
}
}
func processChannelConfigTx(txEnv *common.Envelope, simulator ledger.TxSimulator) error {
configEnvelope := &common.ConfigEnvelope{}
if _, err := utils.UnmarshalEnvelopeOfType(txEnv, common.HeaderType_CONFIG, configEnvelope); err != nil {
return err
}
channelConfig := configEnvelope.Config
if err := persistConf(simulator, channelConfigKey, channelConfig); err != nil {
return err
}
peerLogger.Debugf("channelConfig=%s", channelConfig)
if channelConfig == nil {
return fmt.Errorf("Channel config found nil")
}
return nil
}
func persistConf(simulator ledger.TxSimulator, key string, config *common.Config) error {
serializedConfig, err := serialize(config)
if err != nil {
return err
}
return simulator.SetState(peerNamespace, key, serializedConfig)
}
func retrievePersistedConf(queryExecuter ledger.QueryExecutor, key string) (*common.Config, error) {
serializedConfig, err := queryExecuter.GetState(peerNamespace, key)
if err != nil {
return nil, err
}
if serializedConfig == nil {
return nil, nil
}
return deserialize(serializedConfig)
}