2019-05-17 06:27:02 +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/>.
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
package super_node
|
2019-05-17 06:27:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-06 03:50:12 +00:00
|
|
|
|
2020-02-25 22:38:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2020-02-23 23:14:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2019-05-17 06:27:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-06-18 17:28:57 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2020-02-23 23:14:29 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/btc"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/eth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2019-05-17 06:27:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIName is the namespace used for the state diffing service API
|
2019-10-02 14:10:37 +00:00
|
|
|
const APIName = "vdb"
|
2019-05-17 06:27:02 +00:00
|
|
|
|
|
|
|
// APIVersion is the version of the state diffing service API
|
|
|
|
const APIVersion = "0.0.1"
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
// PublicSuperNodeAPI is the public api for the super node
|
|
|
|
type PublicSuperNodeAPI struct {
|
2020-01-17 23:16:01 +00:00
|
|
|
sn SuperNode
|
2019-05-17 06:27:02 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
// NewPublicSuperNodeAPI creates a new PublicSuperNodeAPI with the provided underlying SyncPublishScreenAndServe process
|
2020-01-17 23:16:01 +00:00
|
|
|
func NewPublicSuperNodeAPI(superNodeInterface SuperNode) *PublicSuperNodeAPI {
|
2019-10-02 14:10:37 +00:00
|
|
|
return &PublicSuperNodeAPI{
|
2020-01-17 23:16:01 +00:00
|
|
|
sn: superNodeInterface,
|
2019-05-17 06:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// Stream is the public method to setup a subscription that fires off super node payloads as they are processed
|
2020-02-23 23:14:29 +00:00
|
|
|
func (api *PublicSuperNodeAPI) Stream(ctx context.Context, rlpParams []byte) (*rpc.Subscription, error) {
|
|
|
|
var params shared.SubscriptionSettings
|
|
|
|
switch api.sn.Chain() {
|
|
|
|
case shared.Ethereum:
|
|
|
|
var ethParams eth.SubscriptionSettings
|
|
|
|
if err := rlp.DecodeBytes(rlpParams, ðParams); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
params = ðParams
|
|
|
|
case shared.Bitcoin:
|
|
|
|
var btcParams btc.SubscriptionSettings
|
|
|
|
if err := rlp.DecodeBytes(rlpParams, &btcParams); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
params = &btcParams
|
|
|
|
default:
|
|
|
|
panic("SuperNode is not configured for a specific chain type")
|
|
|
|
}
|
2019-05-17 06:27:02 +00:00
|
|
|
// ensure that the RPC connection supports subscriptions
|
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
|
|
|
if !supported {
|
|
|
|
return nil, rpc.ErrNotificationsUnsupported
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// create subscription and start waiting for stream events
|
2019-05-17 06:27:02 +00:00
|
|
|
rpcSub := notifier.CreateSubscription()
|
|
|
|
|
|
|
|
go func() {
|
2019-06-06 03:50:12 +00:00
|
|
|
// subscribe to events from the SyncPublishScreenAndServe service
|
2020-01-31 18:03:37 +00:00
|
|
|
payloadChannel := make(chan SubscriptionPayload, PayloadChanBufferSize)
|
2019-06-07 02:09:27 +00:00
|
|
|
quitChan := make(chan bool, 1)
|
2020-01-17 23:16:01 +00:00
|
|
|
go api.sn.Subscribe(rpcSub.ID, payloadChannel, quitChan, params)
|
2019-05-17 06:27:02 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// loop and await payloads and relay them to the subscriber using notifier
|
2019-05-17 06:27:02 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case packet := <-payloadChannel:
|
2020-01-17 23:16:01 +00:00
|
|
|
if err := notifier.Notify(rpcSub.ID, packet); err != nil {
|
|
|
|
log.Error("Failed to send super node packet", "err", err)
|
|
|
|
api.sn.Unsubscribe(rpcSub.ID)
|
2019-06-07 13:42:10 +00:00
|
|
|
return
|
2019-05-17 06:27:02 +00:00
|
|
|
}
|
|
|
|
case <-rpcSub.Err():
|
2020-01-17 23:16:01 +00:00
|
|
|
api.sn.Unsubscribe(rpcSub.ID)
|
2019-05-17 06:27:02 +00:00
|
|
|
return
|
|
|
|
case <-quitChan:
|
2020-01-17 23:16:01 +00:00
|
|
|
// don't need to unsubscribe to super node, the service does so before sending the quit signal this way
|
2019-05-17 06:27:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rpcSub, nil
|
2019-06-06 03:50:12 +00:00
|
|
|
}
|
2019-09-13 19:41:50 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// Node is a public rpc method to allow transformers to fetch the node info for the super node
|
2020-02-25 22:38:27 +00:00
|
|
|
// NOTE: this is the node info for the node that the super node is syncing from, not the node info for the super node itself
|
|
|
|
func (api *PublicSuperNodeAPI) Node() *core.Node {
|
2020-01-17 23:16:01 +00:00
|
|
|
return api.sn.Node()
|
2019-09-13 19:41:50 +00:00
|
|
|
}
|
2020-02-25 22:38:27 +00:00
|
|
|
|
|
|
|
// Chain returns the chain type that this super node instance supports
|
|
|
|
func (api *PublicSuperNodeAPI) Chain() shared.ChainType {
|
|
|
|
return api.sn.Chain()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Struct for holding super node meta data
|
|
|
|
type InfoAPI struct{}
|
|
|
|
|
|
|
|
// NewPublicSuperNodeAPI creates a new PublicSuperNodeAPI with the provided underlying SyncPublishScreenAndServe process
|
|
|
|
func NewInfoAPI() *InfoAPI {
|
|
|
|
return &InfoAPI{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modules returns modules supported by this api
|
|
|
|
func (iapi *InfoAPI) Modules() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"vdb": "Stream",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeInfo gathers and returns a collection of metadata for the super node
|
|
|
|
func (iapi *InfoAPI) NodeInfo() *p2p.NodeInfo {
|
|
|
|
return &p2p.NodeInfo{
|
|
|
|
// TODO: formalize this
|
|
|
|
ID: "vulcanizeDB",
|
|
|
|
Name: "superNode",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the version of the super node
|
|
|
|
func (iapi *InfoAPI) Version() string {
|
|
|
|
return VersionWithMeta
|
|
|
|
}
|