Skip to content

Commit

Permalink
add customCallback feat
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Feb 19, 2025
1 parent 36a65e8 commit 52fb1b5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
1 change: 1 addition & 0 deletions gno/p/basedao/basedao.gno
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func New(conf *Config) *DAO {
NewUnassignRoleHandler(dao),
daokit.NewSetResourceHandler(dao.Core),
daokit.NewRemoveResourceHandler(dao.Core),
daokit.NewCustomCallbackHandler(),
} {
dao.Core.SetResource(&daokit.Resource{Handler: m, Condition: conf.InitialCondition})
}
Expand Down
30 changes: 10 additions & 20 deletions gno/p/basedao/basedao_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestNewDAO(t *testing.T) {
}
}

urequire.Equal(t, 7, dao.Core.ResourcesCount(), "expected 7 resources")
urequire.Equal(t, 8, dao.Core.ResourcesCount(), "expected 7 resources")
urequire.Equal(t, dao.Realm.PkgPath(), daoRealm.PkgPath())

// XXX: check realm and profile
Expand Down Expand Up @@ -754,10 +754,10 @@ func TestAddRemoveResource(t *testing.T) {
Condition: daocond.MembersThreshold(0.2, tdao.dao.Members.IsMember, tdao.dao.Members.MembersCount),
}),
}
urequire.Equal(t, 8, tdao.dao.Core.ResourcesCount(), "expected 8 resources")
urequire.Equal(t, 9, tdao.dao.Core.ResourcesCount(), "expected 9 resources")
std.TestSetOrigCaller(alice)
tdao.dao.InstantExecute(addResourceProposal)
urequire.Equal(t, 9, tdao.dao.Core.ResourcesCount(), "expected 9 resources")
urequire.Equal(t, 10, tdao.dao.Core.ResourcesCount(), "expected 10 resources")

removeResourceProposal := daokit.ProposalRequest{
Title: "My Proposal",
Expand All @@ -770,7 +770,7 @@ func TestAddRemoveResource(t *testing.T) {

tdao.dao.InstantExecute(removeResourceProposal)

urequire.Equal(t, 8, tdao.dao.Core.ResourcesCount(), "expected 8 resources")
urequire.Equal(t, 9, tdao.dao.Core.ResourcesCount(), "expected 9 resources")
}

func TestAddRemoveResourceInvalidateProposals(t *testing.T) {
Expand Down Expand Up @@ -848,8 +848,7 @@ func TestAddRemoveResourceInvalidateProposals(t *testing.T) {
})
}

func TestAddCustomReource(t *testing.T) {
resourceType := "killSwitch"
func TestAddCustomCallback(t *testing.T) {
members := []Member{
{
alice.String(),
Expand All @@ -866,28 +865,19 @@ func TestAddCustomReource(t *testing.T) {
t.Errorf("Expected member %s to have role 'admin'", bob.String())
}

addResourceProposal := daokit.ProposalRequest{
Title: "My Proposal",
Description: "My Proposal Description",
Message: daokit.NewSetResourceMsg(&daokit.Resource{
Handler: daokit.NewCustomHandler(resourceType, func() {
tdao.dao = nil
}),
Condition: daocond.MembersThreshold(0.2, tdao.dao.Members.IsMember, tdao.dao.Members.MembersCount),
}),
}
std.TestSetOrigCaller(alice)
tdao.dao.InstantExecute(addResourceProposal)

if tdao.dao == nil {
t.Error("dao should still be alive")
}

proposalCallCustomResource := daokit.ProposalRequest{
Message: daokit.NewMessage(resourceType, nil),
proposalKillSwitch := daokit.ProposalRequest{
Message: daokit.NewCustomCallback(func() {
tdao.dao = nil
}),
}

tdao.dao.InstantExecute(proposalCallCustomResource)
tdao.dao.InstantExecute(proposalKillSwitch)

if tdao.dao != nil {
t.Error("dao should have been killed")
Expand Down
46 changes: 34 additions & 12 deletions gno/p/daokit/messages.gno
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewRemoveResourceHandler(d *Core) MessageHandler {
panic(errors.New("invalid payload type"))
}
if d.Resources.getResource(payload.Handler.Type()) == nil {
panic("ressource " + payload.Handler.Type() + " does not exists")
panic("resource " + payload.Handler.Type() + " does not exists")
}
d.Resources.removeResource(payload)
d.ProposalModule.InvalidateWithResource(payload.Handler.Type())
Expand All @@ -103,22 +103,44 @@ func NewRemoveResourceMsg(payload *Resource) ExecutableMessage {
return NewMessage(MsgRemoveResourceKind, payload)
}

func NewCustomHandler(resourceType string, cb func()) MessageHandler {
return &CustomMessage{
cb: cb,
resourceType: resourceType,
}
}
const MsgCustomMessageKind = "gno.land/p/teritori/daokit.CustomMessage"

type CustomMessage struct {
cb func()
type CustomCallbackHandler struct {
resourceType string
}

func (m *CustomMessage) Execute(message ExecutableMessage) {
m.cb()
func NewCustomCallbackHandler() MessageHandler {
return &CustomCallbackHandler{
resourceType: MsgCustomMessageKind,
}
}

func (m *CustomMessage) Type() string {
func (m *CustomCallbackHandler) Execute(message ExecutableMessage) {
msg, ok := message.(*CustomCallback)
if !ok {
panic(errors.New("invalid message type"))
}
msg.cb()
}

func (m *CustomCallbackHandler) Type() string {
return m.resourceType
}

type CustomCallback struct {
cb func()
}

// String implements ExecutableMessage.
func (g *CustomCallback) String() string {
return ufmt.Sprintf("%v", g.cb)
}

// Type implements ExecutableMessage.
func (g *CustomCallback) Type() string {
return MsgCustomMessageKind
}

func NewCustomCallback(callback func()) ExecutableMessage {
return &CustomCallback{cb: callback}
}

0 comments on commit 52fb1b5

Please sign in to comment.