2023-06-14 12:43:34 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2021 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 statediff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-06-23 12:42:55 +00:00
|
|
|
|
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/interfaces"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/utils"
|
2023-06-14 12:43:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains instantiation parameters for the state diffing service
|
|
|
|
type Config struct {
|
|
|
|
// The configuration used for the stateDiff Indexer
|
|
|
|
IndexerConfig interfaces.Config
|
|
|
|
// A unique ID used for this service
|
|
|
|
ID string
|
|
|
|
// Name for the client this service is running
|
|
|
|
ClientName string
|
|
|
|
// Whether to enable writing state diffs directly to track blockchain head
|
|
|
|
EnableWriteLoop bool
|
2023-06-24 04:04:57 +00:00
|
|
|
// The maximum number of blocks to backfill when tracking head.
|
2023-07-20 02:15:48 +00:00
|
|
|
BackfillMaxDepth uint64
|
2023-06-24 04:04:57 +00:00
|
|
|
// The maximum number of blocks behind the startup position to check for gaps.
|
|
|
|
BackfillCheckPastBlocks uint64
|
2023-06-14 12:43:34 +00:00
|
|
|
// Size of the worker pool
|
|
|
|
NumWorkers uint
|
|
|
|
// Should the statediff service wait until geth has synced to the head of the blockchain?
|
|
|
|
WaitForSync bool
|
2023-08-27 03:57:26 +00:00
|
|
|
// Context passed to all DB method calls
|
2023-06-14 12:43:34 +00:00
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// Params contains config parameters for the state diff builder
|
|
|
|
type Params struct {
|
2023-08-27 03:57:26 +00:00
|
|
|
IncludeBlock bool // TODO: not used in write-requests
|
2023-06-14 12:43:34 +00:00
|
|
|
IncludeReceipts bool
|
|
|
|
IncludeTD bool
|
2023-08-27 03:57:26 +00:00
|
|
|
IncludeCode bool // TODO: not used by anything?
|
2023-06-14 12:43:34 +00:00
|
|
|
WatchedAddresses []common.Address
|
|
|
|
watchedAddressesLeafPaths [][]byte
|
|
|
|
}
|
|
|
|
|
2023-07-20 02:15:48 +00:00
|
|
|
func (p *Params) Copy() Params {
|
|
|
|
ret := Params{
|
|
|
|
IncludeBlock: p.IncludeBlock,
|
|
|
|
IncludeReceipts: p.IncludeReceipts,
|
|
|
|
IncludeTD: p.IncludeTD,
|
|
|
|
IncludeCode: p.IncludeCode,
|
|
|
|
}
|
|
|
|
ret.WatchedAddresses = make([]common.Address, len(p.WatchedAddresses))
|
|
|
|
copy(ret.WatchedAddresses, p.WatchedAddresses)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
// ComputeWatchedAddressesLeafPaths populates a slice with paths (hex_encoding(Keccak256)) of each of the WatchedAddresses
|
|
|
|
func (p *Params) ComputeWatchedAddressesLeafPaths() {
|
|
|
|
p.watchedAddressesLeafPaths = make([][]byte, len(p.WatchedAddresses))
|
|
|
|
for i, address := range p.WatchedAddresses {
|
2023-06-23 12:42:55 +00:00
|
|
|
p.watchedAddressesLeafPaths[i] = utils.KeybytesToHex(crypto.Keccak256(address[:]))
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParamsWithMutex allows to lock the parameters while they are being updated | read from
|
|
|
|
type ParamsWithMutex struct {
|
|
|
|
Params
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2023-07-20 02:15:48 +00:00
|
|
|
// CopyParams returns a defensive copy of the Params
|
|
|
|
func (p *ParamsWithMutex) CopyParams() Params {
|
|
|
|
p.RLock()
|
2023-07-22 17:33:58 +00:00
|
|
|
copy := p.Params.Copy()
|
|
|
|
p.RUnlock()
|
2023-07-20 02:15:48 +00:00
|
|
|
|
2023-07-22 17:33:58 +00:00
|
|
|
copy.ComputeWatchedAddressesLeafPaths()
|
|
|
|
return copy
|
2023-07-20 02:15:48 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
// Args bundles the arguments for the state diff builder
|
|
|
|
type Args struct {
|
2023-07-19 06:35:54 +00:00
|
|
|
OldStateRoot, NewStateRoot common.Hash
|
|
|
|
BlockHash common.Hash
|
|
|
|
BlockNumber *big.Int
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|