Commit 71e0fd84 authored by protolambda's avatar protolambda

cannon: move sourcemap tracing util to op-chain-ops

parent 26c461ae
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum-optimism/optimism/op-bindings/bindings" "github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-chain-ops/srcmap"
) )
var ( var (
...@@ -78,8 +79,8 @@ type Contract struct { ...@@ -78,8 +79,8 @@ type Contract struct {
// ignore abi,bytecode,etc. // ignore abi,bytecode,etc.
} }
func (c *Contract) SourceMap(sourcePaths []string) (*SourceMap, error) { func (c *Contract) SourceMap(sourcePaths []string) (*srcmap.SourceMap, error) {
return ParseSourceMap(sourcePaths, c.DeployedBytecode.Object, c.DeployedBytecode.SourceMap) return srcmap.ParseSourceMap(sourcePaths, c.DeployedBytecode.Object, c.DeployedBytecode.SourceMap)
} }
type Contracts struct { type Contracts struct {
......
...@@ -15,6 +15,8 @@ import ( ...@@ -15,6 +15,8 @@ import (
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-chain-ops/srcmap"
) )
func testContractsSetup(t *testing.T) (*Contracts, *Addresses, vm.EVMLogger) { func testContractsSetup(t *testing.T) (*Contracts, *Addresses, vm.EVMLogger) {
...@@ -33,7 +35,7 @@ func testContractsSetup(t *testing.T) (*Contracts, *Addresses, vm.EVMLogger) { ...@@ -33,7 +35,7 @@ func testContractsSetup(t *testing.T) (*Contracts, *Addresses, vm.EVMLogger) {
FeeRecipient: common.Address{0xaa}, FeeRecipient: common.Address{0xaa},
} }
var tracer vm.EVMLogger var tracer vm.EVMLogger
tracer = NewSourceMapTracer(map[common.Address]*SourceMap{addrs.MIPS: mipsSrcMap, addrs.Oracle: oracleSrcMap}, os.Stdout) tracer = srcmap.NewSourceMapTracer(map[common.Address]*srcmap.SourceMap{addrs.MIPS: mipsSrcMap, addrs.Oracle: oracleSrcMap}, os.Stdout)
//tracer = logger.NewMarkdownLogger(&logger.Config{}, os.Stdout) //tracer = logger.NewMarkdownLogger(&logger.Config{}, os.Stdout)
tracer = nil // disable tracer tracer = nil // disable tracer
......
package mipsevm package srcmap
import ( import (
"fmt" "fmt"
...@@ -108,6 +108,11 @@ func (s *SourceMap) FormattedInfo(pc uint64) string { ...@@ -108,6 +108,11 @@ func (s *SourceMap) FormattedInfo(pc uint64) string {
// ParseSourceMap parses a solidity sourcemap: mapping bytecode indices to source references. // ParseSourceMap parses a solidity sourcemap: mapping bytecode indices to source references.
// See https://docs.soliditylang.org/en/latest/internals/source_mappings.html // See https://docs.soliditylang.org/en/latest/internals/source_mappings.html
//
// Sources is the list of source files, which will be read from the filesystem
// to transform token numbers into line/column numbers.
// The sources are as referenced in the source-map by index.
// Not all sources are necessary, some indices may be unknown.
func ParseSourceMap(sources []string, bytecode []byte, sourceMap string) (*SourceMap, error) { func ParseSourceMap(sources []string, bytecode []byte, sourceMap string) (*SourceMap, error) {
instructions := strings.Split(sourceMap, ";") instructions := strings.Split(sourceMap, ";")
......
package mipsevm package srcmap
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
) )
func TestSourcemap(t *testing.T) { func TestSourcemap(t *testing.T) {
contract, err := LoadContract("MIPS") sourcePath := "../../packages/contracts-bedrock/contracts/cannon/MIPS.sol"
require.NoError(t, err) deployedByteCode := hexutil.MustDecode(bindings.MIPSDeployedBin)
srcMap, err := contract.SourceMap([]string{"../../packages/contracts-bedrock/contracts/cannon/MIPS.sol"}) srcMap, err := ParseSourceMap(
[]string{sourcePath},
deployedByteCode,
bindings.MIPSDeployedSourceMap)
require.NoError(t, err) require.NoError(t, err)
for i := 0; i < len(contract.DeployedBytecode.Object); i++ {
for i := 0; i < len(deployedByteCode); i++ {
info := srcMap.FormattedInfo(uint64(i)) info := srcMap.FormattedInfo(uint64(i))
if !strings.HasPrefix(info, "generated:") && !strings.HasPrefix(info, "../../packages/contracts-bedrock/contracts/cannon/MIPS.sol") { if !strings.HasPrefix(info, "generated:") && !strings.HasPrefix(info, sourcePath) {
t.Fatalf("unexpected info: %q", info) t.Fatalf("unexpected info: %q", info)
} }
} }
......
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