Skip to content
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(balances): latest implementation #420

Merged
merged 8 commits into from
Jun 14, 2024
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
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ start-network-babe:
cd ../../../..; \
WASMTIME_BACKTRACE_DETAILS=1 RUST_LOG=runtime=trace ./target/release/substrate-node --dev --execution=wasm

start-network:
cp build/runtime.wasm polkadot-sdk/substrate/bin/node-template/runtime.wasm; \
cd polkadot-sdk/substrate/bin/node-template/node; \
cargo build --release; \
cd ../../../..; \
WASMTIME_BACKTRACE_DETAILS=1 RUST_LOG=runtime=trace ./target/release/node-template --dev --execution=wasm

# gossamer node configuration
CHAIN_SPEC_PLAIN = ../testdata/chain-spec/plain.json
CHAIN_SPEC_UPDATED = ../testdata/chain-spec/plain-updated.json
Expand Down
3 changes: 2 additions & 1 deletion api/benchmarking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,15 @@ func (m Module) originAndMaybeAccount(benchmarkConfig benchmarking.BenchmarkConf

func (m Module) whitelistWellKnownKeys() {
keySystemHash := m.hashing.Twox128([]byte("System"))
keyBalancesHash := m.hashing.Twox128([]byte("Balances"))
keyBlockWeight := m.hashing.Twox128([]byte("BlockWeight"))
keyExecutionPhaseHash := m.hashing.Twox128([]byte("ExecutionPhase"))
keyEventCountHash := m.hashing.Twox128([]byte("EventCount"))
keyEventsHash := m.hashing.Twox128([]byte("Events"))
keyNumberHash := m.hashing.Twox128([]byte("Number"))
keyTotalIssuanceHash := m.hashing.Twox128([]byte("TotalIssuance"))

benchmarking.SetWhitelist(append(keySystemHash, keyTotalIssuanceHash...))
benchmarking.SetWhitelist(append(keyBalancesHash, keyTotalIssuanceHash...))
benchmarking.SetWhitelist(append(keySystemHash, keyBlockWeight...))
benchmarking.SetWhitelist(append(keySystemHash, keyNumberHash...))
benchmarking.SetWhitelist(append(keySystemHash, keyExecutionPhaseHash...))
Expand Down
41 changes: 32 additions & 9 deletions api/metadata/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ func (m Module) basicTypes() sc.Sequence[primitives.MetadataType] {
primitives.NewMetadataTypeDefinitionComposite(sc.Sequence[primitives.MetadataTypeDefinitionField]{
primitives.NewMetadataTypeDefinitionField(metadata.TypesFixedSequence32U8)})),

primitives.NewMetadataType(
metadata.TypesCompactU128,
"compact U128",
primitives.NewMetadataTypeDefinitionCompact(
sc.ToCompact(metadata.PrimitiveTypesU128),
),
),

primitives.NewMetadataTypeWithPath(metadata.TypesAddress32, "Address32", sc.Sequence[sc.Str]{"sp_core", "crypto", "AccountId32"}, primitives.NewMetadataTypeDefinitionComposite(
sc.Sequence[primitives.MetadataTypeDefinitionField]{primitives.NewMetadataTypeDefinitionFieldWithName(metadata.TypesFixedSequence32U8, "[u8; 32]")},
)),
Expand Down Expand Up @@ -337,8 +345,8 @@ func (m Module) basicTypes() sc.Sequence[primitives.MetadataType] {
sc.Sequence[primitives.MetadataTypeDefinitionField]{
primitives.NewMetadataTypeDefinitionFieldWithNames(metadata.PrimitiveTypesU128, "free", "Balance"),
primitives.NewMetadataTypeDefinitionFieldWithNames(metadata.PrimitiveTypesU128, "reserved", "Balance"),
primitives.NewMetadataTypeDefinitionFieldWithNames(metadata.PrimitiveTypesU128, "misc_frozen", "Balance"),
primitives.NewMetadataTypeDefinitionFieldWithNames(metadata.PrimitiveTypesU128, "fee_frozen", "Balance"),
primitives.NewMetadataTypeDefinitionFieldWithNames(metadata.PrimitiveTypesU128, "frozen", "Balance"),
primitives.NewMetadataTypeDefinitionFieldWithNames(metadata.PrimitiveTypesU128, "flags", "ExtraFlags"),
},
)),
primitives.NewMetadataTypeWithPath(metadata.TypesAccountInfo, "AccountInfo", sc.Sequence[sc.Str]{"frame_system", "AccountInfo"}, primitives.NewMetadataTypeDefinitionComposite(
Expand Down Expand Up @@ -591,17 +599,17 @@ func (m Module) basicTypes() sc.Sequence[primitives.MetadataType] {
primitives.NewMetadataTypeWithPath(metadata.TypesTokenError, "TokenError", sc.Sequence[sc.Str]{"sp_runtime", "TokenError"}, primitives.NewMetadataTypeDefinitionVariant(
sc.Sequence[primitives.MetadataDefinitionVariant]{
primitives.NewMetadataDefinitionVariant(
"NoFunds",
"FundsUnavailable",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorNoFunds,
"TokenError.NoFunds"),
primitives.TokenErrorFundsUnavailable,
"TokenError.FundsUnavailable"),
primitives.NewMetadataDefinitionVariant(
"WouldDie",
"OnlyProvider",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorWouldDie,
"TokenError.WouldDie"),
primitives.TokenErrorOnlyProvider,
"TokenError.OnlyProvider"),
primitives.NewMetadataDefinitionVariant(
"Mandatory",
"BelowMinimum",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorBelowMinimum,
"TokenError.BelowMinimum"),
Expand All @@ -625,6 +633,21 @@ func (m Module) basicTypes() sc.Sequence[primitives.MetadataType] {
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorUnsupported,
"TokenError.Unsupported"),
primitives.NewMetadataDefinitionVariant(
"CannotCreateHold",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorCannotCreateHold,
"TokenError.CannotCreateHold"),
primitives.NewMetadataDefinitionVariant(
"NotExpendable",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorNotExpendable,
"TokenError.NotExpendable"),
primitives.NewMetadataDefinitionVariant(
"Blocked",
sc.Sequence[primitives.MetadataTypeDefinitionField]{},
primitives.TokenErrorBlocked,
"TokenError.Blocked"),
})),
primitives.NewMetadataTypeWithPath(metadata.TypesArithmeticError, "ArithmeticError", sc.Sequence[sc.Str]{"sp_arithmetic", "ArithmeticError"}, primitives.NewMetadataTypeDefinitionVariant(
sc.Sequence[primitives.MetadataDefinitionVariant]{
Expand Down
Binary file modified build/runtime-benchmarks.wasm
Binary file not shown.
Binary file modified build/runtime.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions constants/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ const (

var (
Zero = sc.NewU128(0)
One = sc.NewU128(1)
DefaultTip = Zero
)
1 change: 1 addition & 0 deletions constants/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (

TypesBalancesEvent
TypesBalanceStatus
TypesBalancesAdjustDirection
TypesVecTopics
TypesLastRuntimeUpgradeInfo
TypesSystemErrors
Expand Down
135 changes: 135 additions & 0 deletions frame/balances/call_force_adjust_total_issuance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package balances

import (
"bytes"

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants"
"github.com/LimeChain/gosemble/frame/balances/types"
primitives "github.com/LimeChain/gosemble/primitives/types"
)

type callForceAdjustTotalIssuance struct {
primitives.Callable
storage *storage
eventDepositor primitives.EventDepositor
}

func newCallForceAdjustTotalIssuance(moduleId sc.U8, functionId sc.U8, eventDepositor primitives.EventDepositor, storage *storage) primitives.Call {
return callForceAdjustTotalIssuance{
Callable: primitives.Callable{
ModuleId: moduleId,
FunctionId: functionId,
Arguments: sc.NewVaryingData(types.AdjustDirection{}, sc.Compact{Number: sc.U128{}}),
},
eventDepositor: eventDepositor,
storage: storage,
}
}

func (c callForceAdjustTotalIssuance) DecodeArgs(buffer *bytes.Buffer) (primitives.Call, error) {
direction, err := types.DecodeAdjustDirection(buffer)
if err != nil {
return nil, err

Check warning on line 33 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L30-L33

Added lines #L30 - L33 were not covered by tests
}
delta, err := sc.DecodeCompact[sc.U128](buffer)
if err != nil {
return nil, err

Check warning on line 37 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}
c.Arguments = sc.NewVaryingData(
direction,
delta,
)

Check warning on line 42 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L39-L42

Added lines #L39 - L42 were not covered by tests

return c, nil

Check warning on line 44 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L44

Added line #L44 was not covered by tests
}

func (c callForceAdjustTotalIssuance) Encode(buffer *bytes.Buffer) error {
return c.Callable.Encode(buffer)

Check warning on line 48 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}

func (c callForceAdjustTotalIssuance) Bytes() []byte {
return c.Callable.Bytes()

Check warning on line 52 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

func (c callForceAdjustTotalIssuance) ModuleIndex() sc.U8 { return c.Callable.ModuleIndex() }

Check warning on line 55 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L55

Added line #L55 was not covered by tests

func (c callForceAdjustTotalIssuance) FunctionIndex() sc.U8 { return c.Callable.FunctionIndex() }

Check warning on line 57 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L57

Added line #L57 was not covered by tests

func (c callForceAdjustTotalIssuance) Args() sc.VaryingData { return c.Callable.Args() }

Check warning on line 59 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L59

Added line #L59 was not covered by tests

func (c callForceAdjustTotalIssuance) BaseWeight() primitives.Weight {
return callForceAdjustTotalIssuanceWeight()

Check warning on line 62 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

func (_ callForceAdjustTotalIssuance) WeighData(baseWeight primitives.Weight) primitives.Weight {
return primitives.WeightFromParts(baseWeight.RefTime, 0)

Check warning on line 66 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

func (_ callForceAdjustTotalIssuance) ClassifyDispatch(baseWeight primitives.Weight) primitives.DispatchClass {
return primitives.NewDispatchClassNormal()

Check warning on line 70 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

func (_ callForceAdjustTotalIssuance) PaysFee(baseWeight primitives.Weight) primitives.Pays {
return primitives.PaysYes

Check warning on line 74 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}

func (_ callForceAdjustTotalIssuance) Docs() string {
return "Adjust the total issuance in a saturating way. Can only be called by root and always needs a positive `delta`."

Check warning on line 78 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}

func (c callForceAdjustTotalIssuance) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
if !origin.IsRootOrigin() {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorBadOrigin()

Check warning on line 83 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L81-L83

Added lines #L81 - L83 were not covered by tests
}
direction, ok := args[0].(types.AdjustDirection)
if !ok {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorOther("invalid direction value when dispatching callForceAdjustTotalIssuance")

Check warning on line 87 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}
deltaCompact, ok := args[1].(sc.Compact)
if !ok {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorOther("invalid amount value when dispatching call force free")

Check warning on line 91 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L89-L91

Added lines #L89 - L91 were not covered by tests
}

delta, ok := deltaCompact.Number.(sc.U128)
if !ok {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorOther("invalid Compact field delta when dispatch call_force_adjust_total_issuance")

Check warning on line 96 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L94-L96

Added lines #L94 - L96 were not covered by tests
}

if delta.Lte(constants.Zero) {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorModule(primitives.CustomModuleError{
Index: c.ModuleId,
Err: sc.U32(ErrorDeltaZero),
Message: sc.NewOption[sc.Str](nil),
})

Check warning on line 104 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L99-L104

Added lines #L99 - L104 were not covered by tests
}

totalIssuance, err := c.storage.TotalIssuance.Get()
if err != nil {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorOther(sc.Str(err.Error()))

Check warning on line 109 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L107-L109

Added lines #L107 - L109 were not covered by tests
}

var newIssuance sc.U128
if direction.IsIncrease() {
newIssuance = sc.SaturatingAddU128(totalIssuance, delta)
} else {
newIssuance = sc.SaturatingSubU128(totalIssuance, delta)

Check warning on line 116 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L112-L116

Added lines #L112 - L116 were not covered by tests
}

inactiveIssuance, err := c.storage.InactiveIssuance.Get()
if err != nil {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorOther(sc.Str(err.Error()))

Check warning on line 121 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L119-L121

Added lines #L119 - L121 were not covered by tests
}

if inactiveIssuance.Gt(newIssuance) {
return primitives.PostDispatchInfo{}, primitives.NewDispatchErrorModule(primitives.CustomModuleError{
Index: c.ModuleId,
Err: sc.U32(ErrorIssuanceDeactivated),
Message: sc.NewOption[sc.Str](nil),
})

Check warning on line 129 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L124-L129

Added lines #L124 - L129 were not covered by tests
}
c.storage.TotalIssuance.Put(newIssuance)
c.eventDepositor.DepositEvent(newEventTotalIssuanceForced(c.ModuleId, totalIssuance, newIssuance))

Check warning on line 132 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L131-L132

Added lines #L131 - L132 were not covered by tests

return primitives.PostDispatchInfo{}, nil

Check warning on line 134 in frame/balances/call_force_adjust_total_issuance.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance.go#L134

Added line #L134 was not covered by tests
}
7 changes: 7 additions & 0 deletions frame/balances/call_force_adjust_total_issuance_weight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package balances

import primitives "github.com/LimeChain/gosemble/primitives/types"

func callForceAdjustTotalIssuanceWeight() primitives.Weight {
return primitives.WeightFromParts(6507000, 0)

Check warning on line 6 in frame/balances/call_force_adjust_total_issuance_weight.go

View check run for this annotation

Codecov / codecov/patch

frame/balances/call_force_adjust_total_issuance_weight.go#L5-L6

Added lines #L5 - L6 were not covered by tests
}
Loading