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

nodeapi: add automatic runtime history provider #383

Merged
merged 9 commits into from
Apr 22, 2023
5 changes: 3 additions & 2 deletions analyzer/aggregate_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/jackc/pgx/v5"

"github.com/oasisprotocol/oasis-indexer/analyzer/queries"
"github.com/oasisprotocol/oasis-indexer/common"
"github.com/oasisprotocol/oasis-indexer/config"
"github.com/oasisprotocol/oasis-indexer/log"
"github.com/oasisprotocol/oasis-indexer/metrics"
Expand Down Expand Up @@ -48,8 +49,8 @@ const (
// by (periodically) querying the `runtime_related_transactions` table.
var dailyActiveAccountsLayers = []string{
layerConsensus,
RuntimeEmerald.String(),
RuntimeSapphire.String(),
common.RuntimeEmerald.String(),
common.RuntimeSapphire.String(),
// RuntimeCipher.String(), // Enable once Cipher is supported by the indexer.
}

Expand Down
108 changes: 0 additions & 108 deletions analyzer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package analyzer

import (
"errors"
"strings"

oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

"github.com/oasisprotocol/oasis-indexer/storage"
)
Expand All @@ -17,14 +14,6 @@ var (
// ErrLatestBlockNotFound is returned if the analyzer has not indexed any
// blocks yet. This indicates to begin from the start of its range.
ErrLatestBlockNotFound = errors.New("latest block not found")

// ErrNetworkUnknown is returned if a chain context does not correspond
// to a known network identifier.
ErrNetworkUnknown = errors.New("network unknown")

// ErrRuntimeUnknown is returned if a chain context does not correspond
// to a known runtime identifier.
ErrRuntimeUnknown = errors.New("runtime unknown")
)

// Analyzer is a worker that analyzes a subset of the Oasis Network.
Expand Down Expand Up @@ -81,100 +70,3 @@ type RoundRange struct {
// To is the last block to process in this range, inclusive.
To uint64
}

// ChainID is the ID of a chain.
type ChainID string

// String returns the string representation of a ChainID.
func (c ChainID) String() string {
return string(c)
}

// Network is an instance of the Oasis Network.
type Network uint8

const (
// NetworkTestnet is the identifier for testnet.
NetworkTestnet Network = iota
// NetworkMainnet is the identifier for mainnet.
NetworkMainnet
// NetworkUnknown is the identifier for an unknown network.
NetworkUnknown = 255
)

// FromChainContext identifies a Network using its ChainContext.
func FromChainContext(chainContext string) (Network, error) {
// TODO: Remove this hardcoded value once indexer config supports multiple nodes.
if chainContext == "53852332637bacb61b91b6411ab4095168ba02a50be4c3f82448438826f23898" {
return NetworkMainnet, nil // cobalt mainnet
}
var network Network
for name, nw := range oasisConfig.DefaultNetworks.All {
if nw.ChainContext == chainContext {
if err := network.Set(name); err != nil {
return NetworkUnknown, err
}
return network, nil
}
}

return NetworkUnknown, ErrNetworkUnknown
}

// Set sets the Network to the value specified by the provided string.
func (n *Network) Set(s string) error {
switch strings.ToLower(s) {
case "mainnet":
*n = NetworkMainnet
case "testnet":
*n = NetworkTestnet
default:
return ErrNetworkUnknown
}

return nil
}

// String returns the string representation of a network.
func (n Network) String() string {
switch n {
case NetworkTestnet:
return "testnet"
case NetworkMainnet:
return "mainnet"
default:
return "unknown"
}
}

// Runtime is an identifier for a runtime on the Oasis Network.
type Runtime string
pro-wh marked this conversation as resolved.
Show resolved Hide resolved

const (
RuntimeEmerald Runtime = "emerald"
RuntimeCipher Runtime = "cipher"
RuntimeSapphire Runtime = "sapphire"
RuntimeUnknown Runtime = "unknown"
)

// String returns the string representation of a runtime.
func (r Runtime) String() string {
return string(r)
}

// ID returns the ID for a Runtime on the provided network.
func (r Runtime) ID(n Network) (string, error) {
for nname, nw := range oasisConfig.DefaultNetworks.All {
if nname == n.String() {
for pname, pt := range nw.ParaTimes.All {
if pname == r.String() {
return pt.ID, nil
}
}

return "", ErrRuntimeUnknown
}
}

return "", ErrRuntimeUnknown
}
4 changes: 2 additions & 2 deletions analyzer/evmtokenbalances/evm_token_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const (
)

type Main struct {
runtime analyzer.Runtime
runtime common.Runtime
cfg analyzer.RuntimeConfig
target storage.TargetStorage
logger *log.Logger
Expand All @@ -88,7 +88,7 @@ type Main struct {
var _ analyzer.Analyzer = (*Main)(nil)

func NewMain(
runtime analyzer.Runtime,
runtime common.Runtime,
sourceConfig *config.SourceConfig,
target storage.TargetStorage,
logger *log.Logger,
Expand Down
5 changes: 3 additions & 2 deletions analyzer/evmtokens/evm_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/oasisprotocol/oasis-indexer/analyzer/queries"
"github.com/oasisprotocol/oasis-indexer/analyzer/runtime"
"github.com/oasisprotocol/oasis-indexer/analyzer/util"
"github.com/oasisprotocol/oasis-indexer/common"
"github.com/oasisprotocol/oasis-indexer/config"
"github.com/oasisprotocol/oasis-indexer/log"
"github.com/oasisprotocol/oasis-indexer/storage"
Expand All @@ -35,7 +36,7 @@ const (
)

type Main struct {
runtime analyzer.Runtime
runtime common.Runtime
cfg analyzer.RuntimeConfig
target storage.TargetStorage
logger *log.Logger
Expand All @@ -44,7 +45,7 @@ type Main struct {
var _ analyzer.Analyzer = (*Main)(nil)

func NewMain(
runtime analyzer.Runtime,
runtime common.Runtime,
sourceConfig *config.SourceConfig,
target storage.TargetStorage,
logger *log.Logger,
Expand Down
26 changes: 13 additions & 13 deletions analyzer/runtime/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/oasisprotocol/oasis-core/go/runtime/client/api"
runtimeClient "github.com/oasisprotocol/oasis-core/go/runtime/client/api"
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"
cmdCommon "github.com/oasisprotocol/oasis-indexer/cmd/common"
"github.com/oasisprotocol/oasis-indexer/common"
"github.com/oasisprotocol/oasis-indexer/config"
"github.com/oasisprotocol/oasis-indexer/storage/oasis"
)
Expand All @@ -38,48 +38,48 @@ var (

func TestEVMDownloadTokenERC20(t *testing.T) {
ctx := context.Background()
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, common.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
require.NoError(t, err)
data, err := evmDownloadTokenERC20(ctx, common.Logger(), source, api.RoundLatest, tokenEthAddr)
data, err := evmDownloadTokenERC20(ctx, cmdCommon.Logger(), source, runtimeClient.RoundLatest, tokenEthAddr)
require.NoError(t, err)
t.Logf("data %#v", data)
}

func TestEVMDownloadTokenBalanceERC20(t *testing.T) {
ctx := context.Background()
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, common.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
require.NoError(t, err)
// An address that possesses no USDT.
accountEthAddr, err := hex.DecodeString("5555555555555555555555555555555555555555")
require.NoError(t, err)
balanceData, err := evmDownloadTokenBalanceERC20(ctx, common.Logger(), source, api.RoundLatest, tokenEthAddr, accountEthAddr)
balanceData, err := evmDownloadTokenBalanceERC20(ctx, cmdCommon.Logger(), source, runtimeClient.RoundLatest, tokenEthAddr, accountEthAddr)
require.NoError(t, err)
t.Logf("balance %#v", balanceData)
}

func TestEVMFailDeterministicUnoccupied(t *testing.T) {
ctx := context.Background()
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, common.RuntimeEmerald)
require.NoError(t, err)
// An address at which no smart contract exists.
tokenEthAddr, err := hex.DecodeString("5555555555555555555555555555555555555555")
require.NoError(t, err)
var name string
err = evmCallWithABI(ctx, source, api.RoundLatest, tokenEthAddr, evmabi.ERC20, &name, "name")
err = evmCallWithABI(ctx, source, runtimeClient.RoundLatest, tokenEthAddr, evmabi.ERC20, &name, "name")
require.Error(t, err)
fmt.Printf("getting ERC-20 name from unoccupied address should fail: %+v\n", err)
require.True(t, errors.Is(err, EVMDeterministicError{}))
}

func TestEVMFailDeterministicOutOfGas(t *testing.T) {
ctx := context.Background()
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, common.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
Expand All @@ -90,15 +90,15 @@ func TestEVMFailDeterministicOutOfGas(t *testing.T) {
gasLimit := uint64(10)
caller := ethCommon.Address{1}.Bytes()
value := []byte{0}
err = evmCallWithABICustom(ctx, source, api.RoundLatest, gasPrice, gasLimit, caller, tokenEthAddr, value, evmabi.ERC20, &name, "name")
err = evmCallWithABICustom(ctx, source, runtimeClient.RoundLatest, gasPrice, gasLimit, caller, tokenEthAddr, value, evmabi.ERC20, &name, "name")
require.Error(t, err)
fmt.Printf("query that runs out of gas should fail: %+v\n", err)
require.True(t, errors.Is(err, EVMDeterministicError{}))
}

func TestEVMFailDeterministicUnsupportedMethod(t *testing.T) {
ctx := context.Background()
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, analyzer.RuntimeEmerald)
source, err := oasis.NewRuntimeClient(ctx, PublicSourceConfig, common.RuntimeEmerald)
require.NoError(t, err)
// Wormhole bridged USDT on Emerald mainnet.
tokenEthAddr, err := hex.DecodeString("dC19A122e268128B5eE20366299fc7b5b199C8e3")
Expand All @@ -119,7 +119,7 @@ func TestEVMFailDeterministicUnsupportedMethod(t *testing.T) {
}]`))
require.NoError(t, err)
var name string
err = evmCallWithABI(ctx, source, api.RoundLatest, tokenEthAddr, &fakeABI, &name, "bike")
err = evmCallWithABI(ctx, source, runtimeClient.RoundLatest, tokenEthAddr, &fakeABI, &name, "bike")
require.Error(t, err)
fmt.Printf("querying an unsupported method should fail: %+v\n", err)
require.True(t, errors.Is(err, EVMDeterministicError{}))
Expand Down
9 changes: 5 additions & 4 deletions analyzer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

"github.com/oasisprotocol/oasis-indexer/analyzer"
"github.com/oasisprotocol/oasis-indexer/analyzer/queries"
common "github.com/oasisprotocol/oasis-indexer/analyzer/uncategorized"
uncategorized "github.com/oasisprotocol/oasis-indexer/analyzer/uncategorized"
"github.com/oasisprotocol/oasis-indexer/analyzer/util"
"github.com/oasisprotocol/oasis-indexer/common"
"github.com/oasisprotocol/oasis-indexer/config"
"github.com/oasisprotocol/oasis-indexer/log"
"github.com/oasisprotocol/oasis-indexer/metrics"
Expand All @@ -28,7 +29,7 @@ const (

// Main is the main Analyzer for runtimes.
type Main struct {
runtime analyzer.Runtime
runtime common.Runtime
cfg analyzer.RuntimeConfig
target storage.TargetStorage
logger *log.Logger
Expand All @@ -39,7 +40,7 @@ var _ analyzer.Analyzer = (*Main)(nil)

// NewRuntimeAnalyzer returns a new main analyzer for a runtime.
func NewRuntimeAnalyzer(
runtime analyzer.Runtime,
runtime common.Runtime,
sourceConfig *config.SourceConfig,
cfg *config.BlockBasedAnalyzerConfig,
target storage.TargetStorage,
Expand Down Expand Up @@ -299,7 +300,7 @@ func (m *Main) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {

// Insert events.
for _, eventData := range data.EventData {
eventRelatedAddresses := common.ExtractAddresses(eventData.RelatedAddresses)
eventRelatedAddresses := uncategorized.ExtractAddresses(eventData.RelatedAddresses)
batch.Queue(
queries.RuntimeEventInsert,
m.runtime,
Expand Down
Loading