ipld-eth-server/pkg/seed_node/api.go

91 lines
3.0 KiB
Go
Raw Normal View History

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-08-05 17:56:15 +00:00
package seed_node
2019-05-17 06:27:02 +00:00
import (
"context"
2019-06-06 03:50:12 +00:00
2019-05-17 06:27:02 +00:00
"github.com/ethereum/go-ethereum/rpc"
log "github.com/sirupsen/logrus"
2019-08-14 18:49:30 +00:00
"github.com/vulcanize/vulcanizedb/libraries/shared/streamer"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core"
2019-05-17 06:27:02 +00:00
)
// APIName is the namespace used for the state diffing service API
const APIName = "vulcanizedb"
// APIVersion is the version of the state diffing service API
const APIVersion = "0.0.1"
// PublicSeedNodeAPI is the public api for the seed node
2019-05-17 06:27:02 +00:00
type PublicSeedNodeAPI struct {
2019-08-26 02:13:40 +00:00
sni NodeInterface
2019-05-17 06:27:02 +00:00
}
// NewPublicSeedNodeAPI creates a new PublicSeedNodeAPI with the provided underlying SyncPublishScreenAndServe process
2019-08-26 02:13:40 +00:00
func NewPublicSeedNodeAPI(seedNodeInterface NodeInterface) *PublicSeedNodeAPI {
2019-05-17 06:27:02 +00:00
return &PublicSeedNodeAPI{
2019-08-26 02:13:40 +00:00
sni: seedNodeInterface,
2019-05-17 06:27:02 +00:00
}
}
2019-06-06 03:50:12 +00:00
// Stream is the public method to setup a subscription that fires off SyncPublishScreenAndServe payloads as they are created
func (api *PublicSeedNodeAPI) Stream(ctx context.Context, streamFilters config.Subscription) (*rpc.Subscription, error) {
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
}
// create subscription and start waiting for statediff events
rpcSub := notifier.CreateSubscription()
go func() {
2019-06-06 03:50:12 +00:00
// subscribe to events from the SyncPublishScreenAndServe service
2019-08-05 17:56:15 +00:00
payloadChannel := make(chan streamer.SeedNodePayload, payloadChanBufferSize)
quitChan := make(chan bool, 1)
2019-08-26 02:13:40 +00:00
go api.sni.Subscribe(rpcSub.ID, payloadChannel, quitChan, streamFilters)
2019-05-17 06:27:02 +00:00
// loop and await state diff payloads and relay them to the subscriber with then notifier
for {
select {
case packet := <-payloadChannel:
if notifyErr := notifier.Notify(rpcSub.ID, packet); notifyErr != nil {
log.Error("Failed to send state diff packet", "err", notifyErr)
2019-08-26 02:13:40 +00:00
api.sni.Unsubscribe(rpcSub.ID)
return
2019-05-17 06:27:02 +00:00
}
case <-rpcSub.Err():
2019-08-26 02:13:40 +00:00
api.sni.Unsubscribe(rpcSub.ID)
2019-05-17 06:27:02 +00:00
return
case <-quitChan:
2019-06-06 03:50:12 +00:00
// don't need to unsubscribe, SyncPublishScreenAndServe service does so before sending the quit signal
2019-05-17 06:27:02 +00:00
return
}
}
}()
return rpcSub, nil
2019-06-06 03:50:12 +00:00
}
// Node is a public rpc method to allow transformers to fetch the Geth node info for the seed node
func (api *PublicSeedNodeAPI) Node() core.Node {
return api.sni.Node()
}