Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
62b9850f
Unverified
Commit
62b9850f
authored
Apr 17, 2023
by
OptimismBot
Committed by
GitHub
Apr 17, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5452 from ethereum-optimism/aj/fpp-store-fetched-data
op-program: Populate preimage store
parents
e6f1f61c
373c5aa3
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
675 additions
and
27 deletions
+675
-27
debug_client.go
op-node/sources/debug_client.go
+49
-0
mock_debug_client.go
op-node/testutils/mock_debug_client.go
+30
-0
hints.go
op-program/client/l1/hints.go
+9
-3
hints.go
op-program/client/l2/hints.go
+11
-4
main.go
op-program/host/cmd/main.go
+59
-10
l1.go
op-program/host/l1/l1.go
+9
-3
l2.go
op-program/host/l2/l2.go
+11
-7
prefetcher.go
op-program/host/prefetcher/prefetcher.go
+164
-0
prefetcher_test.go
op-program/host/prefetcher/prefetcher_test.go
+333
-0
No files found.
op-node/sources/debug_client.go
0 → 100644
View file @
62b9850f
package
sources
import
(
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/rawdb"
)
type
DebugClient
struct
{
callContext
CallContextFn
}
func
NewDebugClient
(
callContext
CallContextFn
)
*
DebugClient
{
return
&
DebugClient
{
callContext
}
}
func
(
o
*
DebugClient
)
NodeByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
([]
byte
,
error
)
{
// MPT nodes are stored as the hash of the node (with no prefix)
node
,
err
:=
o
.
dbGet
(
ctx
,
hash
[
:
])
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to retrieve state MPT node: %w"
,
err
)
}
return
node
,
nil
}
func
(
o
*
DebugClient
)
CodeByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
([]
byte
,
error
)
{
// First try retrieving with the new code prefix
code
,
err
:=
o
.
dbGet
(
ctx
,
append
(
append
(
make
([]
byte
,
0
),
rawdb
.
CodePrefix
...
),
hash
[
:
]
...
))
if
err
!=
nil
{
// Fallback to the legacy un-prefixed version
code
,
err
=
o
.
dbGet
(
ctx
,
hash
[
:
])
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to retrieve contract code, using new and legacy keys, with codehash %s: %w"
,
hash
,
err
)
}
}
return
code
,
nil
}
func
(
o
*
DebugClient
)
dbGet
(
ctx
context
.
Context
,
key
[]
byte
)
([]
byte
,
error
)
{
var
node
hexutil
.
Bytes
err
:=
o
.
callContext
(
ctx
,
&
node
,
"debug_dbGet"
,
hexutil
.
Encode
(
key
))
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"fetch error %x: %w"
,
key
,
err
)
}
return
node
,
nil
}
op-node/testutils/mock_debug_client.go
0 → 100644
View file @
62b9850f
package
testutils
import
(
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/mock"
)
type
MockDebugClient
struct
{
mock
.
Mock
}
func
(
m
*
MockDebugClient
)
ExpectNodeByHash
(
hash
common
.
Hash
,
res
[]
byte
,
err
error
)
{
m
.
Mock
.
On
(
"NodeByHash"
,
hash
)
.
Once
()
.
Return
(
res
,
&
err
)
}
func
(
m
*
MockDebugClient
)
NodeByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
([]
byte
,
error
)
{
out
:=
m
.
Mock
.
MethodCalled
(
"NodeByHash"
,
hash
)
return
out
[
0
]
.
([]
byte
),
*
out
[
1
]
.
(
*
error
)
}
func
(
m
*
MockDebugClient
)
ExpectCodeByHash
(
hash
common
.
Hash
,
res
[]
byte
,
err
error
)
{
m
.
Mock
.
On
(
"CodeByHash"
,
hash
)
.
Once
()
.
Return
(
res
,
&
err
)
}
func
(
m
*
MockDebugClient
)
CodeByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
([]
byte
,
error
)
{
out
:=
m
.
Mock
.
MethodCalled
(
"CodeByHash"
,
hash
)
return
out
[
0
]
.
([]
byte
),
*
out
[
1
]
.
(
*
error
)
}
op-program/client/l1/hints.go
View file @
62b9850f
...
...
@@ -6,12 +6,18 @@ import (
"github.com/ethereum-optimism/optimism/op-program/preimage"
)
const
(
HintL1BlockHeader
=
"l1-block-header"
HintL1Transactions
=
"l1-transactions"
HintL1Receipts
=
"l1-receipts"
)
type
BlockHeaderHint
common
.
Hash
var
_
preimage
.
Hint
=
BlockHeaderHint
{}
func
(
l
BlockHeaderHint
)
Hint
()
string
{
return
"l1-block-header
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL1BlockHeader
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
type
TransactionsHint
common
.
Hash
...
...
@@ -19,7 +25,7 @@ type TransactionsHint common.Hash
var
_
preimage
.
Hint
=
TransactionsHint
{}
func
(
l
TransactionsHint
)
Hint
()
string
{
return
"l1-transactions
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL1Transactions
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
type
ReceiptsHint
common
.
Hash
...
...
@@ -27,5 +33,5 @@ type ReceiptsHint common.Hash
var
_
preimage
.
Hint
=
ReceiptsHint
{}
func
(
l
ReceiptsHint
)
Hint
()
string
{
return
"l1-receipts
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL1Receipts
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
op-program/client/l2/hints.go
View file @
62b9850f
...
...
@@ -6,12 +6,19 @@ import (
"github.com/ethereum-optimism/optimism/op-program/preimage"
)
const
(
HintL2BlockHeader
=
"l2-block-header"
HintL2Transactions
=
"l2-transactions"
HintL2Code
=
"l2-code"
HintL2StateNode
=
"l2-state-node"
)
type
BlockHeaderHint
common
.
Hash
var
_
preimage
.
Hint
=
BlockHeaderHint
{}
func
(
l
BlockHeaderHint
)
Hint
()
string
{
return
"l2-block-header
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL2BlockHeader
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
type
TransactionsHint
common
.
Hash
...
...
@@ -19,7 +26,7 @@ type TransactionsHint common.Hash
var
_
preimage
.
Hint
=
TransactionsHint
{}
func
(
l
TransactionsHint
)
Hint
()
string
{
return
"l2-transactions
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL2Transactions
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
type
CodeHint
common
.
Hash
...
...
@@ -27,7 +34,7 @@ type CodeHint common.Hash
var
_
preimage
.
Hint
=
CodeHint
{}
func
(
l
CodeHint
)
Hint
()
string
{
return
"l2-code
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL2Code
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
type
StateNodeHint
common
.
Hash
...
...
@@ -35,5 +42,5 @@ type StateNodeHint common.Hash
var
_
preimage
.
Hint
=
StateNodeHint
{}
func
(
l
StateNodeHint
)
Hint
()
string
{
return
"l2-state-node
"
+
(
common
.
Hash
)(
l
)
.
String
()
return
HintL2StateNode
+
"
"
+
(
common
.
Hash
)(
l
)
.
String
()
}
op-program/host/cmd/main.go
View file @
62b9850f
...
...
@@ -6,17 +6,20 @@ import (
"fmt"
"io"
"os"
"time"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/client"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/
rollup/derive
"
"github.com/ethereum-optimism/optimism/op-node/
sources
"
cldr
"github.com/ethereum-optimism/optimism/op-program/client/driver"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum-optimism/optimism/op-program/host/flags"
"github.com/ethereum-optimism/optimism/op-program/host/kvstore"
"github.com/ethereum-optimism/optimism/op-program/host/l1"
"github.com/ethereum-optimism/optimism/op-program/host/l2"
"github.com/ethereum-optimism/optimism/op-program/host/prefetcher"
"github.com/ethereum-optimism/optimism/op-program/host/version"
"github.com/ethereum-optimism/optimism/op-program/preimage"
oplog
"github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli"
...
...
@@ -96,6 +99,11 @@ func setupLogging(ctx *cli.Context) (log.Logger, error) {
return
logger
,
nil
}
type
L2Source
struct
{
*
sources
.
L2Client
*
sources
.
DebugClient
}
// FaultProofProgram is the programmatic entry-point for the fault proof program
func
FaultProofProgram
(
logger
log
.
Logger
,
cfg
*
config
.
Config
)
error
{
cfg
.
Rollup
.
LogDescription
(
logger
,
chaincfg
.
L2ChainIDToNetworkName
)
...
...
@@ -104,27 +112,49 @@ func FaultProofProgram(logger log.Logger, cfg *config.Config) error {
}
ctx
:=
context
.
Background
()
kv
:=
kvstore
.
NewMemKV
()
logger
.
Info
(
"Connecting to L1 node"
,
"l1"
,
cfg
.
L1URL
)
l1
Source
,
err
:=
l1
.
NewFetchingL1
(
ctx
,
logger
,
cfg
)
l1
RPC
,
err
:=
client
.
NewRPC
(
ctx
,
logger
,
cfg
.
L1URL
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"
connect l1 oracle
: %w"
,
err
)
return
fmt
.
Errorf
(
"
failed to setup L1 RPC
: %w"
,
err
)
}
logger
.
Info
(
"Connecting to L2 node"
,
"l2"
,
cfg
.
L2URL
)
l2Source
,
err
:=
l2
.
NewFetchingEngine
(
ctx
,
logger
,
cfg
)
l2RPC
,
err
:=
client
.
NewRPC
(
ctx
,
logger
,
cfg
.
L2URL
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to setup L2 RPC: %w"
,
err
)
}
l1ClCfg
:=
sources
.
L1ClientDefaultConfig
(
cfg
.
Rollup
,
cfg
.
L1TrustRPC
,
cfg
.
L1RPCKind
)
l2ClCfg
:=
sources
.
L2ClientDefaultConfig
(
cfg
.
Rollup
,
true
)
l1Cl
,
err
:=
sources
.
NewL1Client
(
l1RPC
,
logger
,
nil
,
l1ClCfg
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to create L1 client: %w"
,
err
)
}
l2Cl
,
err
:=
sources
.
NewL2Client
(
l2RPC
,
logger
,
nil
,
l2ClCfg
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to create L2 client: %w"
,
err
)
}
l2DebugCl
:=
&
L2Source
{
L2Client
:
l2Cl
,
DebugClient
:
sources
.
NewDebugClient
(
l2RPC
.
CallContext
)}
logger
.
Info
(
"Setting up pre-fetcher"
)
prefetch
:=
prefetcher
.
NewPrefetcher
(
l1Cl
,
l2DebugCl
,
kv
)
preimageOracle
:=
asOracleFn
(
ctx
,
prefetch
)
hinter
:=
asHinter
(
prefetch
)
l1Source
:=
l1
.
NewSource
(
logger
,
preimageOracle
,
hinter
,
cfg
.
L1Head
)
logger
.
Info
(
"Connecting to L2 node"
,
"l2"
,
cfg
.
L2URL
)
l2Source
,
err
:=
l2
.
NewEngine
(
logger
,
preimageOracle
,
hinter
,
cfg
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"connect l2 oracle: %w"
,
err
)
}
logger
.
Info
(
"Starting derivation"
)
d
:=
cldr
.
NewDriver
(
logger
,
cfg
.
Rollup
,
l1Source
,
l2Source
)
for
{
if
err
=
d
.
Step
(
ctx
);
errors
.
Is
(
err
,
io
.
EOF
)
{
break
}
else
if
cfg
.
FetchingEnabled
()
&&
errors
.
Is
(
err
,
derive
.
ErrTemporary
)
{
// When in fetching mode, recover from temporary errors to allow us to keep fetching data
// TODO(CLI-3780) Ideally the retry would happen in the fetcher so this is not needed
logger
.
Warn
(
"Temporary error in pipeline"
,
"err"
,
err
)
time
.
Sleep
(
5
*
time
.
Second
)
}
else
if
err
!=
nil
{
return
err
}
...
...
@@ -135,3 +165,22 @@ func FaultProofProgram(logger log.Logger, cfg *config.Config) error {
}
return
nil
}
func
asOracleFn
(
ctx
context
.
Context
,
prefetcher
*
prefetcher
.
Prefetcher
)
preimage
.
OracleFn
{
return
func
(
key
preimage
.
Key
)
[]
byte
{
pre
,
err
:=
prefetcher
.
GetPreimage
(
ctx
,
key
.
PreimageKey
())
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"preimage unavailable for key %v: %w"
,
key
,
err
))
}
return
pre
}
}
func
asHinter
(
prefetcher
*
prefetcher
.
Prefetcher
)
preimage
.
HinterFn
{
return
func
(
v
preimage
.
Hint
)
{
err
:=
prefetcher
.
Hint
(
v
.
Hint
())
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"hint rejected %v: %w"
,
v
,
err
))
}
}
}
op-program/host/l1/l1.go
View file @
62b9850f
...
...
@@ -8,10 +8,12 @@ import (
"github.com/ethereum-optimism/optimism/op-node/sources"
cll1
"github.com/ethereum-optimism/optimism/op-program/client/l1"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum-optimism/optimism/op-program/preimage"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)
func
NewFetching
L1
(
ctx
context
.
Context
,
logger
log
.
Logger
,
cfg
*
config
.
Config
)
(
derive
.
L1Fetcher
,
error
)
{
func
NewFetching
Oracle
(
ctx
context
.
Context
,
logger
log
.
Logger
,
cfg
*
config
.
Config
)
(
cll1
.
Oracle
,
error
)
{
rpc
,
err
:=
client
.
NewRPC
(
ctx
,
logger
,
cfg
.
L1URL
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -21,6 +23,10 @@ func NewFetchingL1(ctx context.Context, logger log.Logger, cfg *config.Config) (
if
err
!=
nil
{
return
nil
,
err
}
oracle
:=
cll1
.
NewCachingOracle
(
NewFetchingL1Oracle
(
ctx
,
logger
,
source
))
return
cll1
.
NewOracleL1Client
(
logger
,
oracle
,
cfg
.
L1Head
),
err
return
NewFetchingL1Oracle
(
ctx
,
logger
,
source
),
nil
}
func
NewSource
(
logger
log
.
Logger
,
oracle
preimage
.
Oracle
,
hint
preimage
.
Hinter
,
l1Head
common
.
Hash
)
derive
.
L1Fetcher
{
l1Oracle
:=
cll1
.
NewCachingOracle
(
cll1
.
NewPreimageOracle
(
oracle
,
hint
))
return
cll1
.
NewOracleL1Client
(
logger
,
l1Oracle
,
l1Head
)
}
op-program/host/l2/l2.go
View file @
62b9850f
...
...
@@ -8,22 +8,18 @@ import (
cll2
"github.com/ethereum-optimism/optimism/op-program/client/l2"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum-optimism/optimism/op-program/preimage"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)
func
NewFetchingEngine
(
ctx
context
.
Context
,
logger
log
.
Logger
,
cfg
*
config
.
Config
)
(
*
cll2
.
OracleEngine
,
error
)
{
func
NewEngine
(
logger
log
.
Logger
,
pre
preimage
.
Oracle
,
hint
preimage
.
Hinter
,
cfg
*
config
.
Config
)
(
*
cll2
.
OracleEngine
,
error
)
{
oracle
:=
cll2
.
NewCachingOracle
(
cll2
.
NewPreimageOracle
(
pre
,
hint
))
genesis
,
err
:=
loadL2Genesis
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
fetcher
,
err
:=
NewFetchingL2Oracle
(
ctx
,
logger
,
cfg
.
L2URL
,
cfg
.
L2Head
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"connect l2 oracle: %w"
,
err
)
}
oracle
:=
cll2
.
NewCachingOracle
(
fetcher
)
engineBackend
,
err
:=
cll2
.
NewOracleBackedL2Chain
(
logger
,
oracle
,
genesis
,
cfg
.
L2Head
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"create l2 chain: %w"
,
err
)
...
...
@@ -31,6 +27,14 @@ func NewFetchingEngine(ctx context.Context, logger log.Logger, cfg *config.Confi
return
cll2
.
NewOracleEngine
(
cfg
.
Rollup
,
logger
,
engineBackend
),
nil
}
func
NewFetchingOracle
(
ctx
context
.
Context
,
logger
log
.
Logger
,
cfg
*
config
.
Config
)
(
cll2
.
Oracle
,
error
)
{
oracle
,
err
:=
NewFetchingL2Oracle
(
ctx
,
logger
,
cfg
.
L2URL
,
cfg
.
L2Head
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"connect l2 oracle: %w"
,
err
)
}
return
oracle
,
nil
}
func
loadL2Genesis
(
cfg
*
config
.
Config
)
(
*
params
.
ChainConfig
,
error
)
{
data
,
err
:=
os
.
ReadFile
(
cfg
.
L2GenesisPath
)
if
err
!=
nil
{
...
...
op-program/host/prefetcher/prefetcher.go
0 → 100644
View file @
62b9850f
package
prefetcher
import
(
"context"
"errors"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-program/client/l1"
"github.com/ethereum-optimism/optimism/op-program/client/l2"
"github.com/ethereum-optimism/optimism/op-program/client/mpt"
"github.com/ethereum-optimism/optimism/op-program/host/kvstore"
"github.com/ethereum-optimism/optimism/op-program/preimage"
)
type
L1Source
interface
{
InfoByHash
(
ctx
context
.
Context
,
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
error
)
InfoAndTxsByHash
(
ctx
context
.
Context
,
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
,
error
)
FetchReceipts
(
ctx
context
.
Context
,
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
,
error
)
}
type
L2Source
interface
{
InfoAndTxsByHash
(
ctx
context
.
Context
,
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
,
error
)
NodeByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
([]
byte
,
error
)
CodeByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
([]
byte
,
error
)
}
type
Prefetcher
struct
{
l1Fetcher
L1Source
l2Fetcher
L2Source
lastHint
string
kvStore
kvstore
.
KV
}
func
NewPrefetcher
(
l1Fetcher
L1Source
,
l2Fetcher
L2Source
,
kvStore
kvstore
.
KV
)
*
Prefetcher
{
return
&
Prefetcher
{
l1Fetcher
:
l1Fetcher
,
l2Fetcher
:
l2Fetcher
,
kvStore
:
kvStore
,
}
}
func
(
p
*
Prefetcher
)
Hint
(
hint
string
)
error
{
p
.
lastHint
=
hint
return
nil
}
func
(
p
*
Prefetcher
)
GetPreimage
(
ctx
context
.
Context
,
key
common
.
Hash
)
([]
byte
,
error
)
{
pre
,
err
:=
p
.
kvStore
.
Get
(
key
)
if
errors
.
Is
(
err
,
kvstore
.
ErrNotFound
)
&&
p
.
lastHint
!=
""
{
hint
:=
p
.
lastHint
p
.
lastHint
=
""
if
err
:=
p
.
prefetch
(
ctx
,
hint
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"prefetch failed: %w"
,
err
)
}
// Should now be available
return
p
.
kvStore
.
Get
(
key
)
}
return
pre
,
err
}
func
(
p
*
Prefetcher
)
prefetch
(
ctx
context
.
Context
,
hint
string
)
error
{
hintType
,
hash
,
err
:=
parseHint
(
hint
)
if
err
!=
nil
{
return
err
}
switch
hintType
{
case
l1
.
HintL1BlockHeader
:
header
,
err
:=
p
.
l1Fetcher
.
InfoByHash
(
ctx
,
hash
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to fetch L1 block %s header: %w"
,
hash
,
err
)
}
data
,
err
:=
header
.
HeaderRLP
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"marshall header: %w"
,
err
)
}
return
p
.
kvStore
.
Put
(
preimage
.
Keccak256Key
(
hash
)
.
PreimageKey
(),
data
)
case
l1
.
HintL1Transactions
:
_
,
txs
,
err
:=
p
.
l1Fetcher
.
InfoAndTxsByHash
(
ctx
,
hash
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to fetch L1 block %s txs: %w"
,
hash
,
err
)
}
return
p
.
storeTransactions
(
txs
)
case
l1
.
HintL1Receipts
:
_
,
receipts
,
err
:=
p
.
l1Fetcher
.
FetchReceipts
(
ctx
,
hash
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to fetch L1 block %s receipts: %w"
,
hash
,
err
)
}
return
p
.
storeReceipts
(
receipts
)
case
l2
.
HintL2BlockHeader
:
header
,
txs
,
err
:=
p
.
l2Fetcher
.
InfoAndTxsByHash
(
ctx
,
hash
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to fetch L2 block %s: %w"
,
hash
,
err
)
}
data
,
err
:=
header
.
HeaderRLP
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to encode header to RLP: %w"
,
err
)
}
err
=
p
.
kvStore
.
Put
(
preimage
.
Keccak256Key
(
hash
)
.
PreimageKey
(),
data
)
if
err
!=
nil
{
return
err
}
return
p
.
storeTransactions
(
txs
)
case
l2
.
HintL2StateNode
:
node
,
err
:=
p
.
l2Fetcher
.
NodeByHash
(
ctx
,
hash
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to fetch L2 state node %s: %w"
,
hash
,
err
)
}
return
p
.
kvStore
.
Put
(
preimage
.
Keccak256Key
(
hash
)
.
PreimageKey
(),
node
)
case
l2
.
HintL2Code
:
code
,
err
:=
p
.
l2Fetcher
.
CodeByHash
(
ctx
,
hash
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to fetch L2 contract code %s: %w"
,
hash
,
err
)
}
return
p
.
kvStore
.
Put
(
preimage
.
Keccak256Key
(
hash
)
.
PreimageKey
(),
code
)
}
return
fmt
.
Errorf
(
"unknown hint type: %v"
,
hintType
)
}
func
(
p
*
Prefetcher
)
storeReceipts
(
receipts
types
.
Receipts
)
error
{
opaqueReceipts
,
err
:=
eth
.
EncodeReceipts
(
receipts
)
if
err
!=
nil
{
return
err
}
return
p
.
storeTrieNodes
(
opaqueReceipts
)
}
func
(
p
*
Prefetcher
)
storeTransactions
(
txs
types
.
Transactions
)
error
{
opaqueTxs
,
err
:=
eth
.
EncodeTransactions
(
txs
)
if
err
!=
nil
{
return
err
}
return
p
.
storeTrieNodes
(
opaqueTxs
)
}
func
(
p
*
Prefetcher
)
storeTrieNodes
(
values
[]
hexutil
.
Bytes
)
error
{
_
,
nodes
:=
mpt
.
WriteTrie
(
values
)
for
_
,
node
:=
range
nodes
{
err
:=
p
.
kvStore
.
Put
(
preimage
.
Keccak256Key
(
crypto
.
Keccak256Hash
(
node
))
.
PreimageKey
(),
node
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to store node: %w"
,
err
)
}
}
return
nil
}
// parseHint parses a hint string in wire protocol. Returns the hint type, requested hash and error (if any).
func
parseHint
(
hint
string
)
(
string
,
common
.
Hash
,
error
)
{
hintType
,
hashStr
,
found
:=
strings
.
Cut
(
hint
,
" "
)
if
!
found
{
return
""
,
common
.
Hash
{},
fmt
.
Errorf
(
"unsupported hint: %s"
,
hint
)
}
hash
:=
common
.
HexToHash
(
hashStr
)
if
hash
==
(
common
.
Hash
{})
{
return
""
,
common
.
Hash
{},
fmt
.
Errorf
(
"invalid hash: %s"
,
hashStr
)
}
return
hintType
,
hash
,
nil
}
op-program/host/prefetcher/prefetcher_test.go
0 → 100644
View file @
62b9850f
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment