-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8fd10f
commit adff3fc
Showing
7 changed files
with
356 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package availabilityCommon | ||
|
||
import ( | ||
"github.com/multiversx/mx-chain-proxy-go/common" | ||
"github.com/multiversx/mx-chain-proxy-go/data" | ||
) | ||
|
||
// AvailabilityProvider is a stateless component that aims to group common operations regarding observers' data availability | ||
type AvailabilityProvider struct { | ||
} | ||
|
||
// AvailabilityForAccountQueryOptions returns the availability needed for the provided query options | ||
func (ap *AvailabilityProvider) AvailabilityForAccountQueryOptions(options common.AccountQueryOptions) data.ObserverDataAvailabilityType { | ||
availability := data.AvailabilityRecent | ||
if options.AreHistoricalCoordinatesSet() { | ||
availability = data.AvailabilityAll | ||
} | ||
return availability | ||
} | ||
|
||
// AvailabilityForVmQuery returns the availability needed for the provided query options | ||
func (ap *AvailabilityProvider) AvailabilityForVmQuery(query *data.SCQuery) data.ObserverDataAvailabilityType { | ||
availability := data.AvailabilityRecent | ||
if query.BlockNonce.HasValue || len(query.BlockHash) > 0 { | ||
availability = data.AvailabilityAll | ||
} | ||
return availability | ||
} | ||
|
||
// IsNodeValid returns true if the provided node is valid based on the availability | ||
func (ap *AvailabilityProvider) IsNodeValid(node *data.NodeData, availability data.ObserverDataAvailabilityType) bool { | ||
isInvalidSnapshotlessNode := availability == data.AvailabilityRecent && !node.IsSnapshotless | ||
isInvalidRegularNode := availability == data.AvailabilityAll && node.IsSnapshotless | ||
isInvalidNode := isInvalidSnapshotlessNode || isInvalidRegularNode | ||
return !isInvalidNode | ||
} | ||
|
||
// GetDescriptionForAvailability returns a short description string about the provided availability | ||
func (ap *AvailabilityProvider) GetDescriptionForAvailability(availability data.ObserverDataAvailabilityType) string { | ||
switch availability { | ||
case data.AvailabilityAll: | ||
return "regular nodes" | ||
case data.AvailabilityRecent: | ||
return "snapshotless nodes" | ||
default: | ||
return "N/A" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package availabilityCommon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core" | ||
"github.com/multiversx/mx-chain-proxy-go/common" | ||
"github.com/multiversx/mx-chain-proxy-go/data" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAvailabilityForAccountQueryOptions(t *testing.T) { | ||
ap := &AvailabilityProvider{} | ||
|
||
// Test with historical coordinates set | ||
options := common.AccountQueryOptions{BlockHash: []byte("hash")} | ||
require.Equal(t, data.AvailabilityAll, ap.AvailabilityForAccountQueryOptions(options)) | ||
|
||
// Test without historical coordinates set | ||
options = common.AccountQueryOptions{} | ||
require.Equal(t, data.AvailabilityRecent, ap.AvailabilityForAccountQueryOptions(options)) | ||
} | ||
|
||
func TestAvailabilityForVmQuery(t *testing.T) { | ||
ap := &AvailabilityProvider{} | ||
|
||
// Test with BlockNonce set | ||
query := &data.SCQuery{BlockNonce: core.OptionalUint64{HasValue: true, Value: 37}} | ||
require.Equal(t, data.AvailabilityAll, ap.AvailabilityForVmQuery(query)) | ||
|
||
// Test without BlockNonce set but with BlockHash | ||
query = &data.SCQuery{BlockHash: []byte("hash")} | ||
require.Equal(t, data.AvailabilityAll, ap.AvailabilityForVmQuery(query)) | ||
|
||
// Test without BlockNonce and BlockHash | ||
query = &data.SCQuery{} | ||
require.Equal(t, data.AvailabilityRecent, ap.AvailabilityForVmQuery(query)) | ||
} | ||
|
||
func TestIsNodeValid(t *testing.T) { | ||
ap := &AvailabilityProvider{} | ||
|
||
// Test with AvailabilityRecent and snapshotless node | ||
node := &data.NodeData{IsSnapshotless: true} | ||
require.True(t, ap.IsNodeValid(node, data.AvailabilityRecent)) | ||
|
||
// Test with AvailabilityRecent and regular node | ||
node = &data.NodeData{} | ||
require.False(t, ap.IsNodeValid(node, data.AvailabilityRecent)) | ||
|
||
// Test with AvailabilityAll and regular node | ||
node = &data.NodeData{} | ||
require.True(t, ap.IsNodeValid(node, data.AvailabilityAll)) | ||
|
||
// Test with AvailabilityAll and Snapshotless node | ||
node = &data.NodeData{IsSnapshotless: true} | ||
require.False(t, ap.IsNodeValid(node, data.AvailabilityAll)) | ||
} | ||
|
||
func TestGetDescriptionForAvailability(t *testing.T) { | ||
ap := &AvailabilityProvider{} | ||
|
||
require.Equal(t, "regular nodes", ap.GetDescriptionForAvailability(data.AvailabilityAll)) | ||
require.Equal(t, "snapshotless nodes", ap.GetDescriptionForAvailability(data.AvailabilityRecent)) | ||
require.Equal(t, "N/A", ap.GetDescriptionForAvailability("invalid")) // Invalid value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.