Skip to content

Commit e7de469

Browse files
Kavinjsirgezerui0124AndyBoWu
authored
Merge staging to main (#42)
* fix(index): rewards indexing * fix(apr): correct apr calculation * fix(apr): correct apr calculation (#24) * refactor(ci): update ci to avoid flacky * docs(*): detailed config * feat(ci): add slack notification * feat(uptime): update uptime calculation (#29) * feat(uptime): update uptime calculation (#30) * feat(uptime): update uptime calculation * feat(uptime): update uptime api (#32) * feat(uptime): update uptime calculation * feat(uptime): add more logs * feat(uptime): logs * feat(rpc): upgrade cometbft dep * feat(uptime): add index lag * LICENSE * docs(contributing): add contributing docs (#37) * docs(contributing): add contributing docs * docs(contributing): add contributing docs * feat(index): index history of total stake amount (#38) * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount (#39) * ci(*): set max cpu to avoid throttling Signed-off-by: Tony Jin <kavinjsir@gmail.com> --------- Signed-off-by: Tony Jin <kavinjsir@gmail.com> Co-authored-by: ezreal1997 <gezerui1997@gmail.com> Co-authored-by: Andy Wu <bwu2sfu@gmail.com>
1 parent 3296b67 commit e7de469

File tree

6 files changed

+156
-85
lines changed

6 files changed

+156
-85
lines changed

.github/workflows/cd.yml

Lines changed: 30 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,40 @@
1-
name: Build and Push Image, Update Helm Chart
1+
name: Build and Push Staking-api Image
22

33
on:
44
push:
55
branches:
66
- 'staging'
77
- 'main'
88

9-
env:
10-
APP_NAME: staking-api
11-
K8S_NS: staking
12-
CLUSTER_NAME: ${{ vars.CLUSTER_NAME }}
13-
149
jobs:
15-
build-and-update:
16-
name: Build and Push Image, Update Helm Chart
10+
define-params:
1711
runs-on: ubuntu-latest
18-
19-
environment: ${{ (github.ref_name == 'main') && 'production' || 'staging' }}
20-
12+
outputs:
13+
app_name: staking-api
14+
k8s_ns: staking
15+
image_tag: ${{ github.sha }}
16+
target_environment: ${{ (github.ref_name == 'main') && 'production' || 'staging' }}
2117
steps:
22-
- name: Checkout Repository
23-
uses: actions/checkout@v4
24-
25-
- name: Authenticate with GCP
26-
uses: google-github-actions/auth@v2
27-
with:
28-
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
29-
30-
- name: Configure Docker for GCP
31-
run: gcloud auth configure-docker
32-
33-
- name: Set up Docker Buildx
34-
uses: docker/setup-buildx-action@v3
35-
36-
- name: Build and Push Docker Image
37-
run: |
38-
IMAGE_NAME=gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ env.APP_NAME }}:${{ github.sha }}
39-
docker build \
40-
--push \
41-
--tag $IMAGE_NAME \
42-
-f ./Dockerfile \
43-
.
44-
env:
45-
IMAGE_NAME: gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ env.APP_NAME }}:${{ github.sha }}
46-
47-
- name: Checkout Target Repository
48-
uses: actions/checkout@v4
49-
with:
50-
repository: storyprotocol/story-helm
51-
ref: main
52-
token: ${{ secrets.CICD_GIT_TOKEN }}
53-
54-
- name: Update Image Tag
55-
uses: mikefarah/yq@master
56-
with:
57-
cmd: |
58-
yq -i '.image.tag = "${{ github.sha }}"' '${{ env.K8S_NS }}/${{ env.APP_NAME }}/${{ env.CLUSTER_NAME }}.yaml'
59-
60-
- name: Push Changed Image Tag
61-
run: |
62-
git config --global user.name "${{ github.actor }}"
63-
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
64-
git checkout main
65-
66-
# If there's nothing to commit, stop here
67-
if [ -z "$(git status --porcelain)" ]; then
68-
echo "Nothing to commit, working directory clean"
69-
exit 0
70-
fi
71-
72-
# We'll try up to 3 attempts
73-
for i in 1 2 3
74-
do
75-
echo "Attempt #$i to commit and push changes..."
76-
# Commit if there's something to commit
77-
git commit -am "${{ env.APP_NAME }}: Update image tag to ${{ github.sha }}"
78-
79-
if git push
80-
then
81-
echo "Push succeeded on attempt #$i!"
82-
break
83-
else
84-
echo "Push failed (attempt #$i). Trying to pull and rebase with origin/main."
85-
git fetch origin main
86-
git pull --rebase origin main || (echo "Rebase failed!" && exit 1)
87-
fi
88-
89-
# If this was the last attempt, fail the job
90-
if [ "$i" -eq "3" ]
91-
then
92-
echo "Exceeded max number of push attempts."
93-
exit 1
94-
fi
95-
done
18+
- run: echo "Exposing target environment"
19+
20+
image-build:
21+
needs: define-params
22+
uses: storyprotocol/gha-workflows/.github/workflows/reusable-gcp-image-build-worker.yml@main
23+
with:
24+
app_name: ${{ needs.define-params.outputs.app_name }}
25+
k8s_ns: ${{ needs.define-params.outputs.k8s_ns }}
26+
target_environment: ${{ needs.define-params.outputs.target_environment }}
27+
image_tag: ${{ needs.define-params.outputs.image_tag }}
28+
dockerfile_path: "./Dockerfile"
29+
dockerfile_context: "."
30+
secrets: inherit
31+
32+
request-app-release:
33+
needs: [ define-params, image-build ]
34+
uses: storyprotocol/gha-workflows/.github/workflows/reusable-gcp-app-release-publisher.yml@main
35+
with:
36+
app_name: ${{ needs.define-params.outputs.app_name }}
37+
k8s_ns: ${{ needs.define-params.outputs.k8s_ns }}
38+
target_environment: ${{ needs.define-params.outputs.target_environment }}
39+
image_tag: ${{ needs.define-params.outputs.image_tag }}
40+
secrets: inherit
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Slack Notification on PR Merge
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
notify:
9+
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == github.event.repository.default_branch
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Send Slack Notification
14+
run: |
15+
curl -X POST \
16+
-H 'Content-type: application/json' \
17+
--data '{
18+
"channel": "qa-messenger",
19+
"text": "---\n**PR Merged** \n*Repo:* '"${{ github.repository }}"'\n*Title:* '"${{ github.event.pull_request.title }}"'\n*Tag:* <'"${{ secrets.SLACK_ROYCE_MEMBER_ID }}"'> <'"${{ secrets.SLACK_BO_MEMBER_ID }}"'>\n*Link:* <'"${{ github.event.pull_request.html_url }}"'|View PR>"
20+
}' \
21+
${{ secrets.SLACK_QA_MESSENGER_WEBHOOK_URL }}

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
The primary function of the Story Staking API is to provide interfaces through which it aggregates and processes data. These interfaces include various modules, each dedicated to different aspects of network data. As of now, the following module has been fully implemented:
1212

1313
### Staking API
14+
1415
- **Network Status**: Provides the status of the network.
1516
- **Network Estimated APR**: Provides an estimate of the Annual Percentage Rate (APR) for the network.
1617
- **Operation History**: Provides a list of stakingoperations for a given address.
@@ -19,7 +20,27 @@ The primary function of the Story Staking API is to provide interfaces through w
1920

2021
## Build and Run
2122

23+
### Prerequisites
24+
25+
1. **Database (Postgres or MySQL)**
26+
You can use your own database or run the following command to quickly set up a local Postgres container:
27+
28+
```bash
29+
# Spin up a local Postgres container
30+
make local-postgres
31+
```
32+
33+
> If you prefer MySQL, ensure you have a local or remote instance running.
34+
35+
2. **Redis Cache**
36+
You can use your own Redis server or run the following command to quickly set up a local Redis container:
37+
```bash
38+
# Spin up a local Redis container
39+
make local-redis
40+
```
41+
2242
### Build
43+
2344
```bash
2445
$ make build
2546
$ ./story-staking-api --help
@@ -62,6 +83,86 @@ engine = "redis"
6283
config_file = "config/redis.yaml"
6384
```
6485

86+
#### DB Credentials
87+
88+
The `config_file` fields in the `[database]` and `[cache]` sections are optional. You can also specify credentials via environment variables. For example:
89+
90+
```bash
91+
# Redis environment variables
92+
REDIS_PASSWORD_MODE="plain"
93+
REDIS_DB="0"
94+
REDIS_ADDR=""
95+
REDIS_PASSWORD=""
96+
97+
# Postgres environment variables
98+
POSTGRES_DB_USERNAME=""
99+
POSTGRES_DB_PASSWORD=""
100+
POSTGRES_DB_PASSWORD_MODE="plain"
101+
POSTGRES_DB_HOST=""
102+
POSTGRES_DB_PORT=""
103+
POSTGRES_DB_NAME=""
104+
POSTGRES_DB_MAX_CONNS="100"
105+
POSTGRES_DB_MIN_CONNS="0"
106+
POSTGRES_DB_MAX_CONN_LIFETIME="12h"
107+
POSTGRES_DB_MAX_CONN_IDLE_TIME="5m"
108+
```
109+
110+
> **Note:** Adjust each value as needed for your setup.
111+
112+
#### Run in Cluster Mode
113+
114+
**Staking API** supports running in a cluster mode, with a designated **writer** node and one or more **reader** nodes. Below are example configuration files:
115+
116+
**Writer Configuration**
117+
118+
Create a `writer-config.toml` with the following content:
119+
120+
```toml
121+
[blockchain]
122+
consensus_chain_id = ""
123+
cometbft_rpc_endpoint = ""
124+
story_api_endpoint = ""
125+
geth_rpc_endpoint = ""
126+
127+
[server]
128+
index_mode = "writer"
129+
service_mode = "debug"
130+
service_port = ":8080"
131+
132+
[database]
133+
engine = "postgres"
134+
config_file = ""
135+
136+
[cache]
137+
engine = "redis"
138+
config_file = ""
139+
```
140+
141+
**Reader Configuration**
142+
143+
Create one `reader-config.toml` files to scale read operations as needed:
144+
145+
```toml
146+
[blockchain]
147+
consensus_chain_id = ""
148+
cometbft_rpc_endpoint = ""
149+
story_api_endpoint = ""
150+
geth_rpc_endpoint = ""
151+
152+
[server]
153+
index_mode = "reader"
154+
service_mode = "debug"
155+
service_port = ":8080"
156+
157+
[database]
158+
engine = "postgres"
159+
config_file = ""
160+
161+
[cache]
162+
engine = "redis"
163+
config_file = ""
164+
```
165+
65166
Note that the `config_file` path is relative to the home directory specified by the `--home` flag when running the binary.
66167

67168
### API Documentation

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/BurntSushi/toml"
1010
kingpin "github.com/alecthomas/kingpin/v2"
1111
"github.com/rs/zerolog/log"
12+
_ "go.uber.org/automaxprocs"
1213

1314
"github.com/piplabs/story-staking-api/pkg/server"
1415
)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ require (
146146
go.opentelemetry.io/otel v1.34.0 // indirect
147147
go.opentelemetry.io/otel/metric v1.34.0 // indirect
148148
go.opentelemetry.io/otel/trace v1.34.0 // indirect
149+
go.uber.org/automaxprocs v1.6.0 // indirect
149150
golang.org/x/arch v0.12.0 // indirect
150151
golang.org/x/crypto v0.36.0 // indirect
151152
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce
423423
go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w=
424424
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
425425
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
426+
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
427+
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
426428
golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg=
427429
golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
428430
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)