From 3fe798bb2be2a2a3bf53f9444260c629bc3cdd57 Mon Sep 17 00:00:00 2001 From: jin Date: Mon, 20 Jan 2025 02:25:10 +0300 Subject: [PATCH] PoW for antispam --- atom/atom.test.ts | 2 +- dict/dict.test.ts | 6 +- empire/empire.test.ts | 2 +- flex/field/field.view.ts | 6 +- flex/form/form.view.ts | 2 +- gift/gift.ts | 23 ++++--- glob/glob.ts | 12 ++-- home/home.test.ts | 2 +- land/grab/grab.view.ts | 6 +- land/land.test.ts | 56 ++++++++-------- land/land.ts | 68 ++++++++++++------- land/rights/rights.locale=ru.json | 10 +-- land/rights/rights.view.tree | 10 +-- land/rights/rights.view.ts | 6 +- node/node.ts | 4 +- pass/pass.ts | 8 +-- rank/rank.ts | 105 +++++++++++++++++++++--------- sand/sand.ts | 4 ++ unit/unit.test.ts | 4 +- unit/unit.ts | 22 +++++++ 20 files changed, 226 insertions(+), 132 deletions(-) diff --git a/atom/atom.test.ts b/atom/atom.test.ts index b794c905..1216df4a 100644 --- a/atom/atom.test.ts +++ b/atom/atom.test.ts @@ -55,7 +55,7 @@ namespace $.$$ { const land = $.$hyoo_crus_glob.home().land() const reg = land.Node( $hyoo_crus_atom_ref_to( ()=> $hyoo_crus_atom_vary ) ).Item( '11111111' ) - const remote = reg.ensure({ '': $hyoo_crus_rank.get })! + const remote = reg.ensure({ '': $hyoo_crus_rank_read })! $mol_assert_unique( reg.land(), remote.land() ) $mol_assert_equal( reg.vary()!, remote.ref() ) diff --git a/dict/dict.test.ts b/dict/dict.test.ts index f3896347..06b6ea30 100644 --- a/dict/dict.test.ts +++ b/dict/dict.test.ts @@ -80,7 +80,7 @@ namespace $.$$ { user.Title(null)!.val( 'Jin' ) $mol_assert_equal( user.Title()!.val() ?? '', 'Jin' ) - const account = user.Account(null)!.ensure({ '': $hyoo_crus_rank.get })! + const account = user.Account(null)!.ensure({ '': $hyoo_crus_rank_read })! $mol_assert_equal( user.Account()?.remote() ?? null, account ) $mol_assert_equal( account.User()?.remote() ?? null, null ) @@ -88,8 +88,8 @@ namespace $.$$ { $mol_assert_equal( account.User()?.remote(), user ) const articles = [ - user.Articles(null)!.remote_make({ '': $hyoo_crus_rank.get }), - user.Articles(null)!.remote_make({ '': $hyoo_crus_rank.get }), + user.Articles(null)!.remote_make({ '': $hyoo_crus_rank_read }), + user.Articles(null)!.remote_make({ '': $hyoo_crus_rank_read }), ] $mol_assert_equal( user.Articles()?.remote_list(), articles ) diff --git a/empire/empire.test.ts b/empire/empire.test.ts index 829ffe77..c76a92cf 100644 --- a/empire/empire.test.ts +++ b/empire/empire.test.ts @@ -16,7 +16,7 @@ namespace $.$$ { const before = start.shift( 'PT-1h' ) const after = start.shift( 'PT1h' ) - const pub = { '': $hyoo_crus_rank.get } + const pub = { '': $hyoo_crus_rank_read } const target = targets.path( [ Place.SPb, Type.Alarm, start ], pub )!.remote_make( pub )! targets.path( [ Place.SPb, Type.Alarm, after ], pub )!.add( target.ref() ) targets.path( [ Place.SPb, Type.Skip, start ], pub )!.add( target.ref() ) diff --git a/flex/field/field.view.ts b/flex/field/field.view.ts index 57bae5e6..c091f911 100644 --- a/flex/field/field.view.ts +++ b/flex/field/field.view.ts @@ -106,9 +106,9 @@ namespace $.$$ { const preset = ( { private: {}, - public: { '': $hyoo_crus_rank.get }, - lobby: { '': $hyoo_crus_rank.reg }, - orgy: { '': $hyoo_crus_rank.mod }, + public: { '': $hyoo_crus_rank_read }, + lobby: { '': $hyoo_crus_rank_join( 'just' ) }, + orgy: { '': $hyoo_crus_rank_post( 'just' ) }, } as Record< string, $hyoo_crus_rank_preset > )[ rights as any ] if( preset ) { diff --git a/flex/form/form.view.ts b/flex/form/form.view.ts index e20236b8..06ffbe20 100644 --- a/flex/form/form.view.ts +++ b/flex/form/form.view.ts @@ -4,7 +4,7 @@ namespace $.$$ { @ $mol_mem kind() { - const land = this.$.$hyoo_crus_glob.home().hall_by( $hyoo_crus_flex_domain, { '': $hyoo_crus_rank.get } )!.land() + const land = this.$.$hyoo_crus_glob.home().hall_by( $hyoo_crus_flex_domain, { '': $hyoo_crus_rank_read } )!.land() const domain = $hyoo_crus_flex_domain.ensure( land ) return this.node().cast( $hyoo_crus_flex_thing ).Kind()?.remote() ?? domain.Kinds()?.remote_list()[0] ?? null! diff --git a/gift/gift.ts b/gift/gift.ts index 00d9701f..641aece4 100644 --- a/gift/gift.ts +++ b/gift/gift.ts @@ -3,16 +3,16 @@ namespace $ { /** Given Rank and Secret */ export class $hyoo_crus_gift extends $hyoo_crus_unit { - rank( next?: $hyoo_crus_rank ) { + rank( next?: typeof $hyoo_crus_rank.Value ) { if( next !== undefined ) this.uint8( 0, $hyoo_crus_unit_kind.gift ) - next = this.uint8( 1, next ) + const res = this.uint8( 1, next ) as typeof $hyoo_crus_rank.Value - if( next < $hyoo_crus_rank.nil || next > $hyoo_crus_rank.law ) { - $mol_fail( new RangeError( `Wrong rank ${ next }` ) ) + if( res < $hyoo_crus_rank_deny || res > $hyoo_crus_rank_rule ) { + $mol_fail( new RangeError( `Wrong rank ${ res }` ) ) } - return next + return res } time( next?: number ) { @@ -49,11 +49,16 @@ namespace $ { kind: this.kind(), peer: this.peer(), dest: this.dest().description!, - rank: $hyoo_crus_rank[ this.rank() ], + tier: $hyoo_crus_rank_tier[ this.rank() &~ $hyoo_crus_rank_rate.just ], + work: this.work(), time: $hyoo_crus_time_dump( this.time() ), } } + rank_min() { + return $hyoo_crus_rank( $hyoo_crus_rank_rule | ( $hyoo_crus_rank_rate.just - this.work() ) ) + } + [ $mol_dev_format_head ]() { return $mol_dev_format_span( {} , $mol_dev_format_native( this ) , @@ -61,8 +66,10 @@ namespace $ { this.peer(), ' ๐Ÿ… ', $mol_dev_format_span( {}, this.dest().description || '_' ), - this.bill().some( v => v ) ? ' ๐Ÿ”' : ' ๐Ÿ“ข', - $hyoo_crus_rank[ this.rank() ], + this.bill().some( v => v ) ? ' ๐Ÿ”' : ' ๐Ÿ‘€', + $hyoo_crus_rank_tier[ this.rank() &~ $hyoo_crus_rank_rate.just ], + ':', + this.rank() & $hyoo_crus_rank_rate.just, ' ', $mol_dev_format_shade( $hyoo_crus_time_dump( this.time() ) ), ) diff --git a/glob/glob.ts b/glob/glob.ts index 32ba4521..30bbd4c5 100644 --- a/glob/glob.ts +++ b/glob/glob.ts @@ -28,18 +28,18 @@ namespace $ { } @ $mol_action - static king_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank.get } ) { + static king_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank_read } ) { const king = this.$.$hyoo_crus_auth.grab() const colony = ( $mol_wire_sync( $hyoo_crus_land ) as typeof $hyoo_crus_land ).make({ $: this.$ }) colony.auth = $mol_const( king ) - if( ( preset[''] ?? $hyoo_crus_rank.nil ) === $hyoo_crus_rank.nil ) { + if( ( preset[''] ?? $hyoo_crus_rank_deny ) === $hyoo_crus_rank_deny ) { colony.encrypted( true ) } const self = this.$.$hyoo_crus_auth.current() - colony.give( self, $hyoo_crus_rank.law ) + colony.give( self, $hyoo_crus_rank_rule ) for( const key in preset ) colony.give( key ? $hyoo_crus_auth.from( key ) : null, preset[ key ] ) @@ -49,17 +49,17 @@ namespace $ { } /** @deprecated Use `this.$.$hyoo_crus_glob` */ - king_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank.get } ) { + king_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank_read } ) { return this.$.$hyoo_crus_glob.king_grab( preset ) } @ $mol_action - static land_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank.get } ) { + static land_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank_read } ) { return this.Land( this.king_grab( preset ).lord() ) } /** @deprecated Use `this.$.$hyoo_crus_glob` */ - land_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank.get } ) { + land_grab( preset : $hyoo_crus_rank_preset = { '': $hyoo_crus_rank_read } ) { return this.$.$hyoo_crus_glob.land_grab( preset ) } diff --git a/home/home.test.ts b/home/home.test.ts index dcb62404..3ae41bc9 100644 --- a/home/home.test.ts +++ b/home/home.test.ts @@ -4,7 +4,7 @@ namespace $ { 'Per app profiles'( $ ) { const base = $.$hyoo_crus_glob.home() - const hall = base.hall_by( $hyoo_crus_dict, { '': $hyoo_crus_rank.get } )! + const hall = base.hall_by( $hyoo_crus_dict, { '': $hyoo_crus_rank_read } )! $mol_assert_unique( base.land(), hall ) diff --git a/land/grab/grab.view.ts b/land/grab/grab.view.ts index 41818bc6..789ec0d4 100644 --- a/land/grab/grab.view.ts +++ b/land/grab/grab.view.ts @@ -5,9 +5,9 @@ namespace $.$$ { const preset = ( { private: {}, - public: { '': $hyoo_crus_rank.get }, - lobby: { '': $hyoo_crus_rank.reg }, - orgy: { '': $hyoo_crus_rank.mod }, + public: { '': $hyoo_crus_rank_read }, + lobby: { '': $hyoo_crus_rank_join( 'slow' ) }, + orgy: { '': $hyoo_crus_rank_post( 'slow' ) }, } as Record< string, $hyoo_crus_rank_preset > )[ rights as any ] if( preset ) this.grab( preset ) diff --git a/land/land.test.ts b/land/land.test.ts index 0db37f1b..3c8d04aa 100644 --- a/land/land.test.ts +++ b/land/land.test.ts @@ -9,7 +9,7 @@ namespace $ { const land = $hyoo_crus_land.make({ $ }) $mol_assert_equal( land.joined_list(), [] ) - $mol_assert_equal( land.lord_rank( land.ref() ), $hyoo_crus_rank.law ) + $mol_assert_equal( land.lord_rank( land.ref() ), $hyoo_crus_rank_rule ) land.join() $mol_assert_equal( land.joined_list(), [ land.ref() ] ) @@ -21,36 +21,36 @@ namespace $ { const land1 = $hyoo_crus_land.make({ $ }) const land2 = $hyoo_crus_land.make({ $, ref: ()=> land1.ref(), auth: ()=> auth1 }) - $mol_assert_equal( land1.lord_rank( land1.ref() ), $hyoo_crus_rank.law ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.get ) + $mol_assert_equal( land1.lord_rank( land1.ref() ), $hyoo_crus_rank_rule ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_read ) - $mol_assert_fail( ()=> land2.give( auth2, $hyoo_crus_rank.reg ), 'Need reg rank to join' ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.get ) + $mol_assert_fail( ()=> land2.give( auth2, $hyoo_crus_rank_join( 'just' ) ), 'Need reg rank to join' ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_read ) - land1.give( auth1, $hyoo_crus_rank.get ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.get ) + land1.give( auth1, $hyoo_crus_rank_read ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_read ) - land1.give( auth1, $hyoo_crus_rank.reg ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.reg ) + land1.give( auth1, $hyoo_crus_rank_join( 'just' ) ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_join( 'just' ) ) - land1.give( auth1, $hyoo_crus_rank.get ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.get ) + land1.give( auth1, $hyoo_crus_rank_read ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_read ) - land1.give( auth1, $hyoo_crus_rank.mod ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.mod ) + land1.give( auth1, $hyoo_crus_rank_post( 'just' ) ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_post( 'just' ) ) - land1.give( auth1, $hyoo_crus_rank.reg ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.reg ) + land1.give( auth1, $hyoo_crus_rank_join( 'just' ) ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_join( 'just' ) ) - land1.give( auth1, $hyoo_crus_rank.law ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.law ) + land1.give( auth1, $hyoo_crus_rank_rule ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_rule ) - land1.give( auth1, $hyoo_crus_rank.mod ) - $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank.mod ) + land1.give( auth1, $hyoo_crus_rank_post( 'just' ) ) + $mol_assert_equal( land1.lord_rank( auth1.lord() ), $hyoo_crus_rank_post( 'just' ) ) land2.apply_unit( land1.delta_unit() ) - $mol_assert_equal( land2.lord_rank( auth1.lord() ), $hyoo_crus_rank.mod ) - $mol_assert_fail( ()=> land2.give( auth2, $hyoo_crus_rank.reg ), 'Need law rank to change rank' ) + $mol_assert_equal( land2.lord_rank( auth1.lord() ), $hyoo_crus_rank_post( 'just' ) ) + $mol_assert_fail( ()=> land2.give( auth2, $hyoo_crus_rank_join( 'just' ) ), 'Need law rank to change rank' ) }, @@ -76,7 +76,7 @@ namespace $ { $mol_assert_equal( land2.delta_unit().length, 3 ) $mol_assert_equal( land2.delta_unit( face ).length, 1 ) - land1.give( auth2, $hyoo_crus_rank.reg ) + land1.give( auth2, $hyoo_crus_rank_join( 'just' ) ) land2.apply_unit( land1.delta_unit() ) land2.join() $mol_assert_equal( land2.delta_unit().length, 5 ) @@ -86,17 +86,17 @@ namespace $ { $mol_assert_equal( land2.delta_unit().length, 5 ) $mol_assert_equal( land2.delta_unit( face ).length, 3 ) - land1.give( auth2, $hyoo_crus_rank.mod ) + land1.give( auth2, $hyoo_crus_rank_post( 'just' ) ) land2.apply_unit( land1.delta_unit() ) land2.post( 'AA222222', '', 'AA333333', new Uint8Array([ 4 ]) ) $mol_assert_equal( land2.delta_unit().length, 6 ) $mol_assert_equal( land2.delta_unit( face ).length, 4 ) - land1.give( auth2, $hyoo_crus_rank.reg ) + land1.give( auth2, $hyoo_crus_rank_join( 'just' ) ) land2.apply_unit( land1.delta_unit() ) $mol_assert_equal( land2.delta_unit().length, 5 ) - land1.give( auth2, $hyoo_crus_rank.get ) + land1.give( auth2, $hyoo_crus_rank_read ) land2.apply_unit( land1.delta_unit() ) $mol_assert_equal( land2.delta_unit().length, 4 ) @@ -181,11 +181,11 @@ namespace $ { 'Land Area inherits rights'( $ ) { - const base = $.$hyoo_crus_glob.land_grab({ '': $hyoo_crus_rank.mod }) + const base = $.$hyoo_crus_glob.land_grab({ '': $hyoo_crus_rank_post( 'just' ) }) const area = base.area_make() - $mol_assert_equal( area.lord_rank( area.auth().lord() ), $hyoo_crus_rank.law ) - $mol_assert_equal( area.lord_rank( $hyoo_crus_ref( '' ) ), $hyoo_crus_rank.mod ) + $mol_assert_equal( area.lord_rank( area.auth().lord() ), $hyoo_crus_rank_rule ) + $mol_assert_equal( area.lord_rank( $hyoo_crus_ref( '' ) ), $hyoo_crus_rank_post( 'just' ) ) }, diff --git a/land/land.ts b/land/land.ts index e28dd4ed..d1b43597 100644 --- a/land/land.ts +++ b/land/land.ts @@ -32,8 +32,7 @@ namespace $ { const auth = this.auth() const rank = this.lord_rank( auth.lord() ) - if( rank === $hyoo_crus_rank.reg ) return auth.peer() - if( rank === $hyoo_crus_rank.nil ) $mol_fail( new Error( 'Rank too low (nil)' ) ) + if( rank < $hyoo_crus_rank_tier.post ) $mol_fail( new Error( `Rank too low (${ rank })` ) ) for( let i = 0; i < 4096; ++i ) { @@ -138,13 +137,13 @@ namespace $ { /** Rights level of Lord for Land. */ @ $mol_mem_key - lord_rank( lord: $hyoo_crus_ref, next?: $hyoo_crus_rank ) { + lord_rank( lord: $hyoo_crus_ref, next?: typeof $hyoo_crus_rank.Value ): typeof $hyoo_crus_rank.Value { - if( lord === $hyoo_crus_ref_lord( this.ref() ) ) return $hyoo_crus_rank.law + if( lord === $hyoo_crus_ref_lord( this.ref() ) ) return $hyoo_crus_rank_rule const prev = this.gift.get( lord )?.rank() ?? this.gift.get( $hyoo_crus_ref( '' ) )?.rank() - ?? ( this.encrypted() ? $hyoo_crus_rank.nil : $hyoo_crus_rank.get ) + ?? ( this.encrypted() ? $hyoo_crus_rank_deny : $hyoo_crus_rank_read ) if( next === undefined ) return prev if( next === prev ) return prev @@ -156,9 +155,12 @@ namespace $ { /** Rights level of Peer for Land. */ peer_rank( peer: string ) { + const auth = this.pass.get( peer )! - if( !auth ) return $hyoo_crus_rank.get - return this.lord_rank( auth.lord() ) + if( auth ) return this.lord_rank( auth.lord() ) + + return this.encrypted() ? $hyoo_crus_rank_deny : $hyoo_crus_rank_read + } unit_sort( units: readonly $hyoo_crus_unit[] ) { @@ -379,7 +381,7 @@ namespace $ { const lord = next.lord() const peer = next.peer() - if( !skip_check && this.lord_rank( lord ) < $hyoo_crus_rank.reg ) return 'Need reg rank to join' + if( !skip_check && this.lord_rank( lord ) < next.rank_min() ) return 'Need reg rank to join' const exists = this.pass.get( peer ) if( exists ) return '' @@ -395,7 +397,7 @@ namespace $ { const peer = next.peer() const dest = next.dest() - if( !skip_check && this.peer_rank( peer ) < $hyoo_crus_rank.law ) return 'Need law rank to change rank' + if( !skip_check && this.peer_rank( peer ) < next.rank_min() ) return 'Need law rank to change rank' const prev = this.gift.get( dest ) if( prev && $hyoo_crus_gift.compare( prev, next ) <= 0 ) return '' @@ -405,7 +407,7 @@ namespace $ { if( !prev ) this.faces.total ++ - if( ( prev?.rank() ?? $hyoo_crus_rank.nil ) > next.rank() ) need_recheck = true + if( ( prev?.rank() ?? $hyoo_crus_rank_deny ) > next.rank() ) need_recheck = true }, @@ -415,7 +417,7 @@ namespace $ { const peer = next.peer() const self = next.self() - if( !skip_check && this.peer_rank( peer ) < $hyoo_crus_rank.mod ) return 'Need mod rank to post data' + if( !skip_check && this.peer_rank( peer ) < next.rank_min() ) return 'Need mod rank to post data' let peers = this.sand.get( head ) if( !peers ) this.sand.set( head, peers = new $mol_wire_dict ) @@ -449,29 +451,32 @@ namespace $ { recheck() { for( const [ peer, pass ] of this.pass ) { - if( this.lord_rank( pass.lord() ) >= $hyoo_crus_rank.reg ) continue + if( this.lord_rank( pass.lord() ) >= pass.rank_min() ) continue this.pass.delete( peer ) this.faces.total -- } for( const [ lord, gift ] of this.gift ) { - if( this.peer_rank( gift.peer() ) >= $hyoo_crus_rank.law ) continue + if( this.peer_rank( gift.peer() ) >= gift.rank_min() ) continue this.gift.delete( lord ) this.faces.total -- } for( const [ head, peers ] of this.sand ) { - for( const [ peer, units ] of peers ) { - if( this.peer_rank( peer ) >= $hyoo_crus_rank.mod ) continue - peers.delete( peer ) - this.faces.total -= units.size + for( const [ peer, sands ] of peers ) { + const rank = this.peer_rank( peer ) + for( const [ self, sand ] of sands ) { + if( rank >= sand.rank_min() ) continue + sands.delete( self ) + this.faces.total -- + } } } } @ $mol_action - fork( preset = { '': $hyoo_crus_rank.get } ) { + fork( preset: $hyoo_crus_rank_preset = { '': $hyoo_crus_rank_read } ) { const land = this.$.$hyoo_crus_glob.land_grab( preset ) land.Tine().items_vary([ this.ref() ]) return land @@ -632,7 +637,7 @@ namespace $ { @ $mol_action give( dest: $hyoo_crus_auth | $hyoo_crus_ref | null, - rank: $hyoo_crus_rank, + rank: typeof $hyoo_crus_rank.Value, ) { this.join() @@ -647,7 +652,7 @@ namespace $ { unit.dest( dest ? dest instanceof $hyoo_crus_auth ? dest.lord() : dest : $hyoo_crus_ref('') ) unit._land = this - if( rank >= $hyoo_crus_rank.get ) { + if( rank >= $hyoo_crus_rank_read ) { const secret_land = this.secret() if( secret_land ) { @@ -655,7 +660,7 @@ namespace $ { if( !dest ) $mol_fail( new Error( `Encrypted land can't be shared to everyone` ) ) const prev = this.gift.get( dest instanceof $hyoo_crus_auth ? dest.lord() : dest ) - if( prev && prev.rank() >= $hyoo_crus_rank.get ) { + if( prev && prev.rank() >= $hyoo_crus_rank_read ) { unit.bill().set( prev.bill() ) } else { @@ -925,8 +930,23 @@ namespace $ { const sens = unit.sens().slice() for( let i = 0; i < mixin.length; ++i ) sens[i+2] ^= mixin[i] - const sign = key.sign( sens ) - unit.sign( sign ) + while( true ) { + + const sign = key.sign( sens ) + unit.sign( sign.slice( 0, 2 ) ) + + const rank = unit instanceof $hyoo_crus_pass + ? this.lord_rank( unit.lord() ) + : this.peer_rank( unit.peer() ) + + if( rank >= unit.rank_min() ) { + unit.sign( sign ) + return + } else { + unit.sign( new Uint8Array([ 0, 0 ]) ) + } + + } } @@ -1041,7 +1061,7 @@ namespace $ { const unit = new $hyoo_crus_gift $hyoo_crus_unit_trusted.add( unit ) - unit.rank( $hyoo_crus_rank.law ) + unit.rank( $hyoo_crus_rank_rule ) unit.time( this.faces.tick() ) unit.peer( auth.peer() ) unit.dest( auth.lord() ) diff --git a/land/rights/rights.locale=ru.json b/land/rights/rights.locale=ru.json index 7762a0be..529c2643 100644 --- a/land/rights/rights.locale=ru.json +++ b/land/rights/rights.locale=ru.json @@ -1,8 +1,8 @@ { "$hyoo_crus_land_rights_Add_key_hint": "ะŸัƒะฑะปะธั‡ะฝั‹ะน ะบะปัŽั‡", - "$hyoo_crus_land_rights_Gift_rank_dictionary_nil": "๐Ÿ›‘ ะ—ะฐะฟั€ะตั‚", - "$hyoo_crus_land_rights_Gift_rank_dictionary_get": "๐Ÿ‘€ ะงั‚ะตะฝะธะต", - "$hyoo_crus_land_rights_Gift_rank_dictionary_reg": "๐Ÿ“ข ะ’ั…ะพะด", - "$hyoo_crus_land_rights_Gift_rank_dictionary_mod": "โœ ะ—ะฐะฟะธััŒ", - "$hyoo_crus_land_rights_Gift_rank_dictionary_law": "๐Ÿ‘‘ ะ’ะปะฐัั‚ัŒ" + "$hyoo_crus_land_rights_Gift_rank_dictionary_deny": "๐Ÿ›‘ ะ—ะฐะฟั€ะตั‚", + "$hyoo_crus_land_rights_Gift_rank_dictionary_read": "๐Ÿ‘€ ะงั‚ะตะฝะธะต", + "$hyoo_crus_land_rights_Gift_rank_dictionary_join": "๐Ÿ“ข ะ’ั…ะพะด", + "$hyoo_crus_land_rights_Gift_rank_dictionary_post": "โœ ะ—ะฐะฟะธััŒ", + "$hyoo_crus_land_rights_Gift_rank_dictionary_rule": "๐Ÿ‘‘ ะ’ะปะฐัั‚ัŒ" } diff --git a/land/rights/rights.view.tree b/land/rights/rights.view.tree index 654a2c5b..175eae3f 100644 --- a/land/rights/rights.view.tree +++ b/land/rights/rights.view.tree @@ -20,8 +20,8 @@ $hyoo_crus_land_rights $mol_list <= Gift_rank* $mol_select value? <=> gift_rank*? \nil dictionary * - nil @ \๐Ÿ›‘ Deny - get @ \๐Ÿ‘€ Read - reg @ \๐Ÿ“ข Auth - mod @ \โœ Post - law @ \๐Ÿ‘‘ Rule + deny @ \๐Ÿ›‘ Deny + read @ \๐Ÿ‘€ Read + join @ \๐Ÿ“ข Join + post @ \โœ Post + rule @ \๐Ÿ‘‘ Rule diff --git a/land/rights/rights.view.ts b/land/rights/rights.view.ts index c72137b5..29ff5360 100644 --- a/land/rights/rights.view.ts +++ b/land/rights/rights.view.ts @@ -18,13 +18,13 @@ namespace $.$$ { } @ $mol_mem_key - override gift_rank( id: string, next?: keyof typeof $hyoo_crus_rank ) { - return $hyoo_crus_rank[ this.land().lord_rank( $hyoo_crus_ref( id ), next && $hyoo_crus_rank[ next ] ) ] + override gift_rank( id: string, next?: keyof typeof $hyoo_crus_rank_tier ) { + return $hyoo_crus_rank_tier[ this.land().lord_rank( $hyoo_crus_ref( id ), next && $hyoo_crus_rank_make( next, 'just' ) ) ] } add_commit() { const auth = $hyoo_crus_auth.from( this.add_key() ) - this.land().give( auth, $hyoo_crus_rank.get ) + this.land().give( auth, $hyoo_crus_rank_read ) this.add_key( '' ) } diff --git a/node/node.ts b/node/node.ts index 37608d36..f382b95c 100644 --- a/node/node.ts +++ b/node/node.ts @@ -63,9 +63,9 @@ namespace $ { return this.units().length > 0 } - /** Ability to make changes by cussrent peer. */ + /** Ability to make changes by current peer. */ can_change() { - return this.land().lord_rank( this.land().auth().lord() ) >= $hyoo_crus_rank.reg + return this.land().lord_rank( this.land().auth().lord() ) >= $hyoo_crus_rank_tier.join } /** Time of last changed unit inside Node subtree */ diff --git a/pass/pass.ts b/pass/pass.ts index 0eef46ae..21bdd3ac 100644 --- a/pass/pass.ts +++ b/pass/pass.ts @@ -3,10 +3,6 @@ namespace $ { /** Public key of Peer */ export class $hyoo_crus_pass extends $hyoo_crus_unit { - work() { - return this.uint8( 1 ) - } - _lord!: $hyoo_crus_ref lord( next?: $hyoo_crus_ref ) { if( next === undefined && this._lord !== undefined ) return this._lord @@ -30,6 +26,10 @@ namespace $ { } } + rank_min() { + return $hyoo_crus_rank( $hyoo_crus_rank_tier.join | ( $hyoo_crus_rank_rate.just - this.work() ) ) + } + [ $mol_dev_format_head ]() { return $mol_dev_format_span( {} , $mol_dev_format_native( this ) , diff --git a/rank/rank.ts b/rank/rank.ts index 2b781079..4d1258a1 100644 --- a/rank/rank.ts +++ b/rank/rank.ts @@ -1,39 +1,80 @@ namespace $ { - /** Access level: nil, get, reg, mod, law */ - export enum $hyoo_crus_rank { - - /** 0. Forbidden. There is no access, neither read nor write. */ - nil = 0b0000, - - /** 1. Read only */ - get = 0b0001, - - /** 3. Join only (Pass) */ - reg = 0b0011, - - /** 7. Data modification (Pass, Sand) */ - mod = 0b0111, - - /** 15. Full administration (Pass, Sand, Gift) */ - law = 0b1111, + export const { $hyoo_crus_rank } = $mol_data_tagged({ + $hyoo_crus_rank: $mol_data_pipe( + $mol_data_integer, + ( rank: number )=> { + if( rank >= $hyoo_crus_rank_deny && rank <= $hyoo_crus_rank_rule ) return rank + $mol_fail( new $mol_data_error( `${rank} is out of Ran range` ) ) + } + ), + }) + + /** Makes Rank from Tier and Fame names. */ + export function $hyoo_crus_rank_make( + tier: keyof typeof $hyoo_crus_rank_tier, + fame: keyof typeof $hyoo_crus_rank_rate, + ) { + return ( $hyoo_crus_rank_tier[ tier ] | $hyoo_crus_rank_rate[ fame ] ) as typeof $hyoo_crus_rank.Value + } + + /** Access level: deny, read, join, post, rule */ + export enum $hyoo_crus_rank_tier { + + /** Forbidden. There is no access, neither read nor write. */ + deny = 0b0_0000_0000, + + /** Read only */ + read = 0b0_0001_0000, + + /** Join only (Pass) */ + join = 0b0_0011_0000, + + /** Post changes (Pass, Sand) */ + post = 0b0_0111_0000, + + /** Full control (Pass, Sand, Gift) */ + rule = 0b0_1111_0000, + + } + + /** Ease of making changes, depends on fame: evil, harm, even, nice, good */ + export enum $hyoo_crus_rank_rate { + + /** Very hard challenge. Minutes to put. */ + late = 0b0_0000, + /** Hard challendge. Tens seconds to put. */ + long = 0b0_0011, + + /** Required some work to prevent spam. Seconds to put. */ + slow = 0b0_0111, + + /** Slow mode. Hundred milliseconds to put. */ + fast = 0b0_1011, + + /** No work required. As fast as possble. Milliseconds to put. */ + just = 0b0_1111, + } - /** Record of Access level: nil, get, add, mod, law */ - export type $hyoo_crus_rank_preset = Record< string, $hyoo_crus_rank > - - /** The creator has full rights and no one else has access - * @deprecated inline value */ - export let $hyoo_crus_rank_private = {} as $hyoo_crus_rank_preset - /** The creator has full rights, but everyone can read it. Rank: get - * @deprecated inline value */ - export let $hyoo_crus_rank_public = { '': $hyoo_crus_rank.get } as $hyoo_crus_rank_preset - /** The creator has full rights, but everyone can add one node at a time. Rank: add - * @deprecated inline value */ - export let $hyoo_crus_rank_lobby = { '': $hyoo_crus_rank.mod } as $hyoo_crus_rank_preset - /** The creator has full rights, but anyone can change the data. Rank: mod - * @deprecated inline value */ - export let $hyoo_crus_rank_orgy = { '': $hyoo_crus_rank.mod } as $hyoo_crus_rank_preset + export const $hyoo_crus_rank_deny = $hyoo_crus_rank_make( 'deny', 'late' ) + export const $hyoo_crus_rank_read = $hyoo_crus_rank_make( 'read', 'late' ) + export const $hyoo_crus_rank_rule = $hyoo_crus_rank_make( 'rule', 'just' ) + export function $hyoo_crus_rank_join( + rate: keyof typeof $hyoo_crus_rank_rate + ) { + return $hyoo_crus_rank_make( 'join', rate ) + } + + export function $hyoo_crus_rank_post( + rate: keyof typeof $hyoo_crus_rank_rate + ) { + return $hyoo_crus_rank_make( 'post', rate ) + } + + /** Mapping Auth to Rank */ + export type $hyoo_crus_rank_preset = Record< string, typeof $hyoo_crus_rank.Value > + } diff --git a/sand/sand.ts b/sand/sand.ts index d32b3e1a..1f103497 100644 --- a/sand/sand.ts +++ b/sand/sand.ts @@ -142,6 +142,10 @@ namespace $ { } } + rank_min() { + return $hyoo_crus_rank( $hyoo_crus_rank_tier.post | ( $hyoo_crus_rank_rate.just - this.work() ) ) + } + [ $mol_dev_format_head ]() { return $mol_dev_format_span( {}, $mol_dev_format_native( this ), diff --git a/unit/unit.test.ts b/unit/unit.test.ts index 6baabb2e..87dbf732 100644 --- a/unit/unit.test.ts +++ b/unit/unit.test.ts @@ -20,10 +20,10 @@ namespace $ { 'gift unit type'() { const gift = new $hyoo_crus_gift - gift.rank( $hyoo_crus_rank.law ) + gift.rank( $hyoo_crus_rank_rule ) $mol_assert_equal( gift.kind(), 'gift' ) - $mol_assert_equal( gift.rank(), $hyoo_crus_rank.law ) + $mol_assert_equal( gift.rank(), $hyoo_crus_rank_rule ) }, diff --git a/unit/unit.ts b/unit/unit.ts index fd0821fd..5991370e 100644 --- a/unit/unit.ts +++ b/unit/unit.ts @@ -120,6 +120,28 @@ namespace $ { return this.sign().some( b => b ) } + work() { + + if( !this.signed() ) { + return $hyoo_crus_rank_rate.just + } + + const sign = this.sign() + let int = sign[0] | ( sign[1] << 8 ) + + let count = 0 + while( int & 1 ) { + int >>>= 1 + ++ count + } + + return count + } + + rank_min() { + return $hyoo_crus_rank_rule + } + _land = null as null | $hyoo_crus_land dump() {