This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(precompile): Default Precompile Plugin (#243)
- Loading branch information
Showing
11 changed files
with
190 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// Use of this software is govered by the Business Source License included | ||
// in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
// | ||
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
// VERSIONS OF THE LICENSED WORK. | ||
// | ||
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
// | ||
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
// TITLE. | ||
|
||
package precompile | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/berachain/stargazer/eth/common" | ||
"github.com/berachain/stargazer/eth/core/vm" | ||
"github.com/berachain/stargazer/lib/registry" | ||
libtypes "github.com/berachain/stargazer/lib/types" | ||
) | ||
|
||
// `defaultPlugin` is the default precompile plugin, should any chain running Stargazer EVM not | ||
// implement their own precompile plugin. Notably, this plugin can only run the default stateless | ||
// precompiles provided by Go-Ethereum. | ||
type defaultPlugin struct { | ||
libtypes.Registry[common.Address, vm.PrecompileContainer] | ||
} | ||
|
||
// `NewDefaultPlugin` returns a new instance of the default precompile plugin. | ||
func NewDefaultPlugin() Plugin { | ||
return &defaultPlugin{ | ||
Registry: registry.NewMap[common.Address, vm.PrecompileContainer](), | ||
} | ||
} | ||
|
||
// `Reset` implements `core.PrecompilePlugin`. | ||
func (dp *defaultPlugin) Reset(ctx context.Context) { | ||
// no-op | ||
} | ||
|
||
// `Run` supports executing stateless precompiles with the background context. | ||
// | ||
// `Run` implements `core.PrecompilePlugin`. | ||
func (dp *defaultPlugin) Run( | ||
sdb vm.GethStateDB, pc vm.PrecompileContainer, input []byte, | ||
caller common.Address, value *big.Int, suppliedGas uint64, readonly bool, | ||
) ([]byte, uint64, error) { | ||
gasCost := pc.RequiredGas(input) | ||
if gasCost > suppliedGas { | ||
return nil, 0, vm.ErrOutOfGas | ||
} | ||
|
||
suppliedGas -= gasCost | ||
output, err := pc.Run(context.Background(), input, caller, value, readonly) | ||
|
||
return output, suppliedGas, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// Use of this software is govered by the Business Source License included | ||
// in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
// | ||
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
// VERSIONS OF THE LICENSED WORK. | ||
// | ||
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
// | ||
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
// TITLE. | ||
|
||
package precompile_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/berachain/stargazer/eth/common" | ||
"github.com/berachain/stargazer/eth/core/precompile" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
//nolint:lll // test data. | ||
const precompInput = `a8b53bdf3306a35a7103ab5504a0c9b492295564b6202b1942a84ef300107281000000000000000000000000000000000000000000000000000000000000001b307835653165303366353363653138623737326363623030393366663731663366353366356337356237346463623331613835616138623838393262346538621122334455667788991011121314151617181920212223242526272829303132` | ||
|
||
var _ = Describe("Default Plugin", func() { | ||
var dp precompile.Plugin | ||
|
||
BeforeEach(func() { | ||
dp = precompile.NewDefaultPlugin() | ||
}) | ||
|
||
It("should reset", func() { | ||
Expect(func() { dp.Reset(context.Background()) }).ToNot(Panic()) | ||
}) | ||
|
||
When("running a stateless contract", func() { | ||
It("should run out of gas", func() { | ||
ret, remainingGas, err := dp.Run(nil, &mockStateless{&mockBase{}}, nil, common.Address{}, nil, 5, false) | ||
Expect(ret).To(BeNil()) | ||
Expect(remainingGas).To(Equal(uint64(0))) | ||
Expect(err.Error()).To(Equal("out of gas")) | ||
}) | ||
|
||
It("should run a geth contract", func() { | ||
pc := vm.PrecompiledContractsHomestead[common.BytesToAddress([]byte{1})] | ||
_, remainingGas, err := dp.Run(nil, pc, []byte(precompInput), common.Address{}, nil, 3000, true) | ||
Expect(remainingGas).To(Equal(uint64(0))) | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters