Skip to content

Commit

Permalink
Fix config hash calculation (encode vs encodePacked)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdehoog committed Oct 3, 2024
1 parent 40ac164 commit eb0eaf1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 52 deletions.
59 changes: 17 additions & 42 deletions enclave/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package enclave

import (
"encoding/binary"
"math/big"

"github.com/ethereum-optimism/optimism/op-chain-ops/deployer/state"
"github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -18,20 +18,10 @@ const (
version0 uint64 = 0
)

var uint256Type abi.Type
var uint64Type abi.Type
var addressType abi.Type
var bytes32Type abi.Type

var chainConfigTemplate params.ChainConfig
var rollupConfigTemplate rollup.Config

func init() {
uint256Type, _ = abi.NewType("uint256", "", nil)
uint64Type, _ = abi.NewType("uint64", "", nil)
addressType, _ = abi.NewType("address", "", nil)
bytes32Type, _ = abi.NewType("bytes32", "", nil)

deployConfig := state.DefaultDeployConfig()
deployConfig.L2ChainID = 1

Expand Down Expand Up @@ -115,37 +105,22 @@ func (p *PerChainConfig) ForceDefaults() {
p.Genesis.SystemConfig.Overhead = eth.Bytes32{}
}

func (p *PerChainConfig) Hash() (common.Hash, error) {
data, err := p.MarshalBinary()
if err != nil {
return common.Hash{}, err
}
return crypto.Keccak256Hash(data), nil
func (p *PerChainConfig) Hash() common.Hash {
return crypto.Keccak256Hash(p.MarshalBinary())
}

func (p *PerChainConfig) MarshalBinary() (data []byte, err error) {
args := abi.Arguments{
{Name: "version", Type: uint64Type},
{Name: "chainID", Type: uint256Type},
{Name: "genesisL1Hash", Type: bytes32Type},
{Name: "genesisL2Hash", Type: bytes32Type},
{Name: "genesisL2Time", Type: uint64Type},
{Name: "genesisBatcherAddress", Type: addressType},
{Name: "genesisScalar", Type: bytes32Type},
{Name: "genesisGasLimit", Type: uint64Type},
{Name: "depositContractAddress", Type: addressType},
{Name: "l1SystemConfigAddress", Type: addressType},
}
return args.Pack(
version0,
p.ChainID,
p.Genesis.L1.Hash,
p.Genesis.L2.Hash,
p.Genesis.L2Time,
p.Genesis.SystemConfig.BatcherAddr,
p.Genesis.SystemConfig.Scalar,
p.Genesis.SystemConfig.GasLimit,
p.DepositContractAddress,
p.L1SystemConfigAddress,
)
func (p *PerChainConfig) MarshalBinary() (data []byte) {
data = binary.BigEndian.AppendUint64(data, version0)
chainIDBytes := p.ChainID.Bytes()
data = append(data, make([]byte, 32-len(chainIDBytes))...)
data = append(data, chainIDBytes...)
data = append(data, p.Genesis.L1.Hash[:]...)
data = append(data, p.Genesis.L2.Hash[:]...)
data = binary.BigEndian.AppendUint64(data, p.Genesis.L2Time)
data = append(data, p.Genesis.SystemConfig.BatcherAddr.Bytes()...)
data = append(data, p.Genesis.SystemConfig.Scalar[:]...)
data = binary.BigEndian.AppendUint64(data, p.Genesis.SystemConfig.GasLimit)
data = append(data, p.DepositContractAddress.Bytes()...)
data = append(data, p.L1SystemConfigAddress.Bytes()...)
return data
}
6 changes: 1 addition & 5 deletions enclave/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,7 @@ func (s *Server) ExecuteStateless(

prevOutputRoot := outputRootV0(previousBlockHeader, prevMessageAccountHash)
outputRoot := outputRootV0(blockHeader, messageAccount.StorageHash)

configHash, err := config.Hash()
if err != nil {
return nil, fmt.Errorf("failed to marshal chain config: %w", err)
}
configHash := config.Hash()

data := append(configHash[:], l1OriginHash[:]...)
data = append(data, prevOutputRoot[:]...)
Expand Down
6 changes: 1 addition & 5 deletions op-proposer/proposer/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ func NewProver(
return nil, fmt.Errorf("failed to fetch rollup config: %w", err)
}
cfg := enclave.FromRollupConfig(rollupConfig)
configHash, err := cfg.Hash()
if err != nil {
return nil, fmt.Errorf("failed to hash rollup config: %w", err)
}

return &Prover{
config: cfg,
configHash: configHash,
configHash: cfg.Hash(),
l1: l1,
l2: l2,
enclave: enclav,
Expand Down

0 comments on commit eb0eaf1

Please sign in to comment.