forked from hyperledger/fabric-x-committer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.go
More file actions
81 lines (70 loc) · 2.33 KB
/
Copy pathnamespace.go
File metadata and controls
81 lines (70 loc) · 2.33 KB
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package workload
import (
"maps"
"slices"
"github.com/cockroachdb/errors"
"google.golang.org/protobuf/proto"
"github.com/hyperledger/fabric-x-committer/api/protoblocktx"
"github.com/hyperledger/fabric-x-committer/api/protoloadgen"
"github.com/hyperledger/fabric-x-committer/api/types"
)
// CreateLoadGenNamespacesTX creating the transaction containing the requested namespaces into the MetaNamespace.
func CreateLoadGenNamespacesTX(policy *PolicyProfile) (*protoloadgen.TX, error) {
txb, txbErr := NewTxBuilderFromPolicy(policy, nil)
if txbErr != nil {
return nil, txbErr
}
_, ok := txb.TxSigner.HashSigners[types.MetaNamespaceID]
if !ok {
return nil, errors.New("no meta namespace signer found; cannot create namespaces")
}
tx, err := CreateNamespacesTxFromSigner(txb.TxSigner, 0)
if err != nil {
return nil, err
}
return txb.MakeTx(tx), nil
}
// CreateNamespacesTX creating the transaction containing the requested namespaces into the MetaNamespace.
func CreateNamespacesTX(
policy *PolicyProfile, metaNamespaceVersion uint64, includeNS ...string,
) (*protoblocktx.Tx, error) {
signer := NewTxSignerVerifier(policy)
return CreateNamespacesTxFromSigner(signer, metaNamespaceVersion, includeNS...)
}
// CreateNamespacesTxFromSigner creating the transaction containing the requested namespaces into the MetaNamespace.
func CreateNamespacesTxFromSigner(
signer *TxSignerVerifier, metaNamespaceVersion uint64, includeNS ...string,
) (*protoblocktx.Tx, error) {
if len(includeNS) == 0 {
includeNS = slices.Collect(maps.Keys(signer.HashSigners))
}
readWrites := make([]*protoblocktx.ReadWrite, 0, len(signer.HashSigners))
for _, ns := range includeNS {
if ns == types.MetaNamespaceID {
continue
}
p, ok := signer.HashSigners[ns]
if !ok {
return nil, errors.Errorf("no policy for '%s'", ns)
}
policyBytes, err := proto.Marshal(p.GetVerificationPolicy())
if err != nil {
return nil, errors.Wrap(err, "failed to serialize namespace policy")
}
readWrites = append(readWrites, &protoblocktx.ReadWrite{
Key: []byte(ns),
Value: policyBytes,
})
}
return &protoblocktx.Tx{
Namespaces: []*protoblocktx.TxNamespace{{
NsId: types.MetaNamespaceID,
NsVersion: metaNamespaceVersion,
ReadWrites: readWrites,
}},
}, nil
}