api

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package api provides the interfaces for the virtual machine that executes smart contracts. This package defines the API between the blockchain and the VM, but is not directly used by smart contracts.

Index

Constants

This section is empty.

Variables

View Source
var BuildParams = []string{"build", "-o", "contract.wasm", "-target", "wasi", "-opt", "z", "-no-debug", "./"}
View Source
var Builder = "tinygo"

Functions

This section is empty.

Types

type ContractConfig

type ContractConfig struct {
	// MaxGas is the maximum amount of gas that can be used by a contract
	MaxGas uint64

	// MaxCallDepth is the maximum depth of contract calls
	MaxCallDepth uint8

	// MaxCodeSize is the maximum size of contract code in bytes
	MaxCodeSize uint64

	// AllowedImports contains the packages that can be imported by contracts
	AllowedImports []string
}

ContractConfig defines configuration for contract validation and execution

type IContractAddressGenerator

type IContractAddressGenerator func(code []byte, sender types.Address) types.Address
var DefaultContractAddressGenerator IContractAddressGenerator = func(code []byte, sender types.Address) types.Address {
	var addr types.Address
	hash := sha256.Sum256(code)
	copy(addr[:], hash[:])
	return addr
}

type IContractConfigGenerator

type IContractConfigGenerator func() ContractConfig
var DefaultContractConfig IContractConfigGenerator = func() ContractConfig {
	return ContractConfig{
		MaxGas:       1000000,
		MaxCallDepth: 8,
		MaxCodeSize:  1024 * 1024,
		AllowedImports: []string{
			"github.com/govm-net/vm/core",
		},
	}
}

type IGoModGenerator

type IGoModGenerator func(moduleName string, imports map[string]string, replaces map[string]string) string
var DefaultGoModGenerator IGoModGenerator = func(moduleName string, imports, replaces map[string]string) string {
	return fmt.Sprintf(`
	module %s

go 1.23.0

require (
	github.com/govm-net/vm v0.1.5
)

// replace github.com/govm-net/vm => ./
`, moduleName)
}

type IKeywordValidator

type IKeywordValidator func(node ast.Node) error
var DefaultKeywordValidator IKeywordValidator = func(node ast.Node) error {
	if node == nil {
		return nil
	}
	switch n := node.(type) {
	case *ast.EmptyStmt:
		if n.Implicit {
			return fmt.Errorf("restricted keyword ';' is not allowed")
		}
	case *ast.IfStmt:

		if n.Init != nil {
			return fmt.Errorf("semicolon in if statement initialization is not allowed")
		}
	case *ast.ForStmt:

		if n.Init != nil {
			return fmt.Errorf("semicolon in for statement initialization is not allowed")
		}
	case *ast.GoStmt:
		return fmt.Errorf("restricted keyword 'go' is not allowed")
	case *ast.SelectStmt:
		return fmt.Errorf("restricted keyword 'select' is not allowed")
	case *ast.RangeStmt:
		return fmt.Errorf("restricted keyword 'range' is not allowed")
	case *ast.ChanType:
		return fmt.Errorf("restricted keyword 'chan' is not allowed")
	case *ast.CallExpr:
		if ident, ok := node.(*ast.CallExpr).Fun.(*ast.Ident); ok && ident.Name == "recover" {
			return fmt.Errorf("restricted keyword 'recover' is not allowed")
		}
	}
	return nil
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL