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

statecheck: Do not use indexer-internal APIs for node access #361

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 0 additions & 5 deletions storage/oasis/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ func (cc *ConsensusClient) GenesisDocument(ctx context.Context) (*genesisAPI.Doc
return cc.nodeApi.GetGenesisDocument(ctx)
}

// GenesisDocumentAtHeight returns the genesis document at the provided height.
func (cc *ConsensusClient) GenesisDocumentAtHeight(ctx context.Context, height int64) (*genesisAPI.Document, error) {
return cc.nodeApi.StateToGenesis(ctx, height)
}

// Name returns the name of the client, for the ConsensusSourceStorage interface.
func (cc *ConsensusClient) Name() string {
return fmt.Sprintf("%s_consensus", moduleName)
Expand Down
9 changes: 0 additions & 9 deletions storage/oasis/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
config "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
connection "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
runtimeSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/accounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm"
sdkTypes "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"

Expand Down Expand Up @@ -65,14 +64,6 @@ func (rc *RuntimeClient) GetEventsRaw(ctx context.Context, round uint64) ([]*sdk
return rc.client.GetEventsRaw(ctx, round)
}

func (rc *RuntimeClient) GetAccountAddresses(ctx context.Context, round uint64, denomination sdkTypes.Denomination) (accounts.Addresses, error) {
return rc.client.Accounts.Addresses(ctx, round, denomination)
}

func (rc *RuntimeClient) GetAccountBalances(ctx context.Context, round uint64, addr sdkTypes.Address) (*accounts.AccountBalances, error) {
return rc.client.Accounts.Balances(ctx, round, addr)
}

func (rc *RuntimeClient) EVMSimulateCall(ctx context.Context, round uint64, gasPrice []byte, gasLimit uint64, caller []byte, address []byte, value []byte, data []byte) ([]byte, error) {
return evm.NewV1(rc.client).SimulateCall(ctx, round, gasPrice, gasLimit, caller, address, value, data)
}
Expand Down
16 changes: 7 additions & 9 deletions tests/statecheck/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

"github.com/oasisprotocol/oasis-core/go/common/entity"
"github.com/oasisprotocol/oasis-core/go/common/node"
consensusAPI "github.com/oasisprotocol/oasis-core/go/consensus/api"
genesisAPI "github.com/oasisprotocol/oasis-core/go/genesis/api"
governanceAPI "github.com/oasisprotocol/oasis-core/go/governance/api"
registryAPI "github.com/oasisprotocol/oasis-core/go/registry/api"
stakingAPI "github.com/oasisprotocol/oasis-core/go/staking/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-indexer/storage/oasis"
"github.com/oasisprotocol/oasis-indexer/storage/postgres"
"github.com/oasisprotocol/oasis-indexer/tests"
)
Expand Down Expand Up @@ -137,11 +137,9 @@ func TestGenesisFull(t *testing.T) {

ctx := context.Background()

factory, err := newSourceClientFactory()
require.Nil(t, err)

oasisClient, err := factory.Consensus()
conn, err := newSdkConnection(ctx)
require.Nil(t, err)
oasisClient := conn.Consensus()
Copy link
Collaborator

Choose a reason for hiding this comment

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

ℹ️ the factory would do signature.SetChainContext, where the SDK connection doesn't. we have a few .Open(), but luckily they're on registry things that don't use the chain context

Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks for the tip; i was making the same mistake in the api-tests


postgresClient, err := newTargetClient(t)
assert.Nil(t, err)
Expand All @@ -167,7 +165,7 @@ func TestGenesisFull(t *testing.T) {
}
} else {
t.Log("Fetching state dump at height", height, "from node")
genesis, err = oasisClient.GenesisDocumentAtHeight(ctx, height)
genesis, err = oasisClient.StateToGenesis(ctx, height)
require.Nil(t, err)
}
registryGenesis := &genesis.Registry
Expand All @@ -180,7 +178,7 @@ func TestGenesisFull(t *testing.T) {
validateGovernanceBackend(t, governanceGenesis, postgresClient)
}

func validateRegistryBackend(t *testing.T, genesis *registryAPI.Genesis, source *oasis.ConsensusClient, target *postgres.Client, height int64) {
func validateRegistryBackend(t *testing.T, genesis *registryAPI.Genesis, source consensusAPI.ClientBackend, target *postgres.Client, height int64) {
t.Log("=== Validating registry backend ===")

validateEntities(t, genesis, target)
Expand Down Expand Up @@ -296,11 +294,11 @@ func validateEntities(t *testing.T, genesis *registryAPI.Genesis, target *postgr
}
}

func validateNodes(t *testing.T, genesis *registryAPI.Genesis, source *oasis.ConsensusClient, target *postgres.Client, height int64) {
func validateNodes(t *testing.T, genesis *registryAPI.Genesis, source consensusAPI.ClientBackend, target *postgres.Client, height int64) {
t.Log("Validating nodes...")
ctx := context.Background()

epoch, err := source.GetEpoch(ctx, height)
epoch, err := source.Beacon().GetEpoch(ctx, height)
assert.Nil(t, err)

expectedNodes := make(map[string]TestNode)
Expand Down
46 changes: 35 additions & 11 deletions tests/statecheck/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
sdkTypes "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -38,24 +39,24 @@ func testRuntimeAccounts(t *testing.T, runtime string) {

ctx := context.Background()

factory, err := newSourceClientFactory()
require.Nil(t, err)

network, err := analyzer.FromChainContext(MainnetChainContext)
require.Nil(t, err)

var id string
var runtimeID string
switch runtime {
case "emerald":
id, err = analyzer.RuntimeEmerald.ID(network)
runtimeID, err = analyzer.RuntimeEmerald.ID(network)
case "sapphire":
id, err = analyzer.RuntimeSapphire.ID(network)
runtimeID, err = analyzer.RuntimeSapphire.ID(network)
}
require.Nil(t, err)
t.Log("Runtime ID determined", "runtime", runtime, "runtime_id", id)
t.Log("Runtime ID determined", "runtime", runtime, "runtime_id", runtimeID)

oasisRuntimeClient, err := factory.Runtime(id)
conn, err := newSdkConnection(ctx)
require.Nil(t, err)
oasisRuntimeClient := conn.Runtime(&config.ParaTime{
ID: runtimeID,
})

postgresClient, err := newTargetClient(t)
assert.Nil(t, err)
Expand All @@ -65,7 +66,7 @@ func testRuntimeAccounts(t *testing.T, runtime string) {
assert.Nil(t, err)

t.Logf("Fetching accounts information at height %d...", height)
addresses, err := oasisRuntimeClient.GetAccountAddresses(ctx, uint64(height), sdkTypes.NativeDenomination)
addresses, err := oasisRuntimeClient.Accounts.Addresses(ctx, uint64(height), sdkTypes.NativeDenomination)
assert.Nil(t, err)
expectedAccts := make(map[sdkTypes.Address]bool)
for _, addr := range addresses {
Expand Down Expand Up @@ -101,10 +102,10 @@ func testRuntimeAccounts(t *testing.T, runtime string) {
}

// Check that the account balance is accurate.
balances, err := oasisRuntimeClient.GetAccountBalances(ctx, uint64(height), actualAddr)
balances, err := oasisRuntimeClient.Accounts.Balances(ctx, uint64(height), actualAddr)
assert.Nil(t, err)
for denom, amount := range balances.Balances {
if oasisRuntimeClient.StringifyDenomination(denom) == a.Symbol {
if stringifyDenomination(denom, runtimeID) == a.Symbol {
assert.Equal(t, amount.ToBigInt(), a.Balance)
}
assert.Equal(t, amount.ToBigInt().Int64(), a.Balance)
Expand All @@ -120,3 +121,26 @@ func testRuntimeAccounts(t *testing.T, runtime string) {
}
}
}

func nativeTokenSymbol(runtimeID string) string {
// Iterate over all networks and find the one that contains the runtime.
// Any network will do; we assume that paratime IDs are unique across networks.
for _, network := range config.DefaultNetworks.All {
for _, paratime := range network.ParaTimes.All {
if paratime.ID == runtimeID {
return paratime.Denominations[config.NativeDenominationKey].Symbol
}
}
}
panic("Cannot find native token symbol for runtime")
}

// StringifyDenomination returns a string representation denomination `d`
// in the context of `runtimeID`. The context matters for the native denomination.
func stringifyDenomination(d sdkTypes.Denomination, runtimeID string) string {
if d.IsNative() {
return nativeTokenSymbol(runtimeID)
}

return d.String()
}
8 changes: 4 additions & 4 deletions tests/statecheck/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/jackc/pgx/v5"
"github.com/oasisprotocol/oasis-indexer/log"
"github.com/oasisprotocol/oasis-indexer/storage"
"github.com/oasisprotocol/oasis-indexer/storage/oasis"
"github.com/oasisprotocol/oasis-indexer/storage/postgres"
oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
"github.com/stretchr/testify/assert"
)

Expand All @@ -28,12 +28,12 @@ func newTargetClient(t *testing.T) (*postgres.Client, error) {
return postgres.NewClient(connString, logger)
}

func newSourceClientFactory() (*oasis.ClientFactory, error) {
network := &oasisConfig.Network{
func newSdkConnection(ctx context.Context) (connection.Connection, error) {
net := &oasisConfig.Network{
ChainContext: os.Getenv("HEALTHCHECK_TEST_CHAIN_CONTEXT"),
RPC: os.Getenv("HEALTHCHECK_TEST_NODE_RPC"),
}
return oasis.NewClientFactory(context.Background(), network, true)
return connection.ConnectNoVerify(ctx, net)
}

func snapshotBackends(target *postgres.Client, analyzer string, tables []string) (int64, error) {
Expand Down