eth-statediff-service/pkg/service.go

283 lines
8.9 KiB
Go
Raw Normal View History

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"
2020-08-19 05:12:58 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"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"
sd "github.com/ethereum/go-ethereum/statediff"
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
2020-08-19 16:35:09 +00:00
"github.com/sirupsen/logrus"
ind "github.com/ethereum/go-ethereum/statediff/indexer"
2020-08-19 05:12:58 +00:00
)
// lvlDBReader are the db interfaces required by the statediffing service
type lvlDBReader interface {
GetBlockByHash(hash common.Hash) (*types.Block, error)
GetBlockByNumber(number uint64) (*types.Block, error)
GetReceiptsByHash(hash common.Hash) (types.Receipts, error)
GetTdByHash(hash common.Hash) (*big.Int, error)
StateDB() state.Database
}
// IService is the state-diffing service interface
type IService interface {
// Start() and Stop()
node.Lifecycle
// For node service registration
APIs() []rpc.API
Protocols() []p2p.Protocol
2020-08-19 05:12:58 +00:00
// Main event loop for processing state diffs
2020-08-19 05:57:57 +00:00
Loop(wg *sync.WaitGroup)
2020-08-19 05:12:58 +00:00
// 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)
2020-08-19 05:12:58 +00:00
// 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)
// Method to write state diff object directly to DB
WriteStateDiffAt(blockNumber 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
2020-09-04 16:50:49 +00:00
Builder Builder
2020-08-19 05:12:58 +00:00
// Used to read data from leveldb
lvlDBReader lvlDBReader
// Used to signal shutdown of the service
QuitChan chan bool
// Interface for publishing statediffs as PG-IPLD objects
indexer ind.Indexer
2020-08-19 05:12:58 +00:00
}
2020-09-04 16:50:49 +00:00
// NewStateDiffService creates a new Service
func NewStateDiffService(lvlDBReader lvlDBReader, indexer ind.Indexer, workers uint) (*Service, error) {
builder, err := NewBuilder(lvlDBReader.StateDB(), workers)
2020-09-15 06:03:46 +00:00
if err != nil {
return nil, err
}
2020-08-19 05:12:58 +00:00
return &Service{
2020-08-19 05:57:57 +00:00
lvlDBReader: lvlDBReader,
2020-09-15 06:03:46 +00:00
Builder: builder,
2020-08-19 05:57:57 +00:00
QuitChan: make(chan bool),
indexer: indexer,
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,
},
}
}
// Loop is an empty service loop for awaiting rpc requests
2020-08-19 05:57:57 +00:00
func (sds *Service) Loop(wg *sync.WaitGroup) {
wg.Add(1)
2020-08-19 05:12:58 +00:00
for {
select {
case <-sds.QuitChan:
2020-08-19 16:35:09 +00:00
logrus.Info("closing the statediff service loop")
2020-08-19 05:57:57 +00:00
wg.Done()
2020-08-19 05:12:58 +00:00
return
}
}
}
// 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
}
2020-08-19 16:35:09 +00:00
logrus.Info(fmt.Sprintf("sending state diff at block %d", blockNumber))
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)
}
// 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
}
stateDiffRlp, err := rlp.EncodeToBytes(stateDiff)
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
}
2020-08-19 16:35:09 +00:00
logrus.Info(fmt.Sprintf("sending state trie at block %d", blockNumber))
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
}
stateTrieRlp, err := rlp.EncodeToBytes(stateNodes)
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
func (sds *Service) Start() error {
2020-08-19 16:35:09 +00:00
logrus.Info("starting statediff service")
2020-08-19 05:57:57 +00:00
go sds.Loop(new(sync.WaitGroup))
2020-08-19 05:12:58 +00:00
return nil
}
// 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")
2020-08-19 05:12:58 +00:00
close(sds.QuitChan)
return nil
}
// 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 {
logrus.Info(fmt.Sprintf("writing state diff at block %d", blockNumber))
currentBlock, err := sds.lvlDBReader.GetBlockByNumber(blockNumber)
if err != nil {
return err
}
parentRoot := common.Hash{}
if blockNumber != 0 {
parentBlock, err := sds.lvlDBReader.GetBlockByHash(currentBlock.ParentHash())
if err != nil {
return err
}
parentRoot = parentBlock.Root()
}
return sds.writeStateDiff(currentBlock, parentRoot, params)
}
// Writes a state diff from the current block, parent state root, and provided params
func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, params sd.Params) error {
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
}
tx, err := sds.indexer.PushBlock(block, receipts, totalDifficulty)
if err != nil {
return err
}
// defer handling of commit/rollback for any return case
defer tx.Close()
output := func(node sdtypes.StateNode) error {
return sds.indexer.PushStateNode(tx, node)
}
codeOutput := func(c sdtypes.CodeAndCodeHash) error {
return sds.indexer.PushCodeAndCodeHash(tx, c)
}
err = sds.Builder.WriteStateDiffObject(sd.StateRoots{
NewStateRoot: block.Root(),
OldStateRoot: parentRoot,
}, params, output, codeOutput)
return nil
}