Skip to content

test(api): transaction payment #234

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

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions api/transaction_payment/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants"
"github.com/LimeChain/gosemble/execution/types"
"github.com/LimeChain/gosemble/frame/transaction_payment"
"github.com/LimeChain/gosemble/primitives/hashing"
Expand All @@ -16,10 +17,6 @@ const (
apiVersion = 3
)

var (
DefaultTip = sc.NewU128(0)
)

type Module struct {
decoder types.RuntimeDecoder
txPayments transaction_payment.Module
Expand Down Expand Up @@ -61,7 +58,7 @@ func (m Module) QueryInfo(dataPtr int32, dataLen int32) int64 {

partialFee := sc.NewU128(0)
if ext.IsSigned() {
partialFee = m.txPayments.ComputeFee(length, dispatchInfo, DefaultTip)
partialFee = m.txPayments.ComputeFee(length, dispatchInfo, constants.DefaultTip)
}

runtimeDispatchInfo := primitives.RuntimeDispatchInfo{
Expand Down Expand Up @@ -91,7 +88,7 @@ func (m Module) QueryFeeDetails(dataPtr int32, dataLen int32) int64 {

var feeDetails primitives.FeeDetails
if ext.IsSigned() {
feeDetails = m.txPayments.ComputeFeeDetails(length, dispatchInfo, DefaultTip)
feeDetails = m.txPayments.ComputeFeeDetails(length, dispatchInfo, constants.DefaultTip)
} else {
feeDetails = primitives.FeeDetails{
InclusionFee: sc.NewOption[primitives.InclusionFee](nil),
Expand Down
220 changes: 220 additions & 0 deletions api/transaction_payment/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package transaction_payment

import (
"bytes"
"testing"

"github.com/ChainSafe/gossamer/lib/common"
sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants"
"github.com/LimeChain/gosemble/mocks"
primitives "github.com/LimeChain/gosemble/primitives/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

var (
dataPtr = int32(0)
dataLen = int32(1)
ptrAndSize = int64(2)

length = sc.U32(5)

baseWeight = primitives.WeightFromParts(1, 2)
dispatchInfoWeight = primitives.WeightFromParts(2, 3)
dispatchInfoClass = primitives.NewDispatchClassNormal()
dispatchInfoPays = primitives.NewPaysYes()

dispatchInfo = primitives.DispatchInfo{
Weight: dispatchInfoWeight,
Class: dispatchInfoClass,
PaysFee: dispatchInfoPays,
}
)

var (
mockTransactionPayment *mocks.TransactionPaymentModule
mockRuntimeDecoder *mocks.RuntimeDecoder
mockMemoryUtils *mocks.MemoryTranslator
mockUxt *mocks.UncheckedExtrinsic
mockCall *mocks.Call
)

func Test_Module_Name(t *testing.T) {
target := setup()

result := target.Name()

assert.Equal(t, ApiModuleName, result)
}

func Test_Module_Item(t *testing.T) {
target := setup()

hexName := common.MustBlake2b8([]byte(ApiModuleName))
expect := primitives.NewApiItem(hexName[:], apiVersion)

result := target.Item()

assert.Equal(t, expect, result)
}

func Test_Module_QueryInfo_Signed(t *testing.T) {
target := setup()

partialFee := sc.NewU128(10)
runtimeDispatchInfo := primitives.RuntimeDispatchInfo{
Weight: dispatchInfoWeight,
Class: dispatchInfoClass,
PartialFee: partialFee,
}

bufferUxt := bytes.NewBuffer(length.Bytes())

mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return(length.Bytes())
mockRuntimeDecoder.On("DecodeUncheckedExtrinsic", bufferUxt).Return(mockUxt)
mockUxt.On("Function").Return(mockCall)
mockCall.On("BaseWeight").Return(baseWeight)
mockCall.On("WeighData", baseWeight).Return(dispatchInfoWeight)
mockCall.On("ClassifyDispatch", baseWeight).Return(dispatchInfoClass)
mockCall.On("PaysFee", baseWeight).Return(dispatchInfoPays)
mockUxt.On("IsSigned").Return(true)
mockTransactionPayment.On("ComputeFee", length, dispatchInfo, constants.DefaultTip).Return(partialFee)
mockMemoryUtils.On("BytesToOffsetAndSize", runtimeDispatchInfo.Bytes()).Return(ptrAndSize)

result := target.QueryInfo(dataPtr, dataLen)

assert.Equal(t, ptrAndSize, result)
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen)
mockRuntimeDecoder.AssertExpectations(t)
mockUxt.AssertCalled(t, "Function")
mockCall.AssertCalled(t, "BaseWeight")
mockCall.AssertCalled(t, "WeighData", baseWeight)
mockCall.AssertCalled(t, "ClassifyDispatch", baseWeight)
mockCall.AssertCalled(t, "PaysFee", baseWeight)
mockUxt.AssertCalled(t, "IsSigned")
mockTransactionPayment.AssertCalled(t, "ComputeFee", length, dispatchInfo, constants.DefaultTip)
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", runtimeDispatchInfo.Bytes())
}

func Test_Module_QueryInfo_Unsigned(t *testing.T) {
target := setup()

runtimeDispatchInfo := primitives.RuntimeDispatchInfo{
Weight: dispatchInfoWeight,
Class: dispatchInfoClass,
PartialFee: constants.Zero,
}

bufferUxt := bytes.NewBuffer(length.Bytes())

mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return(length.Bytes())
mockRuntimeDecoder.On("DecodeUncheckedExtrinsic", bufferUxt).Return(mockUxt)
mockUxt.On("Function").Return(mockCall)
mockCall.On("BaseWeight").Return(baseWeight)
mockCall.On("WeighData", baseWeight).Return(dispatchInfoWeight)
mockCall.On("ClassifyDispatch", baseWeight).Return(dispatchInfoClass)
mockCall.On("PaysFee", baseWeight).Return(dispatchInfoPays)
mockUxt.On("IsSigned").Return(false)
mockMemoryUtils.On("BytesToOffsetAndSize", runtimeDispatchInfo.Bytes()).Return(ptrAndSize)

result := target.QueryInfo(dataPtr, dataLen)

assert.Equal(t, ptrAndSize, result)
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen)
mockRuntimeDecoder.AssertExpectations(t)
mockUxt.AssertCalled(t, "Function")
mockCall.AssertCalled(t, "BaseWeight")
mockCall.AssertCalled(t, "WeighData", baseWeight)
mockCall.AssertCalled(t, "ClassifyDispatch", baseWeight)
mockCall.AssertCalled(t, "PaysFee", baseWeight)
mockUxt.AssertCalled(t, "IsSigned")
mockTransactionPayment.AssertNotCalled(t, "ComputeFee", mock.Anything, mock.Anything, mock.Anything)
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", runtimeDispatchInfo.Bytes())
}

func Test_Module_QueryFeeDetails_Signed(t *testing.T) {
target := setup()

feeDetails := primitives.FeeDetails{
InclusionFee: sc.NewOption[primitives.InclusionFee](
primitives.NewInclusionFee(
sc.NewU128(9),
sc.NewU128(8),
sc.NewU128(7),
)),
Tip: constants.DefaultTip,
}
bufferUxt := bytes.NewBuffer(length.Bytes())

mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return(length.Bytes())
mockRuntimeDecoder.On("DecodeUncheckedExtrinsic", bufferUxt).Return(mockUxt)
mockUxt.On("Function").Return(mockCall)
mockCall.On("BaseWeight").Return(baseWeight)
mockCall.On("WeighData", baseWeight).Return(dispatchInfoWeight)
mockCall.On("ClassifyDispatch", baseWeight).Return(dispatchInfoClass)
mockCall.On("PaysFee", baseWeight).Return(dispatchInfoPays)
mockUxt.On("IsSigned").Return(true)
mockTransactionPayment.On("ComputeFeeDetails", length, dispatchInfo, constants.DefaultTip).Return(feeDetails)
mockMemoryUtils.On("BytesToOffsetAndSize", feeDetails.Bytes()).Return(ptrAndSize)

result := target.QueryFeeDetails(dataPtr, dataLen)

assert.Equal(t, ptrAndSize, result)
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen)
mockRuntimeDecoder.AssertExpectations(t)
mockUxt.AssertCalled(t, "Function")
mockCall.AssertCalled(t, "BaseWeight")
mockCall.AssertCalled(t, "WeighData", baseWeight)
mockCall.AssertCalled(t, "ClassifyDispatch", baseWeight)
mockCall.AssertCalled(t, "PaysFee", baseWeight)
mockUxt.AssertCalled(t, "IsSigned")
mockTransactionPayment.AssertCalled(t, "ComputeFeeDetails", length, dispatchInfo, constants.DefaultTip)
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", feeDetails.Bytes())
}

func Test_Module_QueryFeeDetails_Unsigned(t *testing.T) {
target := setup()

feeDetails := primitives.FeeDetails{
InclusionFee: sc.NewOption[primitives.InclusionFee](nil),
}
bufferUxt := bytes.NewBuffer(length.Bytes())

mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return(length.Bytes())
mockRuntimeDecoder.On("DecodeUncheckedExtrinsic", bufferUxt).Return(mockUxt)
mockUxt.On("Function").Return(mockCall)
mockCall.On("BaseWeight").Return(baseWeight)
mockCall.On("WeighData", baseWeight).Return(dispatchInfoWeight)
mockCall.On("ClassifyDispatch", baseWeight).Return(dispatchInfoClass)
mockCall.On("PaysFee", baseWeight).Return(dispatchInfoPays)
mockUxt.On("IsSigned").Return(false)
mockMemoryUtils.On("BytesToOffsetAndSize", feeDetails.Bytes()).Return(ptrAndSize)

result := target.QueryFeeDetails(dataPtr, dataLen)

assert.Equal(t, ptrAndSize, result)
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen)
mockRuntimeDecoder.AssertExpectations(t)
mockUxt.AssertCalled(t, "Function")
mockCall.AssertCalled(t, "BaseWeight")
mockCall.AssertCalled(t, "WeighData", baseWeight)
mockCall.AssertCalled(t, "ClassifyDispatch", baseWeight)
mockCall.AssertCalled(t, "PaysFee", baseWeight)
mockUxt.AssertCalled(t, "IsSigned")
mockTransactionPayment.AssertNotCalled(t, "ComputeFeeDetails", mock.Anything, mock.Anything, mock.Anything)
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", feeDetails.Bytes())
}

func setup() Module {
mockTransactionPayment = new(mocks.TransactionPaymentModule)
mockRuntimeDecoder = new(mocks.RuntimeDecoder)
mockMemoryUtils = new(mocks.MemoryTranslator)
mockUxt = new(mocks.UncheckedExtrinsic)
mockCall = new(mocks.Call)

target := New(mockRuntimeDecoder, mockTransactionPayment)
target.memUtils = mockMemoryUtils

return target
}
9 changes: 3 additions & 6 deletions api/transaction_payment_call/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants"
"github.com/LimeChain/gosemble/execution/types"
"github.com/LimeChain/gosemble/frame/transaction_payment"
"github.com/LimeChain/gosemble/primitives/hashing"
Expand All @@ -16,10 +17,6 @@ const (
apiVersion = 3
)

var (
DefaultTip = sc.NewU128(0)
)

type Module struct {
decoder types.RuntimeDecoder
txPayments transaction_payment.Module
Expand Down Expand Up @@ -58,7 +55,7 @@ func (m Module) QueryCallInfo(dataPtr int32, dataLen int32) int64 {
length := sc.DecodeU32(buffer)

dispatchInfo := primitives.GetDispatchInfo(call)
partialFee := m.txPayments.ComputeFee(length, dispatchInfo, DefaultTip)
partialFee := m.txPayments.ComputeFee(length, dispatchInfo, constants.DefaultTip)

runtimeDispatchInfo := primitives.RuntimeDispatchInfo{
Weight: dispatchInfo.Weight,
Expand All @@ -84,7 +81,7 @@ func (m Module) QueryCallFeeDetails(dataPtr int32, dataLen int32) int64 {
length := sc.DecodeU32(buffer)

dispatchInfo := primitives.GetDispatchInfo(call)
feeDetails := m.txPayments.ComputeFeeDetails(length, dispatchInfo, DefaultTip)
feeDetails := m.txPayments.ComputeFeeDetails(length, dispatchInfo, constants.DefaultTip)

return m.memUtils.BytesToOffsetAndSize(feeDetails.Bytes())
}
Binary file modified build/runtime.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion constants/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ const (
)

var (
Zero = sc.NewU128(0)
Zero = sc.NewU128(0)
DefaultTip = Zero
)
6 changes: 3 additions & 3 deletions execution/types/unchecked_extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type UncheckedExtrinsic interface {
Function() primitives.Call
Extra() primitives.SignedExtra

IsSigned() sc.Bool
IsSigned() bool
Check(lookup primitives.AccountIdLookup) (sc.Option[primitives.Address32], primitives.TransactionValidityError)
}

Expand Down Expand Up @@ -92,8 +92,8 @@ func (uxt uncheckedExtrinsic) Extra() primitives.SignedExtra {
return uxt.extra
}

func (uxt uncheckedExtrinsic) IsSigned() sc.Bool {
return uxt.signature.HasValue
func (uxt uncheckedExtrinsic) IsSigned() bool {
return bool(uxt.signature.HasValue)
}

func (uxt uncheckedExtrinsic) Check(lookup primitives.AccountIdLookup) (sc.Option[primitives.Address32], primitives.TransactionValidityError) {
Expand Down
4 changes: 2 additions & 2 deletions mocks/unchecked_extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func (uxt *UncheckedExtrinsic) Extra() primitives.SignedExtra {
return args.Get(0).(primitives.SignedExtra)
}

func (uxt *UncheckedExtrinsic) IsSigned() sc.Bool {
func (uxt *UncheckedExtrinsic) IsSigned() bool {
args := uxt.Called()
return args.Get(0).(sc.Bool)
return args.Get(0).(bool)
}

func (uxt *UncheckedExtrinsic) Check(lookup primitives.AccountIdLookup) (sc.Option[primitives.Address32], primitives.TransactionValidityError) {
Expand Down