Skip to content

Commit

Permalink
use interface to stub rpc client responses
Browse files Browse the repository at this point in the history
  • Loading branch information
didaunesp committed Dec 11, 2023
1 parent c6b1e6f commit faceed1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
5 changes: 2 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{})
}()
}

Expand Down
3 changes: 1 addition & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions quaiclient/ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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()

Expand Down
36 changes: 33 additions & 3 deletions quaiclient/quaiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package quaiclient
import (
"context"
"encoding/json"
"fmt"
"math/big"
"time"

Expand All @@ -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) {
Expand Down Expand Up @@ -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}
}

Expand Down

0 comments on commit faceed1

Please sign in to comment.