Skip to content

Commit

Permalink
Fix/events (#86)
Browse files Browse the repository at this point in the history
* Revert "Add ExtrinsicSignatureV5 (#68)"

This reverts commit 461cf42.

* Events fix

* fixes

* Fix DispatchError

* Fix tests
  • Loading branch information
mikiquantum authored Jul 16, 2020
1 parent 53c1093 commit 6c55974
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
41 changes: 41 additions & 0 deletions teste2e/author_submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,47 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/types"
)

func TestChain_Events(t *testing.T) {
targetURL := config.Default().RPCURL // Replace with desired endpoint
api, err := gsrpc.NewSubstrateAPI(targetURL)
if err != nil {
panic(err)
}

meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}

key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
if err != nil {
panic(err)
}

//fmt.Printf("%x\n", key)

blockNUmber := uint64(0) // Replace with desired block to parse events

bh, err := api.RPC.Chain.GetBlockHash(blockNUmber)
if err != nil {
panic(err)
}

raw, err := api.RPC.State.GetStorageRaw(key, bh)
if err != nil {
panic(err)
}

//fmt.Printf("%x\n", *raw)

events := types.EventRecords{}
err = types.EventRecordsRaw(*raw).DecodeEventRecords(meta, &events)
if err != nil {
panic(err)
}

}

func TestChain_SubmitExtrinsic(t *testing.T) {
if testing.Short() {
t.Skip("skipping end-to-end test in short mode.")
Expand Down
7 changes: 5 additions & 2 deletions types/event_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ func (d *DispatchError) Decode(decoder scale.Decoder) error {
return err
}

if b == 1 {
// https://github.com/paritytech/substrate/blob/4da29261bfdc13057a425c1721aeb4ec68092d42/primitives/runtime/src/lib.rs
// Line 391
// Enum index 3 for Module Error
if b == 3 {
d.HasModule = true
err = decoder.Decode(&d.Module)
}
Expand All @@ -327,7 +330,7 @@ func (d *DispatchError) Decode(decoder scale.Decoder) error {
func (d DispatchError) Encode(encoder scale.Encoder) error {
var err error
if d.HasModule {
err = encoder.PushByte(1)
err = encoder.PushByte(3)
if err != nil {
return err
}
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 @@ -239,7 +239,7 @@ func TestEventRecordsRaw_Decode(t *testing.T) {

"0002000000" + // ApplyExtrinsic(2)
"0001" + // System_ExtrinsicFailed
"01" + // HasModule
"03" + // HasModule
"0b" + // Module
"00" + // Error
"10270000" + // Weight
Expand Down
28 changes: 16 additions & 12 deletions types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,16 +792,17 @@ type EventUtilityBatchCompleted struct {
// EventUtilityNewMultisig is emitted when a new multisig operation has begun.
// First param is the account that is approving, second is the multisig account, third is hash of the call.
type EventUtilityNewMultisig struct {
Phase Phase
Who, ID AccountID
CallHash Hash
Topics []Hash
Phase Phase
Who, ID AccountID
// TODO Get CallHash back on for newer versions of substrate
//CallHash Hash
Topics []Hash
}

// TimePoint is a global extrinsic index, formed as the extrinsic index within a block,
// together with that block's height.
type TimePoint struct {
Height BlockNumber
Height U32
Index U32
}

Expand All @@ -812,8 +813,9 @@ type EventUtilityMultisigApproval struct {
Who AccountID
TimePoint TimePoint
ID AccountID
CallHash Hash
Topics []Hash
// TODO Get CallHash back on for newer versions of substrate
//CallHash Hash
Topics []Hash
}

// DispatchResult can be returned from dispatchable functions
Expand Down Expand Up @@ -857,9 +859,10 @@ type EventUtilityMultisigExecuted struct {
Who AccountID
TimePoint TimePoint
ID AccountID
CallHash Hash
Result DispatchResult
Topics []Hash
// TODO Get CallHash back on for newer versions of substrate
//CallHash Hash
Result DispatchResult
Topics []Hash
}

// EventUtility is emitted when a multisig operation has been cancelled. First param is the account that is
Expand All @@ -869,6 +872,7 @@ type EventUtilityMultisigCancelled struct {
Who AccountID
TimePoint TimePoint
ID AccountID
CallHash Hash
Topics []Hash
// TODO Get CallHash back on for newer versions of substrate
//CallHash Hash
Topics []Hash
}
4 changes: 2 additions & 2 deletions types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestDispatchResult_Decode(t *testing.T) {
assert.True(t, res.Ok)

// Dispatch Error
decoder = scale.NewDecoder(bytes.NewReader([]byte{1, 1, 1, 1}))
decoder = scale.NewDecoder(bytes.NewReader([]byte{1, 3, 1, 1}))
res = DispatchResult{}
assert.NoError(t, decoder.Decode(&res))
assert.False(t, res.Ok)
Expand All @@ -49,7 +49,7 @@ func TestDispatchResult_Decode(t *testing.T) {
assert.Equal(t, res.Error.Error, byte(1))

// decoder error
decoder = scale.NewDecoder(bytes.NewReader([]byte{1, 1, 1}))
decoder = scale.NewDecoder(bytes.NewReader([]byte{1, 3, 1}))
res = DispatchResult{}
assert.Error(t, decoder.Decode(&res))
}

0 comments on commit 6c55974

Please sign in to comment.