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

91 lines
2.8 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/>.
2020-08-31 15:58:16 +00:00
package serve
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"
"github.com/vulcanize/ipld-eth-indexer/pkg/shared"
2020-08-31 15:47:06 +00:00
"github.com/vulcanize/ipld-eth-server/pkg/eth"
2019-05-17 06:27:02 +00:00
)
// APIName is the namespace used for the state diffing service API
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"
2020-08-31 15:58:16 +00:00
// PublicServerAPI is the public api for the watcher
type PublicServerAPI struct {
w Server
2019-05-17 06:27:02 +00:00
}
2020-08-31 15:58:16 +00:00
// NewPublicServerAPI creates a new PublicServerAPI with the provided underlying Server process
func NewPublicServerAPI(w Server) *PublicServerAPI {
return &PublicServerAPI{
2020-06-30 00:16:52 +00:00
w: w,
2019-05-17 06:27:02 +00:00
}
}
2020-06-30 00:16:52 +00:00
// Stream is the public method to setup a subscription that fires off IPLD payloads as they are processed
2020-08-31 15:58:16 +00:00
func (api *PublicServerAPI) Stream(ctx context.Context, params eth.SubscriptionSettings) (*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 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
payloadChannel := make(chan SubscriptionPayload, PayloadChanBufferSize)
quitChan := make(chan bool, 1)
2020-06-30 00:16:52 +00:00
go api.w.Subscribe(rpcSub.ID, payloadChannel, quitChan, params)
2019-05-17 06:27:02 +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:
if err := notifier.Notify(rpcSub.ID, packet); err != nil {
2020-06-30 00:16:52 +00:00
log.Error("Failed to send watcher data packet", "err", err)
api.w.Unsubscribe(rpcSub.ID)
return
2019-05-17 06:27:02 +00:00
}
case <-rpcSub.Err():
2020-06-30 00:16:52 +00:00
api.w.Unsubscribe(rpcSub.ID)
2019-05-17 06:27:02 +00:00
return
case <-quitChan:
2020-06-30 00:16:52 +00:00
// don't need to unsubscribe from the watcher, 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
}
2020-06-30 00:16:52 +00:00
// Chain returns the chain type that this watcher instance supports
2020-08-31 15:58:16 +00:00
func (api *PublicServerAPI) Chain() shared.ChainType {
return shared.Ethereum
}