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
1f372382
Commit
1f372382
authored
Jun 29, 2023
by
Hamdi Allam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update database to account for output proposals
parent
ab15393d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
80 deletions
+62
-80
blocks.go
indexer/database/blocks.go
+39
-59
20230523_create_schema.sql
indexer/migrations/20230523_create_schema.sql
+23
-21
No files found.
indexer/database/blocks.go
View file @
1f372382
...
...
@@ -3,9 +3,9 @@ package database
import
(
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"gorm.io/gorm"
)
...
...
@@ -27,11 +27,6 @@ type L1BlockHeader struct {
type
L2BlockHeader
struct
{
BlockHeader
// Marked when the proposed output is finalized on L1.
// All bedrock blocks will have `LegacyStateBatchIndex ^== NULL`
L1BlockHash
*
common
.
Hash
`gorm:"serializer:json"`
LegacyStateBatchIndex
*
uint64
}
type
LegacyStateBatch
struct
{
...
...
@@ -39,25 +34,33 @@ type LegacyStateBatch struct {
// violating the primary key constraint.
Index
uint64
`gorm:"primaryKey;default:0"`
Root
common
.
Hash
`gorm:"serializer:json"`
Size
uint64
PrevTotal
uint64
L1BlockHash
common
.
Hash
`gorm:"serializer:json"`
Root
common
.
Hash
`gorm:"serializer:json"`
Size
uint64
PrevTotal
uint64
L1ContractEventGUID
uuid
.
UUID
}
type
OutputProposal
struct
{
OutputRoot
common
.
Hash
`gorm:"primaryKey;serializer:json"`
L2BlockNumber
U256
L1ContractEventGUID
uuid
.
UUID
}
type
BlocksView
interface
{
FinalizedL1BlockHeader
()
(
*
L1BlockHeader
,
error
)
FinalizedL2BlockHeader
()
(
*
L2BlockHeader
,
error
)
LatestL1BlockHeader
()
(
*
L1BlockHeader
,
error
)
LatestOutputProposed
()
(
*
OutputProposal
,
error
)
LatestL2BlockHeader
()
(
*
L2BlockHeader
,
error
)
}
type
BlocksDB
interface
{
BlocksView
StoreL1BlockHeaders
([]
*
L1BlockHeader
)
error
StoreLegacyStateBatch
(
*
LegacyStateBatch
)
error
StoreL2BlockHeaders
([]
*
L2BlockHeader
)
error
MarkFinalizedL1RootForL2Block
(
common
.
Hash
,
common
.
Hash
)
error
StoreLegacyStateBatches
([]
*
LegacyStateBatch
)
error
StoreOutputProposals
([]
*
OutputProposal
)
error
}
/**
...
...
@@ -79,39 +82,33 @@ func (db *blocksDB) StoreL1BlockHeaders(headers []*L1BlockHeader) error {
return
result
.
Error
}
func
(
db
*
blocksDB
)
StoreLegacyStateBatch
(
stateBatch
*
LegacyStateBatch
)
error
{
result
:=
db
.
gorm
.
Create
(
stateBatch
)
if
result
.
Error
!=
nil
{
return
result
.
Error
}
func
(
db
*
blocksDB
)
StoreLegacyStateBatches
(
stateBatches
[]
*
LegacyStateBatch
)
error
{
result
:=
db
.
gorm
.
Create
(
stateBatches
)
return
result
.
Error
}
// Mark this state batch index & l1 block hash for all applicable l2 blocks
l2Headers
:=
make
([]
*
L2BlockHeader
,
stateBatch
.
Size
)
func
(
db
*
blocksDB
)
StoreOutputProposals
(
outputs
[]
*
OutputProposal
)
error
{
result
:=
db
.
gorm
.
Create
(
outputs
)
return
result
.
Error
}
// [start, end] range is inclusive. Since `PrevTotal` is the index of the prior batch, no
// need to subtract one when adding the size
startHeight
:=
U256
{
Int
:
big
.
NewInt
(
int64
(
stateBatch
.
PrevTotal
+
1
))}
endHeight
:=
U256
{
Int
:
big
.
NewInt
(
int64
(
stateBatch
.
PrevTotal
+
stateBatch
.
Size
))}
result
=
db
.
gorm
.
Where
(
"number BETWEEN ? AND ?"
,
&
startHeight
,
&
endHeight
)
.
Find
(
&
l2Headers
)
func
(
db
*
blocksDB
)
LatestL1BlockHeader
()
(
*
L1BlockHeader
,
error
)
{
var
l1Header
L1BlockHeader
result
:=
db
.
gorm
.
Order
(
"number DESC"
)
.
Take
(
&
l1Header
)
if
result
.
Error
!=
nil
{
return
result
.
Error
}
else
if
result
.
RowsAffected
!=
int64
(
stateBatch
.
Size
)
{
return
errors
.
New
(
"state batch size exceeds number of indexed l2 blocks"
)
}
if
errors
.
Is
(
result
.
Error
,
gorm
.
ErrRecordNotFound
)
{
return
nil
,
nil
}
for
_
,
header
:=
range
l2Headers
{
header
.
LegacyStateBatchIndex
=
&
stateBatch
.
Index
header
.
L1BlockHash
=
&
stateBatch
.
L1BlockHash
return
nil
,
result
.
Error
}
result
=
db
.
gorm
.
Save
(
&
l2Headers
)
return
result
.
Error
return
&
l1Header
,
nil
}
// FinalizedL1BlockHeader returns the latest L1 block header stored in the database, nil otherwise
func
(
db
*
blocksDB
)
FinalizedL1BlockHeader
()
(
*
L1BlockHeader
,
error
)
{
var
l1Header
L1BlockHeader
result
:=
db
.
gorm
.
Order
(
"number DESC"
)
.
Take
(
&
l1Header
)
func
(
db
*
blocksDB
)
LatestOutputProposed
()
(
*
OutputProposal
,
error
)
{
var
outputProposal
OutputProposal
result
:=
db
.
gorm
.
Order
(
"l2_block_number DESC"
)
.
Take
(
&
outputProposal
)
if
result
.
Error
!=
nil
{
if
errors
.
Is
(
result
.
Error
,
gorm
.
ErrRecordNotFound
)
{
return
nil
,
nil
...
...
@@ -120,7 +117,7 @@ func (db *blocksDB) FinalizedL1BlockHeader() (*L1BlockHeader, error) {
return
nil
,
result
.
Error
}
return
&
l1Header
,
nil
return
&
outputProposal
,
nil
}
// L2
...
...
@@ -130,8 +127,7 @@ func (db *blocksDB) StoreL2BlockHeaders(headers []*L2BlockHeader) error {
return
result
.
Error
}
// FinalizedL2BlockHeader returns the latest L2 block header stored in the database, nil otherwise
func
(
db
*
blocksDB
)
FinalizedL2BlockHeader
()
(
*
L2BlockHeader
,
error
)
{
func
(
db
*
blocksDB
)
LatestL2BlockHeader
()
(
*
L2BlockHeader
,
error
)
{
var
l2Header
L2BlockHeader
result
:=
db
.
gorm
.
Order
(
"number DESC"
)
.
Take
(
&
l2Header
)
if
result
.
Error
!=
nil
{
...
...
@@ -145,19 +141,3 @@ func (db *blocksDB) FinalizedL2BlockHeader() (*L2BlockHeader, error) {
result
.
Logger
.
Info
(
context
.
Background
(),
"number "
,
l2Header
.
Number
)
return
&
l2Header
,
nil
}
// MarkFinalizedL1RootForL2Block updates the stored L2 block header with the L1 block
// that contains the output proposal for the L2 root.
func
(
db
*
blocksDB
)
MarkFinalizedL1RootForL2Block
(
l2Root
,
l1Root
common
.
Hash
)
error
{
var
l2Header
L2BlockHeader
l2Header
.
Hash
=
l2Root
// set the primary key
result
:=
db
.
gorm
.
First
(
&
l2Header
)
if
result
.
Error
!=
nil
{
return
result
.
Error
}
l2Header
.
L1BlockHash
=
&
l1Root
result
=
db
.
gorm
.
Save
(
&
l2Header
)
return
result
.
Error
}
indexer/migrations/20230523_create_schema.sql
View file @
1f372382
...
...
@@ -13,28 +13,11 @@ CREATE TABLE IF NOT EXISTS l1_block_headers (
timestamp
INTEGER
NOT
NULL
);
CREATE
TABLE
IF
NOT
EXISTS
legacy_state_batches
(
index
INTEGER
NOT
NULL
PRIMARY
KEY
,
root
VARCHAR
NOT
NULL
,
size
INTEGER
NOT
NULL
,
prev_total
INTEGER
NOT
NULL
,
-- Finalization information. Unlike `l2_block_headers` the NOT NULL
-- constraint is added since the l1 block hash will be known when
-- when reading the output event
l1_block_hash
VARCHAR
NOT
NULL
REFERENCES
l1_block_headers
(
hash
)
);
CREATE
TABLE
IF
NOT
EXISTS
l2_block_headers
(
-- Block header
hash
VARCHAR
NOT
NULL
PRIMARY
KEY
,
parent_hash
VARCHAR
NOT
NULL
,
number
UINT256
,
timestamp
INTEGER
NOT
NULL
,
-- Finalization information
l1_block_hash
VARCHAR
REFERENCES
l1_block_headers
(
hash
),
legacy_state_batch_index
INTEGER
REFERENCES
legacy_state_batches
(
index
)
hash
VARCHAR
NOT
NULL
PRIMARY
KEY
,
parent_hash
VARCHAR
NOT
NULL
,
number
UINT256
,
timestamp
INTEGER
NOT
NULL
);
/**
...
...
@@ -59,6 +42,24 @@ CREATE TABLE IF NOT EXISTS l2_contract_events (
timestamp
INTEGER
NOT
NULL
);
-- Tables that index finalization markers for L2 blocks.
CREATE
TABLE
IF
NOT
EXISTS
legacy_state_batches
(
index
INTEGER
NOT
NULL
PRIMARY
KEY
,
root
VARCHAR
NOT
NULL
,
size
INTEGER
NOT
NULL
,
prev_total
INTEGER
NOT
NULL
,
l1_contract_event_guid
VARCHAR
REFERENCES
l1_contract_events
(
guid
)
);
CREATE
TABLE
IF
NOT
EXISTS
output_proposals
(
output_root
VARCHAR
NOT
NULL
PRIMARY
KEY
,
l2_block_number
UINT256
,
l1_contract_event_guid
VARCHAR
REFERENCES
l1_contract_events
(
guid
)
);
/**
* BRIDGING DATA
*/
...
...
@@ -71,6 +72,7 @@ CREATE TABLE IF NOT EXISTS deposits (
-- Deposit information (do we need indexes on from/to?)
from_address
VARCHAR
NOT
NULL
,
to_address
VARCHAR
NOT
NULL
,
l1_token_address
VARCHAR
NOT
NULL
,
l2_token_address
VARCHAR
NOT
NULL
,
...
...
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