forked from ChainSafe/gossamer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rpc): Implement
payment_queryInfo
RPC call (ChainSafe#1826)
* chore: adding payment rpc module * chore: adding unit test to runtime call * chore: use defined extrinsic * chore: add Payment Query Info * chore: add unit test to runtime layer * chore: add unit tests * chore: resolve uint128 * chore: fix lint warns * chore: put condition back * chore: use pkg/scale uint128 * chore: fix unit tests * chore: remove debug from config.toml * fix: lint * chore: remove tests from wasmtime and life * chore: resolving conflicts * chore: remove life impl form payment query info * chore: simplify chain_test function * chore: ignore unused meth receiver
- Loading branch information
1 parent
afe350c
commit d6e3583
Showing
25 changed files
with
461 additions
and
45 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
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 | ||
|
||
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.