From b2076b47dadaad5cf09d7434e890e0f27a4c9f3b Mon Sep 17 00:00:00 2001 From: Rado M Date: Wed, 18 Oct 2023 14:37:35 +0300 Subject: [PATCH] refactor(primitives/types): related to tx payment module --- api/transaction_payment/module.go | 7 +- api/transaction_payment/module_test.go | 11 +-- api/transaction_payment_call/module_test.go | 7 +- frame/transaction_payment/module.go | 17 ++--- .../transaction_payment}/types/fee_details.go | 6 +- .../types/fee_details_test.go | 71 +++++++++++++++++++ .../types/inclusion_fee.go | 11 +-- .../types/inclusion_fee_test.go | 54 ++++++++++++++ mocks/tx_payment_module.go | 5 +- runtime/transaction_payment_test.go | 23 +++--- 10 files changed, 172 insertions(+), 40 deletions(-) rename {primitives => frame/transaction_payment}/types/fee_details.go (78%) create mode 100644 frame/transaction_payment/types/fee_details_test.go rename {primitives => frame/transaction_payment}/types/inclusion_fee.go (67%) create mode 100644 frame/transaction_payment/types/inclusion_fee_test.go diff --git a/api/transaction_payment/module.go b/api/transaction_payment/module.go index 8c3a25a3..03a933d1 100644 --- a/api/transaction_payment/module.go +++ b/api/transaction_payment/module.go @@ -7,6 +7,7 @@ import ( "github.com/LimeChain/gosemble/constants" "github.com/LimeChain/gosemble/execution/types" "github.com/LimeChain/gosemble/frame/transaction_payment" + tx_types "github.com/LimeChain/gosemble/frame/transaction_payment/types" "github.com/LimeChain/gosemble/primitives/hashing" primitives "github.com/LimeChain/gosemble/primitives/types" "github.com/LimeChain/gosemble/utils" @@ -86,12 +87,12 @@ func (m Module) QueryFeeDetails(dataPtr int32, dataLen int32) int64 { dispatchInfo := primitives.GetDispatchInfo(ext.Function()) - var feeDetails primitives.FeeDetails + var feeDetails tx_types.FeeDetails if ext.IsSigned() { feeDetails = m.txPayments.ComputeFeeDetails(length, dispatchInfo, constants.DefaultTip) } else { - feeDetails = primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee](nil), + feeDetails = tx_types.FeeDetails{ + InclusionFee: sc.NewOption[tx_types.InclusionFee](nil), } } diff --git a/api/transaction_payment/module_test.go b/api/transaction_payment/module_test.go index 7e890868..e420f731 100644 --- a/api/transaction_payment/module_test.go +++ b/api/transaction_payment/module_test.go @@ -7,6 +7,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" sc "github.com/LimeChain/goscale" "github.com/LimeChain/gosemble/constants" + "github.com/LimeChain/gosemble/frame/transaction_payment/types" "github.com/LimeChain/gosemble/mocks" primitives "github.com/LimeChain/gosemble/primitives/types" "github.com/stretchr/testify/assert" @@ -136,9 +137,9 @@ func Test_Module_QueryInfo_Unsigned(t *testing.T) { func Test_Module_QueryFeeDetails_Signed(t *testing.T) { target := setup() - feeDetails := primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee]( - primitives.NewInclusionFee( + feeDetails := types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee]( + types.NewInclusionFee( sc.NewU128(9), sc.NewU128(8), sc.NewU128(7), @@ -176,8 +177,8 @@ func Test_Module_QueryFeeDetails_Signed(t *testing.T) { func Test_Module_QueryFeeDetails_Unsigned(t *testing.T) { target := setup() - feeDetails := primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee](nil), + feeDetails := types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee](nil), } bufferUxt := bytes.NewBuffer(length.Bytes()) diff --git a/api/transaction_payment_call/module_test.go b/api/transaction_payment_call/module_test.go index ea46efbe..f641f147 100644 --- a/api/transaction_payment_call/module_test.go +++ b/api/transaction_payment_call/module_test.go @@ -7,6 +7,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" sc "github.com/LimeChain/goscale" "github.com/LimeChain/gosemble/constants" + "github.com/LimeChain/gosemble/frame/transaction_payment/types" "github.com/LimeChain/gosemble/mocks" primitives "github.com/LimeChain/gosemble/primitives/types" "github.com/stretchr/testify/assert" @@ -93,9 +94,9 @@ func Test_Module_QueryCallInfo(t *testing.T) { func Test_Module_QueryCallFeeDetails(t *testing.T) { target := setup() - feeDetails := primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee]( - primitives.NewInclusionFee( + feeDetails := types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee]( + types.NewInclusionFee( sc.NewU128(9), sc.NewU128(8), sc.NewU128(7), diff --git a/frame/transaction_payment/module.go b/frame/transaction_payment/module.go index 1ccdc60a..1f2b7015 100644 --- a/frame/transaction_payment/module.go +++ b/frame/transaction_payment/module.go @@ -3,6 +3,7 @@ package transaction_payment import ( sc "github.com/LimeChain/goscale" "github.com/LimeChain/gosemble/constants/metadata" + "github.com/LimeChain/gosemble/frame/transaction_payment/types" "github.com/LimeChain/gosemble/hooks" primitives "github.com/LimeChain/gosemble/primitives/types" ) @@ -11,7 +12,7 @@ type Module interface { primitives.Module ComputeFee(len sc.U32, info primitives.DispatchInfo, tip primitives.Balance) primitives.Balance - ComputeFeeDetails(len sc.U32, info primitives.DispatchInfo, tip primitives.Balance) primitives.FeeDetails + ComputeFeeDetails(len sc.U32, info primitives.DispatchInfo, tip primitives.Balance) types.FeeDetails ComputeActualFee(len sc.U32, info primitives.DispatchInfo, postInfo primitives.PostDispatchInfo, tip primitives.Balance) primitives.Balance OperationalFeeMultiplier() sc.U8 } @@ -186,7 +187,7 @@ func (m module) ComputeFee(len sc.U32, info primitives.DispatchInfo, tip primiti return m.ComputeFeeDetails(len, info, tip).FinalFee() } -func (m module) ComputeFeeDetails(len sc.U32, info primitives.DispatchInfo, tip primitives.Balance) primitives.FeeDetails { +func (m module) ComputeFeeDetails(len sc.U32, info primitives.DispatchInfo, tip primitives.Balance) types.FeeDetails { return m.computeFeeRaw(len, info.Weight, tip, info.PaysFee, info.Class) } @@ -194,11 +195,11 @@ func (m module) ComputeActualFee(len sc.U32, info primitives.DispatchInfo, postI return m.computeActualFeeDetails(len, info, postInfo, tip).FinalFee() } -func (m module) computeActualFeeDetails(len sc.U32, info primitives.DispatchInfo, postInfo primitives.PostDispatchInfo, tip primitives.Balance) primitives.FeeDetails { +func (m module) computeActualFeeDetails(len sc.U32, info primitives.DispatchInfo, postInfo primitives.PostDispatchInfo, tip primitives.Balance) types.FeeDetails { return m.computeFeeRaw(len, postInfo.CalcActualWeight(&info), tip, postInfo.Pays(&info), info.Class) } -func (m module) computeFeeRaw(len sc.U32, weight primitives.Weight, tip primitives.Balance, paysFee primitives.Pays, class primitives.DispatchClass) primitives.FeeDetails { +func (m module) computeFeeRaw(len sc.U32, weight primitives.Weight, tip primitives.Balance, paysFee primitives.Pays, class primitives.DispatchClass) types.FeeDetails { if paysFee[0] == primitives.PaysYes { // TODO: type safety unadjustedWeightFee := m.weightToFee(weight) multiplier := m.storage.NextFeeMultiplier.Get() @@ -213,16 +214,16 @@ func (m module) computeFeeRaw(len sc.U32, weight primitives.Weight, tip primitiv lenFee := m.lengthToFee(len) baseFee := m.weightToFee(m.config.BlockWeights.Get(class).BaseExtrinsic) - inclusionFee := sc.NewOption[primitives.InclusionFee](primitives.NewInclusionFee(baseFee, lenFee, adjustedWeightFee)) + inclusionFee := sc.NewOption[types.InclusionFee](types.NewInclusionFee(baseFee, lenFee, adjustedWeightFee)) - return primitives.FeeDetails{ + return types.FeeDetails{ InclusionFee: inclusionFee, Tip: tip, } } - return primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee](nil), + return types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee](nil), Tip: tip, } } diff --git a/primitives/types/fee_details.go b/frame/transaction_payment/types/fee_details.go similarity index 78% rename from primitives/types/fee_details.go rename to frame/transaction_payment/types/fee_details.go index 3a4a04f4..dfba5bd9 100644 --- a/primitives/types/fee_details.go +++ b/frame/transaction_payment/types/fee_details.go @@ -4,12 +4,12 @@ import ( "bytes" sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" ) type FeeDetails struct { InclusionFee sc.Option[InclusionFee] - - Tip Balance // not serializable + Tip primitives.Balance // not serializable } func (fd FeeDetails) Encode(buffer *bytes.Buffer) { @@ -26,7 +26,7 @@ func DecodeFeeDetails(buffer *bytes.Buffer) FeeDetails { } } -func (fd FeeDetails) FinalFee() Balance { +func (fd FeeDetails) FinalFee() primitives.Balance { sum := fd.Tip if fd.InclusionFee.HasValue { diff --git a/frame/transaction_payment/types/fee_details_test.go b/frame/transaction_payment/types/fee_details_test.go new file mode 100644 index 00000000..2b8e6cb8 --- /dev/null +++ b/frame/transaction_payment/types/fee_details_test.go @@ -0,0 +1,71 @@ +package types + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedFeeDetailsBytes, _ = hex.DecodeString( + "01010000000000000000000000000000000200000000000000000000000000000003000000000000000000000000000000", + ) +) + +var ( + targetFeeDetails = FeeDetails{ + InclusionFee: sc.NewOption[InclusionFee](InclusionFee{ + BaseFee: sc.NewU128(1), + LenFee: sc.NewU128(2), + AdjustedWeightFee: sc.NewU128(3), + }), + Tip: sc.NewU128(4), + } +) + +func Test_FeeDetails_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + targetFeeDetails.Encode(buffer) + + assert.Equal(t, expectedFeeDetailsBytes, buffer.Bytes()) + +} + +func Test_FeeDetails_Bytes(t *testing.T) { + assert.Equal(t, expectedFeeDetailsBytes, targetFeeDetails.Bytes()) +} + +func Test_FeeDetails_DecodeFeeDetails(t *testing.T) { + result := DecodeFeeDetails(bytes.NewBuffer(expectedFeeDetailsBytes)) + + expectedFeeDetails := FeeDetails{ + InclusionFee: sc.NewOption[InclusionFee](InclusionFee{ + BaseFee: sc.NewU128(1), + LenFee: sc.NewU128(2), + AdjustedWeightFee: sc.NewU128(3), + }), + } + + assert.Equal(t, expectedFeeDetails, result) +} + +func Test_FeeDetails_FinalFee_WithInclusionFee(t *testing.T) { + result := targetFeeDetails.FinalFee() + + assert.Equal(t, sc.NewU128(10), result) +} + +func Test_FeeDetails_FinalFee_WithoutInclusionFee(t *testing.T) { + targetFeeDetails := FeeDetails{ + InclusionFee: sc.NewOption[InclusionFee](nil), + Tip: sc.NewU128(4), + } + + result := targetFeeDetails.FinalFee() + + assert.Equal(t, sc.NewU128(4), result) +} diff --git a/primitives/types/inclusion_fee.go b/frame/transaction_payment/types/inclusion_fee.go similarity index 67% rename from primitives/types/inclusion_fee.go rename to frame/transaction_payment/types/inclusion_fee.go index 79bc6fb3..979d7989 100644 --- a/primitives/types/inclusion_fee.go +++ b/frame/transaction_payment/types/inclusion_fee.go @@ -4,15 +4,16 @@ import ( "bytes" sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" ) type InclusionFee struct { - BaseFee Balance - LenFee Balance - AdjustedWeightFee Balance + BaseFee primitives.Balance + LenFee primitives.Balance + AdjustedWeightFee primitives.Balance } -func NewInclusionFee(baseFee, lenFee, adjustedWeightFee Balance) InclusionFee { +func NewInclusionFee(baseFee, lenFee, adjustedWeightFee primitives.Balance) InclusionFee { return InclusionFee{ baseFee, lenFee, @@ -38,6 +39,6 @@ func DecodeInclusionFee(buffer *bytes.Buffer) InclusionFee { } } -func (i InclusionFee) InclusionFee() Balance { +func (i InclusionFee) InclusionFee() primitives.Balance { return i.BaseFee.Add(i.LenFee).Add(i.AdjustedWeightFee) } diff --git a/frame/transaction_payment/types/inclusion_fee_test.go b/frame/transaction_payment/types/inclusion_fee_test.go new file mode 100644 index 00000000..ff40fb71 --- /dev/null +++ b/frame/transaction_payment/types/inclusion_fee_test.go @@ -0,0 +1,54 @@ +package types + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedInclusionFeeBytes, _ = hex.DecodeString( + "010000000000000000000000000000000200000000000000000000000000000003000000000000000000000000000000", + ) +) + +var ( + targetInclusionFee = InclusionFee{ + BaseFee: sc.NewU128(1), + LenFee: sc.NewU128(2), + AdjustedWeightFee: sc.NewU128(3), + } +) + +func Test_NewInclusionFee(t *testing.T) { + result := NewInclusionFee(sc.NewU128(1), sc.NewU128(2), sc.NewU128(3)) + + assert.Equal(t, targetInclusionFee, result) +} + +func Test_InclusionFee_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + targetInclusionFee.Encode(buffer) + + assert.Equal(t, expectedInclusionFeeBytes, buffer.Bytes()) +} + +func Test_InclusionFee_Bytes(t *testing.T) { + assert.Equal(t, expectedInclusionFeeBytes, targetInclusionFee.Bytes()) +} + +func Test_DecodeInclusionFee(t *testing.T) { + result := DecodeInclusionFee(bytes.NewBuffer(expectedInclusionFeeBytes)) + + assert.Equal(t, targetInclusionFee, result) +} + +func Test_InclusionFee_InclusionFee(t *testing.T) { + result := targetInclusionFee.InclusionFee() + + assert.Equal(t, sc.NewU128(6), result) +} diff --git a/mocks/tx_payment_module.go b/mocks/tx_payment_module.go index 87c2ba3d..153ecb84 100644 --- a/mocks/tx_payment_module.go +++ b/mocks/tx_payment_module.go @@ -2,6 +2,7 @@ package mocks import ( sc "github.com/LimeChain/goscale" + tx_types "github.com/LimeChain/gosemble/frame/transaction_payment/types" "github.com/LimeChain/gosemble/primitives/types" "github.com/stretchr/testify/mock" ) @@ -83,9 +84,9 @@ func (m *TransactionPaymentModule) ComputeFee(len sc.U32, info types.DispatchInf return args.Get(0).(types.Balance) } -func (m *TransactionPaymentModule) ComputeFeeDetails(len sc.U32, info types.DispatchInfo, tip types.Balance) types.FeeDetails { +func (m *TransactionPaymentModule) ComputeFeeDetails(len sc.U32, info types.DispatchInfo, tip types.Balance) tx_types.FeeDetails { args := m.Called(len, info, tip) - return args.Get(0).(types.FeeDetails) + return args.Get(0).(tx_types.FeeDetails) } func (m *TransactionPaymentModule) ComputeActualFee(len sc.U32, info types.DispatchInfo, postInfo types.PostDispatchInfo, tip types.Balance) types.Balance { diff --git a/runtime/transaction_payment_test.go b/runtime/transaction_payment_test.go index 523fd3a6..f38564d5 100644 --- a/runtime/transaction_payment_test.go +++ b/runtime/transaction_payment_test.go @@ -4,6 +4,7 @@ import ( "bytes" "testing" + "github.com/LimeChain/gosemble/frame/transaction_payment/types" primitives "github.com/LimeChain/gosemble/primitives/types" sc "github.com/LimeChain/goscale" @@ -140,11 +141,11 @@ func Test_TransactionPaymentApi_QueryFeeDetails_Signed_Success(t *testing.T) { buffer.Reset() buffer.Write(bytesFeeDetails) - fd := primitives.DecodeFeeDetails(buffer) + fd := types.DecodeFeeDetails(buffer) - expectedFd := primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee]( - primitives.NewInclusionFee( + expectedFd := types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee]( + types.NewInclusionFee( sc.NewU128(110_536_000), sc.NewU128(107), sc.NewU128(0), @@ -176,10 +177,10 @@ func Test_TransactionPaymentApi_QueryFeeDetails_Unsigned_Success(t *testing.T) { buffer.Reset() buffer.Write(bytesFeeDetails) - fd := primitives.DecodeFeeDetails(buffer) + fd := types.DecodeFeeDetails(buffer) - expectedFd := primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee](nil), + expectedFd := types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee](nil), } assert.Equal(t, expectedFd, fd) @@ -242,11 +243,11 @@ func Test_TransactionPaymentCallApi_QueryCallFeeDetails_Success(t *testing.T) { buffer.Reset() buffer.Write(bytesFeeDetails) - fd := primitives.DecodeFeeDetails(buffer) + fd := types.DecodeFeeDetails(buffer) - expectedFd := primitives.FeeDetails{ - InclusionFee: sc.NewOption[primitives.InclusionFee]( - primitives.NewInclusionFee( + expectedFd := types.FeeDetails{ + InclusionFee: sc.NewOption[types.InclusionFee]( + types.NewInclusionFee( sc.NewU128(110_536_000), sc.NewU128(3), sc.NewU128(0),