-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new job to find out orphan block (#1265)
* Add model for unsynced blocks * Add the function for detecting the orphan blocks * Add new flag for modifying the confidence * Store the orphan blocks
- Loading branch information
Showing
6 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package blocks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/filecoin-project/lily/metrics" | ||
"github.com/filecoin-project/lily/model" | ||
"github.com/filecoin-project/lotus/chain/types" | ||
"go.opencensus.io/tag" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
) | ||
|
||
type UnsyncedBlockHeader struct { | ||
Height int64 `pg:",pk,use_zero,notnull"` | ||
Cid string `pg:",pk,notnull"` | ||
Miner string `pg:",notnull"` | ||
ParentWeight string `pg:",notnull"` | ||
ParentBaseFee string `pg:",notnull"` | ||
ParentStateRoot string `pg:",notnull"` | ||
|
||
WinCount int64 `pg:",use_zero"` | ||
Timestamp uint64 `pg:",use_zero"` | ||
ForkSignaling uint64 `pg:",use_zero"` | ||
IsOrphan bool `pg:",notnull"` | ||
} | ||
|
||
func NewUnsyncedBlockHeader(bh *types.BlockHeader) *UnsyncedBlockHeader { | ||
return &UnsyncedBlockHeader{ | ||
Cid: bh.Cid().String(), | ||
Miner: bh.Miner.String(), | ||
ParentWeight: bh.ParentWeight.String(), | ||
ParentBaseFee: bh.ParentBaseFee.String(), | ||
ParentStateRoot: bh.ParentStateRoot.String(), | ||
Height: int64(bh.Height), | ||
WinCount: bh.ElectionProof.WinCount, | ||
Timestamp: bh.Timestamp, | ||
ForkSignaling: bh.ForkSignaling, | ||
IsOrphan: false, | ||
} | ||
} | ||
|
||
func (bh *UnsyncedBlockHeader) Persist(ctx context.Context, s model.StorageBatch, _ model.Version) error { | ||
ctx, _ = tag.New(ctx, tag.Upsert(metrics.Table, "unsynced_block_headers")) | ||
metrics.RecordCount(ctx, metrics.PersistModel, 1) | ||
return s.PersistModel(ctx, bh) | ||
} | ||
|
||
type UnsyncedBlockHeaders []*UnsyncedBlockHeader | ||
|
||
func (bhl UnsyncedBlockHeaders) Persist(ctx context.Context, s model.StorageBatch, _ model.Version) error { | ||
if len(bhl) == 0 { | ||
return nil | ||
} | ||
ctx, span := otel.Tracer("").Start(ctx, "UnsyncedBlockHeaders.Persist") | ||
if span.IsRecording() { | ||
span.SetAttributes(attribute.Int("count", len(bhl))) | ||
} | ||
defer span.End() | ||
|
||
ctx, _ = tag.New(ctx, tag.Upsert(metrics.Table, "block_headers")) | ||
metrics.RecordCount(ctx, metrics.PersistModel, len(bhl)) | ||
return s.PersistModel(ctx, bhl) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package v1 | ||
|
||
func init() { | ||
patches.Register( | ||
34, | ||
` | ||
CREATE TABLE IF NOT EXISTS {{ .SchemaName | default "public"}}.unsynced_block_headers ( | ||
height BIGINT NOT NULL, | ||
cid TEXT NOT NULL, | ||
miner TEXT, | ||
parent_weight TEXT, | ||
parent_base_fee TEXT, | ||
parent_state_root TEXT, | ||
win_count BIGINT, | ||
"timestamp" BIGINT, | ||
fork_signaling BIGINT, | ||
is_orphan BOOLEAN, | ||
PRIMARY KEY(height, cid) | ||
); | ||
CREATE INDEX IF NOT EXISTS unsynced_block_headers_height_idx ON {{ .SchemaName | default "public"}}.unsynced_block_headers USING btree (height DESC); | ||
CREATE INDEX IF NOT EXISTS unsynced_block_headers_timestamp_idx ON {{ .SchemaName | default "public"}}.unsynced_block_headers USING btree ("timestamp"); | ||
CREATE INDEX IF NOT EXISTS unsynced_block_headers_miner_idx ON {{ .SchemaName | default "public"}}.unsynced_block_headers USING hash (miner); | ||
`, | ||
) | ||
} |