From 16787ff57862fa9656cbffc35d36357410ac27d0 Mon Sep 17 00:00:00 2001 From: bilgehansahin Date: Wed, 27 Mar 2024 13:11:31 -0700 Subject: [PATCH] set P-chain HRP for tx parsing in offline mode (#243) Co-authored-by: Bilgehan Sahin Co-authored-by: dhrubabasu <7675102+dhrubabasu@users.noreply.github.com> --- cmd/server/main.go | 1 - service/backend/pchain/account_test.go | 4 -- service/backend/pchain/backend.go | 43 +++++++++------------ service/backend/pchain/backend_test.go | 1 - service/backend/pchain/block_test.go | 2 +- service/backend/pchain/construction_test.go | 5 --- 6 files changed, 20 insertions(+), 36 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 3423445a..4e2615e1 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -136,7 +136,6 @@ func main() { } pChainBackend, err := pchain.NewBackend( - cfg.Mode, pChainClient, pIndexerParser, avaxAssetID, diff --git a/service/backend/pchain/account_test.go b/service/backend/pchain/account_test.go index 321d26e0..33a9e9ae 100644 --- a/service/backend/pchain/account_test.go +++ b/service/backend/pchain/account_test.go @@ -17,7 +17,6 @@ import ( "github.com/ava-labs/avalanche-rosetta/client" "github.com/ava-labs/avalanche-rosetta/constants" "github.com/ava-labs/avalanche-rosetta/mapper" - "github.com/ava-labs/avalanche-rosetta/service" "github.com/ava-labs/avalanche-rosetta/service/backend/pchain/indexer" pmapper "github.com/ava-labs/avalanche-rosetta/mapper/pchain" @@ -56,7 +55,6 @@ func TestAccountBalance(t *testing.T) { parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) parserMock.EXPECT().ParseNonGenesisBlock(ctx, "", blockHeight).Return(parsedBlock, nil).AnyTimes() backend, err := NewBackend( - service.ModeOnline, pChainMock, parserMock, avaxAssetID, @@ -255,7 +253,6 @@ func TestAccountPendingRewardsBalance(t *testing.T) { } backend, err := NewBackend( - service.ModeOnline, pChainMock, parserMock, avaxAssetID, @@ -361,7 +358,6 @@ func TestAccountCoins(t *testing.T) { parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) parserMock.EXPECT().ParseNonGenesisBlock(ctx, "", blockHeight).Return(parsedBlock, nil).AnyTimes() backend, err := NewBackend( - service.ModeOnline, pChainMock, parserMock, avaxAssetID, diff --git a/service/backend/pchain/backend.go b/service/backend/pchain/backend.go index 22da846d..6de21859 100644 --- a/service/backend/pchain/backend.go +++ b/service/backend/pchain/backend.go @@ -38,10 +38,9 @@ type Backend struct { // NewBackend creates a P-chain service backend func NewBackend( - nodeMode string, pClient client.PChainClient, indexerParser indexer.Parser, - assetID ids.ID, + avaxAssetID ids.ID, networkIdentifier *types.NetworkIdentifier, avalancheNetworkID uint32, ) (*Backend, error) { @@ -50,34 +49,30 @@ func NewBackend( return nil, err } - b := &Backend{ + networkHRP, err := mapper.GetHRP(networkIdentifier) + if err != nil { + return nil, err + } + + return &Backend{ genesisHandler: genHandler, networkID: networkIdentifier, + networkHRP: networkHRP, + avalancheNetworkID: avalancheNetworkID, pClient: pClient, + indexerParser: indexerParser, getUTXOsPageSize: 1024, codec: block.Codec, codecVersion: block.CodecVersion, - indexerParser: indexerParser, - avaxAssetID: assetID, - avalancheNetworkID: avalancheNetworkID, - } - - if nodeMode == service.ModeOnline { - var err error - if b.networkHRP, err = mapper.GetHRP(b.networkID); err != nil { - return nil, err - } - } - - b.txParserCfg = pmapper.TxParserConfig{ - IsConstruction: false, - Hrp: b.networkHRP, - ChainIDs: nil, - AvaxAssetID: b.avaxAssetID, - PChainClient: b.pClient, - } - - return b, nil + avaxAssetID: avaxAssetID, + txParserCfg: pmapper.TxParserConfig{ + IsConstruction: false, + Hrp: networkHRP, + ChainIDs: nil, + AvaxAssetID: avaxAssetID, + PChainClient: pClient, + }, + }, nil } // ShouldHandleRequest returns whether a given request should be handled by this backend diff --git a/service/backend/pchain/backend_test.go b/service/backend/pchain/backend_test.go index c0385aa3..a1e62fb4 100644 --- a/service/backend/pchain/backend_test.go +++ b/service/backend/pchain/backend_test.go @@ -35,7 +35,6 @@ func TestShouldHandleRequest(t *testing.T) { parserMock := indexer.NewMockParser(ctrl) parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) backend, err := NewBackend( - service.ModeOnline, clientMock, parserMock, avaxAssetID, diff --git a/service/backend/pchain/block_test.go b/service/backend/pchain/block_test.go index ad429134..a5d98f70 100644 --- a/service/backend/pchain/block_test.go +++ b/service/backend/pchain/block_test.go @@ -99,7 +99,7 @@ func TestFetchBlkDependencies(t *testing.T) { Encoding: formatting.Hex, }).Return(nil, nil) - backend, err := NewBackend(service.ModeOnline, mockPClient, mockIndexerParser, avaxAssetID, networkIdentifier, networkID) + backend, err := NewBackend(mockPClient, mockIndexerParser, avaxAssetID, networkIdentifier, networkID) require.NoError(t, err) deps, err := backend.fetchBlkDependencies(ctx, []*txs.Tx{tx}) diff --git a/service/backend/pchain/construction_test.go b/service/backend/pchain/construction_test.go index 5d5f22bf..14921f3d 100644 --- a/service/backend/pchain/construction_test.go +++ b/service/backend/pchain/construction_test.go @@ -72,7 +72,6 @@ func TestConstructionDerive(t *testing.T) { parserMock := indexer.NewMockParser(ctrl) parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) backend, err := NewBackend( - service.ModeOnline, pChainMock, parserMock, avaxAssetID, @@ -193,7 +192,6 @@ func TestExportTxConstruction(t *testing.T) { parserMock := indexer.NewMockParser(ctrl) parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) backend, err := NewBackend( - service.ModeOnline, clientMock, parserMock, avaxAssetID, @@ -404,7 +402,6 @@ func TestImportTxConstruction(t *testing.T) { parserMock := indexer.NewMockParser(ctrl) parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) backend, err := NewBackend( - service.ModeOnline, clientMock, parserMock, avaxAssetID, @@ -645,7 +642,6 @@ func TestAddValidatorTxConstruction(t *testing.T) { parserMock := indexer.NewMockParser(ctrl) parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) backend, err := NewBackend( - service.ModeOnline, clientMock, parserMock, avaxAssetID, @@ -880,7 +876,6 @@ func TestAddDelegatorTxConstruction(t *testing.T) { parserMock := indexer.NewMockParser(ctrl) parserMock.EXPECT().GetGenesisBlock(ctx).Return(dummyGenesis, nil) backend, err := NewBackend( - service.ModeOnline, clientMock, parserMock, avaxAssetID,