0g-chain/precompile/registry/registry.go

47 lines
1.5 KiB
Go
Raw Normal View History

feat!(precompile): Add registry and genesis tests (#1999) * 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>
2024-08-09 16:55:31 +00:00
package registry
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/precompile/contract"
"github.com/ethereum/go-ethereum/precompile/modules"
"github.com/kava-labs/kava/precompile/contracts/noop"
)
const (
// NoopContractAddress the primary noop contract address for testing
NoopContractAddress = "0x9000000000000000000000000000000000000001"
// NoopContractAddress2 the secondary noop contract address for testing
NoopContractAddress2 = "0x9000000000000000000000000000000000000002"
)
// init registers stateful precompile contracts with the global precompile registry
// defined in kava-labs/go-ethereum/precompile/modules
func init() {
register(NoopContractAddress, noop.NewContract)
register(NoopContractAddress2, noop.NewContract)
}
// register accepts a 0x address string and a stateful precompile contract constructor, instantiates the
// precompile contract via the constructor, and registers it with the precompile module registry.
//
// This panics if the contract can not be created or the module can not be registered
func register(address string, newContract func() (contract.StatefulPrecompiledContract, error)) {
contract, err := newContract()
if err != nil {
panic(fmt.Errorf("error creating contract for address %s: %w", address, err))
}
module := modules.Module{
Address: common.HexToAddress(address),
Contract: contract,
}
err = modules.RegisterModule(module)
if err != nil {
panic(fmt.Errorf("error registering contract module for address %s: %w", address, err))
}
}