Skip to content

Commit

Permalink
Introduce filtering of block and transaction fields using jq
Browse files Browse the repository at this point in the history
To run this test jq will need to be installed on the machine.
  • Loading branch information
piersy committed Apr 12, 2024
1 parent 7c88aec commit 1e2d180
Showing 1 changed file with 75 additions and 41 deletions.
116 changes: 75 additions & 41 deletions compat_tests/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"math/big"
"os/exec"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -194,23 +196,23 @@ func rpcCallCompare(t *testing.T, c1, c2 *rpc.Client, dumpOutput, failOnFirstMis
if err != nil {
return nil, err
}

// Filter out null feeCurrency fields in transactions, in CEL2 we will omit them as empty.
res1Filtered, err := execJQ(res1, `if type != "array" and has("transactions") and has("totalDifficulty") and has("parentHash") and (.transactions | length > 0) then .transactions |= map(if .feeCurrency == null then del(.feeCurrency) else . end) else . end`)
require.NoError(t, err)

res2, err := rpcCall(c2, dumpOutput, method, args...)
if err != nil {
return nil, err
}
require.NoError(t, err)

dst1 := &bytes.Buffer{}
err = json.Indent(dst1, res1, "", " ")
if err != nil {
return nil, err
}
err = json.Indent(dst1, res1Filtered, "", " ")
require.NoError(t, err, "res1: %v\n\nres1filtered: %v\n", string(res1), string(res1Filtered))

dst2 := &bytes.Buffer{}
err = json.Indent(dst2, res2, "", " ")
if err != nil {
return nil, err
}
require.NoError(t, err)

if dst1.String() != dst2.String() {
if strings.TrimSpace(dst1.String()) != strings.TrimSpace(dst2.String()) {
fmt.Printf("\nmethod: %v\nexpected (c1):\n%v,\nactual (c2):\n%v\n", method, dst1.String(), dst2.String())
if failOnFirstMismatch {
require.JSONEq(t, dst1.String(), dst2.String())
Expand Down Expand Up @@ -243,37 +245,69 @@ func rpcCall(c *rpc.Client, dumpOutput bool, method string, args ...interface{})
}
// Check to see if this is a block
if parsed.Exists("parentHash") && parsed.Exists("totalDifficulty") {
// If so delete fields we don't want to compare
parsed.DeleteP("gasLimit")
parsed.DeleteP("randomness")
parsed.DeleteP("uncles")
parsed.DeleteP("sha3Uncles")
parsed.DeleteP("size")
parsed.DeleteP("epochSnarkData")
parsed.DeleteP("mixHash")
parsed.DeleteP("nonce")
return parsed.Bytes(), nil
return execJQ([]byte(m), "del(.gasLimit, .randomness, .uncles, .sha3Uncles, .size, .epochSnarkData, .mixHash, .nonce, .transactions[].gatewayFee, .transactions[].gatewayFeeRecipient)")
}
return m, nil

}

func execJQ(json []byte, command ...string) ([]byte, error) {
cmd := exec.Command("jq", command...)
cmd.Stdin = bytes.NewBuffer(json)
buf := new(bytes.Buffer)
cmd.Stdout = buf
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("jq failed on input: %v\n\n with command: %v\n error: %v", string(json), command[0], err)
}
return buf.Bytes(), nil
}

func TestExecJq(t *testing.T) {
res, err := execJQ([]byte(testblock), "del(.transactions[].gatewayFeeRecipient)")
require.NoError(t, err)

fmt.Printf("result %v\n", string(res))

res, err = execJQ([]byte(testblock), "--exit-status", `has("kparentHash", "ktotalDifficulty")`)
require.NoError(t, err)
require.Equal(t, "true", string(res))
}

// func TestCompatibilityOfChainWithDiff(t *testing.T) {

// ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
// defer cancel()
// c, err := ethclient.DialContext(ctx, "http://localhost:8545")
// require.NoError(t, err)
// for i := int64(2800); i < 2800+100; i++ {
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
// defer cancel()
// b, err := c.BlockByNumber(ctx, big.NewInt(i))
// require.NoError(t, err)
// for _, tx := range b.Transactions() {
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
// defer cancel()
// , err := c.TransactionReceipt(ctx, tx.Hash())
// require.NoError(t, err)
// }
// }

// }
var testblock = `{
"difficulty": "0x0",
"extraData": "0xd983010000846765746889676f312e31332e3130856c696e7578000000000000f8c2c0c080b841b1e6fb24531d1ee3a145773a1c9b943dda5942a1183ae3375f49fc84b7a5622535743f16ad93b85277d2fec8393dd9a3326a2751e2133b3237ff06006ecf478001f83c890184a277df3fd1fffbb0346de4cec20deee720f3d5723c730328764b6f591d1deed14fea11a194919e67776a0d80e7998e9ece4bc35fdf56678180f83c8901efefffff7ff7fffbb0b37d8e6746f976aa8c5c8d363f514d1ca4fc48c133eecbc3c2bdaccd4153f382a08de7d009ead2bd643758b3882f558080",
"gasUsed": "0x365f1",
"hash": "0x24303658ca351b0cd03269ec9205e4d9d789f7609b3165054d0aeba86fb7c985",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x39ec4f2a82f9f0f39929415c65db9ea5df54e41d",
"number": "0xb05",
"parentHash": "0x5bac312111c91bd96d0b18f1ddd645e8c64ef605cbdfc591b0bd33101e6c7ad8",
"receiptsRoot": "0x0367e31620bf2a40cd63abeeb5dc675b1f10881b508c1f696cd8579867f7c5b6",
"stateRoot": "0x55a2fcfa962c3915a56928bbd5c2ae277994bad019d991e564c68cd822c9499c",
"timestamp": "0x5ea0a46b",
"totalDifficulty": "0xb06",
"transactions": [
{
"blockHash": "0x24303658ca351b0cd03269ec9205e4d9d789f7609b3165054d0aeba86fb7c985",
"blockNumber": "0xb05",
"ethCompatible": false,
"feeCurrency": null,
"from": "0xe23a4c6615669526ab58e9c37088bee4ed2b2dee",
"gas": "0x1312d00",
"gasPrice": "0x2540be400",
"gatewayFeeRecipient": null,
"hash": "0x721a8fc581d2e699294f10398d3e4ffeb9af5d9c6a08ba1eb76e2ad29617f375",
"input": "0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102ae806100606000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80630900f01014610051578063445df0ac146100955780638da5cb5b146100b3578063fdacd576146100fd575b600080fd5b6100936004803603602081101561006757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061012b565b005b61009d6101f7565b6040518082815260200191505060405180910390f35b6100bb6101fd565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6101296004803603602081101561011357600080fd5b8101908080359060200190929190505050610222565b005b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101f45760008190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b1580156101da57600080fd5b505af11580156101ee573d6000803e3d6000fd5b50505050505b50565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561027f57806001819055505b5056fea165627a7a723058208d331f3378bb7ac07b45c7b734ec6883c68482f8eed0e59739f5b82af926f5810029",
"nonce": "0x0",
"r": "0x97d69d5ec9d61c72835acbfa8cfa846fad6584002f0dd503c5f4f67f721025e1",
"s": "0x7aebe0781721a38da5bce745c6b923306e30154e63156e3742b2cd198e0233a7",
"to": null,
"transactionIndex": "0x0",
"type": "0x0",
"v": "0x149fb",
"value": "0x0"
}
],
"transactionsRoot": "0x82674f07b62b9c6596674894e8cc17c50d444b0f2c57fbe1f9a7be9da14d0718"
}`

0 comments on commit 1e2d180

Please sign in to comment.