Skip to content

Commit

Permalink
api: Update event parsing (#1604)
Browse files Browse the repository at this point in the history
* api: Update event parsing

* nft: Drop support for CreateRegistry and TransferNFT

* api: Drop check for proxy events

* api: Simplify extrinsic success check

* api: Add event fields to error

* CI: Enable build for this branch

* api: Do not return events in ExtrinsicInfo
  • Loading branch information
cdamian authored Jul 26, 2023
1 parent 6f09eac commit 7bba051
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 278 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# build workflow builds docker images, pushes images to docker hub and updates swagger API
on:
push:
branches: [legacy]
branches: [legacy, legacy-update-event-parsing]
name: Build
jobs:
build:
Expand Down
105 changes: 46 additions & 59 deletions centchain/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/centrifuge/go-centrifuge/jobs"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/rpc/author"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
Expand Down Expand Up @@ -45,15 +46,6 @@ type ExtrinsicInfo struct {
Hash types.Hash
BlockHash types.Hash
Index uint // index number of extrinsic in a block

// EventsRaw contains all the events in the given block
// if you want to filter events for an extrinsic, use the Index
EventsRaw types.EventRecordsRaw
}

// Events returns all the events occurred in a given block
func (e ExtrinsicInfo) Events(meta *types.Metadata) (events Events, err error) {
return events, e.EventsRaw.DecodeEventRecords(meta, &events)
}

// API exposes required functions to interact with Centrifuge Chain.
Expand Down Expand Up @@ -140,21 +132,23 @@ func (dsa *defaultSubstrateAPI) GetStorageLatest(key types.StorageKey, target in
}

type api struct {
sapi substrateAPI
config Config
dispatcher jobs.Dispatcher
accounts map[string]uint32
accMu sync.Mutex // accMu to protect accounts
sapi substrateAPI
config Config
dispatcher jobs.Dispatcher
accounts map[string]uint32
accMu sync.Mutex // accMu to protect accounts
eventRetriever retriever.EventRetriever
}

// NewAPI returns a new centrifuge chain api.
func NewAPI(sapi substrateAPI, config Config, dispatcher jobs.Dispatcher) API {
func NewAPI(sapi substrateAPI, config Config, dispatcher jobs.Dispatcher, eventRetriever retriever.EventRetriever) API {
return &api{
sapi: sapi,
config: config,
dispatcher: dispatcher,
accounts: make(map[string]uint32),
accMu: sync.Mutex{},
sapi: sapi,
config: config,
dispatcher: dispatcher,
accounts: make(map[string]uint32),
accMu: sync.Mutex{},
eventRetriever: eventRetriever,
}
}

Expand Down Expand Up @@ -300,16 +294,14 @@ func (a *api) SubmitAndWatch(
return nil, fmt.Errorf("extrinsic %s not found in block %d", txHash.Hex(), bn)
}

eventsRaw, err := checkExtrinsicEventSuccess(meta, a.sapi, bh, extIdx)
if err != nil {
if err = a.checkExtrinsicEventSuccess(bh, extIdx); err != nil {
return nil, err
}

info := ExtrinsicInfo{
Hash: txHash,
BlockHash: bh,
Index: uint(extIdx),
EventsRaw: eventsRaw,
}
return info, nil
})
Expand All @@ -328,6 +320,37 @@ func (a *api) SubmitAndWatch(
return result.(ExtrinsicInfo), nil
}

const (
ExtrinsicSuccessEventName = "System.ExtrinsicSuccess"
ExtrinsicFailedEventName = "System.ExtrinsicFailed"
)

func (a *api) checkExtrinsicEventSuccess(
blockHash types.Hash,
extrinsicIdx int,
) error {
events, err := a.eventRetriever.GetEvents(blockHash)

if err != nil {
return fmt.Errorf("event retrieval error: %w", err)
}

for _, event := range events {
switch {
case event.Name == ExtrinsicSuccessEventName &&
event.Phase.IsApplyExtrinsic &&
event.Phase.AsApplyExtrinsic == uint32(extrinsicIdx):
return nil
case event.Name == ExtrinsicFailedEventName &&
event.Phase.IsApplyExtrinsic &&
event.Phase.AsApplyExtrinsic == uint32(extrinsicIdx):
return fmt.Errorf("extrinsic with index %d failed: %v", extrinsicIdx, event.Fields)
}
}

return errors.New("should not have reached this step: %v", events)
}

func (a *api) incrementNonce(accountID []byte) {
a.accMu.Lock()
defer a.accMu.Unlock()
Expand Down Expand Up @@ -392,39 +415,3 @@ func isExtrinsicSignatureInBlock(extSign types.Signature, block types.Block) int
}
return found
}

func checkExtrinsicEventSuccess(meta *types.Metadata, api substrateAPI, blockHash types.Hash,
extrinsicIdx int) (eventsRaw types.EventRecordsRaw, err error) {
key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
if err != nil {
return eventsRaw, err
}

err = api.GetStorage(key, &eventsRaw, blockHash)
if err != nil {
return eventsRaw, err
}

events := Events{}
err = eventsRaw.DecodeEventRecords(meta, &events)
if err != nil {
return eventsRaw, err
}

// Check success events
for _, es := range events.System_ExtrinsicSuccess {
if es.Phase.IsApplyExtrinsic && es.Phase.AsApplyExtrinsic == uint32(extrinsicIdx) {
return eventsRaw, nil // Success executing extrinsic
}
}

// Otherwise, check failure events
for _, es := range events.System_ExtrinsicFailed {
if es.Phase.IsApplyExtrinsic && es.Phase.AsApplyExtrinsic == uint32(extrinsicIdx) {
return eventsRaw, errors.New("extrinsic %d failed %v", extrinsicIdx,
es.DispatchError) // Failure executing extrinsic
}
}

return eventsRaw, errors.New("should not have reached this step: %v", events)
}
6 changes: 1 addition & 5 deletions centchain/api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ func TestApi_SubmitAndWatch(t *testing.T) {
kr, err := caa.KeyRingPair()
assert.NoError(t, err)

info, err := api.SubmitAndWatch(contextutil.WithAccount(context.Background(), acc), meta, call, kr)
_, err = api.SubmitAndWatch(contextutil.WithAccount(context.Background(), acc), meta, call, kr)
assert.NoError(t, err)

events, err := info.Events(meta)
assert.NoError(t, err)
assert.True(t, len(events.System_ExtrinsicSuccess) > 1)
}
8 changes: 7 additions & 1 deletion centchain/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/centrifuge/go-centrifuge/config"
"github.com/centrifuge/go-centrifuge/jobs"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
)

// BootstrappedCentChainClient is a key to mapped client in bootstrap context.
Expand All @@ -24,8 +26,12 @@ func (Bootstrapper) Bootstrap(context map[string]interface{}) error {
if err != nil {
return err
}
eventRetriever, err := retriever.NewDefaultEventRetriever(state.NewEventProvider(sapi.RPC.State), sapi.RPC.State)
if err != nil {
return err
}
centSAPI := &defaultSubstrateAPI{sapi}
client := NewAPI(centSAPI, cfg, dispatcher)
client := NewAPI(centSAPI, cfg, dispatcher, eventRetriever)
context[BootstrappedCentChainClient] = client
return nil
}
50 changes: 24 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ require (
github.com/Masterminds/semver v1.5.0
github.com/centrifuge/centrifuge-protobufs v0.0.0-20210311165003-6866e980f58c
github.com/centrifuge/chain-custom-types v1.0.1
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.13-0.20230111181438-6501f611f49f
github.com/centrifuge/go-substrate-rpc-client/v4 v4.1.0
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6
github.com/centrifuge/precise-proofs v0.0.0-20210311192744-14bb51c630b9
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2
github.com/ethereum/go-ethereum v1.10.17
github.com/ethereum/go-ethereum v1.10.20
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-chi/render v1.0.1
Expand Down Expand Up @@ -38,12 +38,12 @@ require (
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.2
github.com/swaggo/swag v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
github.com/whyrusleeping/go-logging v0.0.1
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/crypto v0.7.0
golang.org/x/net v0.8.0
google.golang.org/grpc v1.40.0
)

Expand All @@ -59,17 +59,17 @@ require (
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.2 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/containerd/cgroups v0.0.0-20201119153540-4cbc285b3327 // indirect
github.com/coreos/go-systemd/v22 v22.1.0 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/base58 v1.0.3 // indirect
github.com/decred/base58 v1.0.4 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
Expand All @@ -81,7 +81,7 @@ require (
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-interpreter/wagon v0.6.0 // indirect
Expand Down Expand Up @@ -109,7 +109,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/huin/goupnp v1.0.3-0.20220313090229-ca81a64b4204 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/influxdata/influxdb v1.8.3 // indirect
Expand Down Expand Up @@ -168,7 +168,7 @@ require (
github.com/miekg/dns v1.1.43 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
Expand All @@ -185,7 +185,7 @@ require (
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
Expand All @@ -205,10 +205,9 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rjeczalik/notify v0.9.2 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -219,16 +218,17 @@ require (
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/urfave/cli/v2 v2.10.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.14.0 // indirect
github.com/vedhavyas/go-subkey v1.0.3 // indirect
github.com/vedhavyas/go-subkey v1.0.4 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/xsleonard/go-merkle v1.1.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
Expand All @@ -238,23 +238,21 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210211221406-4ccc9a5e4183 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/urfave/cli.v1 v1.20.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace (
Expand Down
Loading

0 comments on commit 7bba051

Please sign in to comment.