|
| 1 | +// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls |
| 2 | +// |
| 3 | +// Copyright 2019 Centrifuge GmbH |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package types |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "reflect" |
| 24 | + |
| 25 | + "github.com/centrifuge/go-substrate-rpc-client/scale" |
| 26 | +) |
| 27 | + |
| 28 | +// EventRecordsRaw is a raw record for a set of events, represented as the raw bytes. It exists since |
| 29 | +// decoding of events can only be done with metadata, so events can't follow the static way of decoding |
| 30 | +// other types do. It exposes functions to decode events using metadata and targets. |
| 31 | +type EventRecordsRaw []byte |
| 32 | + |
| 33 | +type EventSystemExtrinsicSuccess struct { |
| 34 | + Phase Phase |
| 35 | + Topics []Hash |
| 36 | +} |
| 37 | +type EventSystemExtrinsicFailed struct { |
| 38 | + Phase Phase |
| 39 | + DispatchError DispatchError |
| 40 | + Topics []Hash |
| 41 | +} |
| 42 | + |
| 43 | +type EventBalancesTransfer struct { |
| 44 | + Phase Phase |
| 45 | + From AccountID |
| 46 | + To AccountID |
| 47 | + Value U128 |
| 48 | + Fees U128 |
| 49 | + Topics []Hash |
| 50 | +} |
| 51 | +type EventIndicesNewAccountIndex struct { |
| 52 | + Phase Phase |
| 53 | + AccountID AccountID |
| 54 | + AccountIndex AccountIndex |
| 55 | + Topics []Hash |
| 56 | +} |
| 57 | +type EventBalancesNewAccount struct { |
| 58 | + Phase Phase |
| 59 | + AccountID AccountID |
| 60 | + Balance U128 |
| 61 | + Topics []Hash |
| 62 | +} |
| 63 | +type EventBalancesReapedAccount struct { |
| 64 | + Phase Phase |
| 65 | + AccountID AccountID |
| 66 | + Topics []Hash |
| 67 | +} |
| 68 | +type EventSessionNewSession struct { |
| 69 | + Phase Phase |
| 70 | + SessionIndex U32 |
| 71 | + Topics []Hash |
| 72 | +} |
| 73 | + |
| 74 | +// EventRecords is a default set of possible event records that can be used as a target for |
| 75 | +// `func (e EventRecordsRaw) Decode(...` |
| 76 | +type EventRecords struct { |
| 77 | + System_ExtrinsicSuccess []EventSystemExtrinsicSuccess //nolint:stylecheck,golint |
| 78 | + System_ExtrinsicFailed []EventSystemExtrinsicFailed //nolint:stylecheck,golint |
| 79 | + Indices_NewAccountIndex []EventIndicesNewAccountIndex //nolint:stylecheck,golint |
| 80 | + Balances_NewAccount []EventBalancesNewAccount //nolint:stylecheck,golint |
| 81 | + Balances_ReapedAccount []EventBalancesReapedAccount //nolint:stylecheck,golint |
| 82 | + Balances_Transfer []EventBalancesTransfer //nolint:stylecheck,golint |
| 83 | + Session_NewSession []EventSessionNewSession //nolint:stylecheck,golint |
| 84 | +} |
| 85 | + |
| 86 | +// Decode decodes the events from an EventRecordRaw into a target t using the given Metadata m |
| 87 | +func (e EventRecordsRaw) Decode(m *Metadata, t interface{}) error { |
| 88 | + // ensure t is a pointer |
| 89 | + ttyp := reflect.TypeOf(t) |
| 90 | + if ttyp.Kind() != reflect.Ptr { |
| 91 | + return errors.New("target must be a pointer, but is " + fmt.Sprint(ttyp)) |
| 92 | + } |
| 93 | + // ensure t is not a nil pointer |
| 94 | + tval := reflect.ValueOf(t) |
| 95 | + if tval.IsNil() { |
| 96 | + return errors.New("target is a nil pointer") |
| 97 | + } |
| 98 | + val := tval.Elem() |
| 99 | + typ := val.Type() |
| 100 | + // ensure val can be set |
| 101 | + if !val.CanSet() { |
| 102 | + return fmt.Errorf("unsettable value %v", typ) |
| 103 | + } |
| 104 | + // ensure val points to a struct |
| 105 | + if val.Kind() != reflect.Struct { |
| 106 | + return fmt.Errorf("target must point to a struct, but is " + fmt.Sprint(typ)) |
| 107 | + } |
| 108 | + |
| 109 | + decoder := scale.NewDecoder(bytes.NewReader(e)) |
| 110 | + |
| 111 | + // determine number of events |
| 112 | + n, err := decoder.DecodeUintCompact() |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + // iterate over events |
| 118 | + for i := uint64(0); i < n; i++ { |
| 119 | + // decode EventID |
| 120 | + id := EventID{} |
| 121 | + err := decoder.Decode(&id) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("unable to decode EventID for event #%v: %v", i, err) |
| 124 | + } |
| 125 | + |
| 126 | + // ask metadata for method & event name for event |
| 127 | + moduleName, eventName, err := m.FindEventNamesForEventID(id) |
| 128 | + // moduleName, eventName, err := "System", "ExtrinsicSuccess", nil |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("unable to find event with EventID %v in metadata for event #%v", id, i) |
| 131 | + } |
| 132 | + |
| 133 | + // check whether name for eventID exists in t |
| 134 | + field := val.FieldByName(fmt.Sprintf("%v_%v", moduleName, eventName)) |
| 135 | + if !field.IsValid() { |
| 136 | + return fmt.Errorf("unable to find field %v_%v for event #%v with EventID %v", moduleName, eventName, i, id) |
| 137 | + } |
| 138 | + |
| 139 | + // create a pointer to with the correct type that will hold the decoded event |
| 140 | + holder := reflect.New(field.Type().Elem()) |
| 141 | + err = decoder.Decode(holder.Interface()) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("unable to decode event #%v with EventID %v, field %v_%v: %v", i, id, moduleName, |
| 144 | + eventName, err) |
| 145 | + } |
| 146 | + |
| 147 | + // add the decoded event to the slice |
| 148 | + field.Set(reflect.Append(field, holder.Elem())) |
| 149 | + |
| 150 | + // fmt.Println("Slice type", field.Type().Elem()) |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// Phase is an enum describing the current phase of the event (applying the extrinsic or finalized) |
| 156 | +type Phase struct { |
| 157 | + IsApplyExtrinsic bool |
| 158 | + AsApplyExtrinsic uint32 |
| 159 | + IsFinalization bool |
| 160 | +} |
| 161 | + |
| 162 | +func (p *Phase) Decode(decoder scale.Decoder) error { |
| 163 | + b, err := decoder.ReadOneByte() |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + if b == 0 { |
| 169 | + p.IsApplyExtrinsic = true |
| 170 | + err = decoder.Decode(&p.AsApplyExtrinsic) |
| 171 | + } else if b == 1 { |
| 172 | + p.IsFinalization = true |
| 173 | + } |
| 174 | + |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func (p Phase) Encode(encoder scale.Encoder) error { |
| 183 | + var err1, err2 error |
| 184 | + if p.IsApplyExtrinsic { |
| 185 | + err1 = encoder.PushByte(0) |
| 186 | + err2 = encoder.Encode(p.AsApplyExtrinsic) |
| 187 | + } else if p.IsFinalization { |
| 188 | + err1 = encoder.PushByte(1) |
| 189 | + } |
| 190 | + |
| 191 | + if err1 != nil { |
| 192 | + return err1 |
| 193 | + } |
| 194 | + if err2 != nil { |
| 195 | + return err2 |
| 196 | + } |
| 197 | + |
| 198 | + return nil |
| 199 | +} |
| 200 | + |
| 201 | +// DispatchError is an error occurring during extrinsic dispatch |
| 202 | +type DispatchError struct { |
| 203 | + HasModule bool |
| 204 | + Module uint8 |
| 205 | + Error uint8 |
| 206 | +} |
| 207 | + |
| 208 | +func (d *DispatchError) Decode(decoder scale.Decoder) error { |
| 209 | + b, err := decoder.ReadOneByte() |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + |
| 214 | + if b == 1 { |
| 215 | + d.HasModule = true |
| 216 | + err = decoder.Decode(&d.Module) |
| 217 | + } |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + |
| 222 | + return decoder.Decode(&d.Error) |
| 223 | +} |
| 224 | + |
| 225 | +func (d DispatchError) Encode(encoder scale.Encoder) error { |
| 226 | + var err error |
| 227 | + if d.HasModule { |
| 228 | + err = encoder.PushByte(1) |
| 229 | + if err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + err = encoder.Encode(d.Module) |
| 233 | + } else { |
| 234 | + err = encoder.PushByte(0) |
| 235 | + } |
| 236 | + |
| 237 | + if err != nil { |
| 238 | + return err |
| 239 | + } |
| 240 | + |
| 241 | + return encoder.Encode(&d.Error) |
| 242 | +} |
| 243 | + |
| 244 | +type EventID [2]byte |
0 commit comments