2023-06-14 12:43:34 +00:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package statediff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-06-23 12:42:55 +00:00
|
|
|
|
|
|
|
"github.com/cerc-io/plugeth-statediff/types"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/utils/log"
|
2023-06-14 12:43:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIName is the namespace used for the state diffing service API
|
|
|
|
const APIName = "statediff"
|
|
|
|
|
|
|
|
// APIVersion is the version of the state diffing service API
|
2023-06-23 12:42:55 +00:00
|
|
|
// TODO: match package version?
|
2023-06-14 12:43:34 +00:00
|
|
|
const APIVersion = "0.0.1"
|
|
|
|
|
|
|
|
// PublicStateDiffAPI provides an RPC subscription interface
|
|
|
|
// that can be used to stream out state diffs as they
|
|
|
|
// are produced by a full node
|
2023-06-23 12:42:55 +00:00
|
|
|
type PublicAPI struct {
|
2023-06-16 09:53:15 +00:00
|
|
|
sds *Service
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicStateDiffAPI creates an rpc subscription interface for the underlying statediff service
|
2023-06-16 09:53:15 +00:00
|
|
|
func NewPublicAPI(sds *Service) *PublicAPI {
|
2023-06-23 12:42:55 +00:00
|
|
|
return &PublicAPI{
|
2023-06-14 12:43:34 +00:00
|
|
|
sds: sds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
// Stream subscribes to statediff payloads as they are created.
|
|
|
|
func (api *PublicAPI) Stream(ctx context.Context, params Params) (<-chan Payload, error) {
|
|
|
|
payloadChan := make(chan Payload, chainEventChanSize)
|
|
|
|
clientChan := make(chan Payload, chainEventChanSize)
|
|
|
|
quitChan := make(chan bool, 1)
|
|
|
|
// subscribe to the service's payload broadcasts
|
|
|
|
id := api.sds.Subscribe(payloadChan, quitChan, params)
|
2023-06-14 12:43:34 +00:00
|
|
|
|
|
|
|
go func() {
|
2023-06-23 12:42:55 +00:00
|
|
|
defer close(clientChan)
|
|
|
|
defer close(payloadChan)
|
|
|
|
defer func() {
|
|
|
|
if err := api.sds.Unsubscribe(id); err != nil {
|
2023-06-16 09:53:15 +00:00
|
|
|
log.Error("Failed to unsubscribe from statediff service", "error", err)
|
2023-06-23 12:42:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
2023-06-23 12:42:55 +00:00
|
|
|
case payload := <-payloadChan:
|
|
|
|
clientChan <- payload
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2023-06-14 12:43:34 +00:00
|
|
|
case <-quitChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
return clientChan, nil
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateDiffAt returns a state diff payload at the specific blockheight
|
2023-06-23 12:42:55 +00:00
|
|
|
func (api *PublicAPI) StateDiffAt(ctx context.Context, blockNumber uint64, params Params) (*Payload, error) {
|
2023-06-14 12:43:34 +00:00
|
|
|
return api.sds.StateDiffAt(blockNumber, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateDiffFor returns a state diff payload for the specific blockhash
|
2023-06-23 12:42:55 +00:00
|
|
|
func (api *PublicAPI) StateDiffFor(ctx context.Context, blockHash common.Hash, params Params) (*Payload, error) {
|
2023-06-14 12:43:34 +00:00
|
|
|
return api.sds.StateDiffFor(blockHash, params)
|
|
|
|
}
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
// StreamCodeAndCodeHash writes all of the codehash=>code pairs at a given block to a websocket channel.
|
|
|
|
func (api *PublicAPI) StreamCodeAndCodeHash(ctx context.Context, blockNumber uint64) (<-chan types.CodeAndCodeHash, error) {
|
2023-06-14 12:43:34 +00:00
|
|
|
payloadChan := make(chan types.CodeAndCodeHash, chainEventChanSize)
|
2023-06-23 12:42:55 +00:00
|
|
|
clientChan := make(chan types.CodeAndCodeHash, chainEventChanSize)
|
|
|
|
quitChan := make(chan bool, 1)
|
2023-06-14 12:43:34 +00:00
|
|
|
api.sds.StreamCodeAndCodeHash(blockNumber, payloadChan, quitChan)
|
2023-06-23 12:42:55 +00:00
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
go func() {
|
2023-06-23 12:42:55 +00:00
|
|
|
defer close(clientChan)
|
|
|
|
defer close(payloadChan)
|
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case payload := <-payloadChan:
|
2023-06-23 12:42:55 +00:00
|
|
|
clientChan <- payload
|
|
|
|
case <-ctx.Done():
|
2023-06-14 12:43:34 +00:00
|
|
|
return
|
|
|
|
case <-quitChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
return clientChan, nil
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteStateDiffAt writes a state diff object directly to DB at the specific blockheight
|
2023-06-23 12:42:55 +00:00
|
|
|
func (api *PublicAPI) WriteStateDiffAt(ctx context.Context, blockNumber uint64, params Params) JobID {
|
2023-06-14 12:43:34 +00:00
|
|
|
var err error
|
|
|
|
start, logger := countApiRequestBegin("writeStateDiffAt", blockNumber)
|
|
|
|
defer countApiRequestEnd(start, logger, err)
|
|
|
|
|
|
|
|
return api.sds.WriteStateDiffAt(blockNumber, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteStateDiffFor writes a state diff object directly to DB for the specific block hash
|
2023-06-23 12:42:55 +00:00
|
|
|
func (api *PublicAPI) WriteStateDiffFor(ctx context.Context, blockHash common.Hash, params Params) error {
|
2023-06-14 12:43:34 +00:00
|
|
|
var err error
|
2023-06-23 12:42:55 +00:00
|
|
|
start, logger := countApiRequestBegin("writeStateDiffFor", blockHash.String())
|
2023-06-14 12:43:34 +00:00
|
|
|
defer countApiRequestEnd(start, logger, err)
|
|
|
|
|
|
|
|
err = api.sds.WriteStateDiffFor(blockHash, params)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
// WatchAddress changes the list of watched addresses to which the direct indexing is restricted
|
|
|
|
// for the given operation.
|
|
|
|
func (api *PublicAPI) WatchAddress(operation types.OperationType, args []types.WatchAddressArg) error {
|
2023-06-14 12:43:34 +00:00
|
|
|
return api.sds.WatchAddress(operation, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamWrites sets up a subscription that streams the status of completed calls to WriteStateDiff*
|
2023-06-23 12:42:55 +00:00
|
|
|
func (api *PublicAPI) StreamWrites(ctx context.Context) (<-chan JobStatus, error) {
|
|
|
|
// subscribe to events from the statediff service
|
|
|
|
statusChan := make(chan JobStatus, chainEventChanSize)
|
|
|
|
clientChan := make(chan JobStatus, chainEventChanSize)
|
2023-06-16 09:53:15 +00:00
|
|
|
id := api.sds.SubscribeWriteStatus(statusChan)
|
2023-06-14 12:43:34 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
2023-06-23 12:42:55 +00:00
|
|
|
close(statusChan)
|
|
|
|
close(clientChan)
|
2023-06-14 12:43:34 +00:00
|
|
|
}()
|
2023-06-23 12:42:55 +00:00
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case status := <-statusChan:
|
2023-06-23 12:42:55 +00:00
|
|
|
clientChan <- status
|
|
|
|
case <-ctx.Done():
|
2023-06-16 09:53:15 +00:00
|
|
|
api.sds.UnsubscribeWriteStatus(id)
|
2023-06-14 12:43:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
return clientChan, nil
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|