From b3827c4a757aa5eb577fd7a29c9cb57c39d317d4 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 31 Mar 2023 00:10:18 -0400 Subject: [PATCH 1/3] Add P-chain indexer example --- indexer/examples/p-chain/main.go | 54 +++++++++++++++++++++++++ indexer/examples/x-chain-blocks/main.go | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 indexer/examples/p-chain/main.go diff --git a/indexer/examples/p-chain/main.go b/indexer/examples/p-chain/main.go new file mode 100644 index 00000000000..482ce679570 --- /dev/null +++ b/indexer/examples/p-chain/main.go @@ -0,0 +1,54 @@ +// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package main + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/ava-labs/avalanchego/indexer" + "github.com/ava-labs/avalanchego/vms/platformvm/blocks" + "github.com/ava-labs/avalanchego/vms/proposervm/block" +) + +// This example program continuously polls for the next P-Chain block +// and prints the ID of the block and its transactions. +func main() { + var ( + uri = fmt.Sprintf("%s/ext/index/P/block", "https://indexer-demo.avax.network") + client = indexer.NewClient(uri) + ctx = context.Background() + nextIndex uint64 = 2_000_000 + ) + for { + container, err := client.GetContainerByIndex(ctx, nextIndex) + if err != nil { + time.Sleep(time.Second) + log.Printf("polling for next accepted block\n") + continue + } + + platformvmBlockBytes := container.Bytes + proposerVMBlock, err := block.Parse(container.Bytes) + if err == nil { + platformvmBlockBytes = proposerVMBlock.Block() + } + + platformvmBlock, err := blocks.Parse(blocks.Codec, platformvmBlockBytes) + if err != nil { + log.Fatalf("failed to parse platformvm block: %s\n", err) + } + + acceptedTxs := platformvmBlock.Txs() + log.Printf("accepted block %s with %d transactions\n", platformvmBlock.ID(), len(acceptedTxs)) + + for _, tx := range acceptedTxs { + log.Printf("accepted transaction %s\n", tx.ID()) + } + + nextIndex++ + } +} diff --git a/indexer/examples/x-chain-blocks/main.go b/indexer/examples/x-chain-blocks/main.go index 64c40f4597b..a995f9612bb 100644 --- a/indexer/examples/x-chain-blocks/main.go +++ b/indexer/examples/x-chain-blocks/main.go @@ -25,10 +25,10 @@ func main() { nextIndex uint64 ) for { - log.Printf("polling for next accepted block\n") container, err := client.GetContainerByIndex(ctx, nextIndex) if err != nil { time.Sleep(time.Second) + log.Printf("polling for next accepted block\n") continue } From e75364463d51035a9c7184efc8a77ca6b33622fb Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 31 Mar 2023 15:03:25 -0400 Subject: [PATCH 2/3] use local network in example --- indexer/examples/p-chain/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/indexer/examples/p-chain/main.go b/indexer/examples/p-chain/main.go index 482ce679570..dda18f401e0 100644 --- a/indexer/examples/p-chain/main.go +++ b/indexer/examples/p-chain/main.go @@ -12,13 +12,14 @@ import ( "github.com/ava-labs/avalanchego/indexer" "github.com/ava-labs/avalanchego/vms/platformvm/blocks" "github.com/ava-labs/avalanchego/vms/proposervm/block" + "github.com/ava-labs/avalanchego/wallet/subnet/primary" ) // This example program continuously polls for the next P-Chain block // and prints the ID of the block and its transactions. func main() { var ( - uri = fmt.Sprintf("%s/ext/index/P/block", "https://indexer-demo.avax.network") + uri = fmt.Sprintf("%s/ext/index/P/block", primary.LocalAPIURI) client = indexer.NewClient(uri) ctx = context.Background() nextIndex uint64 = 2_000_000 From 5afe63c5dc8a8574f875c9349088b0de81668fea Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 31 Mar 2023 15:03:59 -0400 Subject: [PATCH 3/3] nit --- indexer/examples/p-chain/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/indexer/examples/p-chain/main.go b/indexer/examples/p-chain/main.go index dda18f401e0..257591f45b0 100644 --- a/indexer/examples/p-chain/main.go +++ b/indexer/examples/p-chain/main.go @@ -19,10 +19,10 @@ import ( // and prints the ID of the block and its transactions. func main() { var ( - uri = fmt.Sprintf("%s/ext/index/P/block", primary.LocalAPIURI) - client = indexer.NewClient(uri) - ctx = context.Background() - nextIndex uint64 = 2_000_000 + uri = fmt.Sprintf("%s/ext/index/P/block", primary.LocalAPIURI) + client = indexer.NewClient(uri) + ctx = context.Background() + nextIndex uint64 ) for { container, err := client.GetContainerByIndex(ctx, nextIndex)