github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/vnt/api_tracer.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vnt 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "fmt" 24 "io/ioutil" 25 "runtime" 26 "sync" 27 "time" 28 29 "github.com/vntchain/go-vnt/common" 30 "github.com/vntchain/go-vnt/common/hexutil" 31 "github.com/vntchain/go-vnt/core" 32 "github.com/vntchain/go-vnt/core/rawdb" 33 "github.com/vntchain/go-vnt/core/state" 34 "github.com/vntchain/go-vnt/core/types" 35 "github.com/vntchain/go-vnt/core/vm" 36 "github.com/vntchain/go-vnt/core/wavm" 37 "github.com/vntchain/go-vnt/internal/vntapi" 38 "github.com/vntchain/go-vnt/log" 39 "github.com/vntchain/go-vnt/rlp" 40 "github.com/vntchain/go-vnt/rpc" 41 "github.com/vntchain/go-vnt/trie" 42 ) 43 44 const ( 45 // defaultTraceTimeout is the amount of time a single transaction can execute 46 // by default before being forcefully aborted. 47 defaultTraceTimeout = 5 * time.Second 48 49 // defaultTraceReexec is the number of blocks the tracer is willing to go back 50 // and reexecute to produce missing historical state necessary to run a specific 51 // trace. 52 defaultTraceReexec = uint64(128) 53 ) 54 55 // TraceConfig holds extra parameters to trace functions. 56 type TraceConfig struct { 57 *vm.LogConfig 58 Tracer *string 59 Timeout *string 60 Reexec *uint64 61 } 62 63 // txTraceResult is the result of a single transaction trace. 64 type txTraceResult struct { 65 Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer 66 Error string `json:"error,omitempty"` // Trace failure produced by the tracer 67 } 68 69 // blockTraceTask represents a single block trace task when an entire chain is 70 // being traced. 71 type blockTraceTask struct { 72 statedb *state.StateDB // Intermediate state prepped for tracing 73 block *types.Block // Block to trace the transactions from 74 rootref common.Hash // Trie root reference held for this task 75 results []*txTraceResult // Trace results procudes by the task 76 } 77 78 // blockTraceResult represets the results of tracing a single block when an entire 79 // chain is being traced. 80 type blockTraceResult struct { 81 Block hexutil.Uint64 `json:"block"` // Block number corresponding to this trace 82 Hash common.Hash `json:"hash"` // Block hash corresponding to this trace 83 Traces []*txTraceResult `json:"traces"` // Trace results produced by the task 84 } 85 86 // txTraceTask represents a single transaction trace task when an entire block 87 // is being traced. 88 type txTraceTask struct { 89 statedb *state.StateDB // Intermediate state prepped for tracing 90 index int // Transaction offset in the block 91 } 92 93 // TraceChain returns the structured logs created during the execution of WAVM 94 // between two blocks (excluding start) and returns them as a JSON object. 95 func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end rpc.BlockNumber, config *TraceConfig) (*rpc.Subscription, error) { 96 // Fetch the block interval that we want to trace 97 var from, to *types.Block 98 99 switch start { 100 case rpc.PendingBlockNumber: 101 from = api.vnt.producer.PendingBlock() 102 case rpc.LatestBlockNumber: 103 from = api.vnt.blockchain.CurrentBlock() 104 default: 105 from = api.vnt.blockchain.GetBlockByNumber(uint64(start)) 106 } 107 switch end { 108 case rpc.PendingBlockNumber: 109 to = api.vnt.producer.PendingBlock() 110 case rpc.LatestBlockNumber: 111 to = api.vnt.blockchain.CurrentBlock() 112 default: 113 to = api.vnt.blockchain.GetBlockByNumber(uint64(end)) 114 } 115 // Trace the chain if we've found all our blocks 116 if from == nil { 117 return nil, fmt.Errorf("starting block #%d not found", start) 118 } 119 if to == nil { 120 return nil, fmt.Errorf("end block #%d not found", end) 121 } 122 return api.traceChain(ctx, from, to, config) 123 } 124 125 // traceChain configures a new tracer according to the provided configuration, and 126 // executes all the transactions contained within. The return value will be one item 127 // per transaction, dependent on the requestd tracer. 128 func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Block, config *TraceConfig) (*rpc.Subscription, error) { 129 // Tracing a chain is a **long** operation, only do with subscriptions 130 notifier, supported := rpc.NotifierFromContext(ctx) 131 if !supported { 132 return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported 133 } 134 sub := notifier.CreateSubscription() 135 136 // Ensure we have a valid starting state before doing any work 137 origin := start.NumberU64() 138 database := state.NewDatabase(api.vnt.ChainDb()) 139 140 if number := start.NumberU64(); number > 0 { 141 start = api.vnt.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1) 142 if start == nil { 143 return nil, fmt.Errorf("parent block #%d not found", number-1) 144 } 145 } 146 statedb, err := state.New(start.Root(), database) 147 if err != nil { 148 // If the starting state is missing, allow some number of blocks to be reexecuted 149 reexec := defaultTraceReexec 150 if config != nil && config.Reexec != nil { 151 reexec = *config.Reexec 152 } 153 // Find the most recent block that has the state available 154 for i := uint64(0); i < reexec; i++ { 155 start = api.vnt.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1) 156 if start == nil { 157 break 158 } 159 if statedb, err = state.New(start.Root(), database); err == nil { 160 break 161 } 162 } 163 // If we still don't have the state available, bail out 164 if err != nil { 165 switch err.(type) { 166 case *trie.MissingNodeError: 167 return nil, errors.New("required historical state unavailable") 168 default: 169 return nil, err 170 } 171 } 172 } 173 // Execute all the transaction contained within the chain concurrently for each block 174 blocks := int(end.NumberU64() - origin) 175 176 threads := runtime.NumCPU() 177 if threads > blocks { 178 threads = blocks 179 } 180 var ( 181 pend = new(sync.WaitGroup) 182 tasks = make(chan *blockTraceTask, threads) 183 results = make(chan *blockTraceTask, threads) 184 ) 185 for th := 0; th < threads; th++ { 186 pend.Add(1) 187 go func() { 188 defer pend.Done() 189 190 // Fetch and execute the next block trace tasks 191 for task := range tasks { 192 signer := types.MakeSigner(api.config, task.block.Number()) 193 194 // Trace all the transactions contained within 195 for i, tx := range task.block.Transactions() { 196 msg, _ := tx.AsMessage(signer) 197 vmctx := core.NewVMContext(msg, task.block.Header(), api.vnt.blockchain, nil) 198 199 res, err := api.traceTx(ctx, msg, vmctx, task.statedb, config) 200 if err != nil { 201 task.results[i] = &txTraceResult{Error: err.Error()} 202 log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err) 203 break 204 } 205 task.statedb.Finalise(true) 206 task.results[i] = &txTraceResult{Result: res} 207 } 208 // Stream the result back to the user or abort on teardown 209 select { 210 case results <- task: 211 case <-notifier.Closed(): 212 return 213 } 214 } 215 }() 216 } 217 // Start a goroutine to feed all the blocks into the tracers 218 begin := time.Now() 219 220 go func() { 221 var ( 222 logged time.Time 223 number uint64 224 traced uint64 225 failed error 226 proot common.Hash 227 ) 228 // Ensure everything is properly cleaned up on any exit path 229 defer func() { 230 close(tasks) 231 pend.Wait() 232 233 switch { 234 case failed != nil: 235 log.Warn("Chain tracing failed", "start", start.NumberU64(), "end", end.NumberU64(), "transactions", traced, "elapsed", time.Since(begin), "err", failed) 236 case number < end.NumberU64(): 237 log.Warn("Chain tracing aborted", "start", start.NumberU64(), "end", end.NumberU64(), "abort", number, "transactions", traced, "elapsed", time.Since(begin)) 238 default: 239 log.Info("Chain tracing finished", "start", start.NumberU64(), "end", end.NumberU64(), "transactions", traced, "elapsed", time.Since(begin)) 240 } 241 close(results) 242 }() 243 // Feed all the blocks both into the tracer, as well as fast process concurrently 244 for number = start.NumberU64() + 1; number <= end.NumberU64(); number++ { 245 // Stop tracing if interruption was requested 246 select { 247 case <-notifier.Closed(): 248 return 249 default: 250 } 251 // Print progress logs if long enough time elapsed 252 if time.Since(logged) > 8*time.Second { 253 if number > origin { 254 nodes, imgs := database.TrieDB().Size() 255 log.Info("Tracing chain segment", "start", origin, "end", end.NumberU64(), "current", number, "transactions", traced, "elapsed", time.Since(begin), "memory", nodes+imgs) 256 } else { 257 log.Info("Preparing state for chain trace", "block", number, "start", origin, "elapsed", time.Since(begin)) 258 } 259 logged = time.Now() 260 } 261 // Retrieve the next block to trace 262 block := api.vnt.blockchain.GetBlockByNumber(number) 263 if block == nil { 264 failed = fmt.Errorf("block #%d not found", number) 265 break 266 } 267 // Send the block over to the concurrent tracers (if not in the fast-forward phase) 268 if number > origin { 269 txs := block.Transactions() 270 271 select { 272 case tasks <- &blockTraceTask{statedb: statedb.Copy(), block: block, rootref: proot, results: make([]*txTraceResult, len(txs))}: 273 case <-notifier.Closed(): 274 return 275 } 276 traced += uint64(len(txs)) 277 } 278 // Generate the next state snapshot fast without tracing 279 _, _, _, err := api.vnt.blockchain.Processor().Process(block, statedb, vm.Config{}) 280 if err != nil { 281 failed = err 282 break 283 } 284 // Finalize the state so any modifications are written to the trie 285 root, err := statedb.Commit(true) 286 if err != nil { 287 failed = err 288 break 289 } 290 if err := statedb.Reset(root); err != nil { 291 failed = err 292 break 293 } 294 // Reference the trie twice, once for us, once for the trancer 295 database.TrieDB().Reference(root, common.Hash{}) 296 if number >= origin { 297 database.TrieDB().Reference(root, common.Hash{}) 298 } 299 // Dereference all past tries we ourselves are done working with 300 database.TrieDB().Dereference(proot, common.Hash{}) 301 proot = root 302 303 // TODO(karalabe): Do we need the preimages? Won't they accumulate too much? 304 } 305 }() 306 307 // Keep reading the trace results and stream the to the user 308 go func() { 309 var ( 310 done = make(map[uint64]*blockTraceResult) 311 next = origin + 1 312 ) 313 for res := range results { 314 // Queue up next received result 315 result := &blockTraceResult{ 316 Block: hexutil.Uint64(res.block.NumberU64()), 317 Hash: res.block.Hash(), 318 Traces: res.results, 319 } 320 done[uint64(result.Block)] = result 321 322 // Dereference any paret tries held in memory by this task 323 database.TrieDB().Dereference(res.rootref, common.Hash{}) 324 325 // Stream completed traces to the user, aborting on the first error 326 for result, ok := done[next]; ok; result, ok = done[next] { 327 if len(result.Traces) > 0 || next == end.NumberU64() { 328 notifier.Notify(sub.ID, result) 329 } 330 delete(done, next) 331 next++ 332 } 333 } 334 }() 335 return sub, nil 336 } 337 338 // TraceBlockByNumber returns the structured logs created during the execution of 339 // WAVM and returns them as a JSON object. 340 func (api *PrivateDebugAPI) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *TraceConfig) ([]*txTraceResult, error) { 341 // Fetch the block that we want to trace 342 var block *types.Block 343 344 switch number { 345 case rpc.PendingBlockNumber: 346 block = api.vnt.producer.PendingBlock() 347 case rpc.LatestBlockNumber: 348 block = api.vnt.blockchain.CurrentBlock() 349 default: 350 block = api.vnt.blockchain.GetBlockByNumber(uint64(number)) 351 } 352 // Trace the block if it was found 353 if block == nil { 354 return nil, fmt.Errorf("block #%d not found", number) 355 } 356 return api.traceBlock(ctx, block, config) 357 } 358 359 // TraceBlockByHash returns the structured logs created during the execution of 360 // WAVM and returns them as a JSON object. 361 func (api *PrivateDebugAPI) TraceBlockByHash(ctx context.Context, hash common.Hash, config *TraceConfig) ([]*txTraceResult, error) { 362 block := api.vnt.blockchain.GetBlockByHash(hash) 363 if block == nil { 364 return nil, fmt.Errorf("block #%x not found", hash) 365 } 366 return api.traceBlock(ctx, block, config) 367 } 368 369 // TraceBlock returns the structured logs created during the execution of WAVM 370 // and returns them as a JSON object. 371 func (api *PrivateDebugAPI) TraceBlock(ctx context.Context, blob []byte, config *TraceConfig) ([]*txTraceResult, error) { 372 block := new(types.Block) 373 if err := rlp.Decode(bytes.NewReader(blob), block); err != nil { 374 return nil, fmt.Errorf("could not decode block: %v", err) 375 } 376 return api.traceBlock(ctx, block, config) 377 } 378 379 // TraceBlockFromFile returns the structured logs created during the execution of 380 // WAVM and returns them as a JSON object. 381 func (api *PrivateDebugAPI) TraceBlockFromFile(ctx context.Context, file string, config *TraceConfig) ([]*txTraceResult, error) { 382 blob, err := ioutil.ReadFile(file) 383 if err != nil { 384 return nil, fmt.Errorf("could not read file: %v", err) 385 } 386 return api.TraceBlock(ctx, blob, config) 387 } 388 389 // traceBlock configures a new tracer according to the provided configuration, and 390 // executes all the transactions contained within. The return value will be one item 391 // per transaction, dependent on the requestd tracer. 392 func (api *PrivateDebugAPI) traceBlock(ctx context.Context, block *types.Block, config *TraceConfig) ([]*txTraceResult, error) { 393 // Create the parent state database 394 if err := api.vnt.engine.VerifyHeader(api.vnt.blockchain, block.Header(), true); err != nil { 395 return nil, err 396 } 397 parent := api.vnt.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) 398 if parent == nil { 399 return nil, fmt.Errorf("parent %x not found", block.ParentHash()) 400 } 401 reexec := defaultTraceReexec 402 if config != nil && config.Reexec != nil { 403 reexec = *config.Reexec 404 } 405 statedb, err := api.computeStateDB(parent, reexec) 406 if err != nil { 407 return nil, err 408 } 409 // Execute all the transaction contained within the block concurrently 410 var ( 411 signer = types.MakeSigner(api.config, block.Number()) 412 413 txs = block.Transactions() 414 results = make([]*txTraceResult, len(txs)) 415 416 pend = new(sync.WaitGroup) 417 jobs = make(chan *txTraceTask, len(txs)) 418 ) 419 threads := runtime.NumCPU() 420 if threads > len(txs) { 421 threads = len(txs) 422 } 423 for th := 0; th < threads; th++ { 424 pend.Add(1) 425 go func() { 426 defer pend.Done() 427 428 // Fetch and execute the next transaction trace tasks 429 for task := range jobs { 430 msg, _ := txs[task.index].AsMessage(signer) 431 vmctx := core.NewVMContext(msg, block.Header(), api.vnt.blockchain, nil) 432 433 res, err := api.traceTx(ctx, msg, vmctx, task.statedb, config) 434 if err != nil { 435 results[task.index] = &txTraceResult{Error: err.Error()} 436 continue 437 } 438 results[task.index] = &txTraceResult{Result: res} 439 } 440 }() 441 } 442 // Feed the transactions into the tracers and return 443 var failed error 444 for i, tx := range txs { 445 // Send the trace task over for execution 446 jobs <- &txTraceTask{statedb: statedb.Copy(), index: i} 447 448 // Generate the next state snapshot fast without tracing 449 msg, _ := tx.AsMessage(signer) 450 vmctx := core.NewVMContext(msg, block.Header(), api.vnt.blockchain, nil) 451 vmenv := core.GetVM(msg, vmctx, statedb, api.config, vm.Config{}) 452 if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { 453 failed = err 454 break 455 } 456 // Finalize the state so any modifications are written to the trie 457 statedb.Finalise(true) 458 } 459 close(jobs) 460 pend.Wait() 461 462 // If execution failed in between, abort 463 if failed != nil { 464 return nil, failed 465 } 466 return results, nil 467 } 468 469 // computeStateDB retrieves the state database associated with a certain block. 470 // If no state is locally available for the given block, a number of blocks are 471 // attempted to be reexecuted to generate the desired state. 472 func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*state.StateDB, error) { 473 // If we have the state fully available, use that 474 statedb, err := api.vnt.blockchain.StateAt(block.Root()) 475 if err == nil { 476 return statedb, nil 477 } 478 // Otherwise try to reexec blocks until we find a state or reach our limit 479 origin := block.NumberU64() 480 database := state.NewDatabase(api.vnt.ChainDb()) 481 482 for i := uint64(0); i < reexec; i++ { 483 block = api.vnt.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) 484 if block == nil { 485 break 486 } 487 if statedb, err = state.New(block.Root(), database); err == nil { 488 break 489 } 490 } 491 if err != nil { 492 switch err.(type) { 493 case *trie.MissingNodeError: 494 return nil, errors.New("required historical state unavailable") 495 default: 496 return nil, err 497 } 498 } 499 // State was available at historical point, regenerate 500 var ( 501 start = time.Now() 502 logged time.Time 503 proot common.Hash 504 ) 505 for block.NumberU64() < origin { 506 // Print progress logs if long enough time elapsed 507 if time.Since(logged) > 8*time.Second { 508 log.Info("Regenerating historical state", "block", block.NumberU64()+1, "target", origin, "elapsed", time.Since(start)) 509 logged = time.Now() 510 } 511 // Retrieve the next block to regenerate and process it 512 if block = api.vnt.blockchain.GetBlockByNumber(block.NumberU64() + 1); block == nil { 513 return nil, fmt.Errorf("block #%d not found", block.NumberU64()+1) 514 } 515 _, _, _, err := api.vnt.blockchain.Processor().Process(block, statedb, vm.Config{}) 516 if err != nil { 517 return nil, err 518 } 519 // Finalize the state so any modifications are written to the trie 520 root, err := statedb.Commit(true) 521 if err != nil { 522 return nil, err 523 } 524 if err := statedb.Reset(root); err != nil { 525 return nil, err 526 } 527 database.TrieDB().Reference(root, common.Hash{}) 528 database.TrieDB().Dereference(proot, common.Hash{}) 529 proot = root 530 } 531 nodes, imgs := database.TrieDB().Size() 532 log.Info("Historical state regenerated", "block", block.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs) 533 return statedb, nil 534 } 535 536 // TraceTransaction returns the structured logs created during the execution of WAVM 537 // and returns them as a JSON object. 538 func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) { 539 // Retrieve the transaction and assemble its WAVM context 540 tx, blockHash, _, index := rawdb.ReadTransaction(api.vnt.ChainDb(), hash) 541 if tx == nil { 542 return nil, fmt.Errorf("transaction %x not found", hash) 543 } 544 reexec := defaultTraceReexec 545 if config != nil && config.Reexec != nil { 546 reexec = *config.Reexec 547 } 548 msg, vmctx, statedb, err := api.computeTxEnv(blockHash, int(index), reexec) 549 if err != nil { 550 return nil, err 551 } 552 // Trace the transaction and return 553 return api.traceTx(ctx, msg, vmctx, statedb, config) 554 } 555 556 // traceTx configures a new tracer according to the provided configuration, and 557 // executes the given message in the provided environment. The return value will 558 // be tracer dependent. 559 func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, vmctx vm.Context, statedb *state.StateDB, config *TraceConfig) (interface{}, error) { 560 // Assemble the structured logger or the JavaScript tracer 561 var ( 562 tracer vm.Tracer 563 err error 564 ) 565 switch { 566 case config == nil: 567 tracer = wavm.NewWasmLogger(nil) 568 569 default: 570 tracer = wavm.NewWasmLogger(config.LogConfig) 571 } 572 // Run the transaction with tracing enabled. 573 vmenv := core.GetVM(message, vmctx, statedb, api.config, vm.Config{Debug: true, Tracer: tracer}) 574 575 ret, gas, failed, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) 576 if err != nil { 577 return nil, fmt.Errorf("tracing failed: %v", err) 578 } 579 // Depending on the tracer type, format and return the output 580 switch tracer := tracer.(type) { 581 case *wavm.WasmLogger: 582 slogs, dlogs := vntapi.FormatLogs(tracer.StructLogs(), tracer.DebugLogs()) 583 return &vntapi.ExecutionResult{ 584 Gas: gas, 585 Failed: failed, 586 ReturnValue: fmt.Sprintf("%x", ret), 587 StructLogs: slogs, 588 DebugLogs: dlogs, 589 }, nil 590 default: 591 panic(fmt.Sprintf("bad tracer type %T", tracer)) 592 } 593 } 594 595 // computeTxEnv returns the execution environment of a certain transaction. 596 func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, reexec uint64) (core.Message, vm.Context, *state.StateDB, error) { 597 // Create the parent state database 598 block := api.vnt.blockchain.GetBlockByHash(blockHash) 599 if block == nil { 600 return nil, vm.Context{}, nil, fmt.Errorf("block %x not found", blockHash) 601 } 602 parent := api.vnt.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) 603 if parent == nil { 604 return nil, vm.Context{}, nil, fmt.Errorf("parent %x not found", block.ParentHash()) 605 } 606 statedb, err := api.computeStateDB(parent, reexec) 607 if err != nil { 608 return nil, vm.Context{}, nil, err 609 } 610 // Recompute transactions up to the target index. 611 signer := types.MakeSigner(api.config, block.Number()) 612 613 for idx, tx := range block.Transactions() { 614 // Assemble the transaction call message and return if the requested offset 615 msg, _ := tx.AsMessage(signer) 616 context := core.NewVMContext(msg, block.Header(), api.vnt.blockchain, nil) 617 if idx == txIndex { 618 return msg, context, statedb, nil 619 } 620 // Not yet the searched for transaction, execute on top of the current state 621 vmenv := core.GetVM(msg, context, statedb, api.config, vm.Config{}) 622 if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil { 623 return nil, vm.Context{}, nil, fmt.Errorf("tx %x failed: %v", tx.Hash(), err) 624 } 625 // Ensure any modifications are committed to the state 626 statedb.Finalise(true) 627 } 628 return nil, vm.Context{}, nil, fmt.Errorf("tx index %d out of range for block %x", txIndex, blockHash) 629 }