Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: archive node configuration #352

Merged
merged 8 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analyzer/aggregate_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (a *AggregateStatsAnalyzer) Name() string {
return AggregateStatsAnalyzerName
}

func NewAggregateStatsAnalyzer(chainID string, cfg *config.AggregateStatsConfig, target storage.TargetStorage, logger *log.Logger) (*AggregateStatsAnalyzer, error) {
func NewAggregateStatsAnalyzer(cfg *config.AggregateStatsConfig, target storage.TargetStorage, logger *log.Logger) (*AggregateStatsAnalyzer, error) {
logger.Info("starting aggregate_stats analyzer")
return &AggregateStatsAnalyzer{
target: target,
Expand Down
9 changes: 3 additions & 6 deletions analyzer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ type Analyzer interface {
// ConsensusConfig specifies configuration parameters for
// for processing the consensus layer.
type ConsensusConfig struct {
// ChainID is the chain ID for the underlying network.
ChainID string

// ChainContext is the ChainContext (= chain identifier, based on a hash of the
// genesis file) for the underlying network.
ChainContext string
// GenesisChainContext is the chain context that specifies which genesis
// file to analyze.
GenesisChainContext string

// Range is the range of blocks to process.
// If this is set, the analyzer analyzes blocks in the provided range.
Expand Down
61 changes: 34 additions & 27 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

"github.com/oasisprotocol/oasis-indexer/analyzer"
"github.com/oasisprotocol/oasis-indexer/analyzer/queries"
Expand Down Expand Up @@ -42,6 +41,21 @@ type parsedEvent struct {
relatedAddresses []staking.Address
}

// OpenSignedTxNoVerify decodes the Transaction inside a Signed transaction
// without verifying the signature. Callers should be sure to check if the
// transaction actually succeeded. The indexer trusts its oasis-node to
// provide the correct transaction result, which will indicate if there was an
// authentication problem. Skipping the verification saves CPU on the indexer.
// Due to the chain context being global, we cannot verify transactions for
// multiple networks anyway.
func OpenSignedTxNoVerify(signedTx *transaction.SignedTransaction) (*transaction.Transaction, error) {
pro-wh marked this conversation as resolved.
Show resolved Hide resolved
var tx transaction.Transaction
if err := cbor.Unmarshal(signedTx.Blob, &tx); err != nil {
return nil, fmt.Errorf("signed tx unmarshal: %w", err)
}
return &tx, nil
}

// Main is the main Analyzer for the consensus layer.
type Main struct {
cfg analyzer.ConsensusConfig
Expand All @@ -53,22 +67,11 @@ type Main struct {
var _ analyzer.Analyzer = (*Main)(nil)

// NewMain returns a new main analyzer for the consensus layer.
func NewMain(nodeCfg config.NodeConfig, cfg *config.BlockBasedAnalyzerConfig, target storage.TargetStorage, logger *log.Logger) (*Main, error) {
func NewMain(sourceConfig *config.SourceConfig, cfg *config.BlockBasedAnalyzerConfig, target storage.TargetStorage, logger *log.Logger) (*Main, error) {
ctx := context.Background()

// Initialize source storage.
networkCfg := oasisConfig.Network{
ChainContext: nodeCfg.ChainContext,
RPC: nodeCfg.RPC,
}
factory, err := source.NewClientFactory(ctx, &networkCfg, nodeCfg.FastStartup)
if err != nil {
logger.Error("error creating client factory",
"err", err.Error(),
)
return nil, err
}
client, err := factory.Consensus()
client, err := source.NewConsensusClient(ctx, sourceConfig)
if err != nil {
logger.Error("error creating consensus client",
"err", err.Error(),
Expand All @@ -82,10 +85,9 @@ func NewMain(nodeCfg config.NodeConfig, cfg *config.BlockBasedAnalyzerConfig, ta
To: cfg.To,
}
ac := analyzer.ConsensusConfig{
ChainID: nodeCfg.ChainID,
ChainContext: nodeCfg.ChainContext,
Range: blockRange,
Source: client,
GenesisChainContext: sourceConfig.History().CurrentRecord().ChainContext,
Range: blockRange,
Source: client,
}

logger.Info("Starting consensus analyzer", "config", ac)
Expand All @@ -104,15 +106,15 @@ func (m *Main) Start() {
// Get block to be indexed.
var height int64

isGenesisProcessed, err := m.isGenesisProcessed(ctx)
isGenesisProcessed, err := m.isGenesisProcessed(ctx, m.cfg.GenesisChainContext)
if err != nil {
m.logger.Error("failed to check if genesis is processed",
"err", err.Error(),
)
return
}
if !isGenesisProcessed {
if err = m.processGenesis(ctx); err != nil {
if err = m.processGenesis(ctx, m.cfg.GenesisChainContext); err != nil {
m.logger.Error("failed to process genesis",
"err", err.Error(),
)
Expand Down Expand Up @@ -204,19 +206,19 @@ func (m *Main) latestBlock(ctx context.Context) (int64, error) {
return latest, nil
}

func (m *Main) isGenesisProcessed(ctx context.Context) (bool, error) {
func (m *Main) isGenesisProcessed(ctx context.Context, chainContext string) (bool, error) {
var processed bool
if err := m.target.QueryRow(
ctx,
queries.IsGenesisProcessed,
m.cfg.ChainContext,
chainContext,
).Scan(&processed); err != nil {
return false, err
}
return processed, nil
}

func (m *Main) processGenesis(ctx context.Context) error {
func (m *Main) processGenesis(ctx context.Context, chainContext string) error {
m.logger.Info("fetching genesis document")
genesisDoc, err := m.cfg.Source.GenesisDocument(ctx)
if err != nil {
Expand Down Expand Up @@ -247,7 +249,7 @@ func (m *Main) processGenesis(ctx context.Context) error {
}
batch.Queue(
queries.GenesisIndexingProgress,
m.cfg.ChainContext,
chainContext,
)
if err := m.target.SendBatch(ctx, batch); err != nil {
return err
Expand Down Expand Up @@ -400,8 +402,13 @@ func (m *Main) queueTransactionInserts(batch *storage.QueryBatch, data *storage.
signedTx := txr.Transaction
result := txr.Result

var tx transaction.Transaction
if err := signedTx.Open(&tx); err != nil {
tx, err := OpenSignedTxNoVerify(&signedTx)
if err != nil {
m.logger.Info("couldn't parse transaction",
"err", err,
"height", data.Height,
"tx_index", i,
)
continue
}

Expand Down Expand Up @@ -443,7 +450,7 @@ func (m *Main) queueTransactionInserts(batch *storage.QueryBatch, data *storage.

// TODO: Use event when available
// https://github.com/oasisprotocol/oasis-core/issues/4818
if tx.Method == "staking.AmendCommissionSchedule" {
if tx.Method == "staking.AmendCommissionSchedule" && result.IsSuccess() {
var rawSchedule staking.AmendCommissionSchedule
if err := cbor.Unmarshal(tx.Body, &rawSchedule); err != nil {
return err
Expand Down
28 changes: 2 additions & 26 deletions analyzer/evmtokenbalances/evm_token_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/big"
"time"

oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"golang.org/x/sync/errgroup"

"github.com/oasisprotocol/oasis-indexer/analyzer"
Expand Down Expand Up @@ -89,37 +88,14 @@ var _ analyzer.Analyzer = (*Main)(nil)

func NewMain(
runtime analyzer.Runtime,
nodeCfg *config.NodeConfig,
sourceConfig *config.SourceConfig,
target storage.TargetStorage,
logger *log.Logger,
) (*Main, error) {
ctx := context.Background()

// Initialize source storage.
networkCfg := oasisConfig.Network{
ChainContext: nodeCfg.ChainContext,
RPC: nodeCfg.RPC,
}
factory, err := oasis.NewClientFactory(ctx, &networkCfg, nodeCfg.FastStartup)
if err != nil {
logger.Error("error creating client factory",
"err", err,
)
return nil, err
}

network, err := analyzer.FromChainContext(nodeCfg.ChainContext)
if err != nil {
return nil, err
}

id, err := runtime.ID(network)
if err != nil {
return nil, err
}
logger.Info("Runtime ID determined", "runtime", runtime.String(), "runtime_id", id)

client, err := factory.Runtime(id)
client, err := oasis.NewRuntimeClient(ctx, sourceConfig, runtime)
if err != nil {
logger.Error("error creating runtime client",
"err", err,
Expand Down
28 changes: 2 additions & 26 deletions analyzer/evmtokens/evm_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"golang.org/x/sync/errgroup"

"github.com/oasisprotocol/oasis-indexer/analyzer"
Expand Down Expand Up @@ -45,37 +44,14 @@ var _ analyzer.Analyzer = (*Main)(nil)

func NewMain(
runtime analyzer.Runtime,
nodeCfg *config.NodeConfig,
sourceConfig *config.SourceConfig,
target storage.TargetStorage,
logger *log.Logger,
) (*Main, error) {
ctx := context.Background()

// Initialize source storage.
networkCfg := oasisConfig.Network{
ChainContext: nodeCfg.ChainContext,
RPC: nodeCfg.RPC,
}
factory, err := oasis.NewClientFactory(ctx, &networkCfg, nodeCfg.FastStartup)
if err != nil {
logger.Error("error creating client factory",
"err", err,
)
return nil, err
}

network, err := analyzer.FromChainContext(nodeCfg.ChainContext)
if err != nil {
return nil, err
}

id, err := runtime.ID(network)
if err != nil {
return nil, err
}
logger.Info("Runtime ID determined", "runtime", runtime.String(), "runtime_id", id)

client, err := factory.Runtime(id)
Comment on lines -55 to -78
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rojo que the quiero rojo 😗

all these client setup blocks look so much saner now ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

client, err := oasis.NewRuntimeClient(ctx, sourceConfig, runtime)
if err != nil {
logger.Error("error creating runtime client",
"err", err,
Expand Down
6 changes: 1 addition & 5 deletions analyzer/metadata_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ func (a *MetadataRegistryAnalyzer) Name() string {
return MetadataRegistryAnalyzerName
}

func NewMetadataRegistryAnalyzer(chainID string, cfg *config.MetadataRegistryConfig, target storage.TargetStorage, logger *log.Logger) (*MetadataRegistryAnalyzer, error) {
if chainID == "" {
return nil, fmt.Errorf("metadata_registry analyzer: `ChainID` must be specified in the config")
}

func NewMetadataRegistryAnalyzer(cfg *config.MetadataRegistryConfig, target storage.TargetStorage, logger *log.Logger) (*MetadataRegistryAnalyzer, error) {
logger.Info("Starting metadata_registry analyzer")
return &MetadataRegistryAnalyzer{
interval: cfg.Interval,
Expand Down
44 changes: 23 additions & 21 deletions analyzer/runtime/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,34 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/oasisprotocol/oasis-core/go/runtime/client/api"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-indexer/analyzer"
"github.com/oasisprotocol/oasis-indexer/analyzer/evmabi"
"github.com/oasisprotocol/oasis-indexer/cmd/common"
"github.com/oasisprotocol/oasis-indexer/config"
"github.com/oasisprotocol/oasis-indexer/storage/oasis"
)

func TestEVMDownloadTokenERC20(t *testing.T) {
var (
ChainName = "mainnet"
CurrentArchiveName = config.DefaultChains[ChainName].CurrentRecord().ArchiveName
// TODO: Would be nice to have an offline test.
PublicSourceConfig = &config.SourceConfig{
ChainName: ChainName,
Nodes: map[string]*config.NodeConfig{
CurrentArchiveName: {
RPC: sdkConfig.DefaultNetworks.All[ChainName].RPC,
},
},
FastStartup: false,
}
)

func TestEVMDownloadTokenERC20(t *testing.T) {
ctx := context.Background()
cf, err := oasis.NewClientFactory(ctx, config.DefaultNetworks.All["mainnet"], true)
require.NoError(t, err)
source, err := cf.Runtime(config.DefaultNetworks.All["mainnet"].ParaTimes.All["emerald"].ID)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
Expand All @@ -35,11 +49,8 @@ func TestEVMDownloadTokenERC20(t *testing.T) {
}

func TestEVMDownloadTokenBalanceERC20(t *testing.T) {
// TODO: Would be nice to have an offline test.
ctx := context.Background()
cf, err := oasis.NewClientFactory(ctx, config.DefaultNetworks.All["mainnet"], true)
require.NoError(t, err)
source, err := cf.Runtime(config.DefaultNetworks.All["mainnet"].ParaTimes.All["emerald"].ID)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
Expand All @@ -53,11 +64,8 @@ func TestEVMDownloadTokenBalanceERC20(t *testing.T) {
}

func TestEVMFailDeterministicUnoccupied(t *testing.T) {
// TODO: Would be nice to have an offline test.
ctx := context.Background()
cf, err := oasis.NewClientFactory(ctx, config.DefaultNetworks.All["mainnet"], true)
require.NoError(t, err)
source, err := cf.Runtime(config.DefaultNetworks.All["mainnet"].ParaTimes.All["emerald"].ID)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
require.NoError(t, err)
// An address at which no smart contract exists.
tokenEthAddr, err := hex.DecodeString("5555555555555555555555555555555555555555")
Expand All @@ -70,11 +78,8 @@ func TestEVMFailDeterministicUnoccupied(t *testing.T) {
}

func TestEVMFailDeterministicOutOfGas(t *testing.T) {
// TODO: Would be nice to have an offline test.
ctx := context.Background()
cf, err := oasis.NewClientFactory(ctx, config.DefaultNetworks.All["mainnet"], true)
require.NoError(t, err)
source, err := cf.Runtime(config.DefaultNetworks.All["mainnet"].ParaTimes.All["emerald"].ID)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
Expand All @@ -92,11 +97,8 @@ func TestEVMFailDeterministicOutOfGas(t *testing.T) {
}

func TestEVMFailDeterministicUnsupportedMethod(t *testing.T) {
// TODO: Would be nice to have an offline test.
ctx := context.Background()
cf, err := oasis.NewClientFactory(ctx, config.DefaultNetworks.All["mainnet"], true)
require.NoError(t, err)
source, err := cf.Runtime(config.DefaultNetworks.All["mainnet"].ParaTimes.All["emerald"].ID)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
Expand Down
Loading