Skip to content

Commit 27cf4f6

Browse files
author
Jason Yellick
committed
[FAB-8026] configtxlator cli compute update
Presently, configtxlator only exposes its functions through a REST API. This is convenient for cross-language compatibility, but it forces CLI users to fall back to writing CURL commands. This CR simply exposes the update computation logic directly via the CLI. Change-Id: Ifc5e7e33d0a9cb43d181a3c77843d41827938397 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 03d4559 commit 27cf4f6

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

common/tools/configtxlator/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525

2626
"github.com/hyperledger/fabric/common/tools/configtxlator/metadata"
2727
"github.com/hyperledger/fabric/common/tools/configtxlator/rest"
28+
"github.com/hyperledger/fabric/common/tools/configtxlator/update"
2829
"github.com/hyperledger/fabric/common/tools/protolator"
30+
cb "github.com/hyperledger/fabric/protos/common"
2931

3032
"github.com/golang/protobuf/proto"
3133
"github.com/op/go-logging"
@@ -53,6 +55,12 @@ var (
5355
protoDecodeSource = protoDecode.Flag("input", "A file containing the proto message.").Default(os.Stdin.Name()).File()
5456
protoDecodeDest = protoDecode.Flag("output", "A file to write the JSON document to.").Default(os.Stdout.Name()).OpenFile(os.O_RDWR|os.O_CREATE, 0600)
5557

58+
computeUpdate = app.Command("compute_update", "Takes two marshaled common.Config messages and computes the config update which transitions between the two.")
59+
computeUpdateOriginal = computeUpdate.Flag("original", "The original config message.").File()
60+
computeUpdateUpdated = computeUpdate.Flag("updated", "The updated config message.").File()
61+
computeUpdateChannelID = computeUpdate.Flag("channel_id", "The name of the channel for this update.").Required().String()
62+
computeUpdateDest = computeUpdate.Flag("output", "A file to write the JSON document to.").Default(os.Stdout.Name()).OpenFile(os.O_RDWR|os.O_CREATE, 0600)
63+
5664
version = app.Command("version", "Show version information")
5765
)
5866

@@ -77,6 +85,14 @@ func main() {
7785
if err != nil {
7886
app.Fatalf("Error decoding: %s", err)
7987
}
88+
case computeUpdate.FullCommand():
89+
defer (*computeUpdateOriginal).Close()
90+
defer (*computeUpdateUpdated).Close()
91+
defer (*computeUpdateDest).Close()
92+
err := computeUpdt(*computeUpdateOriginal, *computeUpdateUpdated, *computeUpdateDest, *computeUpdateChannelID)
93+
if err != nil {
94+
app.Fatalf("Error computing update: %s", err)
95+
}
8096
// "version" command
8197
case version.FullCommand():
8298
printVersion()
@@ -144,3 +160,46 @@ func decodeProto(msgName string, input, output *os.File) error {
144160

145161
return nil
146162
}
163+
164+
func computeUpdt(original, updated, output *os.File, channelID string) error {
165+
origIn, err := ioutil.ReadAll(original)
166+
if err != nil {
167+
return errors.Wrapf(err, "error reading original config")
168+
}
169+
170+
origConf := &cb.Config{}
171+
err = proto.Unmarshal(origIn, origConf)
172+
if err != nil {
173+
return errors.Wrapf(err, "error unmarshaling original config")
174+
}
175+
176+
updtIn, err := ioutil.ReadAll(updated)
177+
if err != nil {
178+
return errors.Wrapf(err, "error reading updated config")
179+
}
180+
181+
updtConf := &cb.Config{}
182+
err = proto.Unmarshal(updtIn, updtConf)
183+
if err != nil {
184+
return errors.Wrapf(err, "error unmarshaling updated config")
185+
}
186+
187+
cu, err := update.Compute(origConf, updtConf)
188+
if err != nil {
189+
return errors.Wrapf(err, "error computing config update")
190+
}
191+
192+
cu.ChannelId = channelID
193+
194+
outBytes, err := proto.Marshal(cu)
195+
if err != nil {
196+
return errors.Wrapf(err, "error marshaling computed config update")
197+
}
198+
199+
_, err = output.Write(outBytes)
200+
if err != nil {
201+
return errors.Wrapf(err, "error writing config update to output")
202+
}
203+
204+
return nil
205+
}

0 commit comments

Comments
 (0)