-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rpc): Implement payment_queryInfo
RPC call
#1826
Merged
EclesioMeloJunior
merged 31 commits into
development
from
eclesio/payment-queryinfo-rpc
Oct 15, 2021
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
fcfe5f1
chore: adding payment rpc module
EclesioMeloJunior a8b0897
chore: adding unit test to runtime call
EclesioMeloJunior 368316f
chore: use defined extrinsic
EclesioMeloJunior 68f391a
chore: add Payment Query Info
EclesioMeloJunior 2249715
chore: add unit test to runtime layer
EclesioMeloJunior 78afa32
chore: add unit tests
EclesioMeloJunior 00ebedd
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior 798f650
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior 2ed73a5
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior 9f84411
chore: resolve uint128
EclesioMeloJunior ec51ea2
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior 04b3235
chore: fix lint warns
EclesioMeloJunior 46f405f
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior d626e2a
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior b4c9c16
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior c637e45
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior a06b489
chore: put condition back
EclesioMeloJunior cc4e98a
chore: use pkg/scale uint128
EclesioMeloJunior 49e8758
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior 50bb099
chore: fix unit tests
EclesioMeloJunior d0c66e8
Merge branch 'eclesio/payment-queryinfo-rpc' of github.com:ChainSafe/…
EclesioMeloJunior 5352f96
chore: remove debug from config.toml
EclesioMeloJunior 6ff3daf
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior 07e9c5c
fix: lint
EclesioMeloJunior e92f935
chore: remove tests from wasmtime and life
EclesioMeloJunior 591d0aa
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior cb3d56d
chore: resolving conflicts
EclesioMeloJunior dcf7f30
chore: remove life impl form payment query info
EclesioMeloJunior ea9ee3b
chore: simplify chain_test function
EclesioMeloJunior 245148d
chore: ignore unused meth receiver
EclesioMeloJunior 6ff3e44
Merge branch 'development' into eclesio/payment-queryinfo-rpc
EclesioMeloJunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,69 @@ | ||
package modules | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
) | ||
|
||
// PaymentQueryInfoRequest represents the request to get the fee of an extrinsic in a given block | ||
type PaymentQueryInfoRequest struct { | ||
// hex SCALE encoded extrinsic | ||
Ext string | ||
// hex optional block hash indicating the state | ||
Hash *common.Hash | ||
} | ||
|
||
// PaymentQueryInfoResponse holds the response fields to the query info RPC method | ||
type PaymentQueryInfoResponse struct { | ||
Weight uint64 `json:"weight"` | ||
Class int `json:"class"` | ||
PartialFee string `json:"partialFee"` | ||
} | ||
|
||
// PaymentModule holds all the RPC implementation of polkadot payment rpc api | ||
type PaymentModule struct { | ||
blockAPI BlockAPI | ||
} | ||
|
||
// NewPaymentModule returns a pointer to PaymentModule | ||
func NewPaymentModule(blockAPI BlockAPI) *PaymentModule { | ||
return &PaymentModule{ | ||
blockAPI: blockAPI, | ||
} | ||
} | ||
|
||
// QueryInfo query the known data about the fee of an extrinsic at the given block | ||
func (p *PaymentModule) QueryInfo(_ *http.Request, req *PaymentQueryInfoRequest, res *PaymentQueryInfoResponse) error { | ||
var hash common.Hash | ||
if req.Hash == nil { | ||
hash = p.blockAPI.BestBlockHash() | ||
} else { | ||
hash = *req.Hash | ||
} | ||
|
||
r, err := p.blockAPI.GetRuntime(&hash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ext, err := common.HexToBytes(req.Ext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encQueryInfo, err := r.PaymentQueryInfo(ext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if encQueryInfo != nil { | ||
*res = PaymentQueryInfoResponse{ | ||
Weight: encQueryInfo.Weight, | ||
Class: encQueryInfo.Class, | ||
PartialFee: encQueryInfo.PartialFee.String(), | ||
} | ||
} | ||
|
||
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,142 @@ | ||
package modules | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ChainSafe/gossamer/dot/rpc/modules/mocks" | ||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
mocksruntime "github.com/ChainSafe/gossamer/lib/runtime/mocks" | ||
) | ||
|
||
func TestPaymentQueryInfo(t *testing.T) { | ||
state := newTestStateService(t) | ||
bestBlockHash := state.Block.BestBlockHash() | ||
|
||
t.Run("When there is no errors", func(t *testing.T) { | ||
mockedQueryInfo := &types.TransactionPaymentQueryInfo{ | ||
Weight: 0, | ||
Class: 0, | ||
PartialFee: scale.MaxUint128, | ||
} | ||
|
||
expected := PaymentQueryInfoResponse{ | ||
Weight: 0, | ||
Class: 0, | ||
PartialFee: scale.MaxUint128.String(), | ||
} | ||
|
||
runtimeMock := new(mocksruntime.MockInstance) | ||
runtimeMock.On("PaymentQueryInfo", mock.AnythingOfType("[]uint8")).Return(mockedQueryInfo, nil) | ||
|
||
blockAPIMock := new(mocks.MockBlockAPI) | ||
blockAPIMock.On("BestBlockHash").Return(bestBlockHash) | ||
|
||
blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(runtimeMock, nil) | ||
|
||
mod := &PaymentModule{ | ||
blockAPI: blockAPIMock, | ||
} | ||
|
||
var req PaymentQueryInfoRequest | ||
req.Ext = "0x0001" | ||
req.Hash = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to explicitly assign nil. The default value will be nil. |
||
|
||
var res PaymentQueryInfoResponse | ||
err := mod.QueryInfo(nil, &req, &res) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, expected, res) | ||
|
||
// should be called because req.Hash is nil | ||
blockAPIMock.AssertCalled(t, "BestBlockHash") | ||
blockAPIMock.AssertCalled(t, "GetRuntime", mock.AnythingOfType("*common.Hash")) | ||
runtimeMock.AssertCalled(t, "PaymentQueryInfo", mock.AnythingOfType("[]uint8")) | ||
}) | ||
|
||
t.Run("When could not get runtime", func(t *testing.T) { | ||
blockAPIMock := new(mocks.MockBlockAPI) | ||
blockAPIMock.On("BestBlockHash").Return(bestBlockHash) | ||
|
||
blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")). | ||
Return(nil, errors.New("mocked problems")) | ||
|
||
mod := &PaymentModule{ | ||
blockAPI: blockAPIMock, | ||
} | ||
|
||
var req PaymentQueryInfoRequest | ||
req.Ext = "0x0011" | ||
req.Hash = nil | ||
|
||
var res PaymentQueryInfoResponse | ||
err := mod.QueryInfo(nil, &req, &res) | ||
|
||
require.Error(t, err) | ||
require.Equal(t, res, PaymentQueryInfoResponse{}) | ||
|
||
blockAPIMock.AssertCalled(t, "BestBlockHash") | ||
blockAPIMock.AssertCalled(t, "GetRuntime", mock.AnythingOfType("*common.Hash")) | ||
}) | ||
|
||
t.Run("When PaymentQueryInfo returns error", func(t *testing.T) { | ||
runtimeMock := new(mocksruntime.MockInstance) | ||
runtimeMock.On("PaymentQueryInfo", mock.AnythingOfType("[]uint8")).Return(nil, errors.New("mocked error")) | ||
|
||
blockAPIMock := new(mocks.MockBlockAPI) | ||
blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(runtimeMock, nil) | ||
|
||
mod := &PaymentModule{ | ||
blockAPI: blockAPIMock, | ||
} | ||
|
||
mockedHash := common.NewHash([]byte{0x01, 0x02}) | ||
var req PaymentQueryInfoRequest | ||
req.Ext = "0x0000" | ||
req.Hash = &mockedHash | ||
|
||
var res PaymentQueryInfoResponse | ||
err := mod.QueryInfo(nil, &req, &res) | ||
|
||
require.Error(t, err) | ||
require.Equal(t, res, PaymentQueryInfoResponse{}) | ||
|
||
// should be called because req.Hash is nil | ||
blockAPIMock.AssertNotCalled(t, "BestBlockHash") | ||
blockAPIMock.AssertCalled(t, "GetRuntime", mock.AnythingOfType("*common.Hash")) | ||
runtimeMock.AssertCalled(t, "PaymentQueryInfo", mock.AnythingOfType("[]uint8")) | ||
}) | ||
|
||
t.Run("When PaymentQueryInfo returns a nil info", func(t *testing.T) { | ||
runtimeMock := new(mocksruntime.MockInstance) | ||
runtimeMock.On("PaymentQueryInfo", mock.AnythingOfType("[]uint8")).Return(nil, nil) | ||
|
||
blockAPIMock := new(mocks.MockBlockAPI) | ||
blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(runtimeMock, nil) | ||
|
||
mod := &PaymentModule{ | ||
blockAPI: blockAPIMock, | ||
} | ||
|
||
mockedHash := common.NewHash([]byte{0x01, 0x02}) | ||
var req PaymentQueryInfoRequest | ||
req.Ext = "0x0020" | ||
req.Hash = &mockedHash | ||
|
||
var res PaymentQueryInfoResponse | ||
err := mod.QueryInfo(nil, &req, &res) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, res, PaymentQueryInfoResponse{}) | ||
|
||
// should be called because req.Hash is nil | ||
blockAPIMock.AssertNotCalled(t, "BestBlockHash") | ||
blockAPIMock.AssertCalled(t, "GetRuntime", mock.AnythingOfType("*common.Hash")) | ||
runtimeMock.AssertCalled(t, "PaymentQueryInfo", mock.AnythingOfType("[]uint8")) | ||
}) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
*optional.Hash
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I belive changing to
*optional.Hash
will add more complexity than just receive a*common.Hash
and check ifHash == nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also have an open PR that removes all of the optional types, so I think we should avoid using them. If we need an option I think we should use *common.Hash so I am with you @EclesioMeloJunior