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

Add new hard fork Libplanet containing functions for libplanet rollup #7

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
cfg.Eth.OverrideOptimismFjord = &v
}

if ctx.IsSet(utils.OverrideOptimismLibplanet.Name) {
v := ctx.Uint64(utils.OverrideOptimismLibplanet.Name)
cfg.Eth.OverrideOptimismLibplanet = &v
}

if ctx.IsSet(utils.OverrideOptimismInterop.Name) {
v := ctx.Uint64(utils.OverrideOptimismInterop.Name)
cfg.Eth.OverrideOptimismInterop = &v
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var (
utils.OverrideOptimismCanyon,
utils.OverrideOptimismEcotone,
utils.OverrideOptimismFjord,
utils.OverrideOptimismLibplanet,
utils.OverrideOptimismInterop,
utils.EnablePersonal,
utils.TxPoolLocalsFlag,
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ var (
Usage: "Manually specify the Optimism Fjord fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideOptimismLibplanet = &cli.Uint64Flag{
Name: "override.libplanet",
Usage: "Manually specify the Optimism Libplanet fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideOptimismInterop = &cli.Uint64Flag{
Name: "override.interop",
Usage: "Manually specify the Optimsim Interop feature-set fork timestamp, overriding the bundled setting",
Expand Down
14 changes: 9 additions & 5 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ type ChainOverrides struct {
OverrideCancun *uint64
OverrideVerkle *uint64
// optimism
OverrideOptimismCanyon *uint64
OverrideOptimismEcotone *uint64
OverrideOptimismFjord *uint64
ApplySuperchainUpgrades bool
OverrideOptimismInterop *uint64
OverrideOptimismCanyon *uint64
OverrideOptimismEcotone *uint64
OverrideOptimismFjord *uint64
OverrideOptimismLibplanet *uint64
ApplySuperchainUpgrades bool
OverrideOptimismInterop *uint64
}

// SetupGenesisBlock writes or updates the genesis block in db.
Expand Down Expand Up @@ -293,6 +294,9 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g
if overrides != nil && overrides.OverrideOptimismFjord != nil {
config.FjordTime = overrides.OverrideOptimismFjord
}
if overrides != nil && overrides.OverrideOptimismLibplanet != nil {
config.LibplanetTime = overrides.OverrideOptimismLibplanet
}
if overrides != nil && overrides.OverrideOptimismInterop != nil {
config.InteropTime = overrides.OverrideOptimismInterop
}
Expand Down
71 changes: 68 additions & 3 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package vm

import (
"crypto/sha1"
"crypto/sha256"
"encoding/binary"
"errors"
Expand All @@ -33,6 +34,8 @@ import (
"github.com/ethereum/go-ethereum/crypto/secp256r1"
"github.com/ethereum/go-ethereum/libplanet"
"github.com/ethereum/go-ethereum/params"
"github.com/sircoon4/bencodex-go"
"github.com/sircoon4/bencodex-go/bencodextype"
"golang.org/x/crypto/ripemd160"
)

Expand Down Expand Up @@ -123,7 +126,24 @@ var PrecompiledContractsFjord = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
}

// PrecompiledContractsLibplanet contains the default set of pre-compiled Ethereum
// contracts used in the Libplanet release.
var PrecompiledContractsLibplanet = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
common.BytesToAddress([]byte{0x02, 0x00}): &libplanetVerifyProof{},
common.BytesToAddress([]byte{0x02, 0x01}): &libplanetWithdrawalTransactionHashing{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
Expand All @@ -141,6 +161,7 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{
}

var (
PrecompiledAddressesLibplanet []common.Address
PrecompiledAddressesFjord []common.Address
PrecompiledAddressesCancun []common.Address
PrecompiledAddressesBerlin []common.Address
Expand Down Expand Up @@ -168,11 +189,16 @@ func init() {
for k := range PrecompiledContractsFjord {
PrecompiledAddressesFjord = append(PrecompiledAddressesFjord, k)
}
for k := range PrecompiledContractsLibplanet {
PrecompiledAddressesLibplanet = append(PrecompiledAddressesLibplanet, k)
}
}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules params.Rules) []common.Address {
switch {
case rules.IsOptimismLibplanet:
return PrecompiledAddressesLibplanet
case rules.IsOptimismFjord:
return PrecompiledAddressesFjord
case rules.IsCancun:
Expand Down Expand Up @@ -1205,8 +1231,8 @@ func (c *libplanetVerifyProof) RequiredGas(input []byte) uint64 {
func (c *libplanetVerifyProof) Run(input []byte) ([]byte, error) {
proofMap := map[string]any{
"stateRootHash": nil, // sha256(bencoded) []byte
"proof": nil, // bencoded list [][]byte
"key": nil, // keyBytes []byte
"proof": nil, // bencoded(list) []byte
"key": nil, // keyBytes | address []byte
"value": nil, // bencoded []byte
}
proofMap, err := libplanet.ParseMerkleTrieProofInput(input)
Expand All @@ -1215,11 +1241,50 @@ func (c *libplanetVerifyProof) Run(input []byte) ([]byte, error) {
}

stateRootHash := proofMap["stateRootHash"].([]byte)
proof := proofMap["proof"].([][]byte)
proof := proofMap["proof"].([]byte)
key := proofMap["key"].([]byte)
value := proofMap["value"].([]byte)

valid, _ := libplanet.ValidateProof(stateRootHash, proof, key, value)

return common.CopyBytes(libplanet.BoolAbi(valid)), nil
}

type libplanetWithdrawalTransactionHashing struct{}

func (c *libplanetWithdrawalTransactionHashing) RequiredGas(input []byte) uint64 {
return params.LibplanetWithdrawalTransactionHashingGas
}

func (c *libplanetWithdrawalTransactionHashing) Run(input []byte) ([]byte, error) {
withdrawalTransaction := map[string]any{
"nonce": nil, // *big.Int
"from": nil, // common.Address
"to": nil, // common.Address
"amount": nil, // *big.Int
}
withdrawalTransaction, err := libplanet.ParseWithdrawalTransactionInput(input)
if err != nil {
return nil, err
}

nonce := withdrawalTransaction["nonce"].(*big.Int)
from := withdrawalTransaction["from"].(common.Address).Bytes()
to := withdrawalTransaction["to"].(common.Address).Bytes()
amount := withdrawalTransaction["amount"].(*big.Int)

dict := bencodextype.NewDictionary()
dict.Set("nonce", nonce)
dict.Set("from", from)
dict.Set("to", to)
dict.Set("amount", amount)

encoded, err := bencodex.Encode(dict)
if err != nil {
return nil, err
}

sum := sha1.Sum(encoded)

return common.CopyBytes(libplanet.AddressAbi(sum)), nil
}
4 changes: 4 additions & 0 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},

common.BytesToAddress([]byte{0x02, 0x00}): &libplanetVerifyProof{},
common.BytesToAddress([]byte{0x02, 0x01}): &libplanetWithdrawalTransactionHashing{},

common.BytesToAddress([]byte{0x0f, 0x0a}): &bls12381G1Add{},
common.BytesToAddress([]byte{0x0f, 0x0b}): &bls12381G1Mul{},
Expand Down Expand Up @@ -412,3 +413,6 @@ func BenchmarkPrecompiledP256Verify(bench *testing.B) {

func TestPrecompiledP256Verify(t *testing.T) { testJson("p256Verify", "100", t) }
func TestPrecompiledLibplanetVerifyProof(t *testing.T) { testJson("libplanetVerifyProof", "200", t) }
func TestPrecompiledLibplanetWithdrawalTransactionHashing(t *testing.T) {
testJson("libplanetWithdrawalTransactionHashing", "201", t)
}
2 changes: 2 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type (
func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
var precompiles map[common.Address]PrecompiledContract
switch {
case evm.chainRules.IsOptimismLibplanet:
precompiles = PrecompiledContractsLibplanet
case evm.chainRules.IsOptimismFjord:
precompiles = PrecompiledContractsFjord
case evm.chainRules.IsCancun:
Expand Down
Loading