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
cfc45742
Unverified
Commit
cfc45742
authored
Apr 12, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-program: Implement caching for the L1 oracle.
parent
6cebf39c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
183 additions
and
44 deletions
+183
-44
cache.go
op-program/client/l1/cache.go
+56
-0
cache_test.go
op-program/client/l1/cache_test.go
+71
-0
client_test.go
op-program/client/l1/client_test.go
+1
-43
stub_oracle_test.go
op-program/client/l1/stub_oracle_test.go
+54
-0
l1.go
op-program/host/l1/l1.go
+1
-1
No files found.
op-program/client/l1/cache.go
0 → 100644
View file @
cfc45742
package
l1
import
(
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// CachingOracle is an implementation of Oracle that delegates to another implementation, adding caching of all results
type
CachingOracle
struct
{
oracle
Oracle
blocks
map
[
common
.
Hash
]
eth
.
BlockInfo
txs
map
[
common
.
Hash
]
types
.
Transactions
rcpts
map
[
common
.
Hash
]
types
.
Receipts
}
func
NewCachingOracle
(
oracle
Oracle
)
*
CachingOracle
{
return
&
CachingOracle
{
oracle
:
oracle
,
blocks
:
make
(
map
[
common
.
Hash
]
eth
.
BlockInfo
),
txs
:
make
(
map
[
common
.
Hash
]
types
.
Transactions
),
rcpts
:
make
(
map
[
common
.
Hash
]
types
.
Receipts
),
}
}
func
(
o
CachingOracle
)
HeaderByBlockHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
block
,
ok
:=
o
.
blocks
[
blockHash
]
if
ok
{
return
block
}
block
=
o
.
oracle
.
HeaderByBlockHash
(
blockHash
)
o
.
blocks
[
blockHash
]
=
block
return
block
}
func
(
o
CachingOracle
)
TransactionsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
)
{
txs
,
ok
:=
o
.
txs
[
blockHash
]
if
ok
{
return
o
.
HeaderByBlockHash
(
blockHash
),
txs
}
block
,
txs
:=
o
.
oracle
.
TransactionsByBlockHash
(
blockHash
)
o
.
blocks
[
blockHash
]
=
block
o
.
txs
[
blockHash
]
=
txs
return
block
,
txs
}
func
(
o
CachingOracle
)
ReceiptsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
)
{
rcpts
,
ok
:=
o
.
rcpts
[
blockHash
]
if
ok
{
return
o
.
HeaderByBlockHash
(
blockHash
),
rcpts
}
block
,
rcpts
:=
o
.
oracle
.
ReceiptsByBlockHash
(
blockHash
)
o
.
blocks
[
blockHash
]
=
block
o
.
rcpts
[
blockHash
]
=
rcpts
return
block
,
rcpts
}
op-program/client/l1/cache_test.go
0 → 100644
View file @
cfc45742
package
l1
import
(
"math/rand"
"testing"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/stretchr/testify/require"
)
// Should implement Oracle
var
_
Oracle
=
(
*
CachingOracle
)(
nil
)
func
TestCachingOracle_HeaderByBlockHash
(
t
*
testing
.
T
)
{
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
stub
:=
newStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
block
:=
testutils
.
RandomBlockInfo
(
rng
)
// Initial call retrieves from the stub
stub
.
blocks
[
block
.
Hash
()]
=
block
result
:=
oracle
.
HeaderByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
result
)
// Later calls should retrieve from cache
delete
(
stub
.
blocks
,
block
.
Hash
())
result
=
oracle
.
HeaderByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
result
)
}
func
TestCachingOracle_TransactionsByBlockHash
(
t
*
testing
.
T
)
{
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
stub
:=
newStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
block
,
_
:=
testutils
.
RandomBlock
(
rng
,
3
)
// Initial call retrieves from the stub
stub
.
blocks
[
block
.
Hash
()]
=
block
stub
.
txs
[
block
.
Hash
()]
=
block
.
Transactions
()
actualBlock
,
actualTxs
:=
oracle
.
TransactionsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
actualBlock
)
require
.
Equal
(
t
,
block
.
Transactions
(),
actualTxs
)
// Later calls should retrieve from cache
delete
(
stub
.
blocks
,
block
.
Hash
())
delete
(
stub
.
txs
,
block
.
Hash
())
actualBlock
,
actualTxs
=
oracle
.
TransactionsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
actualBlock
)
require
.
Equal
(
t
,
block
.
Transactions
(),
actualTxs
)
}
func
TestCachingOracle_ReceiptsByBlockHash
(
t
*
testing
.
T
)
{
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
stub
:=
newStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
block
,
rcpts
:=
testutils
.
RandomBlock
(
rng
,
3
)
// Initial call retrieves from the stub
stub
.
blocks
[
block
.
Hash
()]
=
block
stub
.
rcpts
[
block
.
Hash
()]
=
rcpts
actualBlock
,
actualRcpts
:=
oracle
.
ReceiptsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
actualBlock
)
require
.
EqualValues
(
t
,
rcpts
,
actualRcpts
)
// Later calls should retrieve from cache
delete
(
stub
.
blocks
,
block
.
Hash
())
delete
(
stub
.
rcpts
,
block
.
Hash
())
actualBlock
,
actualRcpts
=
oracle
.
ReceiptsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
actualBlock
)
require
.
EqualValues
(
t
,
rcpts
,
actualRcpts
)
}
op-program/client/l1/client_test.go
View file @
cfc45742
...
...
@@ -145,54 +145,12 @@ func TestL1BlockRefByNumber(t *testing.T) {
}
func
newClient
(
t
*
testing
.
T
)
(
*
OracleL1Client
,
*
stubOracle
)
{
stub
:=
&
stubOracle
{
t
:
t
,
blocks
:
make
(
map
[
common
.
Hash
]
eth
.
BlockInfo
),
txs
:
make
(
map
[
common
.
Hash
]
types
.
Transactions
),
rcpts
:
make
(
map
[
common
.
Hash
]
types
.
Receipts
),
}
stub
:=
newStubOracle
(
t
)
stub
.
blocks
[
head
.
Hash
()]
=
head
client
:=
NewOracleL1Client
(
testlog
.
Logger
(
t
,
log
.
LvlDebug
),
stub
,
head
.
Hash
())
return
client
,
stub
}
type
stubOracle
struct
{
t
*
testing
.
T
// blocks maps block hash to eth.BlockInfo
blocks
map
[
common
.
Hash
]
eth
.
BlockInfo
// txs maps block hash to transactions
txs
map
[
common
.
Hash
]
types
.
Transactions
// rcpts maps Block hash to receipts
rcpts
map
[
common
.
Hash
]
types
.
Receipts
}
func
(
o
stubOracle
)
HeaderByBlockHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
info
,
ok
:=
o
.
blocks
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown block %s"
,
blockHash
)
}
return
info
}
func
(
o
stubOracle
)
TransactionsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
)
{
txs
,
ok
:=
o
.
txs
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown txs %s"
,
blockHash
)
}
return
o
.
HeaderByBlockHash
(
blockHash
),
txs
}
func
(
o
stubOracle
)
ReceiptsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
)
{
rcpts
,
ok
:=
o
.
rcpts
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown rcpts %s"
,
blockHash
)
}
return
o
.
HeaderByBlockHash
(
blockHash
),
rcpts
}
func
blockNum
(
num
uint64
)
eth
.
BlockInfo
{
parentNum
:=
num
-
1
return
&
testutils
.
MockBlockInfo
{
...
...
op-program/client/l1/stub_oracle_test.go
0 → 100644
View file @
cfc45742
package
l1
import
(
"testing"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type
stubOracle
struct
{
t
*
testing
.
T
// blocks maps block hash to eth.BlockInfo
blocks
map
[
common
.
Hash
]
eth
.
BlockInfo
// txs maps block hash to transactions
txs
map
[
common
.
Hash
]
types
.
Transactions
// rcpts maps Block hash to receipts
rcpts
map
[
common
.
Hash
]
types
.
Receipts
}
func
newStubOracle
(
t
*
testing
.
T
)
*
stubOracle
{
return
&
stubOracle
{
t
:
t
,
blocks
:
make
(
map
[
common
.
Hash
]
eth
.
BlockInfo
),
txs
:
make
(
map
[
common
.
Hash
]
types
.
Transactions
),
rcpts
:
make
(
map
[
common
.
Hash
]
types
.
Receipts
),
}
}
func
(
o
stubOracle
)
HeaderByBlockHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
info
,
ok
:=
o
.
blocks
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown block %s"
,
blockHash
)
}
return
info
}
func
(
o
stubOracle
)
TransactionsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
)
{
txs
,
ok
:=
o
.
txs
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown txs %s"
,
blockHash
)
}
return
o
.
HeaderByBlockHash
(
blockHash
),
txs
}
func
(
o
stubOracle
)
ReceiptsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
)
{
rcpts
,
ok
:=
o
.
rcpts
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown rcpts %s"
,
blockHash
)
}
return
o
.
HeaderByBlockHash
(
blockHash
),
rcpts
}
op-program/host/l1/l1.go
View file @
cfc45742
...
...
@@ -21,6 +21,6 @@ func NewFetchingL1(ctx context.Context, logger log.Logger, cfg *config.Config) (
if
err
!=
nil
{
return
nil
,
err
}
oracle
:=
NewFetchingL1Oracle
(
ctx
,
logger
,
source
)
oracle
:=
cll1
.
NewCachingOracle
(
NewFetchingL1Oracle
(
ctx
,
logger
,
source
)
)
return
cll1
.
NewOracleL1Client
(
logger
,
oracle
,
cfg
.
L1Head
),
err
}
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