* Write state diff to CSV (#2)
* port statediff from 9b7fd9af80/statediff/statediff.go; minor fixes
* integrating state diff extracting, building, and persisting into geth processes
* work towards persisting created statediffs in ipfs; based off github.com/vulcanize/eth-block-extractor
* Add a state diff service
* Remove diff extractor from blockchain
* Update imports
* Move statediff on/off check to geth cmd config
* Update starting state diff service
* Add debugging logs for creating diff
* Add statediff extractor and builder tests and small refactoring
* Start to write statediff to a CSV
* Restructure statediff directory
* Pull CSV publishing methods into their own file
* Reformatting due to go fmt
* Add gomega to vendor dir
* Remove testing focuses
* Update statediff tests to use golang test pkg
instead of ginkgo
- builder_test
- extractor_test
- publisher_test
* Use hexutil.Encode instead of deprecated common.ToHex
* Remove OldValue from DiffBigInt and DiffUint64 fields
* Update builder test
* Remove old storage value from updated accounts
* Remove old values from created/deleted accounts
* Update publisher to account for only storing current account values
* Update service loop and fetching previous block
* Update testing
- remove statediff ginkgo test suite file
- move mocks to their own dir
* Updates per go fmt
* Updates to tests
* Pass statediff mode and path in through cli
* Return filename from publisher
* Remove some duplication in builder
* Remove code field from state diff output
this is the contract byte code, and it can still be obtained by querying
the db by the codeHash
* Consolidate acct diff structs for updated & updated/deleted accts
* Include block number in csv filename
* Clean up error logging
* Cleanup formatting, spelling, etc
* Address PR comments
* Add contract address and storage value to csv
* Refactor accumulating account row in csv publisher
* Add DiffStorage struct
* Add storage key to csv
* Address PR comments
* Fix publisher to include rows for accounts that don't have store updates
* Update builder test after merging in release/1.8
* Update test contract to include storage on contract intialization
- so that we're able to test that storage diffing works for created and
deleted accounts (not just updated accounts).
* Factor out a common trie iterator method in builder
* Apply goimports to statediff
* Apply gosimple changes to statediff
* Gracefully exit geth command(#4)
* Statediff for full node (#6)
* Open a trie from the in-memory database
* Use a node's LeafKey as an identifier instead of the address
It was proving difficult to find look the address up from a given path
with a full node (sometimes the value wouldn't exist in the disk db).
So, instead, for now we are using the node's LeafKey with is a Keccak256
hash of the address, so if we know the address we can figure out which
LeafKey it matches up to.
* Make sure that statediff has been processed before pruning
* Use blockchain stateCache.OpenTrie for storage diffs
* Clean up log lines and remove unnecessary fields from builder
* Apply go fmt changes
* Add a sleep to the blockchain test
* refactoring/reorganizing packages
* refactoring statediff builder and types and adjusted to relay proofs and paths (still need to make this optional)
* refactoring state diff service and adding api which allows for streaming state diff payloads over an rpc websocket subscription
* make proofs and paths optional + compress service loop into single for loop (may be missing something here)
* option to process intermediate nodes
* make state diff rlp serializable
* cli parameter to limit statediffing to select account addresses + test
* review fixes and fixes for issues ran into in integration
* review fixes; proper method signature for api; adjust service so that statediff processing is halted/paused until there is at least one subscriber listening for the results
* adjust buffering to improve stability; doc.go; fix notifier
err handling
* relay receipts with the rest of the data + review fixes/changes
* rpc method to get statediff at specific block; requires archival node or the block be within the pruning range
* fix linter issues
* include total difficulty to the payload
* fix state diff builder: emit actual leaf nodes instead of value nodes; diff on the leaf not on the value; emit correct path for intermediate nodes
* adjust statediff builder tests to changes and extend to test intermediate nodes; golint
* add genesis block to test; handle block 0 in StateDiffAt
* rlp files for mainnet blocks 0-3, for tests
* builder test on mainnet blocks
* common.BytesToHash(path) => crypto.Keaccak256(hash) in builder; BytesToHash produces same hash for e.g. []byte{} and []byte{\x00} - prefix \x00 steps are inconsequential to the hash result
* complete tests for early mainnet blocks
* diff type for representing deleted accounts
* fix builder so that we handle account deletions properly and properly diff storage when an account is moved to a new path; update params
* remove cli params; moving them to subscriber defined
* remove unneeded bc methods
* update service and api; statediffing params are now defined by user through api rather than by service provider by cli
* update top level tests
* add ability to watch specific storage slots (leaf keys) only
* comments; explain logic
* update mainnet blocks test
* update api_test.go
* storage leafkey filter test
* cleanup chain maker
* adjust chain maker for tests to add an empty account in block1 and switch to EIP-158 afterwards (now we just need to generate enough accounts until one causes the empty account to be touched and removed post-EIP-158 so we can simulate and test that process...); also added 2 new blocks where more contract storage is set and old slots are set to zero so they are removed so we can test that
* found an account whose creation causes the empty account to be moved to a new path; this should count as 'touching; the empty account and cause it to be removed according to eip-158... but it doesn't
* use new contract in unit tests that has self-destruct ability, so we can test eip-158 since simply moving an account to new path doesn't count as 'touchin' it
* handle storage deletions
* tests for eip-158 account removal and storage value deletions; there is one edge case left to test where we remove 1 account when only two exist such that the remaining account is moved up and replaces the root branch node
* finish testing known edge cases
* add endpoint to fetch all state and storage nodes at a given blockheight; useful for generating a recent atate cache/snapshot that we can diff forward from rather than needing to collect all diffs from genesis
* test for state trie builder
* if statediffing is on, lock tries in triedb until the statediffing service signals they are done using them
* fix mock blockchain; golint; bump patch
* increase maxRequestContentLength; bump patch
* log the sizes of the state objects we are sending
* CI build (#20)
* CI: run build on PR and on push to master
* CI: debug building geth
* CI: fix coping file
* CI: fix coping file v2
* CI: temporary upload file to release asset
* CI: get release upload_url by tag, upload asset to current relase
* CI: fix tag name
* fix ci build on statediff_at_anyblock-1.9.11 branch
* fix publishing assets in release
* use context deadline for timeout in eth_call
* collect and emit codehash=>code mappings for state objects
* subscription endpoint for retrieving all the codehash=>code mappings that exist at provided height
* Implement WriteStateDiffAt
* Writes state diffs directly to postgres
* Adds CLI flags to configure PG
* Refactors builder output with callbacks
* Copies refactored postgres handling code from ipld-eth-indexer
* rename PostgresCIDWriter.{index->upsert}*
* go.mod update
* rm unused
* cleanup
* output code & codehash iteratively
* had to rf some types for this
* prometheus metrics output
* duplicate recent eth-indexer changes
* migrations and metrics...
* [wip] prom.Init() here? another CLI flag?
* tidy & DRY
* statediff WriteLoop service + CLI flag
* [wip] update test mocks
* todo - do something meaningful to test write loop
* logging
* use geth log
* port tests to go testing
* drop ginkgo/gomega
* fix and cleanup tests
* fail before defer statement
* delete vendor/ dir
* fixes after rebase onto 1.9.23
* fix API registration
* use golang 1.15.5 version (#34)
* bump version meta; add 0.0.11 branch to actions
* bump version meta; update github actions workflows
* statediff: refactor metrics
* Remove redundant statediff/indexer/prom tooling and use existing
prometheus integration.
* "indexer" namespace for metrics
* add reporting loop for db metrics
* doc
* metrics for statediff stats
* metrics namespace/subsystem = statediff/{indexer,service}
* statediff: use a worker pool (for direct writes)
* fix test
* fix chain event subscription
* log tweaks
* func name
* unused import
* intermediate chain event channel for metrics
* update github actions; linting
* add poststate and status to receipt ipld indexes
* stateDiffFor endpoints for fetching or writing statediff object by blockhash; bump statediff version
* fixes after rebase on to v1.10.1
* update github actions and version meta; go fmt
* add leaf key to removed 'nodes'
* include Postgres migrations and schema
* service documentation
* touching up
update github actions after rebase
fix connection leak (misplaced defer) and perform proper rollback on errs
improve error logging; handle PushBlock internal err
* build docker image and publish it to Docker Hub on release
* add access list tx to unit tests
* MarshalBinary and UnmarshalBinary methods for receipt
* fix error caused by 2718 by using MarshalBinary instead of EncodeRLP methods
* ipld encoding/decoding tests
* update TxModel; add AccessListElementModel
* index tx type and access lists
* add access list metrics
* unit tests for tx_type and access list table
* unit tests for receipt marshal/unmarshal binary methods
* improve documentation of the encoding methods
* fix issue identified in linting
506 lines
14 KiB
Go
506 lines
14 KiB
Go
package ipld
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
block "github.com/ipfs/go-block-format"
|
|
"github.com/ipfs/go-cid"
|
|
node "github.com/ipfs/go-ipld-format"
|
|
"github.com/multiformats/go-multihash"
|
|
)
|
|
|
|
/*
|
|
EthBlock
|
|
*/
|
|
|
|
func TestTxTriesInBlockBodyJSONParsing(t *testing.T) {
|
|
// HINT: 306 txs
|
|
// cat test_data/eth-block-body-json-4139497 | jsontool | grep transactionIndex | wc -l
|
|
// or, https://etherscan.io/block/4139497
|
|
fi, err := os.Open("test_data/eth-block-body-json-4139497")
|
|
checkError(err, t)
|
|
|
|
_, _, output, err := FromBlockJSON(fi)
|
|
checkError(err, t)
|
|
if len(output) != 331 {
|
|
t.Fatalf("Wrong number of obtained tx trie nodes\r\nexpected %d\r\n got %d", 331, len(output))
|
|
}
|
|
}
|
|
|
|
/*
|
|
OUTPUT
|
|
*/
|
|
|
|
func TestTxTrieDecodeExtension(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
|
|
if ethTxTrie.nodeKind != "extension" {
|
|
t.Fatalf("Wrong nodeKind\r\nexpected %s\r\ngot %s", "extension", ethTxTrie.nodeKind)
|
|
}
|
|
|
|
if len(ethTxTrie.elements) != 2 {
|
|
t.Fatalf("Wrong number of elements for an extension node\r\nexpected %d\r\ngot %d", 2, len(ethTxTrie.elements))
|
|
}
|
|
|
|
if fmt.Sprintf("%x", ethTxTrie.elements[0].([]byte)) != "0001" {
|
|
t.Fatalf("Wrong key\r\nexpected %s\r\ngot %s", "0001", fmt.Sprintf("%x", ethTxTrie.elements[0].([]byte)))
|
|
}
|
|
|
|
if ethTxTrie.elements[1].(cid.Cid).String() !=
|
|
"bagjacgzak6wdjvshdtb7lrvlteweyd7f5qjr3dmzmh7g2xpi4xrwoujsio2a" {
|
|
t.Fatalf("Wrong CID\r\nexpected %s\r\ngot %s", "bagjacgzak6wdjvshdtb7lrvlteweyd7f5qjr3dmzmh7g2xpi4xrwoujsio2a", ethTxTrie.elements[1].(cid.Cid).String())
|
|
}
|
|
}
|
|
|
|
func TestTxTrieDecodeLeaf(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieLeaf(t)
|
|
|
|
if ethTxTrie.nodeKind != "leaf" {
|
|
t.Fatalf("Wrong nodeKind\r\nexpected %s\r\ngot %s", "leaf", ethTxTrie.nodeKind)
|
|
}
|
|
|
|
if len(ethTxTrie.elements) != 2 {
|
|
t.Fatalf("Wrong number of elements for a leaf node\r\nexpected %d\r\ngot %d", 2, len(ethTxTrie.elements))
|
|
}
|
|
|
|
if fmt.Sprintf("%x", ethTxTrie.elements[0].([]byte)) != "" {
|
|
t.Fatalf("Wrong key\r\nexpected %s\r\ngot %s", "", fmt.Sprintf("%x", ethTxTrie.elements[0].([]byte)))
|
|
}
|
|
|
|
if _, ok := ethTxTrie.elements[1].(*EthTx); !ok {
|
|
t.Fatal("Expected element to be an EthTx")
|
|
}
|
|
|
|
if ethTxTrie.elements[1].(*EthTx).String() !=
|
|
"<EthereumTx bagjqcgzaqsbvff5xrqh5lobxmhuharvkqdc4jmsqfalsu2xs4pbyix7dvfzq>" {
|
|
t.Fatalf("Wrong String()\r\nexpected %s\r\ngot %s", "<EthereumTx bagjqcgzaqsbvff5xrqh5lobxmhuharvkqdc4jmsqfalsu2xs4pbyix7dvfzq>", ethTxTrie.elements[1].(*EthTx).String())
|
|
}
|
|
}
|
|
|
|
func TestTxTrieDecodeBranch(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieBranch(t)
|
|
|
|
if ethTxTrie.nodeKind != "branch" {
|
|
t.Fatalf("Wrong nodeKind\r\nexpected %s\r\ngot %s", "branch", ethTxTrie.nodeKind)
|
|
}
|
|
|
|
if len(ethTxTrie.elements) != 17 {
|
|
t.Fatalf("Wrong number of elements for a branch node\r\nexpected %d\r\ngot %d", 17, len(ethTxTrie.elements))
|
|
}
|
|
|
|
for i, element := range ethTxTrie.elements {
|
|
switch {
|
|
case i < 9:
|
|
if _, ok := element.(cid.Cid); !ok {
|
|
t.Fatal("Expected element to be a cid")
|
|
}
|
|
continue
|
|
default:
|
|
if element != nil {
|
|
t.Fatal("Expected element to be a nil")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
Block INTERFACE
|
|
*/
|
|
|
|
func TestEthTxTrieBlockElements(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
|
|
if fmt.Sprintf("%x", ethTxTrie.RawData())[:10] != "e4820001a0" {
|
|
t.Fatalf("Wrong Data\r\nexpected %s\r\ngot %s", "e4820001a0", fmt.Sprintf("%x", ethTxTrie.RawData())[:10])
|
|
}
|
|
|
|
if ethTxTrie.Cid().String() !=
|
|
"bagjacgzaw6ccgrfc3qnrl6joodbjjiet4haufnt2xww725luwgfhijnmg36q" {
|
|
t.Fatalf("Wrong Cid\r\nexpected %s\r\ngot %s", "bagjacgzaw6ccgrfc3qnrl6joodbjjiet4haufnt2xww725luwgfhijnmg36q", ethTxTrie.Cid().String())
|
|
}
|
|
}
|
|
|
|
func TestEthTxTrieString(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
|
|
if ethTxTrie.String() != "<EthereumTxTrie bagjacgzaw6ccgrfc3qnrl6joodbjjiet4haufnt2xww725luwgfhijnmg36q>" {
|
|
t.Fatalf("Wrong String()\r\nexpected %s\r\ngot %s", "<EthereumTxTrie bagjacgzaw6ccgrfc3qnrl6joodbjjiet4haufnt2xww725luwgfhijnmg36q>", ethTxTrie.String())
|
|
}
|
|
}
|
|
|
|
func TestEthTxTrieLoggable(t *testing.T) {
|
|
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
l := ethTxTrie.Loggable()
|
|
if _, ok := l["type"]; !ok {
|
|
t.Fatal("Loggable map expected the field 'type'")
|
|
}
|
|
|
|
if l["type"] != "eth-tx-trie" {
|
|
t.Fatalf("Wrong Loggable 'type' value\r\nexpected %s\r\ngot %s", "eth-tx-trie", l["type"])
|
|
}
|
|
}
|
|
|
|
/*
|
|
Node INTERFACE
|
|
*/
|
|
|
|
func TestTxTrieResolveExtension(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
|
|
_ = ethTxTrie
|
|
}
|
|
|
|
func TestTxTrieResolveLeaf(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieLeaf(t)
|
|
|
|
_ = ethTxTrie
|
|
}
|
|
|
|
func TestTxTrieResolveBranch(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieBranch(t)
|
|
|
|
indexes := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
|
|
|
|
for j, index := range indexes {
|
|
obj, rest, err := ethTxTrie.Resolve([]string{index, "nonce"})
|
|
|
|
switch {
|
|
case j < 9:
|
|
_, ok := obj.(*node.Link)
|
|
if !ok {
|
|
t.Fatalf("Returned object is not a link (index: %d)", j)
|
|
}
|
|
|
|
if rest[0] != "nonce" {
|
|
t.Fatalf("Wrong rest of the path returned\r\nexpected %s\r\ngot %s", "nonce", rest[0])
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatal("Error should be nil")
|
|
}
|
|
|
|
default:
|
|
if obj != nil {
|
|
t.Fatalf("Returned object should have been nil")
|
|
}
|
|
|
|
if rest != nil {
|
|
t.Fatalf("Rest of the path returned should be nil")
|
|
}
|
|
|
|
if err.Error() != "no such link in this branch" {
|
|
t.Fatalf("Wrong error")
|
|
}
|
|
}
|
|
}
|
|
|
|
otherSuccessCases := [][]string{
|
|
{"0", "1", "banana"},
|
|
{"1", "banana"},
|
|
{"7bc", "def"},
|
|
{"bc", "def"},
|
|
}
|
|
|
|
for i := 0; i < len(otherSuccessCases); i = i + 2 {
|
|
osc := otherSuccessCases[i]
|
|
expectedRest := otherSuccessCases[i+1]
|
|
|
|
obj, rest, err := ethTxTrie.Resolve(osc)
|
|
_, ok := obj.(*node.Link)
|
|
if !ok {
|
|
t.Fatalf("Returned object is not a link")
|
|
}
|
|
|
|
for j, _ := range expectedRest {
|
|
if rest[j] != expectedRest[j] {
|
|
t.Fatalf("Wrong rest of the path returned\r\nexpected %s\r\ngot %s", expectedRest[j], rest[j])
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatal("Error should be nil")
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func TestTraverseTxTrieWithResolve(t *testing.T) {
|
|
var err error
|
|
|
|
txMap := prepareTxTrieMap(t)
|
|
|
|
// This is the cid of the tx root at the block 4,139,497
|
|
currentNode := txMap["bagjacgzaqolvvlyflkdiylijcu4ts6myxczkb2y3ewxmln5oyrsrkfc4v7ua"]
|
|
|
|
// This is the path we want to traverse
|
|
// the transaction id 256, which is RLP encoded to 820100
|
|
var traversePath []string
|
|
for _, s := range "820100" {
|
|
traversePath = append(traversePath, string(s))
|
|
}
|
|
traversePath = append(traversePath, "value")
|
|
|
|
var obj interface{}
|
|
for {
|
|
obj, traversePath, err = currentNode.Resolve(traversePath)
|
|
link, ok := obj.(*node.Link)
|
|
if !ok {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatal("Error should be nil")
|
|
}
|
|
|
|
currentNode = txMap[link.Cid.String()]
|
|
if currentNode == nil {
|
|
t.Fatal("transaction trie node not found in memory map")
|
|
}
|
|
}
|
|
|
|
if fmt.Sprintf("%v", obj) != "0xc495a958603400" {
|
|
t.Fatalf("Wrong value\r\nexpected %s\r\ngot %s", "0xc495a958603400", fmt.Sprintf("%v", obj))
|
|
}
|
|
}
|
|
|
|
func TestTxTrieTreeBadParams(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieBranch(t)
|
|
|
|
tree := ethTxTrie.Tree("non-empty-string", 0)
|
|
if tree != nil {
|
|
t.Fatal("Expected nil to be returned")
|
|
}
|
|
|
|
tree = ethTxTrie.Tree("non-empty-string", 1)
|
|
if tree != nil {
|
|
t.Fatal("Expected nil to be returned")
|
|
}
|
|
|
|
tree = ethTxTrie.Tree("", 0)
|
|
if tree != nil {
|
|
t.Fatal("Expected nil to be returned")
|
|
}
|
|
}
|
|
|
|
func TestTxTrieTreeExtension(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
|
|
tree := ethTxTrie.Tree("", -1)
|
|
|
|
if len(tree) != 1 {
|
|
t.Fatalf("An extension should have one element")
|
|
}
|
|
|
|
if tree[0] != "01" {
|
|
t.Fatalf("Wrong trie element\r\nexpected %s\r\ngot %s", "01", tree[0])
|
|
}
|
|
}
|
|
|
|
func TestTxTrieTreeBranch(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieBranch(t)
|
|
|
|
tree := ethTxTrie.Tree("", -1)
|
|
|
|
lookupElements := map[string]interface{}{
|
|
"0": nil,
|
|
"1": nil,
|
|
"2": nil,
|
|
"3": nil,
|
|
"4": nil,
|
|
"5": nil,
|
|
"6": nil,
|
|
"7": nil,
|
|
"8": nil,
|
|
}
|
|
|
|
if len(tree) != len(lookupElements) {
|
|
t.Fatalf("Wrong number of elements\r\nexpected %d\r\ngot %d", len(lookupElements), len(tree))
|
|
}
|
|
|
|
for _, te := range tree {
|
|
if _, ok := lookupElements[te]; !ok {
|
|
t.Fatalf("Unexpected Element: %v", te)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTxTrieLinksBranch(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieBranch(t)
|
|
|
|
desiredValues := []string{
|
|
"bagjacgzakhtcfpja453ydiaqxgidqmxhh7jwmxujib663deebwfs3m2n3hoa",
|
|
"bagjacgza2p2fuqh4vumknq6x5w7i47usvtu5ixqins6qjjtcks4zge3vx3qq",
|
|
"bagjacgza4fkhn7et3ra66yjkzbtvbxjefuketda6jctlut6it7gfahxhywga",
|
|
"bagjacgzacnryeybs52xryrka5uxi4eg4hi2mh66esaghu7cetzu6fsukrynq",
|
|
"bagjacgzastu5tc7lwz4ap3gznjwkyyepswquub7gvhags5mgdyfynnwbi43a",
|
|
"bagjacgza5qgp76ovvorkydni2lchew6ieu5wb55w6hdliiu6vft7zlxtdhjq",
|
|
"bagjacgzafnssc4yvln6zxmks5roskw4ckngta5n4yfy2skhlu435ve4b575a",
|
|
"bagjacgzagkuei7qxfxefufme2d3xizxokkq4ad3rzl2x4dq2uao6dcr4va2a",
|
|
"bagjacgzaxpaehtananrdxjghwukh2wwkkzcqwveppf6xclkrtd26rm27kqwq",
|
|
}
|
|
|
|
links := ethTxTrie.Links()
|
|
|
|
for i, v := range desiredValues {
|
|
if links[i].Cid.String() != v {
|
|
t.Fatalf("Wrong cid for link %d\r\nexpected %s\r\ngot %s", i, v, links[i].Cid.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
EthTxTrie Functions
|
|
*/
|
|
|
|
func TestTxTrieJSONMarshalExtension(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieExtension(t)
|
|
|
|
jsonOutput, err := ethTxTrie.MarshalJSON()
|
|
checkError(err, t)
|
|
|
|
var data map[string]interface{}
|
|
err = json.Unmarshal(jsonOutput, &data)
|
|
checkError(err, t)
|
|
|
|
if parseMapElement(data["01"]) !=
|
|
"bagjacgzak6wdjvshdtb7lrvlteweyd7f5qjr3dmzmh7g2xpi4xrwoujsio2a" {
|
|
t.Fatalf("Wrong Marshaled Value\r\nexpected %s\r\ngot %s", "bagjacgzak6wdjvshdtb7lrvlteweyd7f5qjr3dmzmh7g2xpi4xrwoujsio2a", parseMapElement(data["01"]))
|
|
}
|
|
|
|
if data["type"] != "extension" {
|
|
t.Fatalf("Wrong node type\r\nexpected %s\r\ngot %s", "extension", data["type"])
|
|
}
|
|
}
|
|
|
|
func TestTxTrieJSONMarshalLeaf(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieLeaf(t)
|
|
|
|
jsonOutput, err := ethTxTrie.MarshalJSON()
|
|
checkError(err, t)
|
|
|
|
var data map[string]interface{}
|
|
err = json.Unmarshal(jsonOutput, &data)
|
|
checkError(err, t)
|
|
|
|
if data["type"] != "leaf" {
|
|
t.Fatalf("Wrong node type\r\nexpected %s\r\ngot %s", "leaf", data["type"])
|
|
}
|
|
|
|
if fmt.Sprintf("%v", data[""].(map[string]interface{})["nonce"]) !=
|
|
"40243" {
|
|
t.Fatalf("Wrong nonce value\r\nexepcted %s\r\ngot %s", "40243", fmt.Sprintf("%v", data[""].(map[string]interface{})["nonce"]))
|
|
}
|
|
}
|
|
|
|
func TestTxTrieJSONMarshalBranch(t *testing.T) {
|
|
ethTxTrie := prepareDecodedEthTxTrieBranch(t)
|
|
|
|
jsonOutput, err := ethTxTrie.MarshalJSON()
|
|
checkError(err, t)
|
|
|
|
var data map[string]interface{}
|
|
err = json.Unmarshal(jsonOutput, &data)
|
|
checkError(err, t)
|
|
|
|
desiredValues := map[string]string{
|
|
"0": "bagjacgzakhtcfpja453ydiaqxgidqmxhh7jwmxujib663deebwfs3m2n3hoa",
|
|
"1": "bagjacgza2p2fuqh4vumknq6x5w7i47usvtu5ixqins6qjjtcks4zge3vx3qq",
|
|
"2": "bagjacgza4fkhn7et3ra66yjkzbtvbxjefuketda6jctlut6it7gfahxhywga",
|
|
"3": "bagjacgzacnryeybs52xryrka5uxi4eg4hi2mh66esaghu7cetzu6fsukrynq",
|
|
"4": "bagjacgzastu5tc7lwz4ap3gznjwkyyepswquub7gvhags5mgdyfynnwbi43a",
|
|
"5": "bagjacgza5qgp76ovvorkydni2lchew6ieu5wb55w6hdliiu6vft7zlxtdhjq",
|
|
"6": "bagjacgzafnssc4yvln6zxmks5roskw4ckngta5n4yfy2skhlu435ve4b575a",
|
|
"7": "bagjacgzagkuei7qxfxefufme2d3xizxokkq4ad3rzl2x4dq2uao6dcr4va2a",
|
|
"8": "bagjacgzaxpaehtananrdxjghwukh2wwkkzcqwveppf6xclkrtd26rm27kqwq",
|
|
}
|
|
|
|
for k, v := range desiredValues {
|
|
if parseMapElement(data[k]) != v {
|
|
t.Fatalf("Wrong Marshaled Value %s\r\nexpected %s\r\ngot %s", k, v, parseMapElement(data[k]))
|
|
}
|
|
}
|
|
|
|
for _, v := range []string{"a", "b", "c", "d", "e", "f"} {
|
|
if data[v] != nil {
|
|
t.Fatal("Expected value to be nil")
|
|
}
|
|
}
|
|
|
|
if data["type"] != "branch" {
|
|
t.Fatalf("Wrong node type\r\nexpected %s\r\ngot %s", "branch", data["type"])
|
|
}
|
|
}
|
|
|
|
/*
|
|
AUXILIARS
|
|
*/
|
|
|
|
// prepareDecodedEthTxTrie simulates an IPLD block available in the datastore,
|
|
// checks the source RLP and tests for the absence of errors during the decoding fase.
|
|
func prepareDecodedEthTxTrie(branchDataRLP string, t *testing.T) *EthTxTrie {
|
|
b, err := hex.DecodeString(branchDataRLP)
|
|
checkError(err, t)
|
|
|
|
c, err := RawdataToCid(MEthTxTrie, b, multihash.KECCAK_256)
|
|
checkError(err, t)
|
|
|
|
storedEthTxTrie, err := block.NewBlockWithCid(b, c)
|
|
checkError(err, t)
|
|
|
|
ethTxTrie, err := DecodeEthTxTrie(storedEthTxTrie.Cid(), storedEthTxTrie.RawData())
|
|
checkError(err, t)
|
|
|
|
return ethTxTrie
|
|
}
|
|
|
|
func prepareDecodedEthTxTrieExtension(t *testing.T) *EthTxTrie {
|
|
extensionDataRLP :=
|
|
"e4820001a057ac34d6471cc3f5c6ab992c4c0fe5ec131d8d9961fe6d5de8e5e367513243b4"
|
|
return prepareDecodedEthTxTrie(extensionDataRLP, t)
|
|
}
|
|
|
|
func prepareDecodedEthTxTrieLeaf(t *testing.T) *EthTxTrie {
|
|
leafDataRLP :=
|
|
"f87220b86ff86d829d3384ee6b280083015f9094e0e6c781b8cba08bc840" +
|
|
"7eac0101b668d1fa6f4987c495a9586034008026a0981b6223c9d3c31971" +
|
|
"6da3cf057da84acf0fef897f4003d8a362d7bda42247dba066be134c4bc4" +
|
|
"32125209b5056ef274b7423bcac7cc398cf60b83aaff7b95469f"
|
|
return prepareDecodedEthTxTrie(leafDataRLP, t)
|
|
}
|
|
|
|
func prepareDecodedEthTxTrieBranch(t *testing.T) *EthTxTrie {
|
|
branchDataRLP :=
|
|
"f90131a051e622bd20e77781a010b9903832e73fd3665e89407ded8c840d8b2db34dd9" +
|
|
"dca0d3f45a40fcad18a6c3d7edbe8e7e92ace9d45e086cbd04a66254b9931375bee1a0" +
|
|
"e15476fc93dc41ef612ac86750dd242d14498c1e48a6ba4fc89fcc501ee7c58ca01363" +
|
|
"826032eeaf1c4540ed2e8e10dc3a34c3fbc4900c7a7c449e69e2ca8a8e1ba094e9d98b" +
|
|
"ebb67807ecd96a6cac608f95a14a07e6a9c06975861e0b86b6c14736a0ec0cfff9d5ab" +
|
|
"a2ac0da8d2c4725bc8253b60f7b6f1c6b4229ea967fcaef319d3a02b652173155b7d9b" +
|
|
"b152ec5d255b82534d3075bcc171a928eba737da9381effaa032a8447e172dc85a1584" +
|
|
"d0f77466ee52a1c00f71caf57e0e1aa01de18a3ca834a0bbc043cc0d03623ba4c7b514" +
|
|
"7d5aca56450b548f797d712d5198f5e8b35f542d8080808080808080"
|
|
return prepareDecodedEthTxTrie(branchDataRLP, t)
|
|
}
|
|
|
|
func prepareTxTrieMap(t *testing.T) map[string]*EthTxTrie {
|
|
fi, err := os.Open("test_data/eth-block-body-json-4139497")
|
|
checkError(err, t)
|
|
|
|
_, _, txTrieNodes, err := FromBlockJSON(fi)
|
|
checkError(err, t)
|
|
|
|
out := make(map[string]*EthTxTrie)
|
|
|
|
for _, txTrieNode := range txTrieNodes {
|
|
decodedNode, err := DecodeEthTxTrie(txTrieNode.Cid(), txTrieNode.RawData())
|
|
checkError(err, t)
|
|
out[txTrieNode.Cid().String()] = decodedNode
|
|
}
|
|
|
|
return out
|
|
}
|