From e0246a9e812ae6615fcfd8a2060b90ed924af564 Mon Sep 17 00:00:00 2001 From: Guilherme Ferreira Date: Fri, 24 Nov 2023 13:25:46 -0300 Subject: [PATCH] use interface to stub rpc client responses --- core/core.go | 5 ++-- core/slice.go | 5 ++-- eth/backend.go | 3 +-- quaiclient/ethclient/ethclient_test.go | 9 ++++--- quaiclient/quaiclient.go | 36 +++++++++++++++++++++++--- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/core/core.go b/core/core.go index f5f707e2f4..d2e6602716 100644 --- a/core/core.go +++ b/core/core.go @@ -24,7 +24,6 @@ import ( "github.com/dominant-strategies/go-quai/log" "github.com/dominant-strategies/go-quai/params" "github.com/dominant-strategies/go-quai/rlp" - "github.com/dominant-strategies/go-quai/rpc" "github.com/dominant-strategies/go-quai/trie" lru "github.com/hnlq715/golang-lru" ) @@ -102,8 +101,8 @@ func NewCore(db ethdb.Database, config *Config, isLocalBlock func(block *types.H } // Used on unit testing -func NewFakeCore(db ethdb.Database, config *Config, isLocalBlock func(block *types.Header) bool, txConfig *TxPoolConfig, txLookupLimit *uint64, chainConfig *params.ChainConfig, slicesRunning []common.Location, domClientUrl string, subClientUrls []string, engine consensus.Engine, cacheConfig *CacheConfig, vmConfig vm.Config, genesis *Genesis, client *rpc.Client) (*Core, error) { - slice, err := NewFakeSlice(db, config, txConfig, txLookupLimit, isLocalBlock, chainConfig, slicesRunning, domClientUrl, subClientUrls, engine, cacheConfig, vmConfig, genesis, client) +func NewFakeCore(db ethdb.Database, config *Config, isLocalBlock func(block *types.Header) bool, txConfig *TxPoolConfig, txLookupLimit *uint64, chainConfig *params.ChainConfig, slicesRunning []common.Location, domClientUrl string, subClientUrls []string, engine consensus.Engine, cacheConfig *CacheConfig, vmConfig vm.Config, genesis *Genesis) (*Core, error) { + slice, err := NewFakeSlice(db, config, txConfig, txLookupLimit, isLocalBlock, chainConfig, slicesRunning, domClientUrl, subClientUrls, engine, cacheConfig, vmConfig, genesis) if err != nil { return nil, err } diff --git a/core/slice.go b/core/slice.go index 130a417d3e..ce3519a8f9 100644 --- a/core/slice.go +++ b/core/slice.go @@ -21,7 +21,6 @@ import ( "github.com/dominant-strategies/go-quai/log" "github.com/dominant-strategies/go-quai/params" "github.com/dominant-strategies/go-quai/quaiclient" - "github.com/dominant-strategies/go-quai/rpc" "github.com/dominant-strategies/go-quai/trie" lru "github.com/hashicorp/golang-lru" ) @@ -142,7 +141,7 @@ func NewSlice(db ethdb.Database, config *Config, txConfig *TxPoolConfig, txLooku return sl, nil } -func NewFakeSlice(db ethdb.Database, config *Config, txConfig *TxPoolConfig, txLookupLimit *uint64, isLocalBlock func(block *types.Header) bool, chainConfig *params.ChainConfig, slicesRunning []common.Location, domClientUrl string, subClientUrls []string, engine consensus.Engine, cacheConfig *CacheConfig, vmConfig vm.Config, genesis *Genesis, client *rpc.Client) (*Slice, error) { +func NewFakeSlice(db ethdb.Database, config *Config, txConfig *TxPoolConfig, txLookupLimit *uint64, isLocalBlock func(block *types.Header) bool, chainConfig *params.ChainConfig, slicesRunning []common.Location, domClientUrl string, subClientUrls []string, engine consensus.Engine, cacheConfig *CacheConfig, vmConfig vm.Config, genesis *Genesis) (*Slice, error) { nodeCtx := common.NodeLocation.Context() sl := &Slice{ config: chainConfig, @@ -179,7 +178,7 @@ func NewFakeSlice(db ethdb.Database, config *Config, txConfig *TxPoolConfig, txL // only set domClient if the chain is not Prime. if nodeCtx != common.PRIME_CTX { go func () { - sl.domClient = quaiclient.NewClient(client) + sl.domClient = quaiclient.NewClient(&quaiclient.TestRpcClient{}) }() } diff --git a/eth/backend.go b/eth/backend.go index af13228860..9cf8d5fd45 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -347,8 +347,7 @@ func NewFake(stack *node.Node, config *ethconfig.Config, chainDb ethdb.Database) } var err error - client, _ := stack.Attach() - eth.core, err = core.NewFakeCore(chainDb, &config.Miner, eth.isLocalBlock, &config.TxPool, &config.TxLookupLimit, chainConfig, eth.config.SlicesRunning, eth.config.DomUrl, eth.config.SubUrls, eth.engine, cacheConfig, vmConfig, config.Genesis, client) + eth.core, err = core.NewFakeCore(chainDb, &config.Miner, eth.isLocalBlock, &config.TxPool, &config.TxLookupLimit, chainConfig, eth.config.SlicesRunning, eth.config.DomUrl, eth.config.SubUrls, eth.engine, cacheConfig, vmConfig, config.Genesis) if err != nil { return nil, err } diff --git a/quaiclient/ethclient/ethclient_test.go b/quaiclient/ethclient/ethclient_test.go index 9bd73d5f69..fe02a2c193 100644 --- a/quaiclient/ethclient/ethclient_test.go +++ b/quaiclient/ethclient/ethclient_test.go @@ -188,7 +188,7 @@ var ( testBalance = big.NewInt(2e15) ) -func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { +func newTestBackend(t *testing.T) (*node.Node, []*types.Block, *rpc.Client) { // Set location to ZONE_CTX common.NodeLocation = common.Location{0, 0} // Generate test chain. @@ -206,6 +206,8 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { config.Progpow.PowMode = progpow.ModeFake config.DomUrl = "http://localhost:8080" + client, _ := n.Attach() + ethservice, err := eth.NewFake(n, config, db) if err != nil { t.Fatalf("can't create new quai service: %v", err) @@ -218,7 +220,7 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { if _, err := ethservice.Core().InsertChain(blocks[1:]); err != nil { t.Fatalf("can't import test blocks: %v", err) } - return n, blocks + return n, blocks, client } func generateTestChain(db ethdb.Database) (*core.Genesis, []*types.Block) { @@ -248,8 +250,7 @@ func generateTestChain(db ethdb.Database) (*core.Genesis, []*types.Block) { } func TestEthClient(t *testing.T) { - backend, chain := newTestBackend(t) - client, _ := backend.Attach() + backend, chain, client := newTestBackend(t) defer backend.Close() defer client.Close() diff --git a/quaiclient/quaiclient.go b/quaiclient/quaiclient.go index f6cc8d8b08..9476c5b5d1 100644 --- a/quaiclient/quaiclient.go +++ b/quaiclient/quaiclient.go @@ -20,6 +20,7 @@ package quaiclient import ( "context" "encoding/json" + "fmt" "math/big" "time" @@ -31,10 +32,39 @@ import ( var exponentialBackoffCeilingSecs int64 = 60 // 1 minute +type IClient interface { + CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error + Close() +} + +// Used on unit tests +type TestRpcClient struct { + +} + +func (trc *TestRpcClient) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error { + if method == "quai_updateDom" { + println("UpdateDom called") + return nil + } + if method == "eth_getBlockByNumber" { + return nil + } + if method == "quai_sendPendingEtxsToDom" { + println("SendPendingEtxsToDom called") + return nil + } + return fmt.Errorf("method %s is not implemented", method) +} + +func (trc *TestRpcClient) Close() { + println("Close called") +} + // Client defines typed wrappers for the Quai RPC API. type Client struct { - c *rpc.Client -} + c IClient + } // Dial connects a client to the given URL. func Dial(rawurl string) (*Client, error) { @@ -71,7 +101,7 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) { } // NewClient creates a client that uses the given RPC client. -func NewClient(c *rpc.Client) *Client { +func NewClient(c IClient) *Client { return &Client{c} }