Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

channels_adapter.gno

4.87 Kb · 83 lines
 1// Fixed channels authority and membership. Content moderation remains with the
 2// current owner and appointed target admins; votes cannot call arbitrary code.
 3package memba_dao
 4
 5import (
 6	"gno.land/p/samcrew/memba_weighted_host"
 7	channels "gno.land/r/samcrew/memba_dao_channels_v2"
 8)
 9
10func channelsState(a memba_weighted_host.ChannelsAction) memba_weighted_host.ChannelsState {
11	owner:=channels.GetOwner()
12	subjectRoles:=""
13	if a.Subject()!="" { subjectRoles=channels.GetMemberRoles(a.Subject()) }
14	s:=memba_weighted_host.ChannelsState{
15		Owner:owner, PendingOwner:channels.GetPendingOwner(), Paused:channels.IsPaused(),
16		MemberCount:channels.GetMemberCount(), MembershipRevision:channels.GetMembershipRevision(), OwnerRoles:channels.GetMemberRoles(owner),
17		OwnerMember:channels.IsMember(owner), DAOMember:channels.IsMember(channelsPolicy.DAO()),
18		SuccessorMember:channels.IsMember(channelsPolicy.Successor()),
19		SubjectMember:a.Subject()!="" && channels.IsMember(a.Subject()),
20		DAORoles:channels.GetMemberRoles(channelsPolicy.DAO()),
21		SuccessorRoles:channels.GetMemberRoles(channelsPolicy.Successor()), SubjectRoles:subjectRoles,
22	}
23	s.ChannelCount,s.ChannelExists,s.ChannelDescription,s.ChannelType,s.ChannelReadRoles,s.ChannelWriteRoles=channels.GetChannelState(a.Name())
24	return s
25}
26func proposeChannels(cur realm,a memba_weighted_host.ChannelsAction) uint64 { return host.ProposeChannels(a,channelsState(a),cur) }
27func ProposeChannelsAccept(cur realm) uint64 { return proposeChannels(cur,channelsPolicy.Accept()) }
28func ProposeChannelsReturn(cur realm) uint64 { return proposeChannels(cur,channelsPolicy.Return()) }
29func ProposeChannelsAbortReturn(cur realm) uint64 { return proposeChannels(cur,channelsPolicy.AbortReturn()) }
30func ProposeChannelsAddMember(cur realm,who address,roles string) uint64 { return proposeChannels(cur,channelsPolicy.AddMember(who,roles)) }
31func ProposeChannelsRemoveMember(cur realm,who address) uint64 { return proposeChannels(cur,channelsPolicy.RemoveMember(who)) }
32func ProposeChannelsSetRoles(cur realm,who address,roles string) uint64 { return proposeChannels(cur,channelsPolicy.SetRoles(who,roles)) }
33func ProposeChannelsCreateText(cur realm,name,description string) uint64 { return proposeChannels(cur,channelsPolicy.CreateText(name,description)) }
34func ProposeChannelsUnpause(cur realm) uint64 { return proposeChannels(cur,memba_weighted_host.ChannelsResume()) }
35func EmergencyPauseChannels(cur realm) {
36	a:=memba_weighted_host.ChannelsResume()
37	before:=channelsState(a)
38	host.PrepareChannelsEmergencyPause(before,cur)
39	expected:=before; expected.Paused=true
40	channels.PauseRealm(cross(cur))
41	if channelsState(a)!=expected { panic("channels pause postcondition mismatch") }
42}
43func executeChannels(cur realm,id uint64) {
44	proposed:=host.Proposal(id).ChannelsAction
45	before:=channelsState(proposed)
46	a:=host.ConsumeChannels(id,before,cur)
47	expected:=before
48	switch a.Kind() {
49	case memba_weighted_host.ChannelsAcceptOwner:
50		expected.Owner=channelsPolicy.DAO();expected.PendingOwner=""
51		expected.OwnerRoles="admin";expected.DAORoles="admin";expected.OwnerMember=true;expected.DAOMember=true
52		expected.MembershipRevision++
53		if before.Owner==channelsPolicy.Successor() { expected.SuccessorRoles="";expected.SuccessorMember=false }
54		channels.AcceptOwnership(cross(cur))
55	case memba_weighted_host.ChannelsReturnOwner:
56		expected.PendingOwner=a.Recipient()
57		channels.ProposeOwner(cross(cur),a.Recipient())
58	case memba_weighted_host.ChannelsAbortReturn:
59		expected.PendingOwner=""
60		channels.CancelPendingOwner(cross(cur))
61	case memba_weighted_host.ChannelsAddMember:
62		expected.MemberCount++;expected.MembershipRevision++;expected.SubjectRoles=a.Roles();expected.SubjectMember=true
63		channels.AddMember(cross(cur),a.Subject(),a.Roles())
64	case memba_weighted_host.ChannelsRemoveMember:
65		expected.MemberCount--;expected.MembershipRevision++;expected.SubjectRoles="";expected.SubjectMember=false
66		if a.Subject()==channelsPolicy.Successor() { expected.SuccessorRoles="";expected.SuccessorMember=false }
67		channels.RemoveMember(cross(cur),a.Subject())
68	case memba_weighted_host.ChannelsSetRoles:
69		expected.MembershipRevision++;expected.SubjectRoles=a.Roles()
70		channels.UpdateMemberRoles(cross(cur),a.Subject(),a.Roles())
71	case memba_weighted_host.ChannelsCreateText:
72		expected.ChannelCount++;expected.ChannelExists=true
73		expected.ChannelDescription=a.Description();expected.ChannelType="text"
74		expected.ChannelReadRoles="admin,dev,ops,member";expected.ChannelWriteRoles="admin,dev,ops,member"
75		channels.CreateChannel(cross(cur),a.Name(),a.Description(),"text")
76	case memba_weighted_host.ChannelsUnpause:
77		expected.Paused=false
78		channels.UnpauseRealm(cross(cur))
79	default: panic("unsupported channels action")
80	}
81	if channelsState(a)!=expected { panic("channels postcondition mismatch") }
82	if a.Kind()==memba_weighted_host.ChannelsAcceptOwner && channels.IsMember(before.Owner) { panic("channels previous owner membership retained") }
83}