Browse Source

JoyBTreeSet sorting fix + compiler-forced UInt values

Leszek Wiesner 4 years ago
parent
commit
a3538fc9fb

+ 1 - 1
cli/src/base/WorkingGroupsCommandBase.ts

@@ -110,7 +110,7 @@ export default abstract class WorkingGroupsCommandBase extends AccountsCommandBa
       })),
     })
 
-    return acceptedApplications.sort() // Sort just in case, since runtime expects them to be sorted
+    return acceptedApplications
   }
 
   async promptForNewOpeningDraftName() {

+ 1 - 1
pioneer/packages/joy-roles/src/tabs/Admin.controller.tsx

@@ -790,7 +790,7 @@ const OpeningView = (props: OpeningViewProps) => {
     case OpeningState.InReview:
       CTAs = (
         <Container align='right'>
-          <Button onClick={() => { props.controller.acceptCuratorApplications(address, props.opening.curatorId, selected.sort()); }}>Accept curator applications</Button>
+          <Button onClick={() => { props.controller.acceptCuratorApplications(address, props.opening.curatorId, selected); }}>Accept curator applications</Button>
         </Container>
       );
   }

+ 3 - 3
types/augment/all/defs.json

@@ -1,7 +1,7 @@
 {
     "MemoText": "Text",
     "Credential": "u64",
-    "CredentialSet": "Vec<Credential>",
+    "CredentialSet": "BTreeSet<Credential>",
     "BlockAndTime": {
         "block": "u32",
         "time": "u64"
@@ -451,7 +451,7 @@
         }
     },
     "CuratorApplicationIdToCuratorIdMap": "BTreeMap<HiringApplicationId,CuratorId>",
-    "CuratorApplicationIdSet": "Vec<CuratorApplicationId>",
+    "CuratorApplicationIdSet": "BTreeSet<CuratorApplicationId>",
     "CuratorRoleStakeProfile": {
         "stake_id": "u64",
         "termination_unstaking_period": "Option<u32>",
@@ -934,7 +934,7 @@
     },
     "AddSchemaSupportToEntityOperation": {
         "entity_id": "ParameterizedEntity",
-        "schema_id": "u16",
+        "schema_id": "SchemaId",
         "parametrized_property_values": "Vec<ParametrizedClassPropertyValue>"
     },
     "OperationType": {

+ 3 - 3
types/augment/all/types.ts

@@ -70,7 +70,7 @@ export interface Address extends AccountId {}
 /** @name AddSchemaSupportToEntityOperation */
 export interface AddSchemaSupportToEntityOperation extends Struct {
   readonly entity_id: ParameterizedEntity;
-  readonly schema_id: u16;
+  readonly schema_id: SchemaId;
   readonly parametrized_property_values: Vec<ParametrizedClassPropertyValue>;
 }
 
@@ -273,7 +273,7 @@ export interface CreateEntityOperation extends Struct {
 export interface Credential extends u64 {}
 
 /** @name CredentialSet */
-export interface CredentialSet extends Vec<Credential> {}
+export interface CredentialSet extends BTreeSet<Credential> {}
 
 /** @name CurationActor */
 export interface CurationActor extends Enum {
@@ -304,7 +304,7 @@ export interface CuratorApplication extends Struct {
 export interface CuratorApplicationId extends u64 {}
 
 /** @name CuratorApplicationIdSet */
-export interface CuratorApplicationIdSet extends Vec<CuratorApplicationId> {}
+export interface CuratorApplicationIdSet extends BTreeSet<CuratorApplicationId> {}
 
 /** @name CuratorApplicationIdToCuratorIdMap */
 export interface CuratorApplicationIdToCuratorIdMap extends BTreeMap<HiringApplicationId, CuratorId> {}

+ 21 - 26
types/src/common.ts

@@ -1,6 +1,6 @@
-import { Struct, Option, Text, bool, Vec, u16, u32, u64, Null, U8aFixed, BTreeSet, Compact } from '@polkadot/types'
-import { BlockNumber, H256, Moment } from '@polkadot/types/interfaces'
-import { Codec, Constructor, InterfaceTypes, Registry, RegistryTypes } from '@polkadot/types/types'
+import { Struct, Option, Text, bool, u16, u32, u64, Null, U8aFixed, BTreeSet, Compact, UInt } from '@polkadot/types'
+import { BlockNumber, Hash as PolkadotHash, Moment } from '@polkadot/types/interfaces'
+import { Codec, Constructor, RegistryTypes } from '@polkadot/types/types'
 import { u8aConcat, u8aToHex } from '@polkadot/util'
 // we get 'moment' because it is a dependency of @polkadot/util, via @polkadot/keyring
 import moment from 'moment'
@@ -9,46 +9,41 @@ import { JoyEnum } from './JoyEnum'
 
 export { JoyEnum, JoyStructCustom, JoyStructDecorated }
 
-// Adds ".sort()" during BTreeSet toU8a encoding (required by the runtime)
+// Adds sorting during BTreeSet toU8a encoding (required by the runtime)
+// Currently only supports values that extend UInt
 // FIXME: Will not cover cases where BTreeSet is part of extrinsic args metadata
-export class JoyBTreeSet<V extends Codec> extends BTreeSet<V> {
-  public static with<V extends Codec>(valType: Constructor<V> | keyof InterfaceTypes): Constructor<JoyBTreeSet<V>> {
-    return class extends JoyBTreeSet<V> {
-      constructor(registry: Registry, value?: Uint8Array | string | Set<any>) {
-        super(registry, valType, value)
-      }
-    }
-  }
+export function JoyBTreeSet<V extends UInt>(valType: Constructor<V>): Constructor<BTreeSet<V>> {
+  return class extends BTreeSet.with(valType) {
+    public toU8a(isBare?: boolean): Uint8Array {
+      const encoded = new Array<Uint8Array>()
 
-  public toU8a(isBare?: boolean): Uint8Array {
-    const encoded = new Array<Uint8Array>()
+      if (!isBare) {
+        encoded.push(Compact.encodeU8a(this.size))
+      }
 
-    if (!isBare) {
-      encoded.push(Compact.encodeU8a(this.size))
-    }
+      const sorted = Array.from(this).sort((a, b) => (a.lt(b) ? -1 : 1))
 
-    Array.from(this)
-      .sort()
-      .forEach((v: V) => {
+      sorted.forEach((v: V) => {
         encoded.push(v.toU8a(isBare))
       })
 
-    return u8aConcat(...encoded)
-  }
+      return u8aConcat(...encoded)
+    }
 
-  public toHex(): string {
-    return u8aToHex(this.toU8a())
+    public toHex(): string {
+      return u8aToHex(this.toU8a())
+    }
   }
 }
 
 export class Credential extends u64 {}
-export class CredentialSet extends Vec.with(Credential) {} // BtreeSet ?
+export class CredentialSet extends JoyBTreeSet(Credential) {}
 
 // common types between Forum and Proposal Discussions modules
 export class ThreadId extends u64 {}
 export class PostId extends u64 {}
 
-export class Hash extends U8aFixed implements H256 {}
+export class Hash extends U8aFixed implements PolkadotHash {}
 
 export type BlockAndTimeType = {
   block: BlockNumber

+ 4 - 4
types/src/content-directory/index.ts

@@ -19,7 +19,7 @@ export class ClassPermissions extends JoyStructDecorated({
   any_member: bool,
   entity_creation_blocked: bool,
   all_entity_property_values_locked: bool,
-  maintainers: JoyBTreeSet.with(CuratorGroupId),
+  maintainers: JoyBTreeSet(CuratorGroupId),
 }) {}
 
 // Named just "Type" in the runtime, but this name conflicts with @polkadot/types/primitive/Type.ts
@@ -61,7 +61,7 @@ export class Property extends JoyStructDecorated({
 }) {}
 
 export class Schema extends JoyStructDecorated({
-  properties: JoyBTreeSet.with(PropertyId),
+  properties: JoyBTreeSet(PropertyId),
   is_active: bool,
 }) {}
 
@@ -132,13 +132,13 @@ export class InboundReferenceCounter extends JoyStructDecorated({
 export class Entity extends JoyStructDecorated({
   entity_permissions: EntityPermissions,
   class_id: ClassId,
-  supported_schemas: JoyBTreeSet.with(SchemaId),
+  supported_schemas: JoyBTreeSet(SchemaId),
   values: BTreeMap.with(PropertyId, StoredPropertyValue),
   reference_counter: InboundReferenceCounter,
 }) {}
 
 export class CuratorGroup extends JoyStructDecorated({
-  curators: JoyBTreeSet.with(CuratorId),
+  curators: JoyBTreeSet(CuratorId),
   active: bool,
   number_of_classes_maintained: u32,
 }) {}

+ 4 - 4
types/src/content-working-group/index.ts

@@ -1,4 +1,4 @@
-import { BTreeMap, bool, u32, Text, Null, Option, Vec } from '@polkadot/types'
+import { BTreeMap, bool, u32, Text, Null, Option, BTreeSet } from '@polkadot/types'
 import { BlockNumber } from '@polkadot/types/interfaces'
 import { OptionText, Credential, JoyEnum, JoyStructDecorated, SlashingTerms, JoyBTreeSet } from '../common'
 import { ActorId, MemberId } from '../members'
@@ -237,13 +237,13 @@ export class OpeningPolicyCommitment
 
 export type ICuratorOpening = {
   opening_id: OpeningId
-  curator_applications: JoyBTreeSet<CuratorApplicationId>
+  curator_applications: BTreeSet<CuratorApplicationId>
   policy_commitment: OpeningPolicyCommitment
 }
 export class CuratorOpening
   extends JoyStructDecorated({
     opening_id: OpeningId,
-    curator_applications: JoyBTreeSet.with(CuratorApplicationId),
+    curator_applications: JoyBTreeSet(CuratorApplicationId),
     policy_commitment: OpeningPolicyCommitment,
   })
   implements ICuratorOpening {
@@ -296,7 +296,7 @@ export class WorkingGroupUnstaker extends JoyEnum({
 
 export class CuratorApplicationIdToCuratorIdMap extends BTreeMap.with(ApplicationId, CuratorId) {}
 
-export class CuratorApplicationIdSet extends Vec.with(CuratorApplicationId) {}
+export class CuratorApplicationIdSet extends JoyBTreeSet(CuratorApplicationId) {}
 
 export const contentWorkingGroupTypes = {
   ChannelId,

+ 3 - 3
types/src/hiring/index.ts

@@ -1,4 +1,4 @@
-import { Null, u128, u64, u32, Option, Text } from '@polkadot/types'
+import { Null, u128, u64, u32, Option, Text, BTreeSet } from '@polkadot/types'
 import { BlockNumber, Balance } from '@polkadot/types/interfaces'
 import { JoyBTreeSet, JoyEnum, JoyStructDecorated } from '../common'
 import { StakeId } from '../stake'
@@ -166,14 +166,14 @@ export class ActiveOpeningStage extends JoyEnum(ActiveOpeningStageDef) {}
 
 export type ActiveOpeningStageVariantType = {
   stage: ActiveOpeningStage
-  applications_added: JoyBTreeSet<ApplicationId>
+  applications_added: BTreeSet<ApplicationId>
   active_application_count: u32
   unstaking_application_count: u32
   deactivated_application_count: u32
 }
 export class ActiveOpeningStageVariant extends JoyStructDecorated({
   stage: ActiveOpeningStage,
-  applications_added: JoyBTreeSet.with(ApplicationId),
+  applications_added: JoyBTreeSet(ApplicationId),
   active_application_count: u32,
   unstaking_application_count: u32,
   deactivated_application_count: u32,

+ 2 - 2
types/src/working-group/index.ts

@@ -34,7 +34,7 @@ export class WorkerId extends ActorId {}
 
 export class StorageProviderId extends WorkerId {}
 
-export class ApplicationIdSet extends JoyBTreeSet.with(ApplicationId) {}
+export class ApplicationIdSet extends JoyBTreeSet(ApplicationId) {}
 
 export class ApplicationIdToWorkerIdMap extends BTreeMap.with(ApplicationId, WorkerId) {}
 
@@ -138,7 +138,7 @@ export type IOpening = {
 export class Opening
   extends JoyStructDecorated({
     hiring_opening_id: OpeningId,
-    applications: JoyBTreeSet.with(ApplicationId),
+    applications: JoyBTreeSet(ApplicationId),
     policy_commitment: WorkingGroupOpeningPolicyCommitment,
     opening_type: OpeningType,
   })