Skip to content

Commit

Permalink
Merge branch 'main' into _lastUnpausedAt_
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Apr 3, 2024
2 parents 24e8196 + c4b705b commit 6894d5d
Show file tree
Hide file tree
Showing 42 changed files with 546 additions and 406 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/bridge-ui-preview-internal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID_BRIDGE_UI_INTERNAL }}

on:
push:
branches-ignore:
- main
- release-please-*
paths:
- "packages/bridge-ui/**"

jobs:
Deploy-Preview:
runs-on: [taiko-runner]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install pnpm dependencies
uses: ./.github/actions/install-pnpm-dependencies

- name: Install Vercel CLI
run: pnpm add --global vercel@latest

- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}

- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}

- name: Deploy Project Artifacts to Vercel
run: vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}
37 changes: 16 additions & 21 deletions .github/workflows/bridge-ui-preview.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Vercel Preview Deployment

env:
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID_BRIDGE_UI }}

on:
push:
Expand All @@ -14,25 +13,21 @@ on:
jobs:
Deploy-Preview:
runs-on: [taiko-runner]
strategy:
matrix:
org: ["Public", "Internal"]
include:
- org: "Public"
org_id_secret: "VERCEL_ORG_ID"
project_id_secret: "VERCEL_PROJECT_ID_BRIDGE_UI"
- org: "Internal"
org_id_secret: "VERCEL_ORG_ID"
project_id_secret: "VERCEL_PROJECT_ID_BRIDGE_UI_INTERNAL"
steps:
- uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v4

- name: Install pnpm dependencies
uses: ./.github/actions/install-pnpm-dependencies

- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Setup Vercel Environment for ${{ matrix.org }}
run: |
vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} --scope=${{ secrets[matrix.org_id_secret] }} --project-id=${{ secrets[matrix.project_id_secret] }}
vercel link --token=${{ secrets.VERCEL_TOKEN }} --confirm --name=${{ matrix.org }} --scope=${{ secrets[matrix.org_id_secret] }}
- name: Build Project Artifacts for ${{ matrix.org }}
run: pnpm add --global vercel@latest

- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}

- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel for ${{ matrix.org }}

- name: Deploy Project Artifacts to Vercel
run: vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}
2 changes: 1 addition & 1 deletion packages/blobstorage/.default.indexer.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RPC_URL=wss://l1ws.internal.taiko.xyz
BEACON_URL=https://l1beacon.internal.taiko.xyz/eth/v1/beacon/blob_sidecars/
BEACON_URL=https://l1beacon.internal.taiko.xyz
TAIKO_L1_CONTRACT_ADDRESS=0xC069c3d2a9f2479F559AD34485698ad5199C555f
DATABASE_HOST=localhost
DATABASE_PORT=3306
Expand Down
135 changes: 135 additions & 0 deletions packages/blobstorage/indexer/beaconclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package indexer

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
)

var (
blobURL = "eth/v1/beacon/blob_sidecars"
genesisURL = "eth/v1/beacon/genesis"
configURL = "eth/v1/config/spec"
)

type GetSpecResponse struct {
Data map[string]string `json:"data"`
}

type GenesisResponse struct {
Data struct {
GenesisTime string `json:"genesis_time"`
} `json:"data"`
}

type BeaconClient struct {
*http.Client
beaconURL string
genesisTime uint64
secondsPerSlot uint64
}

type BlobsResponse struct {
Data []struct {
Index string `json:"index"`
Blob string `json:"blob"`
KzgCommitment string `json:"kzg_commitment"`
KzgCommitmentHex []byte `json:"-"`
} `json:"data"`
}

func NewBeaconClient(cfg *Config, timeout time.Duration) (*BeaconClient, error) {
httpClient := &http.Client{Timeout: timeout}

// Get the genesis time.
url := fmt.Sprintf("%s/%s", cfg.BeaconURL, genesisURL)
genesisTime, err := getGenesisTime(url, httpClient)
if err != nil {
return nil, fmt.Errorf("failed to get genesis time: %v", err)
}

url = fmt.Sprintf("%s/%s", cfg.BeaconURL, configURL)
// Get the seconds per slot.
secondsPerSlot, err := getConfigValue(url, "SECONDS_PER_SLOT", httpClient)
if err != nil {
return nil, fmt.Errorf("failed to get SECONDS_PER_SLOT: %v", err)
}

secondsPerSlotUint64, err := strconv.ParseUint(secondsPerSlot, 10, 64)
if err != nil {
return nil, err
}

return &BeaconClient{
beaconURL: cfg.BeaconURL,
genesisTime: genesisTime,
secondsPerSlot: secondsPerSlotUint64,
}, nil
}

func getGenesisTime(endpoint string, client *http.Client) (uint64, error) {
res, err := client.Get(endpoint)
if err != nil {
return 0, err
}
defer res.Body.Close()

var genesisDetail GenesisResponse
if err := json.NewDecoder(res.Body).Decode(&genesisDetail); err != nil {
return 0, err
}

return strconv.ParseUint(genesisDetail.Data.GenesisTime, 10, 64)
}

func getConfigValue(endpoint, key string, client *http.Client) (string, error) {
res, err := client.Get(endpoint)
if err != nil {
return "", err
}
defer res.Body.Close()

var spec GetSpecResponse
if err := json.NewDecoder(res.Body).Decode(&spec); err != nil {
return "", err
}

value, ok := spec.Data[key]
if !ok {
return "", fmt.Errorf("key %s not found in config spec", key)
}

return value, nil
}

func (c *BeaconClient) getBlobs(ctx context.Context, blockID uint64) (*BlobsResponse, error) {
url := fmt.Sprintf("%s/%s/%v", c.beaconURL, blobURL, blockID)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}

var responseData BlobsResponse
if err := json.Unmarshal(body, &responseData); err != nil {
return nil, err
}

return &responseData, nil
}

func (c *BeaconClient) timeToSlot(timestamp uint64) (uint64, error) {
if timestamp < c.genesisTime {
return 0, fmt.Errorf("provided timestamp (%v) precedes genesis time (%v)", timestamp, c.genesisTime)
}
return (timestamp - c.genesisTime) / c.secondsPerSlot, nil
}
47 changes: 16 additions & 31 deletions packages/blobstorage/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package indexer
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"sync"
"time"

Expand All @@ -27,18 +23,8 @@ import (
"github.com/taikoxyz/taiko-mono/packages/blobstorage/pkg/utils"
)

type Response struct {
Data []struct {
Index string `json:"index"`
Blob string `json:"blob"`
KzgCommitment string `json:"kzg_commitment"`
KzgCommitmentHex []byte `json:"-"`
} `json:"data"`
}

// Indexer struct holds the configuration and state for the Ethereum chain listener.
type Indexer struct {
beaconURL string
ethClient *ethclient.Client
startHeight *uint64
taikoL1 *taikol1.TaikoL1
Expand All @@ -48,6 +34,7 @@ type Indexer struct {
wg *sync.WaitGroup
ctx context.Context
latestIndexedBlockNumber uint64
beaconClient *BeaconClient
}

func (i *Indexer) InitFromCli(ctx context.Context, c *cli.Context) error {
Expand Down Expand Up @@ -81,16 +68,22 @@ func InitFromConfig(ctx context.Context, i *Indexer, cfg *Config) (err error) {
return err
}

l1BeaconClient, err := NewBeaconClient(cfg, utils.DefaultTimeout)
if err != nil {
return err
}

i.blobHashRepo = blobHashRepo
i.ethClient = client
i.beaconURL = cfg.BeaconURL
i.taikoL1 = taikoL1
i.startHeight = cfg.StartingBlockID
i.db = db
i.wg = &sync.WaitGroup{}
i.ctx = ctx
i.cfg = cfg

i.beaconClient = l1BeaconClient

return nil
}

Expand Down Expand Up @@ -291,31 +284,23 @@ func (i *Indexer) checkReorg(ctx context.Context, event *taikol1.TaikoL1BlockPro
}

func (i *Indexer) storeBlob(ctx context.Context, event *taikol1.TaikoL1BlockProposed) error {
slog.Info("blockProposed event found", "blockID", event.Meta.L1Height+1, "emittedIn", event.Raw.BlockNumber, "blobUsed", event.Meta.BlobUsed)

if !event.Meta.BlobUsed {
return nil
}

blockID := event.Meta.L1Height + 1
url := fmt.Sprintf("%s/%v", i.beaconURL, blockID)
response, err := http.Get(url)
blockID, err := i.beaconClient.timeToSlot(event.Meta.Timestamp)
if err != nil {
return err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
slog.Info("blockProposed event found", "blockID", blockID, "emittedIn", event.Raw.BlockNumber, "blobUsed", event.Meta.BlobUsed)

if !event.Meta.BlobUsed {
return nil
}

var responseData Response
if err := json.Unmarshal(body, &responseData); err != nil {
blobsResponse, err := i.beaconClient.getBlobs(ctx, blockID)
if err != nil {
return err
}

for _, data := range responseData.Data {
for _, data := range blobsResponse.Data {
data.KzgCommitmentHex = common.FromHex(data.KzgCommitment)

metaBlobHash := common.BytesToHash(event.Meta.BlobHash[:])
Expand Down
10 changes: 9 additions & 1 deletion packages/blobstorage/pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package utils

import "golang.org/x/exp/constraints"
import (
"time"

"golang.org/x/exp/constraints"
)

const (
DefaultTimeout = 1 * time.Minute
)

// Min return the minimum value of two integers.
func Min[T constraints.Integer](a, b T) T {
Expand Down
3 changes: 3 additions & 0 deletions packages/bridge-ui/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export PUBLIC_DEFAULT_SWAP_URL=https://
# Use the bridge guide Urls
export PUBLIC_GUIDE_URL=https://

# If it's a testnet, set a name here to show in the UI
export PUBLIC_TESTNET_NAME=""

# WalletConnect Project ID
export PUBLIC_WALLETCONNECT_PROJECT_ID=""

Expand Down
3 changes: 3 additions & 0 deletions packages/bridge-ui/scripts/exportJsonToEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const args = process.argv.slice(2);
const isLocal = args.includes('--local');
const isDev = args.includes('--dev');
const isProd = args.includes('--prod');
const isA7 = args.includes('--a7');
const isA6 = args.includes('--a6');
const isA5 = args.includes('--a5');

Expand All @@ -41,6 +42,8 @@ if (isA6) {
version = 'a6';
} else if (isA5) {
version = 'a5';
} else if (isA7) {
version = 'a7';
}

Logger.info(`Detected ${environment} environment and ${version} version.`);
Expand Down
Loading

0 comments on commit 6894d5d

Please sign in to comment.