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 eth
|
|
|
|
|
|
|
|
import (
|
2020-04-20 13:14:02 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-02-10 17:11:44 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
"github.com/vulcanize/ipfs-chain-watcher/pkg/shared"
|
2020-01-17 23:16:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PayloadChanBufferSize = 20000 // the max eth sub buffer size
|
|
|
|
)
|
|
|
|
|
2020-04-20 13:14:02 +00:00
|
|
|
// StreamClient is an interface for subscribing and streaming from geth
|
|
|
|
type StreamClient interface {
|
|
|
|
Subscribe(ctx context.Context, namespace string, payloadChan interface{}, args ...interface{}) (*rpc.ClientSubscription, error)
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// PayloadStreamer satisfies the PayloadStreamer interface for ethereum
|
|
|
|
type PayloadStreamer struct {
|
2020-04-20 13:14:02 +00:00
|
|
|
Client StreamClient
|
2020-05-19 20:09:30 +00:00
|
|
|
params statediff.Params
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 20:37:20 +00:00
|
|
|
// NewPayloadStreamer creates a pointer to a new PayloadStreamer which satisfies the PayloadStreamer interface for ethereum
|
2020-04-20 13:14:02 +00:00
|
|
|
func NewPayloadStreamer(client StreamClient) *PayloadStreamer {
|
2020-01-17 23:16:01 +00:00
|
|
|
return &PayloadStreamer{
|
|
|
|
Client: client,
|
2020-05-19 20:09:30 +00:00
|
|
|
params: statediff.Params{
|
|
|
|
IncludeBlock: true,
|
|
|
|
IncludeTD: true,
|
|
|
|
IncludeReceipts: true,
|
|
|
|
IntermediateStorageNodes: true,
|
|
|
|
IntermediateStateNodes: true,
|
|
|
|
},
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream is the main loop for subscribing to data from the Geth state diff process
|
2020-01-29 20:37:20 +00:00
|
|
|
// Satisfies the shared.PayloadStreamer interface
|
2020-01-31 18:03:37 +00:00
|
|
|
func (ps *PayloadStreamer) Stream(payloadChan chan shared.RawChainData) (shared.ClientSubscription, error) {
|
2020-02-10 17:11:44 +00:00
|
|
|
stateDiffChan := make(chan statediff.Payload, PayloadChanBufferSize)
|
2020-04-23 20:56:37 +00:00
|
|
|
logrus.Debug("streaming diffs from geth")
|
2020-02-10 17:11:44 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case payload := <-stateDiffChan:
|
|
|
|
payloadChan <- payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-05-19 20:09:30 +00:00
|
|
|
return ps.Client.Subscribe(context.Background(), "statediff", stateDiffChan, "stream", ps.params)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|