Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Jun 21, 2024
1 parent 367d549 commit 6cac84e
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 64 deletions.
7 changes: 4 additions & 3 deletions client/blob_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/hex"
"fmt"

"github.com/ethereum/go-ethereum/common/hexutil"

"github.com/bnb-chain/blob-hub/models"
blobproto "github.com/bnb-chain/blob-hub/proto"
"github.com/bnb-chain/blob-hub/service"
"github.com/bnb-chain/blob-hub/types"
"github.com/bnb-chain/blob-hub/util"
"github.com/ethereum/go-ethereum/common/hexutil"
)

type BlobServer struct {
Expand Down Expand Up @@ -53,11 +54,11 @@ func (s *BlobServer) GetBlobSidecars(ctx context.Context, req *blobproto.GetBlob
return nil, err
}
} else {
slot, err := util.StringToUint64(blockID)
blockNumOrSlot, err := util.StringToUint64(blockID)
if err != nil {
return nil, err
}
sidecars, err = service.BlobSvc.GetBlobSidecarsBySlot(slot, indicesInx)
sidecars, err = service.BlobSvc.GetBlobSidecarsByBlockNumOrSlot(blockNumOrSlot, indicesInx)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion cmd/blob-hub-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func main() {
if configFilePath == "" {
configFilePath = os.Getenv(config.EnvVarConfigFilePath)
}
//configFilePath = "config/local/bsc-config-syncer.json"
cfg = config.ParseSyncerConfigFromFile(configFilePath)
if cfg == nil {
panic("failed to get configuration")
Expand Down
11 changes: 9 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ func (s *SyncerConfig) Validate() {
if len(s.PrivateKey) == 0 {
panic("private key is not provided")
}
if s.CreateBundleSlotOrBlockInterval > 100 {
panic("create_bundle_slot_interval is supposed less than 100")
if s.Chain == BSC && s.CreateBundleSlotOrBlockInterval > 200 {
panic("create_bundle_slot_interval is supposed to be less than 100")
}
if s.Chain == ETH && s.CreateBundleSlotOrBlockInterval > 30 {
panic("create_bundle_slot_interval is supposed to be less than 30")
}
if s.BundleNotSealedReuploadThreshold <= 60 {
panic("Bundle_not_sealed_reupload_threshold is supposed larger than 60 (s)")
Expand All @@ -87,13 +90,17 @@ func (s *SyncerConfig) GetReUploadBundleThresh() int64 {
}

type ServerConfig struct {
Chain string `json:"chain"`
BucketName string `json:"bucket_name"`
BundleServiceEndpoints []string `json:"bundle_service_endpoints"` // BundleServiceEndpoints is a list of bundle service address
CacheConfig CacheConfig `json:"cache_config"`
DBConfig DBConfig `json:"db_config"`
}

func (s *ServerConfig) Validate() {
if !strings.EqualFold(s.Chain, ETH) && !strings.EqualFold(s.Chain, BSC) {
panic("chain not support")
}
if len(s.BucketName) == 0 {
panic("the Greenfield bucket name is not is not provided")
}
Expand Down
5 changes: 3 additions & 2 deletions external/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"math/big"
"strconv"

"github.com/bnb-chain/blob-hub/config"
"github.com/bnb-chain/blob-hub/external/eth"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"

"github.com/bnb-chain/blob-hub/config"
"github.com/bnb-chain/blob-hub/external/eth"
)

const BSCBlockConfirmNum = 3
Expand Down
2 changes: 1 addition & 1 deletion models/sidecar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions restapi/configure_blob_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ import (
"net/http"
"os"

"google.golang.org/grpc"

"github.com/bnb-chain/blob-hub/client"
"github.com/bnb-chain/blob-hub/external/cmn"
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
"github.com/go-openapi/swag"
grpcruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"

"github.com/bnb-chain/blob-hub/cache"
"github.com/bnb-chain/blob-hub/client"
"github.com/bnb-chain/blob-hub/config"
syncerdb "github.com/bnb-chain/blob-hub/db"
"github.com/bnb-chain/blob-hub/external/cmn"
blobproto "github.com/bnb-chain/blob-hub/proto"
"github.com/bnb-chain/blob-hub/restapi/handlers"
"github.com/bnb-chain/blob-hub/restapi/operations"
"github.com/bnb-chain/blob-hub/restapi/operations/blob"
"github.com/bnb-chain/blob-hub/service"

grpcruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
)

//go:generate swagger generate server --target ../../blob-syncer --name BlobHub --spec ../swagger.yaml --principal interface{}
Expand Down
6 changes: 4 additions & 2 deletions restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions restapi/handlers/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"encoding/hex"
"fmt"

"github.com/bnb-chain/blob-hub/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/go-openapi/runtime/middleware"

"github.com/bnb-chain/blob-hub/models"
"github.com/bnb-chain/blob-hub/restapi/operations/blob"
"github.com/bnb-chain/blob-hub/service"
"github.com/bnb-chain/blob-hub/types"
"github.com/bnb-chain/blob-hub/util"
)

Expand Down Expand Up @@ -52,7 +52,7 @@ func HandleGetBlobSidecars() func(params blob.GetBlobSidecarsByBlockNumParams) m
if err != nil {
return blob.NewGetBlobSidecarsByBlockNumBadRequest().WithPayload(service.BadRequestWithError(err))
}
sidecars, err = service.BlobSvc.GetBlobSidecarsBySlot(slot, indicesInx)
sidecars, err = service.BlobSvc.GetBlobSidecarsByBlockNumOrSlot(slot, indicesInx)
if err != nil {
return blob.NewGetBlobSidecarsByBlockNumInternalServerError().WithPayload(service.InternalErrorWithError(err))
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PRIVATE_KEY=...
BUCKET_NAME=bsc-testnet-blobs
GRANTEE_BUNDLE_ACCOUNT=0x4605BFc98E0a5EA63D9D5a4a1Df549732a6963f3
BUCKET_NAME=...
GRANTEE_BUNDLE_ACCOUNT=...
GREENFIELD_RPC=https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org:443
GREENFIELD_CHAIN_ID=greenfield_5600-1
ALLOWANCE=10000000000000000000
SP_ADDRESS=0x5fff5a6c94b182fb965b40c7b9f30199b969ed2f
SP_ADDRESS=...
45 changes: 24 additions & 21 deletions service/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ const prefixHex = "0x"

type Blob interface {
GetBlobSidecarsByRoot(root string, indices []int64) ([]*models.Sidecar, error)
GetBlobSidecarsBySlot(slot uint64, indices []int64) ([]*models.Sidecar, error)
GetBlobSidecarsByBlockNumOrSlot(slot uint64, indices []int64) ([]*models.Sidecar, error)
}

type BlobService struct {
blobDB db.BlobDao
bundleClient *cmn.BundleClient
cacheService cache.Cache
config *config.ServerConfig
cfg *config.ServerConfig
}

func NewBlobService(blobDB db.BlobDao, bundleClient *cmn.BundleClient, cache cache.Cache, config *config.ServerConfig) Blob {
return &BlobService{
blobDB: blobDB,
bundleClient: bundleClient,
cacheService: cache,
config: config,
cfg: config,
}
}

func (b BlobService) GetBlobSidecarsBySlot(slot uint64, indices []int64) ([]*models.Sidecar, error) {
func (b BlobService) GetBlobSidecarsByBlockNumOrSlot(blockNumOrSlot uint64, indices []int64) ([]*models.Sidecar, error) {
var err error
blobs, found := b.cacheService.Get(util.Uint64ToString(slot))
blobs, found := b.cacheService.Get(util.Uint64ToString(blockNumOrSlot))
if found {
blobsFound := blobs.([]*models.Sidecar)
if len(indices) != 0 {
blobReturn := make([]*models.Sidecar, 0)
for _, idx := range indices {
if int(idx) >= len(blobsFound) {
return nil, fmt.Errorf("index %d out of bound, only %d blob at slot %d", idx, len(blobsFound), slot)
return nil, fmt.Errorf("index %d out of bound, only %d blob at block %d", idx, len(blobsFound), blockNumOrSlot)
}
blobReturn = append(blobReturn, blobsFound[idx])
}
Expand All @@ -52,39 +52,42 @@ func (b BlobService) GetBlobSidecarsBySlot(slot uint64, indices []int64) ([]*mod
return blobsFound, nil
}

block, err := b.blobDB.GetBlock(slot)
block, err := b.blobDB.GetBlock(blockNumOrSlot)
if err != nil {
return nil, err
}

var blobMetas []*db.Blob
if len(indices) == 0 {
blobMetas, err = b.blobDB.GetBlobByBlockID(slot)
blobMetas, err = b.blobDB.GetBlobByBlockID(blockNumOrSlot)
if err != nil {
return nil, err
}
} else {
blobMetas, err = b.blobDB.GetBlobByBlockIDAndIndices(slot, indices)
blobMetas, err = b.blobDB.GetBlobByBlockIDAndIndices(blockNumOrSlot, indices)
if err != nil {
return nil, err
}
}

sideCars := make([]*models.Sidecar, 0)
for _, meta := range blobMetas {
bundleObject, err := b.bundleClient.GetObject(b.config.BucketName, block.BundleName, meta.Name)
bundleObject, err := b.bundleClient.GetObject(b.cfg.BucketName, block.BundleName, meta.Name)
if err != nil {
return nil, err
}
header := &models.SidecarSignedBlockHeader{
Message: &models.SidecarSignedBlockHeaderMessage{
BodyRoot: fmt.Sprintf("%s%s", prefixHex, block.BodyRoot),
ParentRoot: fmt.Sprintf("%s%s", prefixHex, block.ParentRoot),
StateRoot: fmt.Sprintf("%s%s", prefixHex, block.StateRoot),
ProposerIndex: util.Uint64ToString(block.ProposerIndex),
Slot: util.Uint64ToString(block.Slot),
},
Signature: fmt.Sprintf("%s%s", prefixHex, block.Signature),
var header *models.SidecarSignedBlockHeader
if b.cfg.Chain == config.ETH {
header = &models.SidecarSignedBlockHeader{
Message: &models.SidecarSignedBlockHeaderMessage{
BodyRoot: fmt.Sprintf("%s%s", prefixHex, block.BodyRoot),
ParentRoot: fmt.Sprintf("%s%s", prefixHex, block.ParentRoot),
StateRoot: fmt.Sprintf("%s%s", prefixHex, block.StateRoot),
ProposerIndex: util.Uint64ToString(block.ProposerIndex),
Slot: util.Uint64ToString(block.Slot),
},
Signature: fmt.Sprintf("%s%s", prefixHex, block.Signature),
}
}
sideCars = append(sideCars,
&models.Sidecar{
Expand All @@ -99,7 +102,7 @@ func (b BlobService) GetBlobSidecarsBySlot(slot uint64, indices []int64) ([]*mod

// cache all blobs at a specified slot
if len(indices) == 0 {
b.cacheService.Set(util.Uint64ToString(slot), sideCars)
b.cacheService.Set(util.Uint64ToString(blockNumOrSlot), sideCars)
}
return sideCars, nil
}
Expand All @@ -109,5 +112,5 @@ func (b BlobService) GetBlobSidecarsByRoot(root string, indices []int64) ([]*mod
if err != nil {
return nil, err
}
return b.GetBlobSidecarsBySlot(block.Slot, indices)
return b.GetBlobSidecarsByBlockNumOrSlot(block.Slot, indices)
}
1 change: 1 addition & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ definitions:
type: string
kzg_commitment_inclusion_proof:
type: array
x-omitempty: true
items:
type: string
signed_block_header:
Expand Down
12 changes: 6 additions & 6 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ import (
"strings"
"time"

"gorm.io/gorm"

"github.com/bnb-chain/blob-hub/external"
"github.com/bnb-chain/blob-hub/external/cmn"
"github.com/bnb-chain/blob-hub/external/eth"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
v1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
"gorm.io/gorm"

"github.com/bnb-chain/blob-hub/config"
"github.com/bnb-chain/blob-hub/db"
"github.com/bnb-chain/blob-hub/external"
"github.com/bnb-chain/blob-hub/external/cmn"
"github.com/bnb-chain/blob-hub/external/eth"
"github.com/bnb-chain/blob-hub/logging"
"github.com/bnb-chain/blob-hub/metrics"
"github.com/bnb-chain/blob-hub/types"
"github.com/bnb-chain/blob-hub/util"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

const (
Expand Down Expand Up @@ -463,6 +462,7 @@ func (s *BlobSyncer) toBlockAndBlobs(blockResp *structs.GetBlockV2Response, side
logging.Logger.Errorf("failed to get header, slot=%d, err=%s", blockNumOrSlot, err.Error())
return nil, nil, err
}

rootBz, err := hexutil.Decode(header.Data.Root)
if err != nil {
logging.Logger.Errorf("failed to decode header.Data.Root=%s, err=%s", header.Data.Root, err.Error())
Expand Down
Loading

0 comments on commit 6cac84e

Please sign in to comment.