Skip to content

Commit

Permalink
Hotfix: Event record raw decoding naming conflict, decoding failed
Browse files Browse the repository at this point in the history
  • Loading branch information
philipstanislaus committed Oct 22, 2019
1 parent 6fb88e3 commit 1a8ff1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rpc/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ var mockSrv = MockSrv{
storageDataHex: "0xb82d895d00000000",
storageSize: 926778,
storageHashHex: "0xdf0e877ee1cb973b9a566f53707d365b269d7131b55e65b9790994e4e63b95e1",
childStorageKeyHex: "0x3a6368696c645f73746f726167653a64656661756c743a05470000", //nolint:lll beginning with hex encoded `:child_storage:` as per https://github.com/paritytech/substrate/blob/master/core/primitives/storage/src/lib.rs#L71
childStorageKeyHex: "0x3a6368696c645f73746f726167653a64656661756c743a05470000", //nolint:lll // beginning with hex encoded `:child_storage:` as per https://github.com/paritytech/substrate/blob/master/core/primitives/storage/src/lib.rs#L71
childStorageTrieKeyHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0",
childStorageTrieValueHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d09c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a921000000", //nolint:lll
childStorageTrieValue: ChildStorageTrieTestVal{
Expand Down
28 changes: 26 additions & 2 deletions types/event_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"reflect"

"github.com/centrifuge/go-substrate-rpc-client/scale"
Expand All @@ -28,8 +29,31 @@ import (
// EventRecordsRaw is a raw record for a set of events, represented as the raw bytes. It exists since
// decoding of events can only be done with metadata, so events can't follow the static way of decoding
// other types do. It exposes functions to decode events using metadata and targets.
// Be careful using this in your own structs – it only works as the last value in a struct since it will consume the
// remainder of the encoded data. The reason for this is that it does not contain any length encoding, so it would
// not know where to stop.
type EventRecordsRaw []byte

// Encode implements encoding for Data, which just unwraps the bytes of Data
func (e EventRecordsRaw) Encode(encoder scale.Encoder) error {
return encoder.Write(e)
}

// Decode implements decoding for Data, which just reads all the remaining bytes into Data
func (e *EventRecordsRaw) Decode(decoder scale.Decoder) error {
for i := 0; true; i++ {
b, err := decoder.ReadOneByte()
if err == io.EOF {
break
}
if err != nil {
return err
}
*e = append((*e)[:i], b)
}
return nil
}

type EventSystemExtrinsicSuccess struct {
Phase Phase
Topics []Hash
Expand Down Expand Up @@ -83,8 +107,8 @@ type EventRecords struct {
Session_NewSession []EventSessionNewSession //nolint:stylecheck,golint
}

// Decode decodes the events from an EventRecordRaw into a target t using the given Metadata m
func (e EventRecordsRaw) Decode(m *Metadata, t interface{}) error {
// DecodeEventRecords decodes the events records from an EventRecordRaw into a target t using the given Metadata m
func (e EventRecordsRaw) DecodeEventRecords(m *Metadata, t interface{}) error {
// ensure t is a pointer
ttyp := reflect.TypeOf(t)
if ttyp.Kind() != reflect.Ptr {
Expand Down
2 changes: 1 addition & 1 deletion types/event_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func ExampleEventRecordsRaw_Decode() {
e := EventRecordsRaw(MustHexDecodeString("0x100000000000000000000100000000000000020000000302d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48266d00000000000000000000000000000010a5d4e80000000000000000000000000002000000000000")) //nolint:lll

events := EventRecords{}
err := e.Decode(ExamplaryMetadataV8, &events)
err := e.DecodeEventRecords(ExamplaryMetadataV8, &events)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 1a8ff1e

Please sign in to comment.