Skip to content

Commit

Permalink
fix client genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Feb 10, 2024
1 parent 7c67fcb commit ba117be
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
46 changes: 46 additions & 0 deletions official/wasm/pkg/goanalysis/goanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goanalysis

import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
Expand Down Expand Up @@ -196,3 +197,48 @@ func ReplaceReturn(fileContent, functionName string, returnVars ...string) (stri

return buf.String(), nil
}

// ReplaceCode replaces a function implementation in Go source code content.
func ReplaceCode(fileContent, oldFunctionName, newFunction string) (modifiedContent string, err error) {
fileSet := token.NewFileSet()

// Parse the Go source code content.
f, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
if err != nil {
return "", err
}

found := false
ast.Inspect(f, func(n ast.Node) bool {
if funcDecl, ok := n.(*ast.FuncDecl); ok {
// Check if the function has the name you want to replace.
if funcDecl.Name.Name == oldFunctionName {
// Parse the content of the new function into an ast.File.
newFuncContent := fmt.Sprintf("package p; func _() { %s }", strings.TrimSpace(newFunction))
newFile, err := parser.ParseFile(fileSet, "", newFuncContent, parser.ParseComments)
if err != nil {
return false
}
// Take the body of the new function from the parsed file.
newFunctionBody := newFile.Decls[0].(*ast.FuncDecl).Body
// Replace the function body with the body of the new function.
funcDecl.Body = newFunctionBody
found = true
return false
}
}
return true
})

if !found {
return "", fmt.Errorf("function %s not found in file content", oldFunctionName)
}

// Write the modified AST to a buffer.
var buf bytes.Buffer
if err := format.Node(&buf, fileSet, f); err != nil {
return "", err
}

return buf.String(), nil
}
31 changes: 31 additions & 0 deletions official/wasm/templates/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ import (
"wasm/pkg/goanalysis"
)

const funcRegisterIBCWasm = `
modules := map[string]appmodule.AppModule{
ibcexported.ModuleName: ibc.AppModule{},
ibctransfertypes.ModuleName: ibctransfer.AppModule{},
ibcfeetypes.ModuleName: ibcfee.AppModule{},
icatypes.ModuleName: icamodule.AppModule{},
capabilitytypes.ModuleName: capability.AppModule{},
ibctm.ModuleName: ibctm.AppModule{},
solomachine.ModuleName: solomachine.AppModule{},
wasmtypes.ModuleName: wasm.AppModule{},
}
for _, module := range modules {
if mod, ok := module.(interface {
RegisterInterfaces(registry cdctypes.InterfaceRegistry)
}); ok {
mod.RegisterInterfaces(registry)
}
}
return modules`

//go:embed files/* files/**/*
var fsAppWasm embed.FS

Expand Down Expand Up @@ -161,6 +183,15 @@ ibcRouter.AddRoute(wasmtypes.ModuleName, wasmStack)
)
content = replacer.Replace(content, module.PlaceholderIBCNewModule, replacementIBCModule)

content, err = goanalysis.ReplaceCode(
content,
"RegisterIBC",
funcRegisterIBCWasm,
)
if err != nil {
return err
}

return r.File(genny.NewFileS(ibcPath, content))
}
}
Expand Down

0 comments on commit ba117be

Please sign in to comment.