@@ -41,7 +41,7 @@ type erigonDB struct {
41
41
}
42
42
43
43
type erigonWorkingSetStore struct {
44
- tsw erigonstate. StateWriter
44
+ db * erigonDB
45
45
intraBlockState * erigonstate.IntraBlockState
46
46
tx kv.Tx
47
47
}
@@ -72,14 +72,14 @@ func (db *erigonDB) Stop(ctx context.Context) {
72
72
}
73
73
74
74
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 )
76
76
if err != nil {
77
77
return nil , err
78
78
}
79
- r , tsw := erigonstate .NewPlainStateReader (tx ), erigonstate . NewPlainStateWriter ( tx , tx , height )
79
+ r := erigonstate .NewPlainStateReader (tx )
80
80
intraBlockState := erigonstate .New (r )
81
81
return & erigonWorkingSetStore {
82
- tsw : tsw ,
82
+ db : db ,
83
83
tx : tx ,
84
84
intraBlockState : intraBlockState ,
85
85
}, nil
@@ -93,7 +93,7 @@ func (db *erigonDB) newErigonStoreDryrun(ctx context.Context, height uint64) (*e
93
93
tsw := erigonstate .NewPlainState (tx , height , nil )
94
94
intraBlockState := erigonstate .New (tsw )
95
95
return & erigonWorkingSetStore {
96
- tsw : tsw ,
96
+ db : db ,
97
97
tx : tx ,
98
98
intraBlockState : intraBlockState ,
99
99
}, nil
@@ -120,6 +120,10 @@ func (store *erigonWorkingSetStore) FinalizeTx(ctx context.Context) error {
120
120
}
121
121
122
122
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 {
123
127
blkCtx := protocol .MustGetBlockCtx (ctx )
124
128
height := blkCtx .BlockHeight
125
129
ts := blkCtx .BlockTimeStamp .Unix ()
@@ -131,40 +135,45 @@ func (store *erigonWorkingSetStore) Finalize(ctx context.Context) error {
131
135
132
136
chainRules := chainCfg .Rules (big .NewInt (int64 (height )), g .IsSumatra (height ), uint64 (ts ))
133
137
rules := evm .NewErigonRules (& chainRules )
138
+ tsw := erigonstate .NewPlainStateWriter (tx , tx , height )
134
139
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 )
136
141
if err != nil {
137
142
return err
138
143
}
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 )))
140
145
// store.intraBlockState.Print(*rules)
141
146
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
152
151
}
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
159
160
}
160
161
return nil
161
162
}
162
163
163
164
func (store * erigonWorkingSetStore ) Commit (ctx context.Context ) error {
164
165
defer store .tx .Rollback ()
165
- err := store .tx . Commit ( )
166
+ tx , err := store .db . rw . BeginRw ( ctx )
166
167
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" )
168
177
}
169
178
return nil
170
179
}
0 commit comments