-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4473 from ElrondNetwork/merge-rc1.4-feat-elrond-g…
…o-storage Merge rc1.4 feat elrond go storage
- Loading branch information
Showing
401 changed files
with
16,194 additions
and
4,322 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
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,95 @@ | ||
package groups | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ElrondNetwork/elrond-go-core/data/api" | ||
customErrors "github.com/ElrondNetwork/elrond-go/api/errors" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func extractAccountQueryOptions(c *gin.Context) (api.AccountQueryOptions, error) { | ||
options, err := parseAccountQueryOptions(c) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, fmt.Errorf("%w: %v", customErrors.ErrBadUrlParams, err) | ||
} | ||
|
||
err = checkAccountQueryOptions(options) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, fmt.Errorf("%w: %v", customErrors.ErrBadUrlParams, err) | ||
} | ||
|
||
return options, nil | ||
} | ||
|
||
func parseAccountQueryOptions(c *gin.Context) (api.AccountQueryOptions, error) { | ||
onFinalBlock, err := parseBoolUrlParam(c, urlParamOnFinalBlock) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, err | ||
} | ||
|
||
onStartOfEpoch, err := parseUint32UrlParam(c, urlParamOnStartOfEpoch) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, err | ||
} | ||
|
||
blockNonce, err := parseUint64UrlParam(c, urlParamBlockNonce) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, err | ||
} | ||
|
||
blockHash, err := parseHexBytesUrlParam(c, urlParamBlockHash) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, err | ||
} | ||
|
||
blockRootHash, err := parseHexBytesUrlParam(c, urlParamBlockRootHash) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, err | ||
} | ||
|
||
hintEpoch, err := parseUint32UrlParam(c, urlParamHintEpoch) | ||
if err != nil { | ||
return api.AccountQueryOptions{}, err | ||
} | ||
|
||
options := api.AccountQueryOptions{ | ||
OnFinalBlock: onFinalBlock, | ||
OnStartOfEpoch: onStartOfEpoch, | ||
BlockNonce: blockNonce, | ||
BlockHash: blockHash, | ||
BlockRootHash: blockRootHash, | ||
HintEpoch: hintEpoch, | ||
} | ||
return options, nil | ||
} | ||
|
||
func checkAccountQueryOptions(options api.AccountQueryOptions) error { | ||
numSpecifiedBlockCoordinates := 0 | ||
|
||
if options.BlockNonce.HasValue { | ||
numSpecifiedBlockCoordinates++ | ||
} | ||
if len(options.BlockHash) > 0 { | ||
numSpecifiedBlockCoordinates++ | ||
} | ||
if len(options.BlockRootHash) > 0 { | ||
numSpecifiedBlockCoordinates++ | ||
} | ||
|
||
if numSpecifiedBlockCoordinates > 1 { | ||
return errors.New("only one block coordinate (blockNonce vs. blockHash vs. blockRootHash) can be specified at a time") | ||
} | ||
if options.OnFinalBlock && numSpecifiedBlockCoordinates > 0 { | ||
return errors.New("onFinalBlock is not compatible with any other block coordinates") | ||
} | ||
if options.OnStartOfEpoch.HasValue && numSpecifiedBlockCoordinates > 0 { | ||
return errors.New("onStartOfEpoch is not compatible with any other block coordinates") | ||
} | ||
if options.HintEpoch.HasValue && len(options.BlockRootHash) == 0 { | ||
return errors.New("hintEpoch is optional, but only compatible with blockRootHash") | ||
} | ||
|
||
return nil | ||
} |
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 groups | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ElrondNetwork/elrond-go-core/core" | ||
"github.com/ElrondNetwork/elrond-go-core/data/api" | ||
"github.com/ElrondNetwork/elrond-go/testscommon" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExtractAccountQueryOptions(t *testing.T) { | ||
t.Run("good options", func(t *testing.T) { | ||
options, err := extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("onFinalBlock=true")) | ||
require.Nil(t, err) | ||
require.True(t, options.OnFinalBlock) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("onStartOfEpoch=7")) | ||
require.Nil(t, err) | ||
require.Equal(t, core.OptionalUint32{Value: 7, HasValue: true}, options.OnStartOfEpoch) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockNonce=42")) | ||
require.Nil(t, err) | ||
require.Equal(t, core.OptionalUint64{Value: 42, HasValue: true}, options.BlockNonce) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockHash=aaaa")) | ||
require.Nil(t, err) | ||
require.Equal(t, []byte{0xaa, 0xaa}, options.BlockHash) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockHash=aaaa")) | ||
require.Nil(t, err) | ||
require.Equal(t, []byte{0xaa, 0xaa}, options.BlockHash) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockRootHash=bbbb&hintEpoch=7")) | ||
require.Nil(t, err) | ||
require.Equal(t, []byte{0xbb, 0xbb}, options.BlockRootHash) | ||
require.Equal(t, uint32(7), options.HintEpoch.Value) | ||
}) | ||
|
||
t.Run("bad options", func(t *testing.T) { | ||
options, err := extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockNonce=42&blockHash=aaaa")) | ||
require.ErrorContains(t, err, "only one block coordinate") | ||
require.Equal(t, api.AccountQueryOptions{}, options) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockHash=aaaa&blockRootHash=bbbb")) | ||
require.ErrorContains(t, err, "only one block coordinate") | ||
require.Equal(t, api.AccountQueryOptions{}, options) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("onFinalBlock=true&blockHash=aaaa")) | ||
require.ErrorContains(t, err, "onFinalBlock is not compatible") | ||
require.Equal(t, api.AccountQueryOptions{}, options) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("onStartOfEpoch=7&blockRootHash=bbbb")) | ||
require.ErrorContains(t, err, "onStartOfEpoch is not compatible") | ||
require.Equal(t, api.AccountQueryOptions{}, options) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("onFinalBlock=true&hintEpoch=7")) | ||
require.ErrorContains(t, err, "hintEpoch is optional, but only compatible with blockRootHash") | ||
require.Equal(t, api.AccountQueryOptions{}, options) | ||
|
||
options, err = extractAccountQueryOptions(testscommon.CreateGinContextWithRawQuery("blockHash=aaaa&hintEpoch=7")) | ||
require.ErrorContains(t, err, "hintEpoch is optional, but only compatible with blockRootHash") | ||
require.Equal(t, api.AccountQueryOptions{}, options) | ||
|
||
}) | ||
} |
Oops, something went wrong.