feat: register custom precompiles

This commit is contained in:
MiniFrenchBread 2025-04-03 15:59:17 +08:00
parent d5275fa798
commit 8b9df8dd26
4 changed files with 16 additions and 8 deletions

View file

@ -53,7 +53,10 @@ type PrecompiledContract interface {
// PrecompiledContracts contains the precompiled contracts supported at the given fork. // PrecompiledContracts contains the precompiled contracts supported at the given fork.
type PrecompiledContracts map[common.Address]PrecompiledContract type PrecompiledContracts map[common.Address]PrecompiledContract
var CustomPrecompiledContracts = PrecompiledContracts{} var CustomPrecompiledContracts = PrecompiledContracts{
(&DASignersPrecompile{}).Address(): NewDASignersPrecompile(),
(&WrappedA0giBasePrecompile{}).Address(): NewWrappedA0giBasePrecompile(),
}
// WithCustomPrecompiles merge given precompiles with custom precompiles // WithCustomPrecompiles merge given precompiles with custom precompiles
func WithCustomPrecompiles(base PrecompiledContracts) PrecompiledContracts { func WithCustomPrecompiles(base PrecompiledContracts) PrecompiledContracts {
@ -287,7 +290,11 @@ func RunPrecompiledContract(
inputCopy := make([]byte, len(input)) inputCopy := make([]byte, len(input))
copy(inputCopy, input) copy(inputCopy, input)
contract := NewContract(caller, p.Address(), value, suppliedGas, evm.jumpDests) var jumpDests map[common.Hash]bitvec
if evm != nil {
jumpDests = evm.jumpDests
}
contract := NewContract(caller, p.Address(), value, suppliedGas, jumpDests)
contract.Input = inputCopy contract.Input = inputCopy
gasCost := p.RequiredGas(input) gasCost := p.RequiredGas(input)

View file

@ -274,6 +274,7 @@ func TestPrecompileBlake2FMalformedInput(t *testing.T) {
func TestPrecompiledEcrecover(t *testing.T) { testJson("ecRecover", "01", t) } func TestPrecompiledEcrecover(t *testing.T) { testJson("ecRecover", "01", t) }
func testJson(name, addr string, t *testing.T) { func testJson(name, addr string, t *testing.T) {
fmt.Printf("name: %v, addr: %v\n", name, addr)
tests, err := loadJson(name) tests, err := loadJson(name)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -62,14 +62,14 @@ type DASignersPrecompile struct {
abi abi.ABI abi abi.ABI
} }
func NewDASignersPrecompile() (*DASignersPrecompile, error) { func NewDASignersPrecompile() *DASignersPrecompile {
abi, err := abi.JSON(strings.NewReader(dasigners.DASignersABI)) abi, err := abi.JSON(strings.NewReader(dasigners.DASignersABI))
if err != nil { if err != nil {
return nil, err panic(err)
} }
return &DASignersPrecompile{ return &DASignersPrecompile{
abi: abi, abi: abi,
}, nil }
} }
// Address implements vm.PrecompiledContract. // Address implements vm.PrecompiledContract.

View file

@ -70,14 +70,14 @@ func (w *WrappedA0giBasePrecompile) RequiredGas(input []byte) uint64 {
return WrappedA0GIBaseRequiredGasMax return WrappedA0GIBaseRequiredGasMax
} }
func NewWrappedA0giBasePrecompile() (*WrappedA0giBasePrecompile, error) { func NewWrappedA0giBasePrecompile() *WrappedA0giBasePrecompile {
abi, err := abi.JSON(strings.NewReader(wrappeda0gibase.Wrappeda0gibaseABI)) abi, err := abi.JSON(strings.NewReader(wrappeda0gibase.Wrappeda0gibaseABI))
if err != nil { if err != nil {
return nil, err panic(err)
} }
return &WrappedA0giBasePrecompile{ return &WrappedA0giBasePrecompile{
abi: abi, abi: abi,
}, nil }
} }
// Run implements vm.PrecompiledContract. // Run implements vm.PrecompiledContract.