Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use wazero #582

Draft
wants to merge 129 commits into
base: main
Choose a base branch
from
Draft

use wazero #582

wants to merge 129 commits into from

Conversation

faddat
Copy link
Contributor

@faddat faddat commented Dec 19, 2024

status

  • Testfloats now runs. Working on getting tests that do instantiation to work correctly.

problems

  • gas system immature
  • somewhere, pages is being read as bytes or vice versa
  • Testfloats is getting different results depending on where it is run (m1 vs x86)

what?

To ensure that contracts can run reliably without stopping chains, we shall endeavor to swap out wasmer for wazero. Wasmer is a great wasm vm written in rust, a great programming language. Wazero is a great web assembly virtual machine written in go, a great programming language (with horrible c interop). Both languages get spicy around interop, so it is best to code an entire blockchain application in just one programming language. Additionally, Go memory management breaks when using cgo to import C.

why?

Because cgo doesn't work all that well, and messes up some basic features of Go. Also, similar ABI issues and c interop issues on the rust side, so it is probably not a good idea to use cgo / unsafe in go / unsafe in rust on >40 production networks, even if we are just accounting for the extreme increase in random explosions cause by cgo. But in fact we will be able to reduce explosions in many other ways too.

Probably right now this is a proof of concept.

How?

When the lint PR's are merged to main, they can be merged here also.

  • clarify structure

  • Create an interface for runtimes so that the runtime interface is clear

  • implement wazero in the runtime folder

  • implement the

whats the trick?

The trick is that cosmwasm's vm package is imported by libwasmvm and provides a lot of functions. We will need to recreate that for wazero and use the host functions feature.

there's a pretty clear migration path to wazero insrtead of wasmer in wasmvm.

When I began to work on cosmwasgo, I made a design mistake and made things less modular.

Today, I began to go through all of the code file by file, and I was able to get to a pretty good place. I still need to reconcile your last commit though.

Although I have hardcoded the runtime, I think you can see how using the runtime folder and the runtime interface allows for a pretty singificant simplification of wasmvm.

There are still remaining questions around gas metering, and a few other things.

cheers!

-Jacob

Note: even if you do not reveiw or merge this, I would appreciate not closing it. Among other things I am using the PR to compare.

Random notes that I will add to as I go:

  • the code inside libwasmvm folder defines interfaces for contracts. It needs to be reimplemented in go using wazero.
  • I'm going one by one through libwasmvm/* and setting that up in Go
    • TestAnalyzeCode done
    • TestStoreCode done
    • Memory management (memory.rs) is now in memory.go
    • Various db methods added to the host
  • This way, we will no longer need a huge binary blob to run cosmwasm contracts in chains using go, and performance and reliability will be improved.
  • It's possible that this may enable us to just use the SDK's gas meter
  • This pull request updates none of the tests. The goal is to ensure that this code passes every single test used by the wasmer version, without exception.

@faddat faddat changed the title merged chaos use wazero, and intraoduce an abstraction for the virtual macine Dec 19, 2024
@faddat faddat marked this pull request as draft December 20, 2024 07:06
@faddat faddat changed the title use wazero, and intraoduce an abstraction for the virtual macine use wazero, and introduce an abstraction for the virtual macine Dec 20, 2024
- Updated `MockEnv` to use `types.Uint64` for time representation.
- Introduced helper functions for memory operations: `readMemory` and `writeMemory`.
- Replaced direct memory read/write calls in `hostGet` and other functions with the new helpers.
- Removed the `MemoryAllocator` struct and related functions, simplifying memory management.
- Cleaned up unused code and deleted obsolete files in the `libwasmvm` directory.
- Adjusted function signatures and error handling for better clarity and consistency.

This commit enhances the overall structure and readability of the codebase while ensuring proper memory handling.
run tests
@faddat
Copy link
Contributor Author

faddat commented Jan 12, 2025

This now contains:

I expect that this will reduce the number of test failures by 3-4

faddat added 24 commits January 12, 2025 17:57
… in WazeroRuntime initialization to use the interperter
- Added debug print statements to log JSON data being sent to contracts and the code ID of the stored reflect contract.
- Updated checksum handling to convert the first 8 bytes to a uint64 for the reflect contract's code ID.
- Removed commented-out code related to message processing in the IBC packet dispatch test for clarity.
- Added pretty-printing of JSON structures for better visibility during contract interactions.
- Enhanced debug logs to include checksum, code ID, and gas used for both reflect and IBC contracts.
- Updated checksum handling to directly use the checksum returned from the StoreCode function.
- Removed unnecessary binary conversion for code ID, simplifying the code.
- Updated IBC test cases to improve checksum handling and debugging output.
- Simplified checksum retrieval from StoreCode and added code ID mapping for reflect and IBC contracts.
- Enhanced memory and cache limits in the testing configuration for better performance.
- Removed unused hostCloseIterator function to clean up the codebase.
- Adjusted Wazero runtime configuration to increase memory limit for improved resource management.
- Added time tracking and detailed logging for host function registration and instantiation processes.
- Refactored the AnalyzeCode function to streamline the identification of IBC entry points and capabilities.
- Introduced comprehensive debug output for memory layout and error handling during contract instantiation.
- Improved clarity in printed messages, including a summary of registered host functions and analysis results.
- Enhanced error reporting with memory dumps around failure points for better troubleshooting.
…acking

- Enhanced MockEnvBin function to preserve JSON field order using a custom encoder.
- Updated Secp256r1Verify function to improve signature verification logic and error handling.
- Introduced new gas consumption methods for memory, database read/write, and external queries in GasState.
- Refactored memory management to ensure proper alignment and bounds checking during read/write operations.
- Removed deprecated initialize.go file and consolidated memory-related functions for better organization.
- Improved error reporting and debugging output across various runtime functions.
- Added null checks for Region and memory data to prevent runtime errors.
- Improved validation logic in Region.Validate to include checks for zero capacity and alignment.
- Updated readMemory and writeMemory functions to ensure proper alignment and bounds checking.
- Refactored input data handling in WazeroRuntime to validate and write environment, info, and message data with proper alignment.
- Enhanced error reporting for memory operations and JSON validation to improve debugging capabilities.
- Added new tracing package with configurable tracing capabilities
- Improved contract instantiation and runtime logging with more detailed output
- Enhanced memory initialization and validation during contract execution
- Updated host function registration with more informative debug and tracing
- Added input data write validation with verification checks
- Expanded runtime logging to include memory state, call traces, and detailed execution information
… validation

- Added detailed logging for memory region writing process
- Implemented 8-byte alignment checks for region pointers
- Enhanced debug output in writeRegions and readResultRegionInternal methods
- Added helper functions for data inspection and ASCII readability checks
- Improved error handling and memory data validation
- Updated BLS12-381 hash-to-curve functions to accept custom domain separation tags
- Added constants and size limits for BLS12-381 cryptographic operations
- Implemented gas metering for BLS12-381 aggregate and hash-to-curve functions
- Enhanced host function registration for BLS12-381 cryptographic primitives
- Updated runtime environment to include more flexible gas configuration
- Improved error handling and logging for cryptographic host functions
- Simplified `allocateInContract` with reduced debug logging
- Streamlined `hostDbRead` function with more concise memory handling
- Extracted memory management logic into a new `instantiate.go` file
- Removed verbose debug print statements in runtime functions
- Improved error handling and reduced code complexity in memory allocation routines
- Simplified memory and export validation in `analyzeForValidation`
- Removed commented-out code and unnecessary complexity
- Updated `allocate` and `deallocate` host functions with improved error handling
- Streamlined interface version and export checks
- Removed verbose comments and simplified function implementations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant