Skip to content

Commit

Permalink
db2/BigTableEthRaw: add real condition test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tangui-Bitfly committed Oct 14, 2024
1 parent 1e8227f commit 2635a45
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
6 changes: 0 additions & 6 deletions backend/pkg/commons/db2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,6 @@ func (r *BigTableEthRaw) UncleByBlockNumberAndIndex(ctx context.Context, number
if err != nil {
return nil, err
}

// len of one uncle without tx is 1473
if len(block.Uncles) < 2000 {
return block.Uncles, nil
}
// we have two uncles so we need to retrieve the good index
var uncles []*jsonrpcMessage
_ = json.Unmarshal(block.Uncles, &uncles)
return json.Marshal(uncles[index])
Expand Down
68 changes: 68 additions & 0 deletions backend/pkg/commons/db2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"
"net/http"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -15,6 +16,73 @@ import (
"github.com/gobitfly/beaconchain/pkg/commons/db2/storetest"
)

func TestBigTableClientRealCondition(t *testing.T) {
project := os.Getenv("BIGTABLE_PROJECT")
instance := os.Getenv("BIGTABLE_INSTANCE")
if project == "" || instance == "" {
t.Skip("skipping test, set BIGTABLE_PROJECT and BIGTABLE_INSTANCE")
}

tests := []struct {
name string
chainID uint64
block int64
}{
{
name: "test block",
chainID: 1,
block: 6008149,
},
{
name: "test block 2",
chainID: 1,
block: 141,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bg, err := store.NewBigTable(project, instance, nil)
if err != nil {
t.Fatal(err)
}

rawStore := NewRawStore(store.Wrap(bg, BlocRawTable, ""))
rpcClient, err := rpc.DialOptions(context.Background(), "http://foo.bar", rpc.WithHTTPClient(&http.Client{
Transport: NewBigTableEthRaw(rawStore, tt.chainID),
}))
if err != nil {
t.Fatal(err)
}
ethClient := ethclient.NewClient(rpcClient)

block, err := ethClient.BlockByNumber(context.Background(), big.NewInt(tt.block))
if err != nil {
t.Fatalf("BlockByNumber() error = %v", err)
}
if got, want := block.Number().Int64(), tt.block; got != want {
t.Errorf("got %v, want %v", got, want)
}

receipts, err := ethClient.BlockReceipts(context.Background(), rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(tt.block)))
if err != nil {
t.Fatalf("BlockReceipts() error = %v", err)
}
if len(block.Transactions()) != 0 && len(receipts) == 0 {
t.Errorf("receipts should not be empty")
}

var traces []GethTraceCallResultWrapper
if err := rpcClient.Call(&traces, "debug_traceBlockByNumber", hexutil.EncodeBig(block.Number()), gethTracerArg); err != nil {
t.Fatalf("debug_traceBlockByNumber() error = %v", err)
}
if len(block.Transactions()) != 0 && len(traces) == 0 {
t.Errorf("traces should not be empty")
}
})
}
}

func TestBigTableClient(t *testing.T) {
tests := []struct {
name string
Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/commons/db2/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func (gzipCompressor) compress(src []byte) ([]byte, error) {
}

func (gzipCompressor) decompress(src []byte) ([]byte, error) {
if len(src) == 0 {
return nil, nil
}
zr, err := gzip.NewReader(bytes.NewReader(src))
if err != nil {
return nil, fmt.Errorf("gzip cannot create reader: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions backend/pkg/commons/db2/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ const (
}
]
}`
testUncles = `{
testUncles = `[{
"jsonrpc": "2.0",
"id": 1,
"result": {
Expand All @@ -339,7 +339,7 @@ const (
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"uncles": []
}
}`
}]`

testTwoUnclesBlockNumber = 141
testTwoUnclesBlock = `{
Expand Down

0 comments on commit 2635a45

Please sign in to comment.