watcher-ts/ipld-demo.md

4.5 KiB

Demo for IPLD statediff and checkpointing

  • In the root of watcher-ts, run:

    yarn && yarn build
    
  • In console, run the IPFS daemon:

    # Verify ipfs version
    ipfs version
    # ipfs version 0.12.2
    
    ipfs daemon
    
  • The following services should be running to work with watcher:

  • Deploy Example contract:

    cd packages/graph-node
    
    yarn example:deploy
    
  • Set the returned address to the variable $EXAMPLE_ADDRESS:

    EXAMPLE_ADDRESS=
    
  • In packages/graph-node, run:

    cp .env.example .env
    
    yarn build:example
    
  • In packages/codegen, create a config.yaml file with the following contents:

    contracts:
      - name: Example
        path: ../graph-node/test/contracts/Example.sol
        kind: Example1
    
    outputFolder: ../demo-example-watcher
    mode: all
    kind: active
    port: 3008
    flatten: true
    subgraphPath: ../graph-node/test/subgraph/example1/build
    

    Reference: packages/codegen/README.md

  • Generate watcher:

    cd packages/codegen
    
    yarn codegen --config-file ./config.yaml
    
  • In packages/demo-example-watcher, run:

    yarn
    
  • Create dbs:

    sudo su - postgres
    # Delete databases if they already exist.
    dropdb demo-example-watcher
    dropdb demo-example-watcher-job-queue
    
    # Create databases
    createdb demo-example-watcher
    createdb demo-example-watcher-job-queue
    

    Enable the pgcrypto extension.

    psql -U postgres -h localhost demo-example-watcher-job-queue
    
    demo-example-watcher-job-queue=# CREATE EXTENSION pgcrypto;
    demo-example-watcher-job-queue=# exit
    
  • In a new terminal, in packages/demo-example-watcher, run:

    yarn server
    
    yarn job-runner
    
  • Run the following GQL subscription at the graphql endpoint:

    subscription {
      onEvent {
        event {
          __typename
          ... on TestEvent {
            param1
            param2
            param3
          },
        },
        block {
          number
          hash
        }
      }
    }
    
  • Trigger the Test event by calling example contract method:

    cd packages/graph-node
    
    yarn example:test --address $EXAMPLE_ADDRESS
    

    A Test event shall be visible in the subscription at endpoint.

  • Run the getState query at the endpoint to get the latest IPLDBlock for EXAMPLE_ADDRESS:

    query {
      getState (
        blockHash: "EVENT_BLOCK_HASH"
        contractAddress: "EXAMPLE_ADDRESS"
        kind: "diff_staged"
      ) {
        cid
        block {
          cid
          hash
          number
          timestamp
          parentHash
        }
        contractAddress
        data
      }
    }
    
  • Run the query for entity at the endpoint:

    query {
      author (
        block: {
          hash: "EVENT_BLOCK_HASH"
        }
        id: "0xdc7d7a8920c8eecc098da5b7522a5f31509b5bfc"
      ) {
        __typename
        name
        paramInt
        paramBigInt
        paramBytes
      }
    }
    
  • diff IPLDBlocks get created corresponding to the diff_staged blocks when their respective eth_blocks reach the pruned region.

  • In packages/demo-example-watcher:

    • After the diff block has been created, create a checkpoint:

      cd packages/demo-example-watcher
      
      yarn checkpoint --address $EXAMPLE_ADDRESS
      
      • A checkpoint IPLDBlock should be created at the latest canonical block hash.

      • Run the getState query again at the endpoint with the output blockHash and kind checkpoint.

  • All the IPLDBlock entries can be seen in pg-admin in table ipld_block.

  • All the diff and checkpoint IPLDBlocks should be pushed to IPFS.

  • Open IPFS WebUI http://127.0.0.1:5001/webui and search for IPLDBlocks using their CIDs.