mirror of https://github.com/ethereum/go-ethereum
commit
8a17abc1f4
@ -0,0 +1 @@ |
||||
This test does some EVM execution, and can be used to test callframes emitted by the tracer when they are enabled. |
@ -0,0 +1,30 @@ |
||||
{ |
||||
"0x8a0a19589531694250d570040a0c4b74576919b8": { |
||||
"nonce": "0x00", |
||||
"balance": "0x0de0b6b3a7640000", |
||||
"code": "0x600060006000600060007310000000000000000000000000000000000000015af1600155600060006000600060007310000000000000000000000000000000000000025af16002553d600060003e600051600355", |
||||
"storage": { |
||||
"0x01": "0x0100", |
||||
"0x02": "0x0100", |
||||
"0x03": "0x0100" |
||||
} |
||||
}, |
||||
"0x1000000000000000000000000000000000000001": { |
||||
"nonce": "0x00", |
||||
"balance": "0x29a2241af62c0000", |
||||
"code": "0x6103e8ff", |
||||
"storage": {} |
||||
}, |
||||
"0x1000000000000000000000000000000000000002": { |
||||
"nonce": "0x00", |
||||
"balance": "0x4563918244f40000", |
||||
"code": "0x600060006000600060647310000000000000000000000000000000000000015af1600f0160005260206000fd", |
||||
"storage": {} |
||||
}, |
||||
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": { |
||||
"nonce": "0x00", |
||||
"balance": "0x6124fee993bc0000", |
||||
"code": "0x", |
||||
"storage": {} |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
{ |
||||
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", |
||||
"currentGasLimit": "71794957647893862", |
||||
"currentNumber": "1", |
||||
"currentTimestamp": "1000", |
||||
"currentRandom": "0", |
||||
"currentDifficulty": "0", |
||||
"blockHashes": {}, |
||||
"ommers": [], |
||||
"currentBaseFee": "7", |
||||
"parentUncleHash": "0x0000000000000000000000000000000000000000000000000000000000000000" |
||||
} |
@ -0,0 +1,61 @@ |
||||
{"from":"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b","to":"0x8a0a19589531694250d570040a0c4b74576919b8","gas":"0x74f18","value":"0x0","type":"CALL"} |
||||
{"pc":0,"op":96,"gas":"0x74f18","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":2,"op":96,"gas":"0x74f15","gasCost":"0x3","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":4,"op":96,"gas":"0x74f12","gasCost":"0x3","memSize":0,"stack":["0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":6,"op":96,"gas":"0x74f0f","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":8,"op":96,"gas":"0x74f0c","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":10,"op":115,"gas":"0x74f09","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH20"} |
||||
{"pc":31,"op":90,"gas":"0x74f06","gasCost":"0x2","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x0","0x1000000000000000000000000000000000000001"],"depth":1,"refund":0,"opName":"GAS"} |
||||
{"pc":32,"op":241,"gas":"0x74f04","gasCost":"0x731f1","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x0","0x1000000000000000000000000000000000000001","0x74f04"],"depth":1,"refund":0,"opName":"CALL"} |
||||
{"from":"0x8a0a19589531694250d570040a0c4b74576919b8","to":"0x1000000000000000000000000000000000000001","gas":"0x727c9","value":"0x0","type":"CALL"} |
||||
{"pc":0,"op":97,"gas":"0x727c9","gasCost":"0x3","memSize":0,"stack":[],"depth":2,"refund":0,"opName":"PUSH2"} |
||||
{"pc":3,"op":255,"gas":"0x727c6","gasCost":"0x7f58","memSize":0,"stack":["0x3e8"],"depth":2,"refund":0,"opName":"SELFDESTRUCT"} |
||||
{"from":"0x1000000000000000000000000000000000000001","to":"0x00000000000000000000000000000000000003e8","gas":"0x0","value":"0x29a2241af62c0000","type":"SELFDESTRUCT"} |
||||
{"output":"","gasUsed":"0x0"} |
||||
{"output":"","gasUsed":"0x7f5b"} |
||||
{"pc":33,"op":96,"gas":"0x6c581","gasCost":"0x3","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":35,"op":85,"gas":"0x6c57e","gasCost":"0x1388","memSize":0,"stack":["0x1","0x1"],"depth":1,"refund":0,"opName":"SSTORE"} |
||||
{"pc":36,"op":96,"gas":"0x6b1f6","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":38,"op":96,"gas":"0x6b1f3","gasCost":"0x3","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":40,"op":96,"gas":"0x6b1f0","gasCost":"0x3","memSize":0,"stack":["0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":42,"op":96,"gas":"0x6b1ed","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":44,"op":96,"gas":"0x6b1ea","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":46,"op":115,"gas":"0x6b1e7","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x0"],"depth":1,"refund":0,"opName":"PUSH20"} |
||||
{"pc":67,"op":90,"gas":"0x6b1e4","gasCost":"0x2","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x0","0x1000000000000000000000000000000000000002"],"depth":1,"refund":0,"opName":"GAS"} |
||||
{"pc":68,"op":241,"gas":"0x6b1e2","gasCost":"0x69744","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x0","0x1000000000000000000000000000000000000002","0x6b1e2"],"depth":1,"refund":0,"opName":"CALL"} |
||||
{"from":"0x8a0a19589531694250d570040a0c4b74576919b8","to":"0x1000000000000000000000000000000000000002","gas":"0x68d1c","value":"0x0","type":"CALL"} |
||||
{"pc":0,"op":96,"gas":"0x68d1c","gasCost":"0x3","memSize":0,"stack":[],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":2,"op":96,"gas":"0x68d19","gasCost":"0x3","memSize":0,"stack":["0x0"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":4,"op":96,"gas":"0x68d16","gasCost":"0x3","memSize":0,"stack":["0x0","0x0"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":6,"op":96,"gas":"0x68d13","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":8,"op":96,"gas":"0x68d10","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0","0x0"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":10,"op":115,"gas":"0x68d0d","gasCost":"0x3","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x64"],"depth":2,"refund":0,"opName":"PUSH20"} |
||||
{"pc":31,"op":90,"gas":"0x68d0a","gasCost":"0x2","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x64","0x1000000000000000000000000000000000000001"],"depth":2,"refund":0,"opName":"GAS"} |
||||
{"pc":32,"op":241,"gas":"0x68d08","gasCost":"0x67363","memSize":0,"stack":["0x0","0x0","0x0","0x0","0x64","0x1000000000000000000000000000000000000001","0x68d08"],"depth":2,"refund":0,"opName":"CALL"} |
||||
{"from":"0x1000000000000000000000000000000000000002","to":"0x1000000000000000000000000000000000000001","gas":"0x658d3","value":"0x64","type":"CALL"} |
||||
{"pc":0,"op":97,"gas":"0x658d3","gasCost":"0x3","memSize":0,"stack":[],"depth":3,"refund":0,"opName":"PUSH2"} |
||||
{"pc":3,"op":255,"gas":"0x658d0","gasCost":"0x1388","memSize":0,"stack":["0x3e8"],"depth":3,"refund":0,"opName":"SELFDESTRUCT"} |
||||
{"from":"0x1000000000000000000000000000000000000001","to":"0x00000000000000000000000000000000000003e8","gas":"0x0","value":"0x64","type":"SELFDESTRUCT"} |
||||
{"output":"","gasUsed":"0x0"} |
||||
{"output":"","gasUsed":"0x138b"} |
||||
{"pc":33,"op":96,"gas":"0x65eed","gasCost":"0x3","memSize":0,"stack":["0x1"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":35,"op":1,"gas":"0x65eea","gasCost":"0x3","memSize":0,"stack":["0x1","0xf"],"depth":2,"refund":0,"opName":"ADD"} |
||||
{"pc":36,"op":96,"gas":"0x65ee7","gasCost":"0x3","memSize":0,"stack":["0x10"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":38,"op":82,"gas":"0x65ee4","gasCost":"0x6","memSize":0,"stack":["0x10","0x0"],"depth":2,"refund":0,"opName":"MSTORE"} |
||||
{"pc":39,"op":96,"gas":"0x65ede","gasCost":"0x3","memSize":32,"stack":[],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":41,"op":96,"gas":"0x65edb","gasCost":"0x3","memSize":32,"stack":["0x20"],"depth":2,"refund":0,"opName":"PUSH1"} |
||||
{"pc":43,"op":253,"gas":"0x65ed8","gasCost":"0x0","memSize":32,"stack":["0x20","0x0"],"depth":2,"refund":0,"opName":"REVERT"} |
||||
{"pc":43,"op":253,"gas":"0x65ed8","gasCost":"0x0","memSize":32,"stack":[],"depth":2,"refund":0,"opName":"REVERT","error":"execution reverted"} |
||||
{"output":"0000000000000000000000000000000000000000000000000000000000000010","gasUsed":"0x2e44","error":"execution reverted"} |
||||
{"pc":69,"op":96,"gas":"0x67976","gasCost":"0x3","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"PUSH1"} |
||||
{"pc":71,"op":85,"gas":"0x67973","gasCost":"0x1388","memSize":0,"stack":["0x0","0x2"],"depth":1,"refund":4800,"opName":"SSTORE"} |
||||
{"pc":72,"op":61,"gas":"0x665eb","gasCost":"0x2","memSize":0,"stack":[],"depth":1,"refund":4800,"opName":"RETURNDATASIZE"} |
||||
{"pc":73,"op":96,"gas":"0x665e9","gasCost":"0x3","memSize":0,"stack":["0x20"],"depth":1,"refund":4800,"opName":"PUSH1"} |
||||
{"pc":75,"op":96,"gas":"0x665e6","gasCost":"0x3","memSize":0,"stack":["0x20","0x0"],"depth":1,"refund":4800,"opName":"PUSH1"} |
||||
{"pc":77,"op":62,"gas":"0x665e3","gasCost":"0x9","memSize":0,"stack":["0x20","0x0","0x0"],"depth":1,"refund":4800,"opName":"RETURNDATACOPY"} |
||||
{"pc":78,"op":96,"gas":"0x665da","gasCost":"0x3","memSize":32,"stack":[],"depth":1,"refund":4800,"opName":"PUSH1"} |
||||
{"pc":80,"op":81,"gas":"0x665d7","gasCost":"0x3","memSize":32,"stack":["0x0"],"depth":1,"refund":4800,"opName":"MLOAD"} |
||||
{"pc":81,"op":96,"gas":"0x665d4","gasCost":"0x3","memSize":32,"stack":["0x10"],"depth":1,"refund":4800,"opName":"PUSH1"} |
||||
{"pc":83,"op":85,"gas":"0x665d1","gasCost":"0x1388","memSize":32,"stack":["0x10","0x3"],"depth":1,"refund":4800,"opName":"SSTORE"} |
||||
{"pc":84,"op":0,"gas":"0x65249","gasCost":"0x0","memSize":32,"stack":[],"depth":1,"refund":4800,"opName":"STOP"} |
||||
{"output":"","gasUsed":"0xfccf"} |
@ -0,0 +1,17 @@ |
||||
[ |
||||
{ |
||||
"type": "0x0", |
||||
"chainId": "0x0", |
||||
"nonce": "0x0", |
||||
"gasPrice": "0xa", |
||||
"gas": "0x7a120", |
||||
"to": "0x8a0a19589531694250d570040a0c4b74576919b8", |
||||
"value": "0x0", |
||||
"input": "0x", |
||||
"v": "0x1c", |
||||
"r": "0x9a207ad45b7fc2aa5f8e72a30487f2b0bc489778e6d022f19036efdf2a922a17", |
||||
"s": "0x640d4da05078b5a4aa561f1b4d58176ea828bfa0f88d27d14459c1d789e1a1eb", |
||||
"sender": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", |
||||
"secretKey": "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8" |
||||
} |
||||
] |
@ -0,0 +1,69 @@ |
||||
# Changelog |
||||
|
||||
All notable changes to the tracing interface will be documented in this file. |
||||
|
||||
## [Unreleased] |
||||
|
||||
There has been a major breaking change in the tracing interface for custom native tracers. JS and built-in tracers are not affected by this change and tracing API methods may be used as before. This overhaul has been done as part of the new live tracing feature ([#29189](https://github.com/ethereum/go-ethereum/pull/29189)). To learn more about live tracing please refer to the [docs](https://geth.ethereum.org/docs/developers/evm-tracing/live-tracing). |
||||
|
||||
**The `EVMLogger` interface which the tracers implemented has been removed.** It has been replaced by a new struct `tracing.Hooks`. `Hooks` keeps pointers to event listening functions. Internally the EVM will use these function pointers to emit events and can skip an event if the tracer has opted not to implement it. In fact this is the main reason for this change of approach. Another benefit is the ease of adding new hooks in future, and dynamically assigning event receivers. |
||||
|
||||
The consequence of this change can be seen in the constructor of a tracer. Let's take the 4byte tracer as an example. Previously the constructor return an instance which satisfied the interface. Now it should return a pointer to `tracers.Tracer` (which is now also a struct as opposed to an interface) and explicitly assign the event listeners. As a side-benefit the tracers will not have to provide empty implementation of methods just to satisfy the interface: |
||||
|
||||
```go |
||||
func newFourByteTracer(ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) { |
||||
t := &fourByteTracer{ |
||||
ids: make(map[string]int), |
||||
} |
||||
return t, nil |
||||
|
||||
} |
||||
``` |
||||
|
||||
And now: |
||||
|
||||
```go |
||||
func newFourByteTracer(ctx *tracers.Context, _ json.RawMessage) (*tracers.Tracer, error) { |
||||
t := &fourByteTracer{ |
||||
ids: make(map[string]int), |
||||
} |
||||
return &tracers.Tracer{ |
||||
Hooks: &tracing.Hooks{ |
||||
OnTxStart: t.onTxStart, |
||||
OnEnter: t.onEnter, |
||||
}, |
||||
GetResult: t.getResult, |
||||
Stop: t.stop, |
||||
}, nil |
||||
} |
||||
``` |
||||
|
||||
### Event listeners |
||||
|
||||
If you have sharp eyes you might have noticed the new names for `OnTxStart` and `OnEnter`, previously called `CaptureTxStart` and `CaptureEnter`. Indeed there have been various modifications to the signatures of the event listeners. All method names now follow the `On*` pattern instead of `Capture*`. However the modifications are not limited to the names. |
||||
|
||||
#### New methods |
||||
|
||||
The live tracing feature was half about adding more observability into the state of the blockchain. As such there have been a host of method additions. Please consult the [Hooks](./hooks.go) struct for the full list of methods. Custom tracers which are invoked through the API (as opposed to "live" tracers) can benefit from the following new methods: |
||||
|
||||
- `OnGasChange(old, new uint64, reason GasChangeReason)`: This hook tracks the lifetime of gas within a transaction and its subcalls. It will first track the initial purchase of gas with ether, then the following consumptions and refunds of gas until at the end the rest is returned. |
||||
- `OnBalanceChange(addr common.Address, prev, new *big.Int, reason BalanceChangeReason)`: This hook tracks the balance changes of accounts. Where possible a reason is provided for the change (e.g. a transfer, gas purchase, withdrawal deposit etc). |
||||
- `OnNonceChange(addr common.Address, prev, new uint64)`: This hook tracks the nonce changes of accounts. |
||||
- `OnCodeChange(addr common.Address, prevCodeHash common.Hash, prevCode []byte, codeHash common.Hash, code []byte)`: This hook tracks the code changes of accounts. |
||||
- `OnStorageChange(addr common.Address, slot common.Hash, prev, new common.Hash)`: This hook tracks the storage changes of accounts. |
||||
- `OnLogChange(log *types.Log)`: This hook tracks the logs emitted by the EVM. |
||||
|
||||
#### Removed methods |
||||
|
||||
The hooks `CaptureStart` and `CaptureEnd` have been removed. These hooks signaled the top-level call frame of a transaction. The relevant info will be now emitted by `OnEnter` and `OnExit` which are emitted for every call frame. They now contain a `depth` parameter which can be used to distinguish the top-level call frame when necessary. The `create bool` parameter to `CaptureStart` can now be inferred from `typ byte` in `OnEnter`, i.e. `vm.OpCode(typ) == vm.CREATE`. |
||||
|
||||
#### Modified methods |
||||
|
||||
- `CaptureTxStart` -> `OnTxStart(vm *VMContext, tx *types.Transaction, from common.Address)`. It now emits the full transaction object as well as `from` which should be used to get the sender address. The `*VMContext` is a replacement for the `*vm.EVM` object previously passed to `CaptureStart`. |
||||
- `CaptureTxEnd` -> `OnTxEnd(receipt *types.Receipt, err error)`. It now returns the full receipt object. |
||||
- `CaptureEnter` -> `OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int)`. The new `depth int` parameter indicates the call stack depth. It is 0 for the top-level call. Furthermore, the location where `OnEnter` is called in the EVM is now made a soon as a call is started. This means some specific error cases that were not before calling `OnEnter/OnExit` will now do so, leading some transaction to have an extra call traced. |
||||
- `CaptureExit` -> `OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool)`. It has the new `depth` parameter, same as `OnEnter`. The new `reverted` parameter indicates whether the call frame was reverted. |
||||
- `CaptureState` -> `OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error)`. `op` is of type `byte` which can be cast to `vm.OpCode` when necessary. A `*vm.ScopeContext` is not passed anymore. It is replaced by `tracing.OpContext` which offers access to the memory, stack and current contract. |
||||
- `CaptureFault` -> `OnFault(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error)`. Similar to above. |
||||
|
||||
[unreleased]: https://github.com/ethereum/go-ethereum/compare/v1.13.14...master |
@ -0,0 +1,154 @@ |
||||
// Copyright 2024 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package snap |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
// Legacy sync progress definitions
|
||||
type legacyStorageTask struct { |
||||
Next common.Hash // Next account to sync in this interval
|
||||
Last common.Hash // Last account to sync in this interval
|
||||
} |
||||
|
||||
type legacyAccountTask struct { |
||||
Next common.Hash // Next account to sync in this interval
|
||||
Last common.Hash // Last account to sync in this interval
|
||||
SubTasks map[common.Hash][]*legacyStorageTask // Storage intervals needing fetching for large contracts
|
||||
} |
||||
|
||||
type legacyProgress struct { |
||||
Tasks []*legacyAccountTask // The suspended account tasks (contract tasks within)
|
||||
} |
||||
|
||||
func compareProgress(a legacyProgress, b SyncProgress) bool { |
||||
if len(a.Tasks) != len(b.Tasks) { |
||||
return false |
||||
} |
||||
for i := 0; i < len(a.Tasks); i++ { |
||||
if a.Tasks[i].Next != b.Tasks[i].Next { |
||||
return false |
||||
} |
||||
if a.Tasks[i].Last != b.Tasks[i].Last { |
||||
return false |
||||
} |
||||
// new fields are not checked here
|
||||
|
||||
if len(a.Tasks[i].SubTasks) != len(b.Tasks[i].SubTasks) { |
||||
return false |
||||
} |
||||
for addrHash, subTasksA := range a.Tasks[i].SubTasks { |
||||
subTasksB, ok := b.Tasks[i].SubTasks[addrHash] |
||||
if !ok || len(subTasksB) != len(subTasksA) { |
||||
return false |
||||
} |
||||
for j := 0; j < len(subTasksA); j++ { |
||||
if subTasksA[j].Next != subTasksB[j].Next { |
||||
return false |
||||
} |
||||
if subTasksA[j].Last != subTasksB[j].Last { |
||||
return false |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
func makeLegacyProgress() legacyProgress { |
||||
return legacyProgress{ |
||||
Tasks: []*legacyAccountTask{ |
||||
{ |
||||
Next: common.Hash{}, |
||||
Last: common.Hash{0x77}, |
||||
SubTasks: map[common.Hash][]*legacyStorageTask{ |
||||
common.Hash{0x1}: { |
||||
{ |
||||
Next: common.Hash{}, |
||||
Last: common.Hash{0xff}, |
||||
}, |
||||
}, |
||||
}, |
||||
}, |
||||
{ |
||||
Next: common.Hash{0x88}, |
||||
Last: common.Hash{0xff}, |
||||
}, |
||||
}, |
||||
} |
||||
} |
||||
|
||||
func convertLegacy(legacy legacyProgress) SyncProgress { |
||||
var progress SyncProgress |
||||
for i, task := range legacy.Tasks { |
||||
subTasks := make(map[common.Hash][]*storageTask) |
||||
for owner, list := range task.SubTasks { |
||||
var cpy []*storageTask |
||||
for i := 0; i < len(list); i++ { |
||||
cpy = append(cpy, &storageTask{ |
||||
Next: list[i].Next, |
||||
Last: list[i].Last, |
||||
}) |
||||
} |
||||
subTasks[owner] = cpy |
||||
} |
||||
accountTask := &accountTask{ |
||||
Next: task.Next, |
||||
Last: task.Last, |
||||
SubTasks: subTasks, |
||||
} |
||||
if i == 0 { |
||||
accountTask.StorageCompleted = []common.Hash{{0xaa}, {0xbb}} // fulfill new fields
|
||||
} |
||||
progress.Tasks = append(progress.Tasks, accountTask) |
||||
} |
||||
return progress |
||||
} |
||||
|
||||
func TestSyncProgressCompatibility(t *testing.T) { |
||||
// Decode serialized bytes of legacy progress, backward compatibility
|
||||
legacy := makeLegacyProgress() |
||||
blob, err := json.Marshal(legacy) |
||||
if err != nil { |
||||
t.Fatalf("Failed to marshal progress %v", err) |
||||
} |
||||
var dec SyncProgress |
||||
if err := json.Unmarshal(blob, &dec); err != nil { |
||||
t.Fatalf("Failed to unmarshal progress %v", err) |
||||
} |
||||
if !compareProgress(legacy, dec) { |
||||
t.Fatal("sync progress is not backward compatible") |
||||
} |
||||
|
||||
// Decode serialized bytes of new format progress
|
||||
progress := convertLegacy(legacy) |
||||
blob, err = json.Marshal(progress) |
||||
if err != nil { |
||||
t.Fatalf("Failed to marshal progress %v", err) |
||||
} |
||||
var legacyDec legacyProgress |
||||
if err := json.Unmarshal(blob, &legacyDec); err != nil { |
||||
t.Fatalf("Failed to unmarshal progress %v", err) |
||||
} |
||||
if !compareProgress(legacyDec, progress) { |
||||
t.Fatal("sync progress is not forward compatible") |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue