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

Collector API sync atxs received after given timestamp #136

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Spacemesh explorer backend designed to provide data for explorer-frontends
The explorer backend provides a public REST API that can be used to get data about a Spacemesh network.
Follow these steps to use the API for a public Spacemesh network:

1. Obtain a currently available explorer API endpoint from the [Spacemesh public web services](https://discover.spacemesh.io/networks.json) endpoint. This endpoint lists all available Spacemesh networks such as testnets.
1. Build a REST request using the endpoint. For example, if the explorer api url for net-id 28 is `https://explorer-api-28.spacemesh.io/` then the network-info data is available at `https://explorer-api-28.spacemesh.io/network-info`.
1. Issue an http 'GET' request to get the data. e.g. `curl https://explorer-api-28.spacemesh.io/network-info`.
1. Obtain a currently available explorer API endpoint from the [Spacemesh public web services](https://configs.spacemesh.network/networks.json) endpoint. This endpoint lists all available Spacemesh networks.
1. Build a REST request using the endpoint. For example, explorer api url for mainnet is `https://mainnet-explorer-api.spacemesh.network/` then the network-info data is available at `https://mainnet-explorer-api.spacemesh.network/network-info`.
1. Issue an http 'GET' request to get the data. e.g. `curl https://mainnet-explorer-api.spacemesh.network/network-info`.
1. Live long and prosper.

### Paging and pagination
- Use the `pagesize` and `page` params to get paginated results. The first page number is 1, so for example, to get the first 20 accounts on TN 128 call: `https://explorer-api-28.spacemesh.io/accounts?pagesize=20&page=1` and to get the next 20 accounts use: `https://explorer-api-28.spacemesh.io/accounts?pagesize=20&page=2`
- Use the `pagesize` and `page` params to get paginated results. The first page number is 1, so for example, to get the first 20 layers call: `https://mainnet-explorer-api.spacemesh.network/layers?pagesize=20&page=1` and to get the next 20 layers use: `https://mainnet-explorer-api.spacemesh.network/layers?pagesize=20&page=2`
- API results which support pagination include pagination data in the response json. e.g.:

```
Expand All @@ -25,11 +25,11 @@ Use this pagination data to figure out how many calls you need to make and which


### API Capabilities
The API is not properly documented yet. The best way to identity the supported API methods is via the api server [source code](https://github.com/spacemeshos/explorer-backend/blob/master/api/httpserver/httpserver.go).
The API is not properly documented yet. The best way to identity the supported API methods is via the api server [source code](https://github.com/spacemeshos/explorer-backend/blob/master/internal/api/router/router.go).

### API Usage Examples

- Get an account state: https://explorer-api-28.spacemesh.io/accounts/0xaFEd9A1c17Ca7eaA7A6795dBc7BEe1B1d992c7ba
- Get testnet netid=28 current summary data: https://explorer-api-28.spacemesh.io/network-info
- Get layer details: https://mainnet-explorer-api.spacemesh.network/layers/52410
- Get mainnet current network info: https://mainnet-explorer-api.spacemesh.network/


24 changes: 24 additions & 0 deletions collector/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,37 @@ import (

func (c *Collector) StartHttpServer(apiHost string, apiPort int) {
e := echo.New()

e.GET("/sync/atx/ts/:ts", func(ctx echo.Context) error {
ts := ctx.Param("ts")
timestamp, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return ctx.String(http.StatusBadRequest, "Invalid parameter")
}

log.Info("http syncing atxs from %d", timestamp)
go func() {
err = c.dbClient.GetAtxsReceivedAfter(c.db, timestamp, func(atx *types.VerifiedActivationTx) bool {
c.listener.OnActivation(atx)
return true
})
if err != nil {
log.Warning("syncing atxs from %s failed with error %d", ts, err)
return
}
}()

return ctx.NoContent(http.StatusOK)
})

e.GET("/sync/atx/:epoch", func(ctx echo.Context) error {
epoch := ctx.Param("epoch")
epochId, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
return ctx.String(http.StatusBadRequest, "Invalid parameter")
}

log.Info("http syncing atxs for epoch %s", epoch)
go func() {
err = c.dbClient.GetAtxsByEpoch(c.db, epochId, func(atx *types.VerifiedActivationTx) bool {
c.listener.OnActivation(atx)
Expand Down
Loading