mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-14 20:15:18 +00:00
ab3cf7c994
* feat!(precompile): Add registry and genesis tests Based on evgeniy-scherbina's work, this adds a new precompile module which defines a contract moudule with an example noop contract that will be will be used for implementing test functions. In addition, it defines a registry module that instantiates stateful precompile contracts and associates them with an address in a global registry defined in kava-labs/go-ethereum. See precompile/README.md for more information. The kava-labs/go-ethereum and kava-labs/etheremint replace statements are updated to support these changes as well as an update to kvtool which includes genesis state for the registry.NoopContractAddress and initializes the contract's EthAccount with a non-zero sequence and codehash set to keccak256(0x01), and sets the contract code to 0x01. See tests/e2e/e2e_precompile_genesis_test.go for an overview of the expected genesis state for an enabled precompile. Co-authored-by: evgeniy-scherbina <evgeniy.shcherbina.es@gmail.com> * chore: Precompile readme improvements This fixes a typo (import -> important) and uses package terminology instead of unclear module terminology. This aligns best with golang terminology were modules and packages are distinctly different and modules are defined using go.mod. * chore: Improve noop contract godoc Add a more meaningful godoc where the noop contract is constructed. * chore(e2e): Improve comments around query checks Improve the clarity of comments around where the error is checked for accounts and why it is not checked directly. In addition, improve comment on why both grpc and rpc code is fetched and where they are used. --------- Co-authored-by: evgeniy-scherbina <evgeniy.shcherbina.es@gmail.com>
34 lines
1.3 KiB
Go
34 lines
1.3 KiB
Go
package registry_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/precompile/modules"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestRegisteredPrecompiles asserts precompiles are registered
|
|
//
|
|
// In addition, this serves as an integration test to
|
|
// 1. Ensure modules.RegisteredModules() is returning addresses in the correct ascending order
|
|
// 2. Ensure that that the address defined in the module is correct. Since we use common.HexToAddress and
|
|
// then back to 0x encoded string, we can be certain that the string defined in the module is the
|
|
// expected length, not missing 0's, etc.
|
|
func TestRegisteredPrecompilesAddresses(t *testing.T) {
|
|
// build list of 0x addresses that are registered
|
|
registeredModules := modules.RegisteredModules()
|
|
registeredPrecompiles := make([]string, 0, len(registeredModules))
|
|
for _, rp := range registeredModules {
|
|
registeredPrecompiles = append(registeredPrecompiles, rp.Address.String())
|
|
}
|
|
|
|
expectedPrecompiles := []string{
|
|
// 0x9 address space used for e2e & integration tests
|
|
"0x9000000000000000000000000000000000000001", // noop
|
|
"0x9000000000000000000000000000000000000002", // noop (duplicated for testing)
|
|
}
|
|
|
|
assert.Equal(t, expectedPrecompiles, registeredPrecompiles,
|
|
"expected registered precompile address list to match to match expected")
|
|
}
|