Commit 26868d3a authored by clabby's avatar clabby

Use binding for `ComputeL2OutputRoot`

parent 90ade85a
......@@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/rollup"
......@@ -114,7 +115,16 @@ func (n *nodeAPI) OutputAtBlock(ctx context.Context, number hexutil.Uint64) (*et
}
var l2OutputRootVersion eth.Bytes32 // it's zero for now
l2OutputRoot := rollup.ComputeL2OutputRoot(l2OutputRootVersion, head.Root(), proof.StorageHash, head.Hash())
l2OutputRoot, err := rollup.ComputeL2OutputRoot(&bindings.TypesOutputRootProof{
Version: l2OutputRootVersion,
StateRoot: head.Root(),
MessagePasserStorageRoot: proof.StorageHash,
LatestBlockhash: head.Hash(),
})
if err != nil {
n.log.Error("Error computing L2 output root, nil ptr passed to hashing function")
return nil, err
}
return &eth.OutputResponse{
Version: l2OutputRootVersion,
......
package rollup
import (
"errors"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// ComputeL2OutputRoot computes the L2 output root
func ComputeL2OutputRoot(l2OutputRootVersion eth.Bytes32, blockRoot common.Hash, storageRoot common.Hash, blockHash common.Hash) eth.Bytes32 {
digest := crypto.Keccak256Hash(
l2OutputRootVersion[:],
blockRoot.Bytes(),
storageRoot[:],
blockHash.Bytes(),
)
return eth.Bytes32(digest)
}
var NilProof = errors.New("Output root proof is nil")
// ComputeL2OutputRoot computes the L2 output root by hashing an output root proof.
func ComputeL2OutputRoot(proofElements *bindings.TypesOutputRootProof) (eth.Bytes32, error) {
if proofElements == nil {
return eth.Bytes32{}, NilProof
}
// HashOutputRootProof computes the hash of the output root proof
func HashOutputRootProof(proof *bindings.TypesOutputRootProof) eth.Bytes32 {
return ComputeL2OutputRoot(
proof.Version,
proof.StateRoot,
proof.MessagePasserStorageRoot,
proof.LatestBlockhash,
digest := crypto.Keccak256Hash(
proofElements.Version[:],
proofElements.StateRoot[:],
proofElements.MessagePasserStorageRoot[:],
proofElements.LatestBlockhash[:],
)
return eth.Bytes32(digest), nil
}
......@@ -221,7 +221,8 @@ func main() {
latestBlockHash := common.HexToHash(args[4])
// Hash the output root proof
hash := hashOutputRootProof(version, stateRoot, messagePasserStorageRoot, latestBlockHash)
hash, err := hashOutputRootProof(version, stateRoot, messagePasserStorageRoot, latestBlockHash)
checkErr(err, "Error hashing output root proof")
// Pack hash
packed, err := fixedBytesArgs.Pack(&hash)
......@@ -289,7 +290,8 @@ func main() {
checkErr(state.Prove(predeploys.L2ToL1MessagePasserAddr.Bytes(), 0, &proof), "Error getting proof")
// Get the output root
outputRoot := hashOutputRootProof(common.Hash{}, world.Hash(), state.Hash(), common.Hash{})
outputRoot, err := hashOutputRootProof(common.Hash{}, world.Hash(), state.Hash(), common.Hash{})
checkErr(err, "Error hashing output root proof")
// Pack the output
output := struct {
......
......@@ -64,14 +64,17 @@ func hashWithdrawal(nonce *big.Int, sender common.Address, target common.Address
}
// hashOutputRootProof hashes an output root proof.
func hashOutputRootProof(version common.Hash, stateRoot common.Hash, messagePasserStorageRoot common.Hash, latestBlockHash common.Hash) common.Hash {
proof := bindings.TypesOutputRootProof{
func hashOutputRootProof(version common.Hash, stateRoot common.Hash, messagePasserStorageRoot common.Hash, latestBlockHash common.Hash) (common.Hash, error) {
hash, err := rollup.ComputeL2OutputRoot(&bindings.TypesOutputRootProof{
Version: version,
StateRoot: stateRoot,
MessagePasserStorageRoot: messagePasserStorageRoot,
LatestBlockhash: latestBlockHash,
})
if err != nil {
return common.Hash{}, err
}
return common.Hash(rollup.HashOutputRootProof(&proof))
return common.Hash(hash), nil
}
// makeDepositTx creates a deposit transaction type.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment