|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package types |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "errors" |
| 12 | + "fmt" |
| 13 | + "net" |
| 14 | + "slices" |
| 15 | + "strconv" |
| 16 | + "strings" |
| 17 | + |
| 18 | + "gopkg.in/yaml.v3" |
| 19 | +) |
| 20 | + |
| 21 | +type ( |
| 22 | + // OrdererEndpoint defines an orderer party's endpoint. |
| 23 | + OrdererEndpoint struct { |
| 24 | + Host string `mapstructure:"host" json:"host,omitempty" yaml:"host,omitempty"` |
| 25 | + Port int `mapstructure:"port" json:"port,omitempty" yaml:"port,omitempty"` |
| 26 | + // ID is the party ID. |
| 27 | + ID uint32 `mapstructure:"id" json:"id,omitempty" yaml:"id,omitempty"` |
| 28 | + MspID string `mapstructure:"msp-id" json:"msp-id,omitempty" yaml:"msp-id,omitempty"` |
| 29 | + // API should be broadcast and/or deliver. Empty value means all API is supported. |
| 30 | + API []string `mapstructure:"api" json:"api,omitempty" yaml:"api,omitempty"` |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + // Broadcast support by endpoint. |
| 36 | + Broadcast = "broadcast" |
| 37 | + // Deliver support by endpoint. |
| 38 | + Deliver = "deliver" |
| 39 | + // NoID indicates that a party ID is not specified (default). |
| 40 | + // This allows backward compatibility with orderers that doesn't support this syntax. |
| 41 | + NoID = uint32(0x100000) |
| 42 | +) |
| 43 | + |
| 44 | +// ErrInvalidEndpoint orderer endpoints error. |
| 45 | +var ErrInvalidEndpoint = errors.New("invalid endpoint") |
| 46 | + |
| 47 | +// Address returns a string representation of the endpoint's address. |
| 48 | +func (e *OrdererEndpoint) Address() string { |
| 49 | + return net.JoinHostPort(e.Host, strconv.Itoa(e.Port)) |
| 50 | +} |
| 51 | + |
| 52 | +// String returns a deterministic representation of the endpoint. |
| 53 | +func (e *OrdererEndpoint) String() string { |
| 54 | + var output strings.Builder |
| 55 | + isFirst := true |
| 56 | + if e.ID < NoID { |
| 57 | + output.WriteString("id=") |
| 58 | + output.WriteString(strconv.FormatUint(uint64(e.ID), 10)) |
| 59 | + isFirst = false |
| 60 | + } |
| 61 | + if len(e.MspID) > 0 { |
| 62 | + if !isFirst { |
| 63 | + output.WriteRune(',') |
| 64 | + } |
| 65 | + output.WriteString("msp-id=") |
| 66 | + output.WriteString(e.MspID) |
| 67 | + isFirst = false |
| 68 | + } |
| 69 | + for _, api := range e.API { |
| 70 | + if !isFirst { |
| 71 | + output.WriteRune(',') |
| 72 | + } |
| 73 | + output.WriteString(api) |
| 74 | + isFirst = false |
| 75 | + } |
| 76 | + if len(e.Host) > 0 || e.Port > 0 { |
| 77 | + if !isFirst { |
| 78 | + output.WriteRune(',') |
| 79 | + } |
| 80 | + output.WriteString(e.Address()) |
| 81 | + } |
| 82 | + return output.String() |
| 83 | +} |
| 84 | + |
| 85 | +// SupportsAPI returns true if this endpoint supports API. |
| 86 | +// It also returns true if no APIs are specified, as we cannot know. |
| 87 | +func (e *OrdererEndpoint) SupportsAPI(api string) bool { |
| 88 | + return len(e.API) == 0 || slices.Contains(e.API, api) |
| 89 | +} |
| 90 | + |
| 91 | +// ParseOrdererEndpoint parses a string according to the following schema order (the first that succeeds). |
| 92 | +// Schema 1: YAML. |
| 93 | +// Schema 2: JSON. |
| 94 | +// Schema 3: [id=ID,][msp-id=MspID,][broadcast,][deliver,][host=Host,][port=Port,][Host:Port]. |
| 95 | +func ParseOrdererEndpoint(valueRaw string) (*OrdererEndpoint, error) { |
| 96 | + ret := &OrdererEndpoint{ID: NoID} |
| 97 | + if len(valueRaw) == 0 { |
| 98 | + return ret, nil |
| 99 | + } |
| 100 | + if err := yaml.Unmarshal([]byte(valueRaw), ret); err == nil { |
| 101 | + return ret, nil |
| 102 | + } |
| 103 | + if err := json.Unmarshal([]byte(valueRaw), ret); err == nil { |
| 104 | + return ret, nil |
| 105 | + } |
| 106 | + err := unmarshalOrdererEndpoint(valueRaw, ret) |
| 107 | + return ret, err |
| 108 | +} |
| 109 | + |
| 110 | +func unmarshalOrdererEndpoint(valueRaw string, out *OrdererEndpoint) error { |
| 111 | + metaParts := strings.Split(valueRaw, ",") |
| 112 | + for _, item := range metaParts { |
| 113 | + item = strings.TrimSpace(item) |
| 114 | + equalIdx := strings.Index(item, "=") |
| 115 | + colonIdx := strings.Index(item, ":") |
| 116 | + var err error |
| 117 | + switch { |
| 118 | + case item == Broadcast || item == Deliver: |
| 119 | + out.API = append(out.API, item) |
| 120 | + case equalIdx >= 0: |
| 121 | + key, value := strings.TrimSpace(item[:equalIdx]), strings.TrimSpace(item[equalIdx+1:]) |
| 122 | + switch key { |
| 123 | + case "msp-id": |
| 124 | + out.MspID = value |
| 125 | + case "host": |
| 126 | + out.Host = value |
| 127 | + case "id": |
| 128 | + err = out.setID(value) |
| 129 | + case "port": |
| 130 | + err = out.setPort(value) |
| 131 | + default: |
| 132 | + return fmt.Errorf("invalid key '%s' for item '%s': %w", key, item, ErrInvalidEndpoint) |
| 133 | + } |
| 134 | + case colonIdx >= 0: |
| 135 | + var port string |
| 136 | + out.Host, port, err = net.SplitHostPort(strings.TrimSpace(item)) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("invalid host/port '%s': %w", item, err) |
| 139 | + } |
| 140 | + err = out.setPort(strings.TrimSpace(port)) |
| 141 | + default: |
| 142 | + return fmt.Errorf("invalid item '%s': %w", item, ErrInvalidEndpoint) |
| 143 | + } |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func (e *OrdererEndpoint) setPort(portStr string) error { |
| 152 | + port, err := strconv.ParseInt(portStr, 10, 32) |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("failed to parse port: %w", err) |
| 155 | + } |
| 156 | + e.Port = int(port) |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +func (e *OrdererEndpoint) setID(idStr string) error { |
| 161 | + id, err := strconv.ParseUint(idStr, 10, 32) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("invalid id value: %w", err) |
| 164 | + } |
| 165 | + e.ID = uint32(id) |
| 166 | + return nil |
| 167 | +} |
0 commit comments