mirror of
https://github.com/grassrootseconomics/cic-chain-events.git
synced 2024-11-25 08:36:46 +01:00
refactor: remove RPC fetcher support
This commit is contained in:
parent
f0b65a59ad
commit
749455bbdc
1
go.mod
1
go.mod
@ -7,7 +7,6 @@ require (
|
||||
github.com/alitto/pond v1.8.2
|
||||
github.com/celo-org/celo-blockchain v1.6.1
|
||||
github.com/goccy/go-json v0.10.0
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.1
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.1.0
|
||||
github.com/jackc/pgx/v5 v5.2.0
|
||||
github.com/knadh/goyesql/v2 v2.2.0
|
||||
|
2
go.sum
2
go.sum
@ -248,8 +248,6 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.1 h1:SzmMFrqxSIdgePqwbUdoS3PNP82MFnlOecycVk2ZYWg=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.1/go.mod h1:EiR6d03GYu6jlVKNL1MbTAw/bqAW2WP3J/lkrZxPMdU=
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.1.0 h1:0fev2hYkGEyFX2D4oUG8yy4jXhtHv7qUtLLboXL5ycw=
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.1.0/go.mod h1:JtkXc+yDUiQQJdhYTqddZI/itdYGHY7H8PNZzBo4hCk=
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||
|
@ -1,50 +0,0 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
)
|
||||
|
||||
func Benchmark_RPC(b *testing.B) {
|
||||
celoProvider, err := celo.NewProvider(celo.ProviderOpts{
|
||||
ChainId: celo.MainnetChainId,
|
||||
RpcEndpoint: rpcEndpoint,
|
||||
})
|
||||
|
||||
rpc := NewRPCFetcher(RPCOpts{
|
||||
RPCProvider: celoProvider,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
b.Run("RPC_Block_Fetcher_Benchmark", func(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, err := rpc.Block(context.Background(), 14974600)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
b.ReportAllocs()
|
||||
})
|
||||
}
|
||||
|
||||
func Benchmark_GraphQL(b *testing.B) {
|
||||
graphql := NewGraphqlFetcher(GraphqlOpts{
|
||||
GraphqlEndpoint: graphqlEndpoint,
|
||||
})
|
||||
|
||||
b.Run("GraphQL_Block_Fetcher_Benchmark", func(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, err := graphql.Block(context.Background(), 14974600)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
b.ReportAllocs()
|
||||
})
|
||||
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||
"github.com/celo-org/celo-blockchain/core/types"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/w3types"
|
||||
)
|
||||
|
||||
// RPCOpts reprsents the required paramters for an RPC fetcher.
|
||||
type RPCOpts struct {
|
||||
RPCProvider *celo.Provider
|
||||
}
|
||||
|
||||
// RPC is a RPC based block and transaction fetcher.
|
||||
type RPC struct {
|
||||
provider *celo.Provider
|
||||
}
|
||||
|
||||
// NewRPCFetcher returns a new RPC fetcher which implemnts Fetch.
|
||||
// Note: No rate limiting feeature.
|
||||
func NewRPCFetcher(o RPCOpts) Fetch {
|
||||
return &RPC{
|
||||
provider: o.RPCProvider,
|
||||
}
|
||||
}
|
||||
|
||||
// Block fetches via RPC and transforms the response to adapt to the GraphQL JSON response struct.
|
||||
func (f *RPC) Block(ctx context.Context, blockNumber uint64) (FetchResponse, error) {
|
||||
var (
|
||||
block types.Block
|
||||
fetchResponse FetchResponse
|
||||
)
|
||||
|
||||
if err := f.provider.Client.CallCtx(
|
||||
ctx,
|
||||
eth.BlockByNumber(big.NewInt(int64(blockNumber))).Returns(&block),
|
||||
); err != nil {
|
||||
return fetchResponse, err
|
||||
}
|
||||
|
||||
txCount := len(block.Transactions())
|
||||
batchCalls := make([]w3types.Caller, txCount*2)
|
||||
|
||||
txs := make([]types.Transaction, txCount)
|
||||
txsReceipt := make([]types.Receipt, txCount)
|
||||
|
||||
// Prepare batch calls.
|
||||
for i, tx := range block.Transactions() {
|
||||
batchCalls[i] = eth.Tx(tx.Hash()).Returns(&txs[i])
|
||||
batchCalls[txCount+i] = eth.TxReceipt(tx.Hash()).Returns(&txsReceipt[i])
|
||||
}
|
||||
|
||||
if err := f.provider.Client.CallCtx(
|
||||
ctx,
|
||||
batchCalls...,
|
||||
); err != nil {
|
||||
return fetchResponse, err
|
||||
}
|
||||
|
||||
// Transform response and adapt to FetchResponse.
|
||||
for i := 0; i < txCount; i++ {
|
||||
var txObject Transaction
|
||||
|
||||
txObject.Block.Number = block.NumberU64()
|
||||
txObject.Block.Timestamp = hexutil.EncodeUint64(block.Time())
|
||||
|
||||
from, err := types.Sender(types.LatestSignerForChainID(txs[i].ChainId()), &txs[i])
|
||||
if err != nil {
|
||||
return fetchResponse, err
|
||||
}
|
||||
txObject.From.Address = strings.ToLower(from.Hex())
|
||||
// This check ignores contract deployment transactions.
|
||||
if txs[i].To() != nil {
|
||||
txObject.To.Address = strings.ToLower(txs[i].To().Hex())
|
||||
}
|
||||
txObject.Value = hexutil.EncodeBig(txs[i].Value())
|
||||
txObject.InputData = hexutil.Encode(txs[i].Data())
|
||||
|
||||
txObject.Hash = txsReceipt[i].TxHash.Hex()
|
||||
txObject.Index = txsReceipt[i].TransactionIndex
|
||||
txObject.Status = txsReceipt[i].Status
|
||||
txObject.GasUsed = txsReceipt[i].GasUsed
|
||||
|
||||
fetchResponse.Data.Block.Transactions = append(
|
||||
fetchResponse.Data.Block.Transactions,
|
||||
txObject,
|
||||
)
|
||||
}
|
||||
|
||||
return fetchResponse, nil
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
var (
|
||||
rpcEndpoint = os.Getenv("TEST_RPC_ENDPOINT")
|
||||
)
|
||||
|
||||
type RPCTestSuite struct {
|
||||
suite.Suite
|
||||
fetch Fetch
|
||||
}
|
||||
|
||||
func (s *RPCTestSuite) SetupSuite() {
|
||||
celoProvider, err := celo.NewProvider(celo.ProviderOpts{
|
||||
ChainId: celo.MainnetChainId,
|
||||
RpcEndpoint: rpcEndpoint,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.fetch = NewRPCFetcher(RPCOpts{
|
||||
RPCProvider: celoProvider,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *RPCTestSuite) Test_E2E_Fetch_Existing_Block() {
|
||||
resp, err := s.fetch.Block(context.Background(), 14974600)
|
||||
s.NoError(err)
|
||||
s.Len(resp.Data.Block.Transactions, 3)
|
||||
}
|
||||
|
||||
func (s *RPCTestSuite) Test_E2E_Fetch_Non_Existing_Block() {
|
||||
_, err := s.fetch.Block(context.Background(), 14974600000)
|
||||
s.Error(err)
|
||||
}
|
||||
|
||||
func TestRPCSuite(t *testing.T) {
|
||||
suite.Run(t, new(RPCTestSuite))
|
||||
}
|
Loading…
Reference in New Issue
Block a user