Skip to content

Commit

Permalink
feat: dao_maker (#1267)
Browse files Browse the repository at this point in the history
* feat: dao_maker

from gnolang/gno#1925

Signed-off-by: Norman Meier <[email protected]>

* tmp

Signed-off-by: Norman Meier <[email protected]>

* chore: add back ujson and fix social feed

Signed-off-by: Norman Meier <[email protected]>

* chore: remove artifact

Signed-off-by: Norman Meier <[email protected]>

* chore: remove artifact

Signed-off-by: Norman Meier <[email protected]>

* chore: ci fix

Signed-off-by: Norman Meier <[email protected]>

* fix: revert bad change

Signed-off-by: Norman Meier <[email protected]>

---------

Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz authored Oct 9, 2024
1 parent 5b0c900 commit 4e7a64d
Show file tree
Hide file tree
Showing 39 changed files with 1,596 additions and 276 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,11 @@ start.gnodev-e2e:

.PHONY: clone-gno
clone-gno:
rm -fr gnobuild
mkdir -p gnobuild
cd gnobuild && git clone https://github.com/gnolang/gno.git && cd gno && git checkout 8f800ece85a765113dfa4924da1c06f56865460c
cp -r ./gno/p ./gnobuild/gno/examples/gno.land/p/teritori
cp -r ./gno/r ./gnobuild/gno/examples/gno.land/r/teritori

.PHONY: build-gno
build-gno:
Expand Down
39 changes: 31 additions & 8 deletions gno/p/dao_core/dao_core.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package core

import (
"std"
"strconv"
"strings"

dao_interfaces "gno.land/p/teritori/dao_interfaces"
"gno.land/p/teritori/markdown_utils"
)

// TODO: add wrapper message handler to handle multiple proposal modules messages
Expand Down Expand Up @@ -49,6 +50,7 @@ func NewDAOCore(
if mod == nil {
panic("proposal module factory returned nil")
}

core.proposalModules[i] = dao_interfaces.ActivableProposalModule{
Enabled: true,
Module: mod,
Expand All @@ -63,6 +65,7 @@ func NewDAOCore(
if handler == nil {
panic("message handler factory returned nil")
}

core.registry.Register(handler)
}

Expand All @@ -76,7 +79,6 @@ func (d *daoCore) UpdateVotingModule(newVotingModule dao_interfaces.IVotingModul
panic(ErrUnauthorized)
}

// FIXME: check da0-da0 implem
d.votingModule = newVotingModule
}

Expand All @@ -95,6 +97,7 @@ func (d *daoCore) UpdateProposalModules(toAdd []dao_interfaces.IProposalModule,
if !module.Enabled {
panic(ErrModuleAlreadyDisabled)
}

module.Enabled = false

d.activeProposalModuleCount--
Expand Down Expand Up @@ -123,16 +126,33 @@ func (d *daoCore) ActiveProposalModuleCount() int {
}

func (d *daoCore) Render(path string) string {
s := "# DAO Core\n"
s += "This is an attempt at porting [DA0-DA0 contracts](https://github.com/DA0-DA0/dao-contracts)\n"
s += markdown_utils.Indent(d.votingModule.Render(path)) + "\n"
for _, propMod := range d.proposalModules {
sb := strings.Builder{}
sb.WriteString("# DAO Core\n")
votingInfo := d.votingModule.Info()
sb.WriteString("## Voting Module: ")
sb.WriteString(votingInfo.String())
sb.WriteRune('\n')
sb.WriteString(d.votingModule.Render(""))
sb.WriteString("## Supported Messages:\n")
sb.WriteString(d.registry.Render())

sb.WriteString("## Proposal Modules:\n")
for i, propMod := range d.proposalModules {
if !propMod.Enabled {
continue
}
s += markdown_utils.Indent(propMod.Module.Render(path)) + "\n"

info := propMod.Module.Info()
sb.WriteString("### #")
sb.WriteString(strconv.Itoa(i))
sb.WriteString(": ")
sb.WriteString(info.String())
sb.WriteRune('\n')
sb.WriteString(propMod.Module.Render(""))
}
return s

sb.WriteRune('\n')
return sb.String()
}

func (d *daoCore) Registry() *dao_interfaces.MessagesRegistry {
Expand All @@ -145,10 +165,12 @@ func GetProposalModule(core dao_interfaces.IDAOCore, moduleIndex int) *dao_inter
if moduleIndex < 0 {
panic("module index must be >= 0")
}

mods := core.ProposalModules()
if moduleIndex >= len(mods) {
panic("invalid module index")
}

return &mods[moduleIndex]
}

Expand All @@ -169,6 +191,7 @@ func (d *daoCore) addProposalModule(proposalMod dao_interfaces.IProposalModule)
if mod.Enabled {
panic(ErrModuleAlreadyAdded)
}

mod.Enabled = true
d.activeProposalModuleCount++
return
Expand Down
7 changes: 7 additions & 0 deletions gno/p/dao_core/dao_core_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestDAOCore(t *testing.T) {
if core == nil {
t.Fatal("core is nil")
}

if core.ActiveProposalModuleCount() != 1 {
t.Fatal("expected 1 active proposal module")
}
Expand All @@ -108,6 +109,7 @@ func TestDAOCore(t *testing.T) {
if votingMod == nil {
t.Fatal("voting module is nil")
}

if votingMod.Info().Kind != "TestVoting" {
t.Fatal("voting module has wrong kind")
}
Expand All @@ -121,9 +123,11 @@ func TestDAOCore(t *testing.T) {
if !propMod.Enabled {
t.Fatal("proposal module is not enabled")
}

if propMod.Module == nil {
t.Fatal("proposal module is nil")
}

if propMod.Module.Info().Kind != "TestProposal" {
t.Fatal("proposal module has wrong kind")
}
Expand All @@ -132,6 +136,7 @@ func TestDAOCore(t *testing.T) {
if registry == nil {
t.Fatal("registry is nil")
}

msg := &dao_interfaces.CopyMessage{Value: "test"}
registry.Execute(msg)
if testValue != "test" {
Expand Down Expand Up @@ -163,6 +168,7 @@ func TestDAOCore(t *testing.T) {
if !propMod.Enabled {
t.Errorf("new proposal module is not enabled")
}

if propMod.Module != newProposalModule {
t.Errorf("new proposal module is not the same as the one added")
}
Expand All @@ -171,6 +177,7 @@ func TestDAOCore(t *testing.T) {
updateVotingModMsg := &UpdateVotingModuleExecutableMessage{
Module: newVotingModule,
}

registry.Execute(updateVotingModMsg)

votingMod = core.VotingModule()
Expand Down
3 changes: 1 addition & 2 deletions gno/p/dao_core/gno.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module gno.land/p/teritori/dao_core

require (
gno.land/p/demo/json v0.0.0-latest
gno.land/p/teritori/dao_interfaces v0.0.0-latest
gno.land/p/teritori/markdown_utils v0.0.0-latest
gno.land/p/teritori/ujson v0.0.0-latest
)
26 changes: 17 additions & 9 deletions gno/p/dao_core/messages.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package core

import (
"gno.land/p/demo/json"
dao_interfaces "gno.land/p/teritori/dao_interfaces"
"gno.land/p/teritori/ujson"
)

// UpdateProposalModules
Expand All @@ -12,6 +12,8 @@ type UpdateProposalModulesExecutableMessage struct {
ToDisable []int
}

var _ dao_interfaces.ExecutableMessage = &UpdateProposalModulesExecutableMessage{}

func (msg UpdateProposalModulesExecutableMessage) Type() string {
return "gno.land/p/teritori/dao_core.UpdateProposalModules"
}
Expand All @@ -20,18 +22,20 @@ func (msg *UpdateProposalModulesExecutableMessage) String() string {
panic(ErrNotImplemented)
}

func (msg *UpdateProposalModulesExecutableMessage) ToJSON() string {
func (msg *UpdateProposalModulesExecutableMessage) ToJSON() *json.Node {
panic(ErrNotImplemented)
}

func (msg *UpdateProposalModulesExecutableMessage) FromJSON(ast *ujson.JSONASTNode) {
func (msg *UpdateProposalModulesExecutableMessage) FromJSON(ast *json.Node) {
panic(ErrNotImplemented)
}

type UpdateProposalModulesMessageHandler struct {
dao dao_interfaces.IDAOCore
}

var _ dao_interfaces.MessageHandler = &UpdateProposalModulesMessageHandler{}

func NewUpdateProposalModulesMessageHandler(dao dao_interfaces.IDAOCore) *UpdateProposalModulesMessageHandler {
return &UpdateProposalModulesMessageHandler{dao: dao}
}
Expand All @@ -45,8 +49,8 @@ func (handler *UpdateProposalModulesMessageHandler) Execute(message dao_interfac
handler.dao.UpdateProposalModules(msg.ToAdd, msg.ToDisable)
}

func (handler *UpdateProposalModulesMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) dao_interfaces.ExecutableMessage {
panic(ErrNotSupported)
func (handler *UpdateProposalModulesMessageHandler) Instantiate() dao_interfaces.ExecutableMessage {
return &UpdateProposalModulesExecutableMessage{}
}

// UpdateVotingModule
Expand All @@ -55,6 +59,8 @@ type UpdateVotingModuleExecutableMessage struct {
Module dao_interfaces.IVotingModule
}

var _ dao_interfaces.ExecutableMessage = &UpdateVotingModuleExecutableMessage{}

func (msg UpdateVotingModuleExecutableMessage) Type() string {
return "gno.land/p/teritori/dao_core.UpdateVotingModule"
}
Expand All @@ -63,18 +69,20 @@ func (msg *UpdateVotingModuleExecutableMessage) String() string {
panic(ErrNotImplemented)
}

func (msg *UpdateVotingModuleExecutableMessage) ToJSON() string {
func (msg *UpdateVotingModuleExecutableMessage) ToJSON() *json.Node {
panic(ErrNotImplemented)
}

func (msg *UpdateVotingModuleExecutableMessage) FromJSON(ast *ujson.JSONASTNode) {
func (msg *UpdateVotingModuleExecutableMessage) FromJSON(ast *json.Node) {
panic(ErrNotImplemented)
}

type UpdateVotingModuleMessageHandler struct {
dao dao_interfaces.IDAOCore
}

var _ dao_interfaces.MessageHandler = &UpdateVotingModuleMessageHandler{}

func NewUpdateVotingModuleMessageHandler(dao dao_interfaces.IDAOCore) *UpdateVotingModuleMessageHandler {
return &UpdateVotingModuleMessageHandler{dao: dao}
}
Expand All @@ -88,6 +96,6 @@ func (handler *UpdateVotingModuleMessageHandler) Execute(message dao_interfaces.
handler.dao.UpdateVotingModule(msg.Module)
}

func (handler *UpdateVotingModuleMessageHandler) MessageFromJSON(ast *ujson.JSONASTNode) dao_interfaces.ExecutableMessage {
panic(ErrNotSupported)
func (handler *UpdateVotingModuleMessageHandler) Instantiate() dao_interfaces.ExecutableMessage {
return &UpdateVotingModuleExecutableMessage{}
}
2 changes: 2 additions & 0 deletions gno/p/dao_interfaces/core.gno
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dao_interfaces

// Inspired by DA0-DA0: https://github.com/DA0-DA0/dao-contracts

type ActivableProposalModule struct {
Enabled bool
Module IProposalModule
Expand Down
2 changes: 1 addition & 1 deletion gno/p/dao_interfaces/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module gno.land/p/teritori/dao_interfaces

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/teritori/ujson v0.0.0-latest
gno.land/p/demo/json v0.0.0-latest
)
8 changes: 4 additions & 4 deletions gno/p/dao_interfaces/messages.gno
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package dao_interfaces

import (
"gno.land/p/teritori/ujson"
"gno.land/p/demo/json"
)

type ExecutableMessage interface {
ujson.JSONAble
ujson.FromJSONAble
ToJSON() *json.Node
FromJSON(ast *json.Node)

String() string
Type() string
}

type MessageHandler interface {
Execute(message ExecutableMessage)
MessageFromJSON(ast *ujson.JSONASTNode) ExecutableMessage
Instantiate() ExecutableMessage
Type() string
}

Expand Down
Loading

0 comments on commit 4e7a64d

Please sign in to comment.