Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Tangui-Bitfly committed Nov 6, 2024
1 parent 21ce344 commit 14f473d
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 296 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package database

import (
"context"
Expand All @@ -18,65 +18,65 @@ const (
)

type TableWrapper struct {
*BigTableStore
*BigTable
table string
family string
}

func Wrap(db *BigTableStore, table string, family string) TableWrapper {
func Wrap(db *BigTable, table string, family string) TableWrapper {
return TableWrapper{
BigTableStore: db,
table: table,
family: family,
BigTable: db,
table: table,
family: family,
}
}

func (w TableWrapper) Add(key, column string, data []byte, allowDuplicate bool) error {
return w.BigTableStore.Add(w.table, w.family, key, column, data, allowDuplicate)
return w.BigTable.Add(w.table, w.family, key, column, data, allowDuplicate)
}

func (w TableWrapper) Read(prefix string) ([][]byte, error) {
return w.BigTableStore.Read(w.table, w.family, prefix)
return w.BigTable.Read(w.table, w.family, prefix)
}

func (w TableWrapper) GetLatestValue(key string) ([]byte, error) {
return w.BigTableStore.GetLatestValue(w.table, w.family, key)
return w.BigTable.GetLatestValue(w.table, w.family, key)
}

func (w TableWrapper) GetRow(key string) (map[string][]byte, error) {
return w.BigTableStore.GetRow(w.table, key)
return w.BigTable.GetRow(w.table, key)
}

func (w TableWrapper) GetRowKeys(prefix string) ([]string, error) {
return w.BigTableStore.GetRowKeys(w.table, prefix)
return w.BigTable.GetRowKeys(w.table, prefix)
}

func (w TableWrapper) BulkAdd(itemsByKey map[string][]Item) error {
return w.BigTableStore.BulkAdd(w.table, itemsByKey)
return w.BigTable.BulkAdd(w.table, itemsByKey)
}

func (w TableWrapper) GetRowsRange(high, low string) ([]Row, error) {
return w.BigTableStore.GetRowsRange(w.table, high, low)
return w.BigTable.GetRowsRange(w.table, high, low)
}

// BigTableStore is a wrapper around Google Cloud Bigtable for storing and retrieving data
type BigTableStore struct {
// BigTable is a wrapper around Google Cloud Bigtable for storing and retrieving data
type BigTable struct {
client *bigtable.Client
admin *bigtable.AdminClient
}

func NewBigTableWithClient(ctx context.Context, client *bigtable.Client, adminClient *bigtable.AdminClient, tablesAndFamilies map[string][]string) (*BigTableStore, error) {
func NewBigTableWithClient(ctx context.Context, client *bigtable.Client, adminClient *bigtable.AdminClient, tablesAndFamilies map[string][]string) (*BigTable, error) {
// Initialize the Bigtable table and column family
if err := initTable(ctx, adminClient, tablesAndFamilies); err != nil {
return nil, err
}

return &BigTableStore{client: client, admin: adminClient}, nil
return &BigTable{client: client, admin: adminClient}, nil
}

// NewBigTable initializes a new BigTableStore
// It returns a BigTableStore and an error if any part of the setup fails
func NewBigTable(project, instance string, tablesAndFamilies map[string][]string) (*BigTableStore, error) {
// NewBigTable initializes a new BigTable
// It returns a BigTable and an error if any part of the setup fails
func NewBigTable(project, instance string, tablesAndFamilies map[string][]string) (*BigTable, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand Down Expand Up @@ -142,7 +142,7 @@ type Item struct {
Data []byte
}

func (b BigTableStore) BulkAdd(table string, itemsByKey map[string][]Item) error {
func (b BigTable) BulkAdd(table string, itemsByKey map[string][]Item) error {
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand Down Expand Up @@ -171,7 +171,7 @@ func (b BigTableStore) BulkAdd(table string, itemsByKey map[string][]Item) error
// Add inserts a new row with the given key, column, and data into the Bigtable
// It applies a mutation that stores data in the receiver column family
// It returns error if the operation fails
func (b BigTableStore) Add(table, family string, key string, column string, data []byte, allowDuplicate bool) error {
func (b BigTable) Add(table, family string, key string, column string, data []byte, allowDuplicate bool) error {
// Open the transfer table for data operations
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -193,7 +193,7 @@ func (b BigTableStore) Add(table, family string, key string, column string, data

// Read retrieves all rows from the Bigtable's receiver column family
// It returns the data in the form of a 2D byte slice and an error if the operation fails
func (b BigTableStore) Read(table, family, prefix string) ([][]byte, error) {
func (b BigTable) Read(table, family, prefix string) ([][]byte, error) {
// Open the transfer table for reading
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -215,7 +215,7 @@ func (b BigTableStore) Read(table, family, prefix string) ([][]byte, error) {
return data, nil
}

func (b BigTableStore) GetLatestValue(table, family, key string) ([]byte, error) {
func (b BigTable) GetLatestValue(table, family, key string) ([]byte, error) {
// Open the transfer table for reading
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -234,7 +234,7 @@ func (b BigTableStore) GetLatestValue(table, family, key string) ([]byte, error)
return data, nil
}

func (b BigTableStore) GetRow(table, key string) (map[string][]byte, error) {
func (b BigTable) GetRow(table, key string) (map[string][]byte, error) {
// Open the transfer table for reading
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down Expand Up @@ -265,7 +265,7 @@ type Row struct {
Values map[string][]byte
}

func (b BigTableStore) GetRowsRange(table, high, low string) ([]Row, error) {
func (b BigTable) GetRowsRange(table, high, low string) ([]Row, error) {
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand Down Expand Up @@ -296,7 +296,7 @@ func (b BigTableStore) GetRowsRange(table, high, low string) ([]Row, error) {
return data, nil
}

func (b BigTableStore) GetRowKeys(table, prefix string) ([]string, error) {
func (b BigTable) GetRowKeys(table, prefix string) ([]string, error) {
// Open the transfer table for reading
tbl := b.client.Open(table)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -316,7 +316,7 @@ func (b BigTableStore) GetRowKeys(table, prefix string) ([]string, error) {
return data, nil
}

func (b BigTableStore) Clear() error {
func (b BigTable) Clear() error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand All @@ -332,9 +332,9 @@ func (b BigTableStore) Clear() error {
return nil
}

// Close shuts down the BigTableStore by closing the Bigtable client connection
// Close shuts down the BigTable by closing the Bigtable client connection
// It returns an error if the operation fails
func (b BigTableStore) Close() error {
func (b BigTable) Close() error {
if err := b.client.Close(); err != nil && status.Code(err) != codes.Canceled {
return fmt.Errorf("could not close client: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package store
package database

import (
"context"
"slices"
"strings"
"testing"

"github.com/gobitfly/beaconchain/pkg/commons/db2/storetest"
"github.com/gobitfly/beaconchain/pkg/commons/db2/databasetest"
)

func TestBigTableStore(t *testing.T) {
func TestBigTable(t *testing.T) {
type item struct {
key string
column string
Expand Down Expand Up @@ -76,12 +76,12 @@ func TestBigTableStore(t *testing.T) {
},
}
tables := map[string][]string{"testTable": {"testFamily"}}
client, admin := storetest.NewBigTable(t)
store, err := NewBigTableWithClient(context.Background(), client, admin, tables)
client, admin := databasetest.NewBigTable(t)
bt, err := NewBigTableWithClient(context.Background(), client, admin, tables)
if err != nil {
t.Fatal(err)
}
db := Wrap(store, "testTable", "testFamily")
db := Wrap(bt, "testTable", "testFamily")

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -172,12 +172,12 @@ func TestBigTableStore(t *testing.T) {

func TestRangeIncludeLimits(t *testing.T) {
tables := map[string][]string{"testTable": {"testFamily"}}
client, admin := storetest.NewBigTable(t)
store, err := NewBigTableWithClient(context.Background(), client, admin, tables)
client, admin := databasetest.NewBigTable(t)
bt, err := NewBigTableWithClient(context.Background(), client, admin, tables)
if err != nil {
t.Fatal(err)
}
db := Wrap(store, "testTable", "testFamily")
db := Wrap(bt, "testTable", "testFamily")

_ = db.Add("1:999999999999", "", []byte("0"), false)
_ = db.Add("1:999999999998", "", []byte("1"), false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package database

import (
"bytes"
Expand All @@ -14,11 +14,11 @@ const (
)

type RemoteServer struct {
store Store
db Database
}

func NewRemoteStore(store Store) RemoteServer {
return RemoteServer{store: store}
func NewRemote(db Database) RemoteServer {
return RemoteServer{db: db}
}

func (api RemoteServer) Routes() http.Handler {
Expand All @@ -42,7 +42,7 @@ func (api RemoteServer) GetRowsRange(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(err.Error()))
return
}
rows, err := api.store.GetRowsRange(args.High, args.Low)
rows, err := api.db.GetRowsRange(args.High, args.Low)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
Expand All @@ -69,7 +69,7 @@ func (api RemoteServer) GetRow(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(err.Error()))
return
}
row, err := api.store.GetRow(args.Key)
row, err := api.db.GetRow(args.Key)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package store
package database

type Store interface {
type Database interface {
Add(key, column string, data []byte, allowDuplicate bool) error
BulkAdd(itemsByKey map[string][]Item) error
Read(prefix string) ([][]byte, error)
Expand All @@ -13,6 +13,6 @@ type Store interface {
}

var (
_ Store = (*TableWrapper)(nil)
_ Store = (*RemoteClient)(nil)
_ Database = (*TableWrapper)(nil)
_ Database = (*RemoteClient)(nil)
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package storetest
package databasetest

import (
"context"
Expand Down
20 changes: 20 additions & 0 deletions backend/pkg/commons/db2/jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jsonrpc

import (
"encoding/json"
)

type Message struct {
Version string `json:"jsonrpc,omitempty"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Error *Error `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
}

type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
11 changes: 7 additions & 4 deletions backend/pkg/commons/db2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/gobitfly/beaconchain/pkg/commons/db2/jsonrpc"
"github.com/gobitfly/beaconchain/pkg/commons/db2/raw"
)

type GethTrace struct {
Expand All @@ -31,13 +34,13 @@ type GethTraceCall struct {
Calls []*GethTraceCall
}

var EthParse = func(rawBlock *FullBlockRawData) (*types.Block, []*types.Receipt, []*GethTrace, error) {
var blockResp, receiptsResp, tracesResp jsonrpcMessage
var EthParse = func(rawBlock *raw.FullBlockData) (*types.Block, []*types.Receipt, []*GethTrace, error) {
var blockResp, receiptsResp, tracesResp jsonrpc.Message
_ = json.Unmarshal(rawBlock.Receipts, &receiptsResp)
_ = json.Unmarshal(rawBlock.Block, &blockResp)
_ = json.Unmarshal(rawBlock.Traces, &tracesResp)

var unclesResp []jsonrpcMessage
var unclesResp []jsonrpc.Message
_ = json.Unmarshal(rawBlock.Uncles, &unclesResp)

block, err := parseEthBlock(blockResp.Result, unclesResp)
Expand Down Expand Up @@ -98,7 +101,7 @@ type txExtraInfo struct {
// parseEthBlock is a copy of ethclient.Client.getBlock
// modified to work the with raw db
// https://github.com/ethereum/go-ethereum/blob/v1.14.11/ethclient/ethclient.go#L129
func parseEthBlock(raw json.RawMessage, rawUncles []jsonrpcMessage) (*types.Block, error) {
func parseEthBlock(raw json.RawMessage, rawUncles []jsonrpc.Message) (*types.Block, error) {
// Decode header and transactions.
var head *types.Header
if err := json.Unmarshal(raw, &head); err != nil {
Expand Down
Loading

0 comments on commit 14f473d

Please sign in to comment.