Skip to content

Commit

Permalink
Fix getOldestNonceWithHistoricalStateGivenNodeStatus(). Accept new CL…
Browse files Browse the repository at this point in the history
…I parameter: --num-historical-blocks.
  • Loading branch information
andreibancioiu committed Aug 23, 2022
1 parent 8702a97 commit 881645c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
9 changes: 9 additions & 0 deletions cmd/rosetta/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ VERSION:
Usage: "Specifies the symbol of the native currency (must be EGLD for mainnet, XeGLD for testnet and devnet).",
Value: "EGLD",
}

cliFlagNumHistoricalBlocks = cli.Uint64Flag{
Name: "num-historical-blocks",
Usage: "Specifies the (approximate) number of available historical blocks. Usually, correlated with observer's `NumEpochsToKeep`.",
Value: 1500,
}
)

func getAllCliFlags() []cli.Flag {
Expand All @@ -141,6 +147,7 @@ func getAllCliFlags() []cli.Flag {
cliFlagMinGasLimit,
cliFlagGasPerDataByte,
cliFlagNativeCurrencySymbol,
cliFlagNumHistoricalBlocks,
}
}

Expand All @@ -162,6 +169,7 @@ type parsedCliFlags struct {
minGasLimit uint64
gasPerDataByte uint64
nativeCurrencySymbol string
numHistoricalBlocks uint64
}

func getParsedCliFlags(ctx *cli.Context) parsedCliFlags {
Expand All @@ -183,5 +191,6 @@ func getParsedCliFlags(ctx *cli.Context) parsedCliFlags {
minGasLimit: ctx.GlobalUint64(cliFlagMinGasLimit.Name),
gasPerDataByte: ctx.GlobalUint64(cliFlagGasPerDataByte.Name),
nativeCurrencySymbol: ctx.GlobalString(cliFlagNativeCurrencySymbol.Name),
numHistoricalBlocks: ctx.GlobalUint64(cliFlagNumHistoricalBlocks.Name),
}
}
2 changes: 2 additions & 0 deletions server/factory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ArgsCreateNetworkProvider struct {
NativeCurrencySymbol string
GenesisBlockHash string
GenesisTimestamp int64
NumHistoricalBlocks uint64
}

// CreateNetworkProvider creates a network provider
Expand Down Expand Up @@ -121,6 +122,7 @@ func CreateNetworkProvider(args ArgsCreateNetworkProvider) (networkProvider, err
NativeCurrencySymbol: args.NativeCurrencySymbol,
GenesisBlockHash: args.GenesisBlockHash,
GenesisTimestamp: args.GenesisTimestamp,
NumHistoricalBlocks: args.NumHistoricalBlocks,

ObserverFacade: &components.ObserverFacade{
Processor: baseProcessor,
Expand Down
3 changes: 3 additions & 0 deletions server/provider/networkProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ArgsNewNetworkProvider struct {
NativeCurrencySymbol string
GenesisBlockHash string
GenesisTimestamp int64
NumHistoricalBlocks uint64

ObserverFacade observerFacade

Expand All @@ -50,6 +51,7 @@ type networkProvider struct {
nativeCurrencySymbol string
genesisBlockHash string
genesisTimestamp int64
numHistoricalBlocks uint64

observerFacade observerFacade

Expand All @@ -72,6 +74,7 @@ func NewNetworkProvider(args ArgsNewNetworkProvider) (*networkProvider, error) {
nativeCurrencySymbol: args.NativeCurrencySymbol,
genesisBlockHash: args.GenesisBlockHash,
genesisTimestamp: args.GenesisTimestamp,
numHistoricalBlocks: args.NumHistoricalBlocks,

observerFacade: args.ObserverFacade,

Expand Down
1 change: 1 addition & 0 deletions server/provider/networkProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ func createDefaultArgsNewNetworkProvider() ArgsNewNetworkProvider {
Hasher: testscommon.RealWorldBlake2bHasher,
MarshalizerForHashing: testscommon.MarshalizerForHashing,
PubKeyConverter: testscommon.RealWorldBech32PubkeyConverter,
NumHistoricalBlocks: 10000,
}
}
10 changes: 6 additions & 4 deletions server/provider/nodeStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (provider *networkProvider) GetNodeStatus() (*resources.AggregatedNodeStatu
return nil, err
}

oldestNonceWithHistoricalState := getOldestNonceWithHistoricalStateGivenNodeStatus(plainNodeStatus)
oldestNonceWithHistoricalState := provider.getOldestNonceWithHistoricalStateGivenNodeStatus(plainNodeStatus)
oldestBlockWithHistoricalState, err := provider.getBlockSummaryByNonce(oldestNonceWithHistoricalState)
if err != nil {
return nil, err
Expand Down Expand Up @@ -59,9 +59,11 @@ func getLatestNonceGivenHighestFinalNonce(highestFinalNonce uint64) uint64 {
return highestFinalNonce - 1
}

func getOldestNonceWithHistoricalStateGivenNodeStatus(status *resources.NodeStatus) uint64 {
if status.NonceAtEpochStart == 0 {
func (provider *networkProvider) getOldestNonceWithHistoricalStateGivenNodeStatus(status *resources.NodeStatus) uint64 {
oldest := int64(status.HighestFinalNonce) - int64(provider.numHistoricalBlocks)
if oldest < int64(oldestPossibleNonceWithHistoricalState) {
return oldestPossibleNonceWithHistoricalState
}
return status.NonceAtEpochStart

return uint64(oldest)
}
21 changes: 12 additions & 9 deletions server/provider/nodeStatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func TestNetworkProvider_GetNodeStatusWithSuccess(t *testing.T) {
IsSyncing: 1,
HighestNonce: 7,
HighestFinalNonce: 5,
NonceAtEpochStart: 0,
RoundsPerEpoch: 100,
},
}
}
Expand All @@ -52,7 +50,6 @@ func TestNetworkProvider_GetNodeStatusWithSuccess(t *testing.T) {
}

// Oldest nonce with historical state (as considered by Rosetta)
// max(1, NonceAtEpochStart - RoundsPerEpoch)
if nonce == 1 {
return &data.BlockApiResponse{
Data: data.BlockApiResponsePayload{
Expand Down Expand Up @@ -109,15 +106,21 @@ func TestNetworkProvider_GetNodeStatusWithError(t *testing.T) {
}

func TestGetOldestNonceWithHistoricalStateGivenNodeStatus(t *testing.T) {
oldestNonce := getOldestNonceWithHistoricalStateGivenNodeStatus(&resources.NodeStatus{
NonceAtEpochStart: 0,
args := createDefaultArgsNewNetworkProvider()
args.NumHistoricalBlocks = 42
provider, err := NewNetworkProvider(args)
require.Nil(t, err)
require.NotNil(t, provider)

oldestNonce := provider.getOldestNonceWithHistoricalStateGivenNodeStatus(&resources.NodeStatus{
HighestFinalNonce: 50,
})

require.Equal(t, uint64(1), oldestNonce)
require.Equal(t, uint64(8), oldestNonce)

oldestNonce = getOldestNonceWithHistoricalStateGivenNodeStatus(&resources.NodeStatus{
NonceAtEpochStart: 50000,
oldestNonce = provider.getOldestNonceWithHistoricalStateGivenNodeStatus(&resources.NodeStatus{
HighestFinalNonce: 40,
})

require.Equal(t, uint64(50000), oldestNonce)
require.Equal(t, uint64(1), oldestNonce)
}
2 changes: 0 additions & 2 deletions server/resources/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type NodeStatus struct {
IsSyncing int `json:"erd_is_syncing"`
HighestNonce uint64 `json:"erd_nonce"`
HighestFinalNonce uint64 `json:"erd_highest_final_nonce"`
NonceAtEpochStart uint64 `json:"erd_nonce_at_epoch_start"`
RoundsPerEpoch uint32 `json:"erd_rounds_per_epoch"`
}

// AggregatedNodeStatus is an aggregated resource
Expand Down
2 changes: 1 addition & 1 deletion version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
RosettaVersion = "v1.4.12"

// RosettaMiddlewareVersion is the version of this package (application)
RosettaMiddlewareVersion = "v0.1.9"
RosettaMiddlewareVersion = "v0.2.0"

// NodeVersion is the canonical version of the node runtime
NodeVersion = "rc/2022-july"
Expand Down

0 comments on commit 881645c

Please sign in to comment.