Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Commit

Permalink
Fixing known events (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikiquantum committed Jul 16, 2020
1 parent 07c1cd0 commit a92ea6f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ lint:
fi;
./bin/golangci-lint run ./... --timeout 5m0s

lint-fix:
if [ ! -f ./bin/golangci-lint ]; then \
$(MAKE) get-lint; \
fi;
./bin/golangci-lint run ./... --timeout 5m0s --fix

build:
@echo " > \033[32mBuilding binary...\033[0m "
cd cmd/chainbridge && env GOARCH=amd64 go build -o ../../build/chainbridge
Expand Down Expand Up @@ -88,4 +94,4 @@ docker-e2e:
docker-compose -f ./docker-compose-e2e.yml up -V

clean:
rm -rf build/ solidity/
rm -rf build/ solidity/
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e
github.com/ChainSafe/log15 v1.0.0
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.3+incompatible
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.4+incompatible
github.com/ethereum/go-ethereum v1.9.13
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/stretchr/testify v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.3+incompatible h1:d8hQYVrpemZ6ZN38kL1XdQtezXTwrgiVXgQg+M3Lay0=
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.3+incompatible/go.mod h1:GBMLH8MQs5g4FcrytcMm9uRgBnTL1LIkNTue6lUPhZU=
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.4+incompatible h1:qlmlrelmXOzo090rJgvGpO4Cg4OYx5RwyMBw5yK3gGw=
github.com/centrifuge/go-substrate-rpc-client v2.0.0-alpha.4+incompatible/go.mod h1:GBMLH8MQs5g4FcrytcMm9uRgBnTL1LIkNTue6lUPhZU=
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
Expand Down
4 changes: 2 additions & 2 deletions shared/substrate/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type EventNewMultisig struct {

// TimePoint contains height and index
type TimePoint struct {
Height types.BlockNumber
Height types.U32
Index types.U32
}

Expand Down Expand Up @@ -126,7 +126,7 @@ type Events struct {
Example_Remark []EventExampleRemark //nolint:stylecheck,golint
Nfts_DepositAsset []EventNFTDeposited //nolint:stylecheck,golint
Council_Proposed []types.EventCollectiveProposed //nolint:stylecheck,golint
Council_Voted []types.EventCollectiveProposed //nolint:stylecheck,golint
Council_Voted []types.EventCollectiveVoted //nolint:stylecheck,golint
Council_Approved []types.EventCollectiveApproved //nolint:stylecheck,golint
Council_Disapproved []types.EventCollectiveDisapproved //nolint:stylecheck,golint
Council_Executed []types.EventCollectiveExecuted //nolint:stylecheck,golint
Expand Down
80 changes: 80 additions & 0 deletions shared/substrate/testing/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

package subtest

import (
"fmt"
"testing"

"github.com/ChainSafe/ChainBridge/e2e/substrate"
utils "github.com/ChainSafe/ChainBridge/shared/substrate"
gsrpc "github.com/centrifuge/go-substrate-rpc-client"
"github.com/centrifuge/go-substrate-rpc-client/types"
)

func TestChain_Events(t *testing.T) {
targetURL := substrate.TestSubEndpoint // 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)

//latest, err := api.RPC.Chain.GetBlockLatest()
//if err != nil {
// panic(err)
//}
latestNumber := uint32(1) // Set to uint32(latest.Block.Header.Number)

batchSize := uint32(1) // Set to higher value accordingly, like 1000
current := uint64(0) // Start block
numBatches := latestNumber - uint32(current)/batchSize

// Not smart enough to adjust batch size to last batch
// Manually trigger the last one with minimum batch size
for i := uint32(0); i < numBatches; i++ {
lower, err := api.RPC.Chain.GetBlockHash(current)
if err != nil {
panic(err)
}

upperBlock := current + uint64(batchSize)
upper, err := api.RPC.Chain.GetBlockHash(upperBlock)
if err != nil {
panic(err)
}

raws, err := api.RPC.State.QueryStorage([]types.StorageKey{key}, lower, upper)
if err != nil {
panic(err)
}

for j := 0; j < len(raws); j++ {
events := utils.Events{}
for k := 0; k < len(raws[j].Changes); k++ {
raw := raws[j].Changes[k]
fmt.Printf("Processing events for block %s with data: %x\n", raws[j].Block.Hex(), raw.StorageData)
err = types.EventRecordsRaw(raw.StorageData).DecodeEventRecords(meta, &events)
if err != nil {
panic(err)
}
}
}

fmt.Println("Events batch successfully processed: ", i, "until block", upperBlock)
current += uint64(batchSize)
}

}

0 comments on commit a92ea6f

Please sign in to comment.