2020-01-17 23:16:01 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package super_node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-19 23:26:23 +00:00
|
|
|
"time"
|
2020-02-05 01:02:01 +00:00
|
|
|
|
2020-02-04 21:20:49 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2020-01-29 20:37:20 +00:00
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
2020-02-03 18:22:29 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/btc"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/eth"
|
2020-02-03 18:22:29 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2020-01-17 23:16:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewResponseFilterer constructs a ResponseFilterer for the provided chain type
|
2020-02-03 18:22:29 +00:00
|
|
|
func NewResponseFilterer(chain shared.ChainType) (shared.ResponseFilterer, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-01-17 23:16:01 +00:00
|
|
|
return eth.NewResponseFilterer(), nil
|
2020-02-09 22:10:42 +00:00
|
|
|
case shared.Bitcoin:
|
|
|
|
return btc.NewResponseFilterer(), nil
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for filterer constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCIDIndexer constructs a CIDIndexer for the provided chain type
|
2020-04-27 20:37:41 +00:00
|
|
|
func NewCIDIndexer(chain shared.ChainType, db *postgres.DB, ipfsMode shared.IPFSMode) (shared.CIDIndexer, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-04-27 20:37:41 +00:00
|
|
|
switch ipfsMode {
|
|
|
|
case shared.LocalInterface, shared.RemoteClient:
|
|
|
|
return eth.NewCIDIndexer(db), nil
|
|
|
|
case shared.DirectPostgres:
|
|
|
|
return eth.NewIPLDPublisherAndIndexer(db), nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("ethereum CIDIndexer unexpected ipfs mode %s", ipfsMode.String())
|
|
|
|
}
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Bitcoin:
|
2020-04-27 20:37:41 +00:00
|
|
|
switch ipfsMode {
|
|
|
|
case shared.LocalInterface, shared.RemoteClient:
|
|
|
|
return btc.NewCIDIndexer(db), nil
|
|
|
|
case shared.DirectPostgres:
|
2020-04-30 21:26:32 +00:00
|
|
|
return eth.NewIPLDPublisherAndIndexer(db), nil
|
2020-04-27 20:37:41 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("bitcoin CIDIndexer unexpected ipfs mode %s", ipfsMode.String())
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for indexer constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCIDRetriever constructs a CIDRetriever for the provided chain type
|
2020-02-03 18:22:29 +00:00
|
|
|
func NewCIDRetriever(chain shared.ChainType, db *postgres.DB) (shared.CIDRetriever, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-01-17 23:16:01 +00:00
|
|
|
return eth.NewCIDRetriever(db), nil
|
2020-02-09 22:10:42 +00:00
|
|
|
case shared.Bitcoin:
|
|
|
|
return btc.NewCIDRetriever(db), nil
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for retriever constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPayloadStreamer constructs a PayloadStreamer for the provided chain type
|
2020-02-09 22:10:42 +00:00
|
|
|
func NewPayloadStreamer(chain shared.ChainType, clientOrConfig interface{}) (shared.PayloadStreamer, chan shared.RawChainData, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-04-20 13:14:02 +00:00
|
|
|
ethClient, ok := clientOrConfig.(*rpc.Client)
|
2020-01-17 23:16:01 +00:00
|
|
|
if !ok {
|
2020-04-20 13:14:02 +00:00
|
|
|
return nil, nil, fmt.Errorf("ethereum payload streamer constructor expected client type %T got %T", &rpc.Client{}, clientOrConfig)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-01-31 18:03:37 +00:00
|
|
|
streamChan := make(chan shared.RawChainData, eth.PayloadChanBufferSize)
|
2020-01-17 23:16:01 +00:00
|
|
|
return eth.NewPayloadStreamer(ethClient), streamChan, nil
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Bitcoin:
|
2020-02-09 22:10:42 +00:00
|
|
|
btcClientConn, ok := clientOrConfig.(*rpcclient.ConnConfig)
|
2020-01-29 20:37:20 +00:00
|
|
|
if !ok {
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, nil, fmt.Errorf("bitcoin payload streamer constructor expected client config type %T got %T", rpcclient.ConnConfig{}, clientOrConfig)
|
2020-01-29 20:37:20 +00:00
|
|
|
}
|
2020-01-31 18:03:37 +00:00
|
|
|
streamChan := make(chan shared.RawChainData, btc.PayloadChanBufferSize)
|
2020-02-10 17:16:57 +00:00
|
|
|
return btc.NewHTTPPayloadStreamer(btcClientConn), streamChan, nil
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, nil, fmt.Errorf("invalid chain %s for streamer constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPaylaodFetcher constructs a PayloadFetcher for the provided chain type
|
2020-04-19 23:26:23 +00:00
|
|
|
func NewPaylaodFetcher(chain shared.ChainType, client interface{}, timeout time.Duration) (shared.PayloadFetcher, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-04-20 13:14:02 +00:00
|
|
|
batchClient, ok := client.(*rpc.Client)
|
2020-01-17 23:16:01 +00:00
|
|
|
if !ok {
|
2020-04-20 13:14:02 +00:00
|
|
|
return nil, fmt.Errorf("ethereum payload fetcher constructor expected client type %T got %T", &rpc.Client{}, client)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-04-19 23:26:23 +00:00
|
|
|
return eth.NewPayloadFetcher(batchClient, timeout), nil
|
2020-02-09 22:10:42 +00:00
|
|
|
case shared.Bitcoin:
|
2020-02-10 15:00:55 +00:00
|
|
|
connConfig, ok := client.(*rpcclient.ConnConfig)
|
2020-02-09 22:10:42 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("bitcoin payload fetcher constructor expected client type %T got %T", &rpcclient.Client{}, client)
|
|
|
|
}
|
2020-02-10 15:00:55 +00:00
|
|
|
return btc.NewPayloadFetcher(connConfig)
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for payload fetcher constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPayloadConverter constructs a PayloadConverter for the provided chain type
|
2020-02-03 18:22:29 +00:00
|
|
|
func NewPayloadConverter(chain shared.ChainType) (shared.PayloadConverter, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-01-31 18:03:37 +00:00
|
|
|
return eth.NewPayloadConverter(params.MainnetChainConfig), nil
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Bitcoin:
|
2020-02-04 21:20:49 +00:00
|
|
|
return btc.NewPayloadConverter(&chaincfg.MainNetParams), nil
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for converter constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIPLDFetcher constructs an IPLDFetcher for the provided chain type
|
2020-04-30 21:26:32 +00:00
|
|
|
func NewIPLDFetcher(chain shared.ChainType, ipfsPath string, db *postgres.DB, ipfsMode shared.IPFSMode) (shared.IPLDFetcher, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-04-30 21:26:32 +00:00
|
|
|
switch ipfsMode {
|
|
|
|
case shared.LocalInterface, shared.RemoteClient:
|
|
|
|
return eth.NewIPLDFetcher(ipfsPath)
|
|
|
|
case shared.DirectPostgres:
|
|
|
|
return eth.NewIPLDPGFetcher(db), nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("ethereum IPLDFetcher unexpected ipfs mode %s", ipfsMode.String())
|
|
|
|
}
|
2020-02-09 22:10:42 +00:00
|
|
|
case shared.Bitcoin:
|
2020-04-30 21:26:32 +00:00
|
|
|
switch ipfsMode {
|
|
|
|
case shared.LocalInterface, shared.RemoteClient:
|
|
|
|
return btc.NewIPLDFetcher(ipfsPath)
|
|
|
|
case shared.DirectPostgres:
|
|
|
|
return btc.NewIPLDPGFetcher(db), nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("bitcoin IPLDFetcher unexpected ipfs mode %s", ipfsMode.String())
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for IPLD fetcher constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIPLDPublisher constructs an IPLDPublisher for the provided chain type
|
2020-04-30 01:00:50 +00:00
|
|
|
func NewIPLDPublisher(chain shared.ChainType, ipfsPath string, db *postgres.DB, ipfsMode shared.IPFSMode) (shared.IPLDPublisher, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-04-27 20:37:41 +00:00
|
|
|
switch ipfsMode {
|
|
|
|
case shared.LocalInterface, shared.RemoteClient:
|
|
|
|
return eth.NewIPLDPublisher(ipfsPath)
|
|
|
|
case shared.DirectPostgres:
|
|
|
|
return eth.NewIPLDPublisherAndIndexer(db), nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("ethereum IPLDPublisher unexpected ipfs mode %s", ipfsMode.String())
|
|
|
|
}
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Bitcoin:
|
2020-04-27 20:37:41 +00:00
|
|
|
switch ipfsMode {
|
|
|
|
case shared.LocalInterface, shared.RemoteClient:
|
|
|
|
return btc.NewIPLDPublisher(ipfsPath)
|
|
|
|
case shared.DirectPostgres:
|
2020-04-30 21:26:32 +00:00
|
|
|
return btc.NewIPLDPublisherAndIndexer(db), nil
|
2020-04-27 20:37:41 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("bitcoin IPLDPublisher unexpected ipfs mode %s", ipfsMode.String())
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for publisher constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicAPI constructs a PublicAPI for the provided chain type
|
2020-02-03 18:22:29 +00:00
|
|
|
func NewPublicAPI(chain shared.ChainType, db *postgres.DB, ipfsPath string) (rpc.API, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
switch chain {
|
2020-02-03 18:22:29 +00:00
|
|
|
case shared.Ethereum:
|
2020-05-01 16:07:47 +00:00
|
|
|
backend, err := eth.NewEthBackend(db)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return rpc.API{}, err
|
|
|
|
}
|
|
|
|
return rpc.API{
|
|
|
|
Namespace: eth.APIName,
|
|
|
|
Version: eth.APIVersion,
|
|
|
|
Service: eth.NewPublicEthAPI(backend),
|
|
|
|
Public: true,
|
|
|
|
}, nil
|
|
|
|
default:
|
2020-02-09 22:10:42 +00:00
|
|
|
return rpc.API{}, fmt.Errorf("invalid chain %s for public api constructor", chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-12 18:51:58 +00:00
|
|
|
|
|
|
|
// NewCleaner constructs a Cleaner for the provided chain type
|
|
|
|
func NewCleaner(chain shared.ChainType, db *postgres.DB) (shared.Cleaner, error) {
|
|
|
|
switch chain {
|
|
|
|
case shared.Ethereum:
|
|
|
|
return eth.NewCleaner(db), nil
|
2020-03-18 00:42:53 +00:00
|
|
|
case shared.Bitcoin:
|
|
|
|
return btc.NewCleaner(db), nil
|
2020-03-12 18:51:58 +00:00
|
|
|
default:
|
2020-03-18 00:42:53 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain %s for cleaner constructor", chain.String())
|
2020-03-12 18:51:58 +00:00
|
|
|
}
|
|
|
|
}
|