Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: use chainhook to listen for chain events instead of a direct stacks api connection #200

Merged
merged 57 commits into from
Aug 21, 2024

Conversation

rafaelcr
Copy link
Collaborator

@rafaelcr rafaelcr commented Mar 3, 2024

This PR replaces the direct connection between this API and the Stacks Blockchain API DB with a Chainhook predicate that feeds every Stacks block for analysis. This API then checks for token contract deployments, metadata notifications, and token mints in order to fetch their metadata.

  • Add re-org support
  • Group tests in suites for parallel GH action runs
  • Migrate to node v20
  • Persist chainhook predicates in local disk for retrieval upon restarts
  • Rewrite image cache processing in TypeScript and move to a step-by-step image transformation where local files are used temporarily to optimize memory use
  • Improve error reporting so if tokens have user-faults (metadata not parseable, unreachable URLs, size too large, etc.) we print the correct error in token responses
  • Add profiler server
  • Include admin RPCs to start/stop job queue at will

This PR significantly changes the DB schema so it represents a breaking change.

Copy link

github-actions bot commented Mar 3, 2024

Vercel deployment URL: https://token-metadata-eco3k2mc9-hirosystems.vercel.app 🚀

* fix: image cache ts

* test: more

* fix: code

* build: use v2 docker compose

* fix: admin tests

* fix: try finally

* test: debug logs

* test: only 1

* test: one more

* fix: not the timeout

* fix: different ports

* fix: improve test servers

* fix: add cause to error message

* fix: try 127.0.0.1

* fix: try timeout sleep

* fix: standard close

* test: more

* fix: improve message

* fix: echo status

* fix: detect

* fix: finally

* fix: x

* fix: timeouts

* fix: revert debug stuff

* chore: remove old test
* fix: try to use native sharp pipeline

* fix: job queue controls, profiler

* fix: tests

* fix: upgrade toolkit

* fix: improve error propagation

* fix: listen to stream errors

* fix: process cached image step by step

* fix: report errors retried immediately

* fix: image stream

* fix: wait for sharp stream correctly
…errors (#236)

* chore: demote httperror to usererror

* fix: add errors

* fix: custom messages
@@ -361,17 +370,25 @@ export function getContractLogMetadataUpdateNotification(
}
}

export type NftMintEvent = {
contractId: string;
tokenId: bigint;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few areas where token-id int sizes are different, like in some postgres tables it's an int32. Anything to worry about?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll make a pass on these to double check

src/chainhook/server.ts Outdated Show resolved Hide resolved
}
}

async function uploadImage(localPath: string, remoteName: string): Promise<string> {
Copy link
Member

@zone117x zone117x Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a good time to switch to @google-cloud/storage #215 it would simplify a lot of the auth and streaming here. Just a suggestion, this code seems fine too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah for sure... I'll prioritize that issue to work on it next

@@ -86,6 +109,17 @@ export type DbJob = {
updated_at?: string;
};

export type DbUpdateNotification = {
token_id: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token-id is type number here, just a heads up in case it needs to handle larger int sizes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that particular token ID is a postgres ID, not a bigint clarity token ID

Copy link
Member

@zone117x zone117x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome refactor! Left some nits but overall looks great 💯

@rafaelcr rafaelcr merged commit 2ddb2c7 into develop Aug 21, 2024
8 of 9 checks passed
@rafaelcr rafaelcr deleted the refactor/chainhooks branch August 21, 2024 15:54
blockstack-devops pushed a commit that referenced this pull request Aug 21, 2024
## [1.0.0-beta.1](v0.7.0...v1.0.0-beta.1) (2024-08-21)

### ⚠ BREAKING CHANGES

* use chainhook to listen for chain events instead of a direct stacks api connection (#200)

### Bug Fixes

* use prometheus port configured in ENV ([c769d29](c769d29))

### Code Refactoring

* use chainhook to listen for chain events instead of a direct stacks api connection ([#200](#200)) ([2ddb2c7](2ddb2c7)), closes [#229](#229) [#232](#232) [#233](#233) [#234](#234) [#235](#235) [#236](#236)
@blockstack-devops
Copy link

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

blockstack-devops pushed a commit that referenced this pull request Aug 26, 2024
## [1.0.0](v0.7.0...v1.0.0) (2024-08-26)

### ⚠ BREAKING CHANGES

* use chainhook to listen for chain events instead of a direct stacks api connection (#200)

### Features

* convert data: image uris into image files and upload to cdn ([#245](#245)) ([903b0aa](903b0aa))

### Bug Fixes

* catch econnreset errors ([#247](#247)) ([51347d6](51347d6))
* set maximum job timeout ([#244](#244)) ([3444917](3444917))
* take only first page of gif images ([#241](#241)) ([334f8c5](334f8c5))
* use bignumber to handle FT supplies ([#239](#239)) ([053d622](053d622))
* use google cloud library for image uploads ([#238](#238)) ([c7f1b43](c7f1b43))
* use prometheus port configured in ENV ([c769d29](c769d29))

### Code Refactoring

* use chainhook to listen for chain events instead of a direct stacks api connection ([#200](#200)) ([2ddb2c7](2ddb2c7)), closes [#229](#229) [#232](#232) [#233](#233) [#234](#234) [#235](#235) [#236](#236)
@blockstack-devops
Copy link

🎉 This PR is included in version 1.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
4 participants