From b50e63620497d66d8f3427a8113072c17bebd398 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Thu, 28 Sep 2023 18:01:51 +0800 Subject: [PATCH] Set state node `diff` field to false for snapshots --- indexer/database/sql/indexer.go | 20 +++++++++++++++++--- indexer/database/sql/writer.go | 12 +++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/indexer/database/sql/indexer.go b/indexer/database/sql/indexer.go index 6dab963..d34f5b0 100644 --- a/indexer/database/sql/indexer.go +++ b/indexer/database/sql/indexer.go @@ -43,7 +43,7 @@ import ( var _ interfaces.StateDiffIndexer = &StateDiffIndexer{} -// StateDiffIndexer satisfies the indexer.StateDiffIndexer interface for ethereum statediff objects on top of an SQL sql +// StateDiffIndexer satisfies the indexer.StateDiffIndexer interface for ethereum statediff objects on top of an SQL DB. type StateDiffIndexer struct { ctx context.Context chainConfig *params.ChainConfig @@ -51,11 +51,25 @@ type StateDiffIndexer struct { } // NewStateDiffIndexer creates a sql implementation of interfaces.StateDiffIndexer -func NewStateDiffIndexer(ctx context.Context, chainConfig *params.ChainConfig, db Database) (*StateDiffIndexer, error) { +func NewStateDiffIndexer( + ctx context.Context, chainConfig *params.ChainConfig, db Database, +) (*StateDiffIndexer, error) { return &StateDiffIndexer{ ctx: ctx, chainConfig: chainConfig, - dbWriter: NewWriter(db), + dbWriter: NewWriter(db, true), + }, nil +} + +// NewStateDiffIndexer creates a sql implementation of interfaces.StateDiffIndexer for writing full +// snapshots (meaning inserted nodes have the `diff` field set to false). +func NewSnapshotIndexer( + ctx context.Context, chainConfig *params.ChainConfig, db Database, +) (*StateDiffIndexer, error) { + return &StateDiffIndexer{ + ctx: ctx, + chainConfig: chainConfig, + dbWriter: NewWriter(db, false), }, nil } diff --git a/indexer/database/sql/writer.go b/indexer/database/sql/writer.go index e5e0c81..10f8b43 100644 --- a/indexer/database/sql/writer.go +++ b/indexer/database/sql/writer.go @@ -34,13 +34,15 @@ import ( // Writer handles processing and writing of indexed IPLD objects to Postgres type Writer struct { - db Database + db Database + isDiff bool } -// NewWriter creates a new pointer to a Writer -func NewWriter(db Database) *Writer { +// NewWriter creates a new pointer to a Writer. `diff` indicates whether this is part of an +// incremental diff (as opposed to a snapshot). +func NewWriter(db Database, diff bool) *Writer { return &Writer{ - db: db, + db: db, isDiff: diff, } } @@ -318,7 +320,7 @@ func (w *Writer) upsertStateCID(tx Tx, stateNode models.StateNodeModel) error { stateNode.HeaderID, stateNode.StateKey, stateNode.CID, - true, + w.isDiff, bal, stateNode.Nonce, stateNode.CodeHash,