Skip to content

Commit e43c724

Browse files
envestccCoderZhi
andauthored
delay create erigon rw tx to commiting (#4653)
Co-authored-by: CoderZhi <thecoderzhi@gmail.com>
1 parent 0aa34fd commit e43c724

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

state/factory/workingsetstore_erigon.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type erigonDB struct {
4141
}
4242

4343
type erigonWorkingSetStore struct {
44-
tsw erigonstate.StateWriter
44+
db *erigonDB
4545
intraBlockState *erigonstate.IntraBlockState
4646
tx kv.Tx
4747
}
@@ -72,14 +72,14 @@ func (db *erigonDB) Stop(ctx context.Context) {
7272
}
7373

7474
func (db *erigonDB) newErigonStore(ctx context.Context, height uint64) (*erigonWorkingSetStore, error) {
75-
tx, err := db.rw.BeginRw(ctx)
75+
tx, err := db.rw.BeginRo(ctx)
7676
if err != nil {
7777
return nil, err
7878
}
79-
r, tsw := erigonstate.NewPlainStateReader(tx), erigonstate.NewPlainStateWriter(tx, tx, height)
79+
r := erigonstate.NewPlainStateReader(tx)
8080
intraBlockState := erigonstate.New(r)
8181
return &erigonWorkingSetStore{
82-
tsw: tsw,
82+
db: db,
8383
tx: tx,
8484
intraBlockState: intraBlockState,
8585
}, nil
@@ -93,7 +93,7 @@ func (db *erigonDB) newErigonStoreDryrun(ctx context.Context, height uint64) (*e
9393
tsw := erigonstate.NewPlainState(tx, height, nil)
9494
intraBlockState := erigonstate.New(tsw)
9595
return &erigonWorkingSetStore{
96-
tsw: tsw,
96+
db: db,
9797
tx: tx,
9898
intraBlockState: intraBlockState,
9999
}, nil
@@ -120,6 +120,10 @@ func (store *erigonWorkingSetStore) FinalizeTx(ctx context.Context) error {
120120
}
121121

122122
func (store *erigonWorkingSetStore) Finalize(ctx context.Context) error {
123+
return nil
124+
}
125+
126+
func (store *erigonWorkingSetStore) prepareCommit(ctx context.Context, tx kv.RwTx) error {
123127
blkCtx := protocol.MustGetBlockCtx(ctx)
124128
height := blkCtx.BlockHeight
125129
ts := blkCtx.BlockTimeStamp.Unix()
@@ -131,40 +135,45 @@ func (store *erigonWorkingSetStore) Finalize(ctx context.Context) error {
131135

132136
chainRules := chainCfg.Rules(big.NewInt(int64(height)), g.IsSumatra(height), uint64(ts))
133137
rules := evm.NewErigonRules(&chainRules)
138+
tsw := erigonstate.NewPlainStateWriter(tx, tx, height)
134139
log.L().Debug("intraBlockState Commit block", zap.Uint64("height", height))
135-
err = store.intraBlockState.CommitBlock(rules, store.tsw)
140+
err = store.intraBlockState.CommitBlock(rules, tsw)
136141
if err != nil {
137142
return err
138143
}
139-
log.L().Debug("erigon store finalize", zap.Uint64("height", height), zap.String("tsw", fmt.Sprintf("%+T", store.tsw)))
144+
log.L().Debug("erigon store finalize", zap.Uint64("height", height), zap.String("tsw", fmt.Sprintf("%T", tsw)))
140145
// store.intraBlockState.Print(*rules)
141146

142-
if c, ok := store.tsw.(erigonstate.WriterWithChangeSets); ok {
143-
log.L().Debug("erigon store write changesets", zap.Uint64("height", height))
144-
err = c.WriteChangeSets()
145-
if err != nil {
146-
return err
147-
}
148-
err = c.WriteHistory()
149-
if err != nil {
150-
return err
151-
}
147+
log.L().Debug("erigon store write changesets", zap.Uint64("height", height))
148+
err = tsw.WriteChangeSets()
149+
if err != nil {
150+
return err
152151
}
153-
if tx, ok := store.tx.(kv.RwTx); ok {
154-
log.L().Debug("erigon store commit tx", zap.Uint64("height", height))
155-
err = tx.Put(systemNS, heightKey, uint256.NewInt(height).Bytes())
156-
if err != nil {
157-
return err
158-
}
152+
err = tsw.WriteHistory()
153+
if err != nil {
154+
return err
155+
}
156+
log.L().Debug("erigon store commit tx", zap.Uint64("height", height))
157+
err = tx.Put(systemNS, heightKey, uint256.NewInt(height).Bytes())
158+
if err != nil {
159+
return err
159160
}
160161
return nil
161162
}
162163

163164
func (store *erigonWorkingSetStore) Commit(ctx context.Context) error {
164165
defer store.tx.Rollback()
165-
err := store.tx.Commit()
166+
tx, err := store.db.rw.BeginRw(ctx)
166167
if err != nil {
167-
return err
168+
return errors.Wrap(err, "failed to begin erigon working set store transaction")
169+
}
170+
defer tx.Rollback()
171+
172+
if err = store.prepareCommit(ctx, tx); err != nil {
173+
return errors.Wrap(err, "failed to prepare erigon working set store commit")
174+
}
175+
if err = tx.Commit(); err != nil {
176+
return errors.Wrap(err, "failed to commit erigon working set store transaction")
168177
}
169178
return nil
170179
}

0 commit comments

Comments
 (0)