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"
|
2020-08-19 16:35:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
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 {
|
|
|
|
// APIs(), Protocols(), Start() and Stop()
|
|
|
|
node.Service
|
|
|
|
// 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-04 16:50:49 +00:00
|
|
|
StateDiffAt(blockNumber uint64, params Params) (*Payload, error)
|
2020-08-19 05:12:58 +00:00
|
|
|
// Method to get state trie object at specific block
|
2020-09-04 16:50:49 +00:00
|
|
|
StateTrieAt(blockNumber uint64, params Params) (*Payload, 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
|
|
|
|
}
|
|
|
|
|
2020-09-04 16:50:49 +00:00
|
|
|
// NewStateDiffService creates a new Service
|
2020-08-19 05:12:58 +00:00
|
|
|
func NewStateDiffService(lvlDBReader lvlDBReader) (*Service, error) {
|
|
|
|
return &Service{
|
2020-08-19 05:57:57 +00:00
|
|
|
lvlDBReader: lvlDBReader,
|
2020-09-04 16:50:49 +00:00
|
|
|
Builder: NewBuilder(lvlDBReader.StateDB()),
|
2020-08-19 05:57:57 +00:00
|
|
|
QuitChan: make(chan bool),
|
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-04 16:50:49 +00:00
|
|
|
func (sds *Service) StateDiffAt(blockNumber uint64, params Params) (*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-04 16:50:49 +00:00
|
|
|
func (sds *Service) processStateDiff(currentBlock *types.Block, parentRoot common.Hash, params Params) (*Payload, error) {
|
|
|
|
stateDiff, err := sds.Builder.BuildStateDiffObject(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-04 16:50:49 +00:00
|
|
|
func (sds *Service) newPayload(stateObject []byte, block *types.Block, params Params) (*Payload, error) {
|
|
|
|
payload := &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-04 16:50:49 +00:00
|
|
|
func (sds *Service) StateTrieAt(blockNumber uint64, params Params) (*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-04 16:50:49 +00:00
|
|
|
func (sds *Service) processStateTrie(block *types.Block, params Params) (*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(*p2p.Server) 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
|
|
|
|
}
|