@@ -23,7 +23,6 @@ import (
23
23
"os"
24
24
"time"
25
25
26
- "github.com/holiman/uint256"
27
26
"github.com/ledgerwatch/erigon/common"
28
27
"github.com/ledgerwatch/erigon/common/mclock"
29
28
"github.com/ledgerwatch/erigon/common/u256"
@@ -106,6 +105,12 @@ func ExecuteBlockEphemerally(
106
105
gp := new (GasPool )
107
106
gp .AddGas (block .GasLimit ())
108
107
108
+ if ! vmConfig .ReadOnly {
109
+ if err := InitializeBlockExecution (engine , block .Header (), block .Transactions (), block .Uncles (), stateWriter , chainConfig , ibs ); err != nil {
110
+ return nil , err
111
+ }
112
+ }
113
+
109
114
if chainConfig .DAOForkSupport && chainConfig .DAOForkBlock != nil && chainConfig .DAOForkBlock .Cmp (block .Number ()) == 0 {
110
115
misc .ApplyDAOHardFork (ibs )
111
116
}
@@ -167,6 +172,52 @@ func ExecuteBlockEphemerally(
167
172
return receipts , nil
168
173
}
169
174
175
+ // SystemAddress - sender address for internal state updates.
176
+ var SystemAddress = common .HexToAddress ("0xfffffffffffffffffffffffffffffffffffffffe" )
177
+
178
+ func SysCallContract (contract common.Address , data []byte , chainConfig params.ChainConfig , ibs * state.IntraBlockState , header * types.Header , engine consensus.Engine ) (result []byte , err error ) {
179
+ gp := new (GasPool )
180
+ gp .AddGas (50_000_000 )
181
+ var gasUsed uint64
182
+
183
+ if chainConfig .DAOForkSupport && chainConfig .DAOForkBlock != nil && chainConfig .DAOForkBlock .Cmp (header .Number ) == 0 {
184
+ misc .ApplyDAOHardFork (ibs )
185
+ }
186
+
187
+ noop := state .NewNoopWriter ()
188
+ tx , err := SysCallContractTx (contract , data , ibs )
189
+ if err != nil {
190
+ return nil , fmt .Errorf ("SysCallContract: %w " , err )
191
+ }
192
+ // Set infinite balance to the fake caller account.
193
+ fmt .Printf ("call contract: %d,%x,%x\n " , header .Number .Uint64 (), contract , data )
194
+
195
+ vmConfig := vm.Config {NoReceipts : true , Debug : true , Tracer : vm .NewStructLogger (& vm.LogConfig {})}
196
+ _ , result , err = ApplyTransaction (& chainConfig , nil , engine , & SystemAddress , gp , ibs , noop , header , tx , & gasUsed , vmConfig , nil )
197
+ if err != nil {
198
+ return result , fmt .Errorf ("SysCallContract: %w " , err )
199
+ }
200
+ ibs .SetNonce (SystemAddress , 0 )
201
+
202
+ //w, err1 := os.Create(fmt.Sprintf("txtrace_before.json"))
203
+ //if err1 != nil {
204
+ // panic(err1)
205
+ //}
206
+ //encoder := json.NewEncoder(w)
207
+ //logs := FormatLogs(vmConfig.Tracer.(*vm.StructLogger).StructLogs())
208
+ //if err2 := encoder.Encode(logs); err2 != nil {
209
+ // panic(err2)
210
+ //}
211
+ return result , nil
212
+ }
213
+
214
+ // from the null sender, with 50M gas.
215
+ func SysCallContractTx (contract common.Address , data []byte , ibs * state.IntraBlockState ) (tx types.Transaction , err error ) {
216
+ //nonce := ibs.GetNonce(SystemAddress)
217
+ tx = types .NewTransaction (0 , contract , u256 .Num0 , 50_000_000 , u256 .Num0 , data )
218
+ return tx .FakeSign (SystemAddress )
219
+ }
220
+
170
221
func CallContract (contract common.Address , data []byte , chainConfig params.ChainConfig , ibs * state.IntraBlockState , header * types.Header , engine consensus.Engine ) (result []byte , err error ) {
171
222
gp := new (GasPool )
172
223
gp .AddGas (50_000_000 )
@@ -178,24 +229,33 @@ func CallContract(contract common.Address, data []byte, chainConfig params.Chain
178
229
noop := state .NewNoopWriter ()
179
230
tx , err := CallContractTx (contract , data , ibs )
180
231
if err != nil {
181
- return nil , fmt .Errorf ("CallContract : %w " , err )
232
+ return nil , fmt .Errorf ("SysCallContract : %w " , err )
182
233
}
183
234
// Set infinite balance to the fake caller account.
184
- from := ibs .GetOrNewStateObject (common.Address {})
185
- from .SetBalance (uint256 .NewInt (0 ).SetAllOne ())
235
+ fmt .Printf ("call contract: %d,%x,%x\n " , header .Number .Uint64 (), contract , data )
186
236
187
- _ , result , err = ApplyTransaction (& chainConfig , nil , engine , nil , gp , ibs , noop , header , tx , & gasUsed , vm.Config {}, nil )
237
+ vmConfig := vm.Config {NoReceipts : true , Debug : true , Tracer : vm .NewStructLogger (& vm.LogConfig {})}
238
+ _ , result , err = ApplyTransaction (& chainConfig , nil , engine , & SystemAddress , gp , ibs , noop , header , tx , & gasUsed , vmConfig , nil )
188
239
if err != nil {
189
- return result , fmt .Errorf ("CallContract : %w " , err )
240
+ return result , fmt .Errorf ("SysCallContract : %w " , err )
190
241
}
242
+ //w, err1 := os.Create(fmt.Sprintf("txtrace_before.json"))
243
+ //if err1 != nil {
244
+ // panic(err1)
245
+ //}
246
+ //encoder := json.NewEncoder(w)
247
+ //logs := FormatLogs(vmConfig.Tracer.(*vm.StructLogger).StructLogs())
248
+ //if err2 := encoder.Encode(logs); err2 != nil {
249
+ // panic(err2)
250
+ //}
191
251
return result , nil
192
252
}
193
253
194
254
// from the null sender, with 50M gas.
195
255
func CallContractTx (contract common.Address , data []byte , ibs * state.IntraBlockState ) (tx types.Transaction , err error ) {
196
- var from common.Address
256
+ from := common.Address {}
197
257
nonce := ibs .GetNonce (from )
198
- tx = types .NewTransaction (nonce , contract , u256 .Num0 , 50_000_000 , u256 .Num1 , data )
258
+ tx = types .NewTransaction (nonce , contract , u256 .Num0 , 50_000_000 , u256 .Num0 , data )
199
259
return tx .FakeSign (from )
200
260
}
201
261
@@ -213,3 +273,12 @@ func FinalizeBlockExecution(engine consensus.Engine, header *types.Header, txs t
213
273
}
214
274
return nil
215
275
}
276
+
277
+ func InitializeBlockExecution (engine consensus.Engine , header * types.Header , txs types.Transactions , uncles []* types.Header , stateWriter state.WriterWithChangeSets , cc * params.ChainConfig , ibs * state.IntraBlockState ) error {
278
+ // Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
279
+ engine .Initialize (cc , header , ibs , txs , uncles , func (contract common.Address , data []byte ) ([]byte , error ) {
280
+ return SysCallContract (contract , data , * cc , ibs , header , engine )
281
+ })
282
+
283
+ return nil
284
+ }
0 commit comments