From 52b5c99760f758cff895b41c31f55bc05422b3b4 Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Mon, 10 Aug 2020 10:11:51 -0500 Subject: [PATCH] fix support for eth testnet chains (use correct tx signer) --- pkg/builders/builders.go | 15 +++++++++------ pkg/eth/helpers.go | 22 +++++++++++++++++++++- pkg/historical/service.go | 2 +- pkg/node/node.go | 1 + pkg/resync/service.go | 2 +- pkg/shared/chain_type.go | 7 +++++++ pkg/shared/env.go | 6 ++++++ pkg/shared/types.go | 3 +++ pkg/validate/service.go | 1 - pkg/watch/service.go | 12 ++++++------ 10 files changed, 55 insertions(+), 16 deletions(-) delete mode 100644 pkg/validate/service.go diff --git a/pkg/builders/builders.go b/pkg/builders/builders.go index 51aa3432..d44b48d1 100644 --- a/pkg/builders/builders.go +++ b/pkg/builders/builders.go @@ -22,7 +22,6 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/rpcclient" - "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/vulcanize/ipfs-blockchain-watcher/pkg/btc" @@ -124,14 +123,18 @@ func NewPaylaodFetcher(chain shared.ChainType, client interface{}, timeout time. } // NewPayloadConverter constructs a PayloadConverter for the provided chain type -func NewPayloadConverter(chain shared.ChainType) (shared.PayloadConverter, error) { - switch chain { +func NewPayloadConverter(chainType shared.ChainType, chainID uint64) (shared.PayloadConverter, error) { + switch chainType { case shared.Ethereum: - return eth.NewPayloadConverter(params.MainnetChainConfig), nil + chainConfig, err := eth.ChainConfig(chainID) + if err != nil { + return nil, err + } + return eth.NewPayloadConverter(chainConfig), nil case shared.Bitcoin: return btc.NewPayloadConverter(&chaincfg.MainNetParams), nil default: - return nil, fmt.Errorf("invalid chain %s for converter constructor", chain.String()) + return nil, fmt.Errorf("invalid chain %s for converter constructor", chainType.String()) } } @@ -188,7 +191,7 @@ func NewIPLDPublisher(chain shared.ChainType, ipfsPath string, db *postgres.DB, } // NewPublicAPI constructs a PublicAPI for the provided chain type -func NewPublicAPI(chain shared.ChainType, db *postgres.DB, ipfsPath string) (rpc.API, error) { +func NewPublicAPI(chain shared.ChainType, db *postgres.DB) (rpc.API, error) { switch chain { case shared.Ethereum: backend, err := eth.NewEthBackend(db) diff --git a/pkg/eth/helpers.go b/pkg/eth/helpers.go index 7d9021ba..ad08022c 100644 --- a/pkg/eth/helpers.go +++ b/pkg/eth/helpers.go @@ -16,7 +16,11 @@ package eth -import "github.com/ethereum/go-ethereum/statediff" +import ( + "fmt" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/statediff" +) func ResolveFromNodeType(nodeType statediff.NodeType) int { switch nodeType { @@ -47,3 +51,19 @@ func ResolveToNodeType(nodeType int) statediff.NodeType { return statediff.Unknown } } + +// ChainConfig returns the appropriate ethereum chain config for the provided chain id +func ChainConfig(chainID uint64) (*params.ChainConfig, error) { + switch chainID { + case 1: + return params.MainnetChainConfig, nil + case 3: + return params.TestnetChainConfig, nil // Ropsten + case 4: + return params.RinkebyChainConfig, nil + case 5: + return params.GoerliChainConfig, nil + default: + return nil, fmt.Errorf("chain config for chainid %d not available", chainID) + } +} \ No newline at end of file diff --git a/pkg/historical/service.go b/pkg/historical/service.go index f942c1dc..c02c2a17 100644 --- a/pkg/historical/service.go +++ b/pkg/historical/service.go @@ -72,7 +72,7 @@ func NewBackFillService(settings *Config, screenAndServeChan chan shared.Convert if err != nil { return nil, err } - converter, err := builders.NewPayloadConverter(settings.Chain) + converter, err := builders.NewPayloadConverter(settings.Chain, settings.NodeInfo.ChainID) if err != nil { return nil, err } diff --git a/pkg/node/node.go b/pkg/node/node.go index 7faa881f..66f91d6d 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -19,6 +19,7 @@ package node type Node struct { GenesisBlock string NetworkID string + ChainID uint64 ID string ClientName string } diff --git a/pkg/resync/service.go b/pkg/resync/service.go index 34bd1bf0..08d22305 100644 --- a/pkg/resync/service.go +++ b/pkg/resync/service.go @@ -71,7 +71,7 @@ func NewResyncService(settings *Config) (Resync, error) { if err != nil { return nil, err } - converter, err := builders.NewPayloadConverter(settings.Chain) + converter, err := builders.NewPayloadConverter(settings.Chain, settings.NodeInfo.ChainID) if err != nil { return nil, err } diff --git a/pkg/shared/chain_type.go b/pkg/shared/chain_type.go index 83445192..c3dedfe3 100644 --- a/pkg/shared/chain_type.go +++ b/pkg/shared/chain_type.go @@ -29,6 +29,7 @@ const ( Ethereum Bitcoin Omni + EthereumClassic ) func (c ChainType) String() string { @@ -39,6 +40,8 @@ func (c ChainType) String() string { return "Bitcoin" case Omni: return "Omni" + case EthereumClassic: + return "EthereumClassic" default: return "" } @@ -52,6 +55,8 @@ func (c ChainType) API() string { return "btc" case Omni: return "omni" + case EthereumClassic: + return "etc" default: return "" } @@ -65,6 +70,8 @@ func NewChainType(name string) (ChainType, error) { return Bitcoin, nil case "omni": return Omni, nil + case "classic", "etc": + return EthereumClassic, nil default: return UnknownChain, errors.New("invalid name for chain") } diff --git a/pkg/shared/env.go b/pkg/shared/env.go index 58191613..b5a53e77 100644 --- a/pkg/shared/env.go +++ b/pkg/shared/env.go @@ -39,6 +39,7 @@ const ( ETH_CLIENT_NAME = "ETH_CLIENT_NAME" ETH_GENESIS_BLOCK = "ETH_GENESIS_BLOCK" ETH_NETWORK_ID = "ETH_NETWORK_ID" + ETH_CHAIN_ID = "ETH_CHAIN_ID" BTC_WS_PATH = "BTC_WS_PATH" BTC_HTTP_PATH = "BTC_HTTP_PATH" @@ -48,6 +49,7 @@ const ( BTC_CLIENT_NAME = "BTC_CLIENT_NAME" BTC_GENESIS_BLOCK = "BTC_GENESIS_BLOCK" BTC_NETWORK_ID = "BTC_NETWORK_ID" + BTC_CHAIN_ID = "BTC_CHAIN_ID" ) // GetEthNodeAndClient returns eth node info and client from path url @@ -56,6 +58,7 @@ func GetEthNodeAndClient(path string) (node.Node, *rpc.Client, error) { viper.BindEnv("ethereum.clientName", ETH_CLIENT_NAME) viper.BindEnv("ethereum.genesisBlock", ETH_GENESIS_BLOCK) viper.BindEnv("ethereum.networkID", ETH_NETWORK_ID) + viper.BindEnv("ethereum.chainID", ETH_CHAIN_ID) rpcClient, err := rpc.Dial(path) if err != nil { @@ -66,6 +69,7 @@ func GetEthNodeAndClient(path string) (node.Node, *rpc.Client, error) { ClientName: viper.GetString("ethereum.clientName"), GenesisBlock: viper.GetString("ethereum.genesisBlock"), NetworkID: viper.GetString("ethereum.networkID"), + ChainID: viper.GetUint64("ethereum.chainID"), }, rpcClient, nil } @@ -101,6 +105,7 @@ func GetBtcNodeAndClient(path string) (node.Node, *rpcclient.ConnConfig) { viper.BindEnv("bitcoin.networkID", BTC_NETWORK_ID) viper.BindEnv("bitcoin.pass", BTC_NODE_PASSWORD) viper.BindEnv("bitcoin.user", BTC_NODE_USER) + viper.BindEnv("bitcoin.chainID", BTC_CHAIN_ID) // For bitcoin we load in node info from the config because there is no RPC endpoint to retrieve this from the node return node.Node{ @@ -108,6 +113,7 @@ func GetBtcNodeAndClient(path string) (node.Node, *rpcclient.ConnConfig) { ClientName: viper.GetString("bitcoin.clientName"), GenesisBlock: viper.GetString("bitcoin.genesisBlock"), NetworkID: viper.GetString("bitcoin.networkID"), + ChainID: viper.GetUint64("bitcoin.chainID"), }, &rpcclient.ConnConfig{ Host: path, HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode diff --git a/pkg/shared/types.go b/pkg/shared/types.go index e213f7f4..7dd52f30 100644 --- a/pkg/shared/types.go +++ b/pkg/shared/types.go @@ -16,6 +16,9 @@ package shared +// Very loose interface types for generic processing of different blockchains +// TODO: split different blockchain support into separate repos + // These types serve as very loose wrappers around a generic underlying interface{} type RawChainData interface{} diff --git a/pkg/validate/service.go b/pkg/validate/service.go deleted file mode 100644 index 77fb2737..00000000 --- a/pkg/validate/service.go +++ /dev/null @@ -1 +0,0 @@ -package validate diff --git a/pkg/watch/service.go b/pkg/watch/service.go index 922ff6fe..43a493b0 100644 --- a/pkg/watch/service.go +++ b/pkg/watch/service.go @@ -108,7 +108,7 @@ func NewWatcher(settings *Config) (Watcher, error) { if err != nil { return nil, err } - sn.Converter, err = builders.NewPayloadConverter(settings.Chain) + sn.Converter, err = builders.NewPayloadConverter(settings.Chain, settings.NodeInfo.ChainID) if err != nil { return nil, err } @@ -154,7 +154,7 @@ func (sap *Service) Protocols() []p2p.Protocol { // APIs returns the RPC descriptors the watcher service offers func (sap *Service) APIs() []rpc.API { - ifnoAPI := NewInfoAPI() + infoAPI := NewInfoAPI() apis := []rpc.API{ { Namespace: APIName, @@ -165,23 +165,23 @@ func (sap *Service) APIs() []rpc.API { { Namespace: "rpc", Version: APIVersion, - Service: ifnoAPI, + Service: infoAPI, Public: true, }, { Namespace: "net", Version: APIVersion, - Service: ifnoAPI, + Service: infoAPI, Public: true, }, { Namespace: "admin", Version: APIVersion, - Service: ifnoAPI, + Service: infoAPI, Public: true, }, } - chainAPI, err := builders.NewPublicAPI(sap.chain, sap.db, sap.ipfsPath) + chainAPI, err := builders.NewPublicAPI(sap.chain, sap.db) if err != nil { log.Error(err) return apis