From ba117be1192f53aeff500a0cf12742fac7b61e32 Mon Sep 17 00:00:00 2001 From: Pantani Date: Fri, 9 Feb 2024 22:52:28 -0300 Subject: [PATCH] fix client genesis --- official/wasm/pkg/goanalysis/goanalysis.go | 46 ++++++++++++++++++++++ official/wasm/templates/wasm/wasm.go | 31 +++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/official/wasm/pkg/goanalysis/goanalysis.go b/official/wasm/pkg/goanalysis/goanalysis.go index e3631fe9..8122ac8d 100644 --- a/official/wasm/pkg/goanalysis/goanalysis.go +++ b/official/wasm/pkg/goanalysis/goanalysis.go @@ -2,6 +2,7 @@ package goanalysis import ( "bytes" + "fmt" "go/ast" "go/format" "go/parser" @@ -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 +} diff --git a/official/wasm/templates/wasm/wasm.go b/official/wasm/templates/wasm/wasm.go index 394e20c6..dc58d970 100644 --- a/official/wasm/templates/wasm/wasm.go +++ b/official/wasm/templates/wasm/wasm.go @@ -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 @@ -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)) } }