Skip to content

Commit

Permalink
some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 31, 2022
1 parent 59a0ef4 commit c8a6985
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 86 deletions.
20 changes: 7 additions & 13 deletions backend/cchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,44 @@ package cchain
import (
"math/big"

"github.com/ava-labs/avalanche-rosetta/constants"
ethtypes "github.com/ava-labs/coreth/core/types"
"github.com/coinbase/rosetta-sdk-go/types"
)

// Config holds the service configuration
type Config struct {
Mode string
Mode constants.NodeMode
ChainID *big.Int
NetworkID *types.NetworkIdentifier
GenesisBlockHash string
AvaxAssetID string
IngestionMode string
IngestionMode constants.NodeIngestion
TokenWhiteList []string
IndexUnknownTokens bool

// Upgrade Times
AP5Activation uint64
}

const (
ModeOffline = "offline"
ModeOnline = "online"
StandardIngestion = "standard"
AnalyticsIngestion = "analytics"
)

// IsOfflineMode returns true if running in offline mode
func (c Config) IsOfflineMode() bool {
return c.Mode == ModeOffline
return c.Mode == constants.Offline
}

// IsOnlineMode returns true if running in online mode
func (c Config) IsOnlineMode() bool {
return c.Mode == ModeOnline
return c.Mode == constants.Online
}

// IsAnalyticsMode returns true if running in analytics ingestion mode
func (c Config) IsAnalyticsMode() bool {
return c.IngestionMode == AnalyticsIngestion
return c.IngestionMode == constants.AnalyticsIngestion
}

// IsStandardMode returns true if running in standard ingestion mode
func (c Config) IsStandardMode() bool {
return c.IngestionMode == StandardIngestion
return c.IngestionMode == constants.StandardIngestion
}

// IsTokenListEmpty returns true if the token addresses list is empty
Expand Down
5 changes: 3 additions & 2 deletions backend/cchain/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"
"testing"

"github.com/ava-labs/avalanche-rosetta/constants"
ethtypes "github.com/ava-labs/coreth/core/types"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/stretchr/testify/assert"
Expand All @@ -12,7 +13,7 @@ import (
func TestConfig(t *testing.T) {
t.Run("online", func(t *testing.T) {
cfg := Config{
Mode: "online",
Mode: constants.Online,
ChainID: big.NewInt(1),
NetworkID: &types.NetworkIdentifier{},
}
Expand All @@ -23,7 +24,7 @@ func TestConfig(t *testing.T) {

t.Run("offline", func(t *testing.T) {
cfg := Config{
Mode: "offline",
Mode: constants.Offline,
ChainID: big.NewInt(1),
NetworkID: &types.NetworkIdentifier{},
}
Expand Down
4 changes: 2 additions & 2 deletions backend/pchain/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAccountBalance(t *testing.T) {
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
parserMock.Mock.On("ParseNonGenesisBlock", ctx, "", blockHeight).Return(parsedBlock, nil)
backend, err := NewBackend(constants.ModeOnline, pChainMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, pChainMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)
backend.getUTXOsPageSize = 2

Expand Down Expand Up @@ -209,7 +209,7 @@ func TestAccountCoins(t *testing.T) {
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
parserMock.Mock.On("ParseNonGenesisBlock", ctx, "", blockHeight).Return(parsedBlock, nil)
backend, err := NewBackend(constants.ModeOnline, pChainMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, pChainMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

t.Run("Account Coins Test regular coins", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions backend/pchain/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Backend struct {

// NewBackend creates a P-chain service backend
func NewBackend(
nodeMode string,
nodeMode constants.NodeMode,
pClient client.PChainClient,
indexerParser indexer.Parser,
assetID ids.ID,
Expand All @@ -66,7 +66,7 @@ func NewBackend(
avaxAssetID: assetID,
}

if nodeMode == constants.ModeOnline {
if nodeMode == constants.Online {
if err := backEnd.initChainIDs(); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/pchain/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestShouldHandleRequest(t *testing.T) {
clientMock.Mock.On("GetBlockchainID", ctx, constants.XChain.String()).Return(ids.ID{'X'}, nil)
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
backend, err := NewBackend(constants.ModeOnline, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

testData := []struct {
Expand Down
2 changes: 1 addition & 1 deletion backend/pchain/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (b *Backend) ConstructionParse(ctx context.Context, req *types.Construction
return nil, backend.WrapError(backend.ErrInvalidInput, err)
}

netID, _ := constants.FromString(rosettaTx.DestinationChain)
netID, _ := constants.GetChainIDAlias(rosettaTx.DestinationChain)
chainIDs := map[ids.ID]constants.ChainIDAlias{}
if rosettaTx.DestinationChainID != nil {
chainIDs[*rosettaTx.DestinationChainID] = netID
Expand Down
10 changes: 5 additions & 5 deletions backend/pchain/construction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestConstructionDerive(t *testing.T) {
pChainMock.Mock.On("GetNetworkID", ctx).Return(uint32(5), nil)
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
backend, err := NewBackend(constants.ModeOnline, pChainMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, pChainMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

t.Run("p-chain address", func(t *testing.T) {
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestExportTxConstruction(t *testing.T) {
clientMock.Mock.On("GetBlockchainID", ctx, constants.XChain.String()).Return(ids.ID{'X'}, nil)
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
backend, err := NewBackend(constants.ModeOnline, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

t.Run("preprocess endpoint", func(t *testing.T) {
Expand Down Expand Up @@ -404,7 +404,7 @@ func TestImportTxConstruction(t *testing.T) {
clientMock.Mock.On("GetBlockchainID", ctx, constants.XChain.String()).Return(ids.ID{'X'}, nil)
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
backend, err := NewBackend(constants.ModeOnline, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

t.Run("preprocess endpoint", func(t *testing.T) {
Expand Down Expand Up @@ -643,7 +643,7 @@ func TestAddValidatorTxConstruction(t *testing.T) {
clientMock.Mock.On("GetBlockchainID", ctx, constants.XChain.String()).Return(ids.ID{'X'}, nil)
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
backend, err := NewBackend(constants.ModeOnline, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

t.Run("preprocess endpoint", func(t *testing.T) {
Expand Down Expand Up @@ -877,7 +877,7 @@ func TestAddDelegatorTxConstruction(t *testing.T) {
clientMock.Mock.On("GetBlockchainID", ctx, constants.XChain.String()).Return(ids.ID{'X'}, nil)
parserMock := &idxmocks.Parser{}
parserMock.Mock.On("GetGenesisBlock", ctx).Return(dummyGenesis, nil)
backend, err := NewBackend(constants.ModeOnline, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
backend, err := NewBackend(constants.Online, clientMock, parserMock, avaxAssetID, pChainNetworkIdentifier)
assert.Nil(t, err)

t.Run("preprocess endpoint", func(t *testing.T) {
Expand Down
15 changes: 7 additions & 8 deletions cmd/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

var (
errInvalidMode = errors.New("invalid rosetta mode")
errGenesisBlockRequired = errors.New("genesis block hash is not provided")
errInvalidTokenAddress = errors.New("invalid token address provided")
errInvalidErc20Address = errors.New("not all token addresses provided are valid erc20s")
Expand Down Expand Up @@ -51,11 +50,11 @@ func readConfig(path string) (*config, error) {

func (c *config) applyDefaults() {
if c.Mode == "" {
c.Mode = constants.ModeOnline
c.Mode = constants.Online.String()
}

if c.IngestionMode == "" {
c.IngestionMode = constants.StandardIngestion
c.IngestionMode = constants.StandardIngestion.String()
}

if c.RPCBaseURL == "" {
Expand All @@ -72,8 +71,8 @@ func (c *config) applyDefaults() {
}

func (c *config) validate() error {
if !(c.Mode == constants.ModeOffline || c.Mode == constants.ModeOnline) {
return errInvalidMode
if _, err := constants.GetNodeMode(c.Mode); err != nil {
return err
}

if c.GenesisBlockHash == "" {
Expand All @@ -88,11 +87,11 @@ func (c *config) validate() error {
}
}

if !(c.IngestionMode == constants.AnalyticsIngestion || c.IngestionMode == constants.StandardIngestion) {
return errInvalidIngestionMode
if _, err := constants.GetNodeIngestion(c.Mode); err != nil {
return err
}

if c.IngestionMode == constants.StandardIngestion && c.IndexUnknownTokens {
if c.IngestionMode == constants.StandardIngestion.String() && c.IndexUnknownTokens {
return errInvalidUnknownTokenMode
}
return nil
Expand Down
18 changes: 10 additions & 8 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
// bootstrapping, it will fail.
//
// TODO: Only perform this check after the underlying node is bootstrapped
if cfg.Mode == constants.ModeOnline && cfg.ValidateERC20Whitelist {
if cfg.Mode == constants.Online.String() && cfg.ValidateERC20Whitelist {
if err := cfg.validateWhitelistOnlyValidErc20s(cChainClient); err != nil {
log.Fatal("token whitelist validation error:", err)
}
Expand All @@ -85,7 +85,7 @@ func main() {
if cfg.CChainID == 0 {
log.Println("chain id is not provided, fetching from rpc...")

if cfg.Mode == constants.ModeOffline {
if cfg.Mode == constants.Offline.String() {
log.Fatal("cant fetch chain id in offline mode")
}

Expand Down Expand Up @@ -131,15 +131,17 @@ func main() {
}

// Create C-chain backend
nodeMode, _ := constants.GetNodeMode(cfg.Mode)
ingestionMode, _ := constants.GetNodeIngestion(cfg.Mode)
cChainConfig := &cchain.Config{
Mode: cfg.Mode,
Mode: nodeMode,
ChainID: big.NewInt(cfg.CChainID),
NetworkID: networkC,
GenesisBlockHash: cfg.GenesisBlockHash,
AvaxAssetID: assetID,
AP5Activation: AP5Activation,
IndexUnknownTokens: cfg.IndexUnknownTokens,
IngestionMode: cfg.IngestionMode,
IngestionMode: ingestionMode,
TokenWhiteList: cfg.TokenWhiteList,
}
cChainBackend := cchain.NewBackend(cChainConfig, cChainClient)
Expand All @@ -150,7 +152,7 @@ func main() {
if err != nil {
log.Fatal("unable to initialize p-chain indexer parser:", err)
}
pChainBackend, err := pchain.NewBackend(cfg.Mode, pChainClient, pIndexerParser, avaxAssetID, networkP)
pChainBackend, err := pchain.NewBackend(nodeMode, pChainClient, pIndexerParser, avaxAssetID, networkP)
if err != nil {
log.Fatal("unable to initialize p-chain backend:", err)
}
Expand All @@ -177,7 +179,7 @@ func main() {
}

handler := configureRouter(
cfg.Mode,
nodeMode,
asserter,
cChainBackend,
pChainBackend,
Expand All @@ -203,7 +205,7 @@ func main() {
}

func validateNetworkName(cfg *config, cChainClient client.Client) error {
if cfg.Mode == constants.ModeOffline {
if cfg.Mode == constants.Offline.String() {
if cfg.NetworkName == "" {
return fmt.Errorf("network name is not provided, can't fetch network name in offline mode")
}
Expand Down Expand Up @@ -233,7 +235,7 @@ func validateNetworkName(cfg *config, cChainClient client.Client) error {
}

func configureRouter(
mode string,
mode constants.NodeMode,
asserter *asserter.Asserter,
cChainBackend *cchain.Backend,
pChainBackend *pchain.Backend,
Expand Down
2 changes: 1 addition & 1 deletion constants/chain_id_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ni ChainIDAlias) String() string {
}
}

func FromString(s string) (ChainIDAlias, error) {
func GetChainIDAlias(s string) (ChainIDAlias, error) {
switch {
case s == "P":
return PChain, nil
Expand Down
69 changes: 64 additions & 5 deletions constants/node.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
package constants

// TODO: consider turning these into enums
import "errors"

var (
ErrInvalidMode = errors.New("invalid rosetta mode")
ErrInvalidIngestionMode = errors.New("invalid rosetta ingestion mode")
)

type NodeMode uint8

const (
Unknown NodeMode = iota + 1
Offline
Online
)

func (m NodeMode) String() string {
switch m {
case Offline:
return "offline"
case Online:
return "online"
default:
return "unknown"
}
}

func GetNodeMode(s string) (NodeMode, error) {
switch {
case s == "offline":
return Offline, nil
case s == "online":
return Online, nil
default:
return Unknown, ErrInvalidMode
}
}

type NodeIngestion uint8

const (
ModeOffline = "offline"
ModeOnline = "online"
StandardIngestion = "standard"
AnalyticsIngestion = "analytics"
UnknownIngestion NodeIngestion = iota + 1
StandardIngestion
AnalyticsIngestion
)

func (m NodeIngestion) String() string {
switch m {
case StandardIngestion:
return "standard"
case AnalyticsIngestion:
return "analytics"
default:
return "unknown"
}
}

func GetNodeIngestion(s string) (NodeIngestion, error) {
switch {
case s == "standard":
return StandardIngestion, nil
case s == "analytics":
return AnalyticsIngestion, nil
default:
return UnknownIngestion, ErrInvalidIngestionMode
}
}
Loading

0 comments on commit c8a6985

Please sign in to comment.