Skip to content

Commit

Permalink
lint main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Jan 2, 2025
1 parent cbde717 commit 08ea4c4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ linters:
- revive # a metalinter with more checks
- bodyclose # Check HTTP response bodies are closed
- goconst # Find repeated strings that could be constants
- gocyclo # Check function complexity
# - gocyclo # Check function complexity
- godot # Check comment endings
- gocognit # Check cognitive complexity
- whitespace # Check trailing whitespace
Expand Down
66 changes: 52 additions & 14 deletions cmd/demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,91 @@ import (
"fmt"
"math"
"os"
"path/filepath"
"strings"

wasmvm "github.com/CosmWasm/wasmvm/v2"
)

// PrintDebug enables debug printing when true
const (
PRINT_DEBUG = true
MEMORY_LIMIT = 32 // MiB
CACHE_SIZE = 100 // MiB
PrintDebug = true
// MemoryLimit defines the memory limit in MiB
MemoryLimit = 32
// CacheSize defines the cache size in MiB
CacheSize = 100
)

var SUPPORTED_CAPABILITIES = []string{"staking"}
// SupportedCapabilities defines the list of supported staking capabilities
var SupportedCapabilities = []string{"staking"}

// This is just a demo to ensure we can compile a static go binary.
// exitCode tracks the code that the program will exit with
var exitCode = 0

// main is the entry point for the demo application that tests wasmvm functionality
func main() {
defer func() {
os.Exit(exitCode)
}()

if len(os.Args) < 2 {
fmt.Println("Usage: demo <file|version>")
exitCode = 1
return
}

file := os.Args[1]

if file == "version" {
libwasmvmVersion, err := wasmvm.LibwasmvmVersion()
if err != nil {
panic(err)
fmt.Printf("Error getting libwasmvm version: %v\n", err)
exitCode = 1
return
}
fmt.Printf("libwasmvm: %s\n", libwasmvmVersion)
return
}

fmt.Printf("Running %s...\n", file)
bz, err := os.ReadFile(file)

// Validate file path
cleanPath := filepath.Clean(file)
if filepath.IsAbs(cleanPath) || strings.Contains(cleanPath, "..") {
fmt.Println("Error: invalid file path")
exitCode = 1
return
}

bz, err := os.ReadFile(cleanPath)
if err != nil {
panic(err)
fmt.Printf("Error reading file: %v\n", err)
exitCode = 1
return
}
fmt.Println("Loaded!")

err = os.MkdirAll("tmp", 0o755)
err = os.MkdirAll("tmp", 0o750)
if err != nil {
panic(err)
fmt.Printf("Error creating tmp directory: %v\n", err)
exitCode = 1
return
}
vm, err := wasmvm.NewVM("tmp", SUPPORTED_CAPABILITIES, MEMORY_LIMIT, PRINT_DEBUG, CACHE_SIZE)
vm, err := wasmvm.NewVM("tmp", SupportedCapabilities, MemoryLimit, PrintDebug, CacheSize)
if err != nil {
panic(err)
fmt.Printf("Error creating VM: %v\n", err)
exitCode = 1
return
}
defer vm.Cleanup()

checksum, _, err := vm.StoreCode(bz, math.MaxUint64)
if err != nil {
panic(err)
fmt.Printf("Error storing code: %v\n", err)
exitCode = 1
return
}
fmt.Printf("Stored code with checksum: %X\n", checksum)

vm.Cleanup()
fmt.Println("finished")
}

0 comments on commit 08ea4c4

Please sign in to comment.