From 36a65e865314243bf440faf97487c6046f7fee5c Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 19 Feb 2025 12:16:28 +0100 Subject: [PATCH 1/3] add custom resource --- gno/p/basedao/basedao_test.gno | 46 ++++++++++++++++++++++++++++++++++ gno/p/daokit/messages.gno | 20 +++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/gno/p/basedao/basedao_test.gno b/gno/p/basedao/basedao_test.gno index 7de13d3154..6a9865e7bd 100644 --- a/gno/p/basedao/basedao_test.gno +++ b/gno/p/basedao/basedao_test.gno @@ -847,3 +847,49 @@ func TestAddRemoveResourceInvalidateProposals(t *testing.T) { return false }) } + +func TestAddCustomReource(t *testing.T) { + resourceType := "killSwitch" + members := []Member{ + { + alice.String(), + []string{"admin"}, + }, + { + bob.String(), + []string{"admin"}, + }, + } + tdao := newTestingDAO(t, 0.2, members) + + if !tdao.dao.Members.HasRole(bob.String(), "admin") { + 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), + } + + tdao.dao.InstantExecute(proposalCallCustomResource) + + if tdao.dao != nil { + t.Error("dao should have been killed") + } +} diff --git a/gno/p/daokit/messages.gno b/gno/p/daokit/messages.gno index ecfdd49225..c0206ad8d5 100644 --- a/gno/p/daokit/messages.gno +++ b/gno/p/daokit/messages.gno @@ -102,3 +102,23 @@ func NewRemoveResourceHandler(d *Core) MessageHandler { func NewRemoveResourceMsg(payload *Resource) ExecutableMessage { return NewMessage(MsgRemoveResourceKind, payload) } + +func NewCustomHandler(resourceType string, cb func()) MessageHandler { + return &CustomMessage{ + cb: cb, + resourceType: resourceType, + } +} + +type CustomMessage struct { + cb func() + resourceType string +} + +func (m *CustomMessage) Execute(message ExecutableMessage) { + m.cb() +} + +func (m *CustomMessage) Type() string { + return m.resourceType +} From 52fb1b5b03ea75377a02ad8c0e0599713e033f14 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 19 Feb 2025 13:04:54 +0100 Subject: [PATCH 2/3] add customCallback feat --- gno/p/basedao/basedao.gno | 1 + gno/p/basedao/basedao_test.gno | 30 ++++++++-------------- gno/p/daokit/messages.gno | 46 +++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/gno/p/basedao/basedao.gno b/gno/p/basedao/basedao.gno index 631dfab3d5..2480856fe4 100644 --- a/gno/p/basedao/basedao.gno +++ b/gno/p/basedao/basedao.gno @@ -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}) } diff --git a/gno/p/basedao/basedao_test.gno b/gno/p/basedao/basedao_test.gno index 6a9865e7bd..f5fde469b9 100644 --- a/gno/p/basedao/basedao_test.gno +++ b/gno/p/basedao/basedao_test.gno @@ -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 @@ -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", @@ -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) { @@ -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(), @@ -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") diff --git a/gno/p/daokit/messages.gno b/gno/p/daokit/messages.gno index c0206ad8d5..d964fac3df 100644 --- a/gno/p/daokit/messages.gno +++ b/gno/p/daokit/messages.gno @@ -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()) @@ -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} +} From aba671c5a88650a161c7311e7047f936f18c1c68 Mon Sep 17 00:00:00 2001 From: Norman Date: Wed, 19 Feb 2025 15:55:48 +0100 Subject: [PATCH 3/3] chore: refacto Signed-off-by: Norman --- gno/p/basedao/basedao.gno | 2 +- gno/p/basedao/basedao_test.gno | 2 +- gno/p/daokit/messages.gno | 58 ++++++++++------------------------ 3 files changed, 18 insertions(+), 44 deletions(-) diff --git a/gno/p/basedao/basedao.gno b/gno/p/basedao/basedao.gno index 2480856fe4..0cd2007a74 100644 --- a/gno/p/basedao/basedao.gno +++ b/gno/p/basedao/basedao.gno @@ -87,7 +87,7 @@ func New(conf *Config) *DAO { NewUnassignRoleHandler(dao), daokit.NewSetResourceHandler(dao.Core), daokit.NewRemoveResourceHandler(dao.Core), - daokit.NewCustomCallbackHandler(), + daokit.NewExecuteLambdaHandler(), } { dao.Core.SetResource(&daokit.Resource{Handler: m, Condition: conf.InitialCondition}) } diff --git a/gno/p/basedao/basedao_test.gno b/gno/p/basedao/basedao_test.gno index f5fde469b9..69e08670a5 100644 --- a/gno/p/basedao/basedao_test.gno +++ b/gno/p/basedao/basedao_test.gno @@ -872,7 +872,7 @@ func TestAddCustomCallback(t *testing.T) { } proposalKillSwitch := daokit.ProposalRequest{ - Message: daokit.NewCustomCallback(func() { + Message: daokit.NewExecuteLambdaMsg(func() { tdao.dao = nil }), } diff --git a/gno/p/daokit/messages.gno b/gno/p/daokit/messages.gno index d964fac3df..582c35bc54 100644 --- a/gno/p/daokit/messages.gno +++ b/gno/p/daokit/messages.gno @@ -65,6 +65,22 @@ func (g *genericMessageHandler) Type() string { return g.kind } +const MsgExecuteLambdaKind = "gno.land/p/teritori/daokit.ExecuteLambda" + +func NewExecuteLambdaHandler() MessageHandler { + return NewMessageHandler(MsgExecuteLambdaKind, func(i interface{}) { + cb, ok := i.(func()) + if !ok { + panic(errors.New("invalid msg type")) + } + cb() + }) +} + +func NewExecuteLambdaMsg(cb func()) ExecutableMessage { + return NewMessage(MsgExecuteLambdaKind, cb) +} + const MsgSetResourceKind = "gno.land/p/teritori/daokit.SetResource" func NewSetResourceHandler(d *Core) MessageHandler { @@ -102,45 +118,3 @@ func NewRemoveResourceHandler(d *Core) MessageHandler { func NewRemoveResourceMsg(payload *Resource) ExecutableMessage { return NewMessage(MsgRemoveResourceKind, payload) } - -const MsgCustomMessageKind = "gno.land/p/teritori/daokit.CustomMessage" - -type CustomCallbackHandler struct { - resourceType string -} - -func NewCustomCallbackHandler() MessageHandler { - return &CustomCallbackHandler{ - resourceType: MsgCustomMessageKind, - } -} - -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} -}