forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change balance and nonce in zero's tracer output from decimal to hex (#…
…1350) * Change balance and nonce in zero's tracer output from decimal to hex * test: add unit test for TxnTrace_MarshalJSON --------- Co-authored-by: Stefan Negovanović <[email protected]>
- Loading branch information
1 parent
2723db7
commit 6ff23b3
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/holiman/uint256" | ||
"github.com/ledgerwatch/erigon-lib/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTxnTrace_MarshalJSON(t *testing.T) { | ||
balance := uint256.NewInt(32) | ||
nonce := uint256.NewInt(64) | ||
storageRead := []common.Hash{common.HexToHash("0x1")} | ||
selfDestructed := false | ||
codeRead := common.HexToHash("0x6") | ||
|
||
trace := TxnTrace{ | ||
Balance: balance, | ||
Nonce: nonce, | ||
StorageRead: storageRead, | ||
StorageWritten: map[common.Hash]*uint256.Int{ | ||
common.HexToHash("0x7"): uint256.NewInt(7), | ||
common.HexToHash("0x8"): uint256.NewInt(8), | ||
common.HexToHash("0x9"): uint256.NewInt(9), | ||
}, | ||
CodeUsage: &ContractCodeUsage{ | ||
Read: &codeRead, | ||
}, | ||
SelfDestructed: &selfDestructed, | ||
} | ||
|
||
traceJSON, err := trace.MarshalJSON() | ||
require.NoError(t, err) | ||
|
||
// Parse the JSON output into a generic map and assert balance and nonce are hex encoded | ||
var genericTraceResult map[string]interface{} | ||
err = json.Unmarshal(traceJSON, &genericTraceResult) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, balance.Hex(), genericTraceResult["balance"]) | ||
require.Equal(t, nonce.Hex(), genericTraceResult["nonce"]) | ||
|
||
var resultTrace TxnTrace | ||
err = json.Unmarshal(traceJSON, &resultTrace) | ||
require.NoError(t, err) | ||
require.Equal(t, trace, resultTrace) | ||
} |