|
| 1 | +// Copyright 2020 ChainSafe Systems |
| 2 | +// SPDX-License-Identifier: LGPL-3.0-only |
| 3 | + |
| 4 | +package subtest |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ChainSafe/ChainBridge/e2e/substrate" |
| 11 | + utils "github.com/ChainSafe/ChainBridge/shared/substrate" |
| 12 | + gsrpc "github.com/centrifuge/go-substrate-rpc-client" |
| 13 | + "github.com/centrifuge/go-substrate-rpc-client/types" |
| 14 | +) |
| 15 | + |
| 16 | +func TestChain_Events(t *testing.T) { |
| 17 | + targetURL := substrate.TestSubEndpoint // Replace with desired endpoint |
| 18 | + api, err := gsrpc.NewSubstrateAPI(targetURL) |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + |
| 23 | + meta, err := api.RPC.State.GetMetadataLatest() |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + |
| 28 | + key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil) |
| 29 | + if err != nil { |
| 30 | + panic(err) |
| 31 | + } |
| 32 | + |
| 33 | + //fmt.Printf("%x\n", key) |
| 34 | + |
| 35 | + //latest, err := api.RPC.Chain.GetBlockLatest() |
| 36 | + //if err != nil { |
| 37 | + // panic(err) |
| 38 | + //} |
| 39 | + latestNumber := uint32(1) // Set to uint32(latest.Block.Header.Number) |
| 40 | + |
| 41 | + batchSize := uint32(1) // Set to higher value accordingly, like 1000 |
| 42 | + current := uint64(0) // Start block |
| 43 | + numBatches := latestNumber - uint32(current)/batchSize |
| 44 | + |
| 45 | + // Not smart enough to adjust batch size to last batch |
| 46 | + // Manually trigger the last one with minimum batch size |
| 47 | + for i := uint32(0); i < numBatches; i++ { |
| 48 | + lower, err := api.RPC.Chain.GetBlockHash(current) |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + |
| 53 | + upperBlock := current + uint64(batchSize) |
| 54 | + upper, err := api.RPC.Chain.GetBlockHash(upperBlock) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + |
| 59 | + raws, err := api.RPC.State.QueryStorage([]types.StorageKey{key}, lower, upper) |
| 60 | + if err != nil { |
| 61 | + panic(err) |
| 62 | + } |
| 63 | + |
| 64 | + for j := 0; j < len(raws); j++ { |
| 65 | + events := utils.Events{} |
| 66 | + for k := 0; k < len(raws[j].Changes); k++ { |
| 67 | + raw := raws[j].Changes[k] |
| 68 | + fmt.Printf("Processing events for block %s with data: %x\n", raws[j].Block.Hex(), raw.StorageData) |
| 69 | + err = types.EventRecordsRaw(raw.StorageData).DecodeEventRecords(meta, &events) |
| 70 | + if err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Println("Events batch successfully processed: ", i, "until block", upperBlock) |
| 77 | + current += uint64(batchSize) |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments