2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
package fakes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-02 12:31:26 +00:00
|
|
|
"errors"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2018-09-18 19:46:16 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-07-02 12:31:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2018-09-18 19:46:16 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
2018-07-20 16:37:46 +00:00
|
|
|
)
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
type MockRPCClient struct {
|
2019-07-08 22:52:06 +00:00
|
|
|
callContextErr error
|
|
|
|
ipcPath string
|
|
|
|
nodeType core.NodeType
|
|
|
|
passedContext context.Context
|
|
|
|
passedMethod string
|
|
|
|
passedResult interface{}
|
|
|
|
passedBatch []client.BatchElem
|
|
|
|
passedNamespace string
|
|
|
|
passedPayloadChan chan statediff.Payload
|
2019-07-02 12:31:26 +00:00
|
|
|
passedSubscribeArgs []interface{}
|
2019-07-08 22:52:06 +00:00
|
|
|
lengthOfBatch int
|
|
|
|
returnPOAHeader core.POAHeader
|
|
|
|
returnPOAHeaders []core.POAHeader
|
|
|
|
returnPOWHeaders []*types.Header
|
|
|
|
supportedModules map[string]string
|
2019-07-02 12:31:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) Subscribe(namespace string, payloadChan interface{}, args ...interface{}) (*rpc.ClientSubscription, error) {
|
2019-07-02 12:31:26 +00:00
|
|
|
client.passedNamespace = namespace
|
|
|
|
|
|
|
|
passedPayloadChan, ok := payloadChan.(chan statediff.Payload)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("passed in channel is not of the correct type")
|
|
|
|
}
|
|
|
|
client.passedPayloadChan = passedPayloadChan
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
client.passedSubscribeArgs = append(client.passedSubscribeArgs, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription := rpc.ClientSubscription{}
|
|
|
|
return &subscription, nil
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) AssertSubscribeCalledWith(namespace string, payloadChan chan statediff.Payload, args []interface{}) {
|
2019-07-02 12:31:26 +00:00
|
|
|
Expect(client.passedNamespace).To(Equal(namespace))
|
|
|
|
Expect(client.passedPayloadChan).To(Equal(payloadChan))
|
|
|
|
Expect(client.passedSubscribeArgs).To(Equal(args))
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func NewMockRPCClient() *MockRPCClient {
|
|
|
|
return &MockRPCClient{}
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SetIpcPath(ipcPath string) {
|
2018-07-20 16:37:46 +00:00
|
|
|
client.ipcPath = ipcPath
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) BatchCall(batch []client.BatchElem) error {
|
2018-12-12 16:01:50 +00:00
|
|
|
client.passedBatch = batch
|
|
|
|
client.passedMethod = batch[0].Method
|
|
|
|
client.lengthOfBatch = len(batch)
|
|
|
|
|
|
|
|
for _, batchElem := range batch {
|
|
|
|
client.passedContext = context.Background()
|
|
|
|
client.passedResult = &batchElem.Result
|
|
|
|
client.passedMethod = batchElem.Method
|
|
|
|
if p, ok := batchElem.Result.(*types.Header); ok {
|
|
|
|
*p = types.Header{Number: big.NewInt(100)}
|
|
|
|
}
|
|
|
|
if p, ok := batchElem.Result.(*core.POAHeader); ok {
|
|
|
|
|
|
|
|
*p = client.returnPOAHeader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
|
2018-09-18 19:46:16 +00:00
|
|
|
client.passedContext = ctx
|
|
|
|
client.passedResult = result
|
|
|
|
client.passedMethod = method
|
2018-07-20 16:37:46 +00:00
|
|
|
switch method {
|
|
|
|
case "admin_nodeInfo":
|
|
|
|
if p, ok := result.(*p2p.NodeInfo); ok {
|
|
|
|
p.ID = "enode://GethNode@172.17.0.1:30303"
|
|
|
|
p.Name = "Geth/v1.7"
|
|
|
|
}
|
|
|
|
case "eth_getBlockByNumber":
|
|
|
|
if p, ok := result.(*types.Header); ok {
|
2018-12-12 16:01:50 +00:00
|
|
|
*p = types.Header{Number: big.NewInt(100)}
|
2018-09-18 19:46:16 +00:00
|
|
|
}
|
|
|
|
if p, ok := result.(*core.POAHeader); ok {
|
2018-12-12 16:01:50 +00:00
|
|
|
|
2018-09-20 22:15:06 +00:00
|
|
|
*p = client.returnPOAHeader
|
2018-09-18 19:46:16 +00:00
|
|
|
}
|
|
|
|
if client.callContextErr != nil {
|
|
|
|
return client.callContextErr
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
case "parity_versionInfo":
|
|
|
|
if p, ok := result.(*core.ParityNodeInfo); ok {
|
|
|
|
*p = core.ParityNodeInfo{
|
|
|
|
Track: "",
|
|
|
|
ParityVersion: core.ParityVersion{
|
|
|
|
Major: 1,
|
|
|
|
Minor: 2,
|
|
|
|
Patch: 3,
|
|
|
|
},
|
|
|
|
Hash: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "parity_enode":
|
|
|
|
if p, ok := result.(*string); ok {
|
|
|
|
*p = "enode://ParityNode@172.17.0.1:30303"
|
|
|
|
}
|
|
|
|
case "net_version":
|
|
|
|
if p, ok := result.(*string); ok {
|
|
|
|
*p = "1234"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) IpcPath() string {
|
2018-07-20 16:37:46 +00:00
|
|
|
return client.ipcPath
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SupportedModules() (map[string]string, error) {
|
2018-08-07 15:51:34 +00:00
|
|
|
return client.supportedModules, nil
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
2018-08-07 15:51:34 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SetSupporedModules(supportedModules map[string]string) {
|
2018-08-07 15:51:34 +00:00
|
|
|
client.supportedModules = supportedModules
|
2018-08-07 20:10:17 +00:00
|
|
|
}
|
2018-09-18 19:46:16 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SetCallContextErr(err error) {
|
2018-09-18 19:46:16 +00:00
|
|
|
client.callContextErr = err
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SetReturnPOAHeader(header core.POAHeader) {
|
2018-09-20 22:15:06 +00:00
|
|
|
client.returnPOAHeader = header
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SetReturnPOWHeaders(headers []*types.Header) {
|
2018-12-12 16:01:50 +00:00
|
|
|
client.returnPOWHeaders = headers
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) SetReturnPOAHeaders(headers []core.POAHeader) {
|
2018-12-12 16:01:50 +00:00
|
|
|
client.returnPOAHeaders = headers
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) AssertCallContextCalledWith(ctx context.Context, result interface{}, method string) {
|
2018-09-18 19:46:16 +00:00
|
|
|
Expect(client.passedContext).To(Equal(ctx))
|
|
|
|
Expect(client.passedResult).To(BeAssignableToTypeOf(result))
|
|
|
|
Expect(client.passedMethod).To(Equal(method))
|
|
|
|
}
|
2018-12-12 16:01:50 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (client *MockRPCClient) AssertBatchCalledWith(method string, lengthOfBatch int) {
|
2018-12-12 16:01:50 +00:00
|
|
|
Expect(client.lengthOfBatch).To(Equal(lengthOfBatch))
|
|
|
|
for _, batch := range client.passedBatch {
|
|
|
|
Expect(batch.Method).To(Equal(method))
|
|
|
|
}
|
|
|
|
Expect(client.passedMethod).To(Equal(method))
|
|
|
|
}
|
2019-04-18 12:39:37 +00:00
|
|
|
|
|
|
|
func (client *MockRpcClient) Subscribe(namespace string, payloadChan interface{}, args ...interface{}) (*rpc.ClientSubscription, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|