2020-08-19 05:12:58 +00:00
|
|
|
// Copyright © 2020 Vulcanize, Inc
|
|
|
|
//
|
|
|
|
// 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 statediff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-08-19 05:57:57 +00:00
|
|
|
"sync"
|
2021-10-21 18:00:30 +00:00
|
|
|
"time"
|
2020-08-19 05:12:58 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-11-17 01:37:36 +00:00
|
|
|
sd "github.com/ethereum/go-ethereum/statediff"
|
2021-11-13 00:45:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/interfaces"
|
2020-11-17 01:37:36 +00:00
|
|
|
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
|
2020-08-19 16:35:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-11-10 09:07:59 +00:00
|
|
|
|
2021-11-13 00:45:08 +00:00
|
|
|
"github.com/vulcanize/eth-statediff-service/pkg/prom"
|
2020-08-19 05:12:58 +00:00
|
|
|
)
|
|
|
|
|
2021-10-25 19:06:05 +00:00
|
|
|
const defaultQueueSize = 1024
|
2020-08-19 05:12:58 +00:00
|
|
|
|
2021-10-25 19:06:05 +00:00
|
|
|
// StateDiffService is the state-diffing service interface
|
|
|
|
type StateDiffService interface {
|
|
|
|
// Lifecycle Start() and Stop()
|
2020-11-16 09:49:59 +00:00
|
|
|
node.Lifecycle
|
2021-10-25 19:06:05 +00:00
|
|
|
// APIs and Protocols() interface for node service registration
|
2020-11-16 09:49:59 +00:00
|
|
|
APIs() []rpc.API
|
|
|
|
Protocols() []p2p.Protocol
|
2021-10-25 19:06:05 +00:00
|
|
|
// Loop is the main event loop for processing state diffs
|
2021-10-25 19:26:41 +00:00
|
|
|
Loop(wg *sync.WaitGroup) error
|
2021-10-27 15:40:08 +00:00
|
|
|
// Run is a one-off command to run on a predefined set of ranges
|
2022-08-31 04:31:54 +00:00
|
|
|
Run(ranges []RangeRequest, parallel bool) error
|
2021-10-25 19:06:05 +00:00
|
|
|
// StateDiffAt method to get state diff object at specific block
|
2020-09-15 03:46:50 +00:00
|
|
|
StateDiffAt(blockNumber uint64, params sd.Params) (*sd.Payload, error)
|
2021-10-25 19:06:05 +00:00
|
|
|
// StateDiffFor method to get state diff object at specific block
|
2021-02-27 18:39:16 +00:00
|
|
|
StateDiffFor(blockHash common.Hash, params sd.Params) (*sd.Payload, error)
|
2021-10-25 19:06:05 +00:00
|
|
|
// StateTrieAt method to get state trie object at specific block
|
2020-09-15 03:46:50 +00:00
|
|
|
StateTrieAt(blockNumber uint64, params sd.Params) (*sd.Payload, error)
|
2021-10-25 19:06:05 +00:00
|
|
|
// WriteStateDiffAt method to write state diff object directly to DB
|
2020-11-10 09:07:59 +00:00
|
|
|
WriteStateDiffAt(blockNumber uint64, params sd.Params) error
|
2021-10-25 19:06:05 +00:00
|
|
|
// WriteStateDiffFor method to get state trie object at specific block
|
2021-02-27 18:39:16 +00:00
|
|
|
WriteStateDiffFor(blockHash common.Hash, params sd.Params) error
|
2021-10-25 19:06:05 +00:00
|
|
|
// WriteStateDiffsInRange method to wrtie state diff objects within the range directly to the DB
|
|
|
|
WriteStateDiffsInRange(start, stop uint64, params sd.Params) error
|
2020-08-19 05:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Service is the underlying struct for the state diffing service
|
|
|
|
type Service struct {
|
|
|
|
// Used to build the state diff objects
|
2022-06-24 08:42:58 +00:00
|
|
|
Builder sd.Builder
|
2022-05-17 09:05:11 +00:00
|
|
|
// Used to read data from LevelDB
|
2021-10-25 19:06:05 +00:00
|
|
|
lvlDBReader Reader
|
2020-08-19 05:12:58 +00:00
|
|
|
// Used to signal shutdown of the service
|
2021-10-25 19:26:41 +00:00
|
|
|
quitChan chan struct{}
|
2020-11-10 09:07:59 +00:00
|
|
|
// Interface for publishing statediffs as PG-IPLD objects
|
2021-11-13 00:45:08 +00:00
|
|
|
indexer interfaces.StateDiffIndexer
|
2021-10-25 19:06:05 +00:00
|
|
|
// range queue
|
|
|
|
queue chan RangeRequest
|
|
|
|
// number of ranges we can work over concurrently
|
|
|
|
workers uint
|
|
|
|
// ranges configured locally
|
|
|
|
preruns []RangeRequest
|
2020-08-19 05:12:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 16:50:49 +00:00
|
|
|
// NewStateDiffService creates a new Service
|
2021-11-13 00:45:08 +00:00
|
|
|
func NewStateDiffService(lvlDBReader Reader, indexer interfaces.StateDiffIndexer, conf Config) (*Service, error) {
|
|
|
|
b, err := NewBuilder(lvlDBReader.StateDB(), conf.TrieWorkers)
|
2020-09-15 06:03:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-25 19:06:05 +00:00
|
|
|
if conf.WorkerQueueSize == 0 {
|
|
|
|
conf.WorkerQueueSize = defaultQueueSize
|
|
|
|
}
|
2020-08-19 05:12:58 +00:00
|
|
|
return &Service{
|
2020-08-19 05:57:57 +00:00
|
|
|
lvlDBReader: lvlDBReader,
|
2021-11-13 00:45:08 +00:00
|
|
|
Builder: b,
|
2020-11-10 09:07:59 +00:00
|
|
|
indexer: indexer,
|
2021-10-25 19:06:05 +00:00
|
|
|
workers: conf.ServiceWorkers,
|
|
|
|
queue: make(chan RangeRequest, conf.WorkerQueueSize),
|
|
|
|
preruns: conf.PreRuns,
|
2020-08-19 05:12:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocols exports the services p2p protocols, this service has none
|
|
|
|
func (sds *Service) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{}
|
|
|
|
}
|
|
|
|
|
2020-09-04 16:50:49 +00:00
|
|
|
// APIs returns the RPC descriptors the Service offers
|
2020-08-19 05:12:58 +00:00
|
|
|
func (sds *Service) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
2020-09-04 16:50:49 +00:00
|
|
|
Namespace: APIName,
|
|
|
|
Version: APIVersion,
|
2020-08-19 05:12:58 +00:00
|
|
|
Service: NewPublicStateDiffAPI(sds),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:40:08 +00:00
|
|
|
// Run does a one-off processing run on the provided RangeRequests + any pre-runs, exiting afterwards
|
2022-08-31 04:31:54 +00:00
|
|
|
func (sds *Service) Run(rngs []RangeRequest, parallel bool) error {
|
2021-10-27 15:40:08 +00:00
|
|
|
for _, preRun := range sds.preruns {
|
2022-08-31 04:31:54 +00:00
|
|
|
if parallel {
|
|
|
|
// Chunk overall range into N subranges for workers
|
|
|
|
chunkSize := (preRun.Stop - preRun.Start) / uint64(sds.workers)
|
|
|
|
logrus.Infof("parallel processing prerun range (%d, %d) (%d blocks) divided into %d sized chunks with %d workers", preRun.Start, preRun.Stop,
|
2022-09-21 19:06:41 +00:00
|
|
|
preRun.Stop-preRun.Start+1, chunkSize, sds.workers)
|
2022-08-31 04:31:54 +00:00
|
|
|
// Sanity floor the chunk size
|
|
|
|
if chunkSize < 100 {
|
|
|
|
chunkSize = 100
|
|
|
|
logrus.Infof("Computed range chunk size for each worker is too small, defaulting to 100")
|
|
|
|
}
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
for i := 0; i < int(sds.workers); i++ {
|
|
|
|
blockRange := RangeRequest{
|
2022-08-31 13:54:16 +00:00
|
|
|
// TODO(dboreham): check this math doesn't leave gaps (are start/stop inclusive?)
|
2022-08-31 04:31:54 +00:00
|
|
|
Start: preRun.Start + uint64(i)*chunkSize,
|
|
|
|
Stop: preRun.Start + uint64(i)*chunkSize + chunkSize - 1,
|
|
|
|
Params: preRun.Params,
|
|
|
|
}
|
|
|
|
// TODO(hack) this fixes quantization
|
|
|
|
if blockRange.Stop < preRun.Stop && preRun.Stop-blockRange.Stop < chunkSize {
|
|
|
|
blockRange.Stop = preRun.Stop
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(id int) {
|
|
|
|
defer wg.Done()
|
|
|
|
logrus.Infof("prerun worker %d processing range (%d, %d)", id, blockRange.Start, blockRange.Stop)
|
|
|
|
for j := blockRange.Start; j <= blockRange.Stop; j++ {
|
|
|
|
if err := sds.WriteStateDiffAt(j, blockRange.Params); err != nil {
|
|
|
|
logrus.Errorf("error writing statediff at height %d in range (%d, %d) : %v", i, blockRange.Start, blockRange.Stop, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.Infof("prerun worker %d finished processing range (%d, %d)", id, blockRange.Start, blockRange.Stop)
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
} else {
|
|
|
|
logrus.Infof("sequential processing prerun range (%d, %d)", preRun.Start, preRun.Stop)
|
|
|
|
for i := preRun.Start; i <= preRun.Stop; i++ {
|
|
|
|
if err := sds.WriteStateDiffAt(i, preRun.Params); err != nil {
|
|
|
|
return fmt.Errorf("error writing statediff at height %d in range (%d, %d) : %v", i, preRun.Start, preRun.Stop, err)
|
|
|
|
}
|
2021-10-27 15:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sds.preruns = nil
|
2022-08-31 04:31:54 +00:00
|
|
|
// TODO(dboreham): seems like this code is never called so we have not written the parallel version
|
2021-10-27 15:40:08 +00:00
|
|
|
for _, rng := range rngs {
|
2022-08-31 04:31:54 +00:00
|
|
|
logrus.Infof("processing requested range (%d, %d)", rng.Start, rng.Stop)
|
2021-10-27 15:40:08 +00:00
|
|
|
for i := rng.Start; i <= rng.Stop; i++ {
|
|
|
|
if err := sds.WriteStateDiffAt(i, rng.Params); err != nil {
|
|
|
|
return fmt.Errorf("error writing statediff at height %d in range (%d, %d) : %v", i, rng.Start, rng.Stop, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-19 05:12:58 +00:00
|
|
|
// Loop is an empty service loop for awaiting rpc requests
|
2021-10-25 19:26:41 +00:00
|
|
|
func (sds *Service) Loop(wg *sync.WaitGroup) error {
|
|
|
|
if sds.quitChan != nil {
|
|
|
|
return fmt.Errorf("service loop is already running")
|
|
|
|
}
|
2021-10-27 15:40:08 +00:00
|
|
|
|
2021-10-25 19:26:41 +00:00
|
|
|
sds.quitChan = make(chan struct{})
|
2021-10-25 19:06:05 +00:00
|
|
|
for i := 0; i < int(sds.workers); i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(id int) {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case blockRange := <-sds.queue:
|
2021-10-27 15:40:08 +00:00
|
|
|
logrus.Infof("service worker %d received range (%d, %d) off of work queue, beginning processing", id, blockRange.Start, blockRange.Stop)
|
2021-10-25 19:26:41 +00:00
|
|
|
prom.DecQueuedRanges()
|
2021-10-25 19:54:55 +00:00
|
|
|
for j := blockRange.Start; j <= blockRange.Stop; j++ {
|
2021-10-25 19:06:05 +00:00
|
|
|
if err := sds.WriteStateDiffAt(j, blockRange.Params); err != nil {
|
|
|
|
logrus.Errorf("service worker %d error writing statediff at height %d in range (%d, %d) : %v", id, j, blockRange.Start, blockRange.Stop, err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-sds.quitChan:
|
|
|
|
logrus.Infof("closing service worker %d\n"+
|
|
|
|
"working in range (%d, %d)\n"+
|
|
|
|
"last processed height: %d", id, blockRange.Start, blockRange.Stop, j)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
logrus.Infof("service worker %d finished processing statediff height %d in range (%d, %d)", id, j, blockRange.Start, blockRange.Stop)
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 19:54:55 +00:00
|
|
|
logrus.Infof("service worker %d finished processing range (%d, %d)", id, blockRange.Start, blockRange.Stop)
|
2021-10-25 19:06:05 +00:00
|
|
|
case <-sds.quitChan:
|
|
|
|
logrus.Infof("closing the statediff service loop worker %d", id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
for _, preRun := range sds.preruns {
|
2021-10-25 19:26:41 +00:00
|
|
|
if err := sds.WriteStateDiffsInRange(preRun.Start, preRun.Stop, preRun.Params); err != nil {
|
|
|
|
close(sds.quitChan)
|
|
|
|
return err
|
|
|
|
}
|
2020-08-19 05:12:58 +00:00
|
|
|
}
|
2021-10-25 19:26:41 +00:00
|
|
|
return nil
|
2020-08-19 05:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateDiffAt returns a state diff object payload at the specific blockheight
|
|
|
|
// This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data
|
2020-09-15 03:46:50 +00:00
|
|
|
func (sds *Service) StateDiffAt(blockNumber uint64, params sd.Params) (*sd.Payload, error) {
|
2020-08-19 05:12:58 +00:00
|
|
|
currentBlock, err := sds.lvlDBReader.GetBlockByNumber(blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-09 01:08:36 +00:00
|
|
|
logrus.Infof("sending state diff at block %d", blockNumber)
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2022-07-12 11:08:05 +00:00
|
|
|
// compute leaf paths of watched addresses in the params
|
2022-07-01 07:59:48 +00:00
|
|
|
params.ComputeWatchedAddressesLeafPaths()
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2020-08-19 05:12:58 +00:00
|
|
|
if blockNumber == 0 {
|
|
|
|
return sds.processStateDiff(currentBlock, common.Hash{}, params)
|
|
|
|
}
|
|
|
|
parentBlock, err := sds.lvlDBReader.GetBlockByHash(currentBlock.ParentHash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:39:16 +00:00
|
|
|
// StateDiffFor returns a state diff object payload for the specific blockhash
|
|
|
|
// This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data
|
|
|
|
func (sds *Service) StateDiffFor(blockHash common.Hash, params sd.Params) (*sd.Payload, error) {
|
|
|
|
currentBlock, err := sds.lvlDBReader.GetBlockByHash(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-09 01:08:36 +00:00
|
|
|
logrus.Infof("sending state diff at block %s", blockHash.Hex())
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2022-07-12 11:08:05 +00:00
|
|
|
// compute leaf paths of watched addresses in the params
|
2022-07-01 07:59:48 +00:00
|
|
|
params.ComputeWatchedAddressesLeafPaths()
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2021-02-27 18:39:16 +00:00
|
|
|
if currentBlock.NumberU64() == 0 {
|
|
|
|
return sds.processStateDiff(currentBlock, common.Hash{}, params)
|
|
|
|
}
|
|
|
|
parentBlock, err := sds.lvlDBReader.GetBlockByHash(currentBlock.ParentHash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
|
|
|
|
}
|
|
|
|
|
2020-08-19 05:12:58 +00:00
|
|
|
// processStateDiff method builds the state diff payload from the current block, parent state root, and provided params
|
2020-09-15 03:46:50 +00:00
|
|
|
func (sds *Service) processStateDiff(currentBlock *types.Block, parentRoot common.Hash, params sd.Params) (*sd.Payload, error) {
|
|
|
|
stateDiff, err := sds.Builder.BuildStateDiffObject(sd.Args{
|
2020-08-19 05:12:58 +00:00
|
|
|
BlockHash: currentBlock.Hash(),
|
|
|
|
BlockNumber: currentBlock.Number(),
|
2020-09-04 16:50:49 +00:00
|
|
|
OldStateRoot: parentRoot,
|
|
|
|
NewStateRoot: currentBlock.Root(),
|
2020-08-19 05:12:58 +00:00
|
|
|
}, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-11 09:51:23 +00:00
|
|
|
stateDiffRlp, err := rlp.EncodeToBytes(&stateDiff)
|
2020-08-19 05:12:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-19 19:34:05 +00:00
|
|
|
logrus.Infof("state diff object at block %d is %d bytes in length", currentBlock.Number().Uint64(), len(stateDiffRlp))
|
2020-08-19 05:12:58 +00:00
|
|
|
return sds.newPayload(stateDiffRlp, currentBlock, params)
|
|
|
|
}
|
|
|
|
|
2020-09-15 03:46:50 +00:00
|
|
|
func (sds *Service) newPayload(stateObject []byte, block *types.Block, params sd.Params) (*sd.Payload, error) {
|
2020-09-15 00:25:02 +00:00
|
|
|
payload := &sd.Payload{
|
2020-08-19 05:12:58 +00:00
|
|
|
StateObjectRlp: stateObject,
|
|
|
|
}
|
|
|
|
if params.IncludeBlock {
|
|
|
|
blockBuff := new(bytes.Buffer)
|
|
|
|
if err := block.EncodeRLP(blockBuff); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
payload.BlockRlp = blockBuff.Bytes()
|
|
|
|
}
|
|
|
|
if params.IncludeTD {
|
|
|
|
var err error
|
2020-08-19 05:57:57 +00:00
|
|
|
payload.TotalDifficulty, err = sds.lvlDBReader.GetTdByHash(block.Hash())
|
2020-08-19 05:12:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if params.IncludeReceipts {
|
|
|
|
receiptBuff := new(bytes.Buffer)
|
|
|
|
receipts, err := sds.lvlDBReader.GetReceiptsByHash(block.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := rlp.Encode(receiptBuff, receipts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
payload.ReceiptsRlp = receiptBuff.Bytes()
|
|
|
|
}
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateTrieAt returns a state trie object payload at the specified blockheight
|
|
|
|
// This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data
|
2020-09-15 03:46:50 +00:00
|
|
|
func (sds *Service) StateTrieAt(blockNumber uint64, params sd.Params) (*sd.Payload, error) {
|
2020-08-19 05:12:58 +00:00
|
|
|
currentBlock, err := sds.lvlDBReader.GetBlockByNumber(blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-09 01:08:36 +00:00
|
|
|
logrus.Infof("sending state trie at block %d", blockNumber)
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2022-07-12 11:08:05 +00:00
|
|
|
// compute leaf paths of watched addresses in the params
|
2022-07-01 07:59:48 +00:00
|
|
|
params.ComputeWatchedAddressesLeafPaths()
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2020-08-19 05:12:58 +00:00
|
|
|
return sds.processStateTrie(currentBlock, params)
|
|
|
|
}
|
|
|
|
|
2020-09-15 03:46:50 +00:00
|
|
|
func (sds *Service) processStateTrie(block *types.Block, params sd.Params) (*sd.Payload, error) {
|
2020-08-19 05:12:58 +00:00
|
|
|
stateNodes, err := sds.Builder.BuildStateTrieObject(block)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-11 09:51:23 +00:00
|
|
|
stateTrieRlp, err := rlp.EncodeToBytes(&stateNodes)
|
2020-08-19 05:12:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-19 19:34:05 +00:00
|
|
|
logrus.Infof("state trie object at block %d is %d bytes in length", block.Number().Uint64(), len(stateTrieRlp))
|
2020-08-19 05:12:58 +00:00
|
|
|
return sds.newPayload(stateTrieRlp, block, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start is used to begin the service
|
2020-11-16 09:49:59 +00:00
|
|
|
func (sds *Service) Start() error {
|
2020-08-19 16:35:09 +00:00
|
|
|
logrus.Info("starting statediff service")
|
2021-10-25 19:26:41 +00:00
|
|
|
return sds.Loop(new(sync.WaitGroup))
|
2020-08-19 05:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is used to close down the service
|
|
|
|
func (sds *Service) Stop() error {
|
2020-08-19 16:35:09 +00:00
|
|
|
logrus.Info("stopping statediff service")
|
2021-10-25 19:06:05 +00:00
|
|
|
close(sds.quitChan)
|
2020-08-19 05:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-10 09:07:59 +00:00
|
|
|
|
|
|
|
// WriteStateDiffAt writes a state diff at the specific blockheight directly to the database
|
|
|
|
// This operation cannot be performed back past the point of db pruning; it requires an archival node
|
|
|
|
// for historical data
|
|
|
|
func (sds *Service) WriteStateDiffAt(blockNumber uint64, params sd.Params) error {
|
2021-11-09 01:08:36 +00:00
|
|
|
logrus.Infof("Writing state diff at block %d", blockNumber)
|
2021-10-21 18:00:30 +00:00
|
|
|
t := time.Now()
|
2020-11-10 09:07:59 +00:00
|
|
|
currentBlock, err := sds.lvlDBReader.GetBlockByNumber(blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2022-07-12 11:08:05 +00:00
|
|
|
// compute leaf paths of watched addresses in the params
|
2022-07-01 07:59:48 +00:00
|
|
|
params.ComputeWatchedAddressesLeafPaths()
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
parentRoot := common.Hash{}
|
|
|
|
if blockNumber != 0 {
|
|
|
|
parentBlock, err := sds.lvlDBReader.GetBlockByHash(currentBlock.ParentHash())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
parentRoot = parentBlock.Root()
|
|
|
|
}
|
2021-10-21 18:00:30 +00:00
|
|
|
return sds.writeStateDiff(currentBlock, parentRoot, params, t)
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 18:39:16 +00:00
|
|
|
// WriteStateDiffFor writes a state diff for the specific blockHash directly to the database
|
|
|
|
// This operation cannot be performed back past the point of db pruning; it requires an archival node
|
|
|
|
// for historical data
|
|
|
|
func (sds *Service) WriteStateDiffFor(blockHash common.Hash, params sd.Params) error {
|
2021-11-09 01:08:36 +00:00
|
|
|
logrus.Infof("Writing state diff for block %s", blockHash.Hex())
|
2021-10-21 18:00:30 +00:00
|
|
|
t := time.Now()
|
2021-02-27 18:39:16 +00:00
|
|
|
currentBlock, err := sds.lvlDBReader.GetBlockByHash(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2022-07-12 11:08:05 +00:00
|
|
|
// compute leaf paths of watched addresses in the params
|
2022-07-01 07:59:48 +00:00
|
|
|
params.ComputeWatchedAddressesLeafPaths()
|
2022-05-12 10:21:46 +00:00
|
|
|
|
2021-02-27 18:39:16 +00:00
|
|
|
parentRoot := common.Hash{}
|
|
|
|
if currentBlock.NumberU64() != 0 {
|
|
|
|
parentBlock, err := sds.lvlDBReader.GetBlockByHash(currentBlock.ParentHash())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
parentRoot = parentBlock.Root()
|
|
|
|
}
|
2021-10-21 18:00:30 +00:00
|
|
|
return sds.writeStateDiff(currentBlock, parentRoot, params, t)
|
2021-02-27 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
// Writes a state diff from the current block, parent state root, and provided params
|
2021-10-21 18:00:30 +00:00
|
|
|
func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, params sd.Params, t time.Time) error {
|
2020-11-10 09:07:59 +00:00
|
|
|
var totalDifficulty *big.Int
|
|
|
|
var receipts types.Receipts
|
|
|
|
var err error
|
|
|
|
if params.IncludeTD {
|
|
|
|
totalDifficulty, err = sds.lvlDBReader.GetTdByHash(block.Hash())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if params.IncludeReceipts {
|
|
|
|
receipts, err = sds.lvlDBReader.GetReceiptsByHash(block.Hash())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-21 18:00:30 +00:00
|
|
|
height := block.Number().Int64()
|
|
|
|
prom.SetLastLoadedHeight(height)
|
2021-10-24 23:36:49 +00:00
|
|
|
prom.SetTimeMetric(prom.T_BLOCK_LOAD, time.Now().Sub(t))
|
2021-10-21 18:00:30 +00:00
|
|
|
t = time.Now()
|
2020-11-10 09:07:59 +00:00
|
|
|
tx, err := sds.indexer.PushBlock(block, receipts, totalDifficulty)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// defer handling of commit/rollback for any return case
|
2020-11-17 01:37:36 +00:00
|
|
|
output := func(node sdtypes.StateNode) error {
|
2021-11-19 00:43:16 +00:00
|
|
|
return sds.indexer.PushStateNode(tx, node, block.Hash().String())
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2020-11-17 01:37:36 +00:00
|
|
|
codeOutput := func(c sdtypes.CodeAndCodeHash) error {
|
2020-11-10 09:07:59 +00:00
|
|
|
return sds.indexer.PushCodeAndCodeHash(tx, c)
|
|
|
|
}
|
2021-10-24 23:36:49 +00:00
|
|
|
prom.SetTimeMetric(prom.T_BLOCK_PROCESSING, time.Now().Sub(t))
|
2021-10-21 18:00:30 +00:00
|
|
|
t = time.Now()
|
2021-11-13 00:45:08 +00:00
|
|
|
err = sds.Builder.WriteStateDiffObject(sdtypes.StateRoots{
|
2020-11-10 09:07:59 +00:00
|
|
|
NewStateRoot: block.Root(),
|
|
|
|
OldStateRoot: parentRoot,
|
|
|
|
}, params, output, codeOutput)
|
2021-10-24 23:36:49 +00:00
|
|
|
prom.SetTimeMetric(prom.T_STATE_PROCESSING, time.Now().Sub(t))
|
2021-10-21 18:00:30 +00:00
|
|
|
t = time.Now()
|
2021-11-13 00:45:08 +00:00
|
|
|
err = tx.Submit(err)
|
2021-10-21 18:00:30 +00:00
|
|
|
prom.SetLastProcessedHeight(height)
|
2021-10-24 23:36:49 +00:00
|
|
|
prom.SetTimeMetric(prom.T_POSTGRES_TX_COMMIT, time.Now().Sub(t))
|
2021-10-21 18:00:30 +00:00
|
|
|
return err
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2021-10-25 19:06:05 +00:00
|
|
|
|
|
|
|
// WriteStateDiffsInRange adds a RangeRequest to the work queue
|
|
|
|
func (sds *Service) WriteStateDiffsInRange(start, stop uint64, params sd.Params) error {
|
|
|
|
if stop < start {
|
|
|
|
return fmt.Errorf("invalid block range (%d, %d): stop height must be greater or equal to start height", start, stop)
|
|
|
|
}
|
|
|
|
blocked := time.NewTimer(30 * time.Second)
|
|
|
|
select {
|
|
|
|
case sds.queue <- RangeRequest{Start: start, Stop: stop, Params: params}:
|
2021-10-25 19:26:41 +00:00
|
|
|
prom.IncQueuedRanges()
|
|
|
|
logrus.Infof("added range (%d, %d) to the worker queue", start, stop)
|
2021-10-25 19:06:05 +00:00
|
|
|
return nil
|
|
|
|
case <-blocked.C:
|
|
|
|
return fmt.Errorf("unable to add range (%d, %d) to the worker queue", start, stop)
|
|
|
|
}
|
|
|
|
}
|