goat-systems / go-tezos

Go Tezos Is a Go library that exposes and builds upon the Tezos RPC.
MIT License
71 stars 45 forks source link

cannot unmarshal object into Go struct field OperationResults.operations.contents.metadata.operation_result.errors of type rpc.Errors #169

Closed utdrmac closed 3 years ago

utdrmac commented 3 years ago

Testing on carthagenet and came across this one. https://carthagenet.tzstats.com/BMess41PKzQXeJ3tt6NQVz4TG7hgF2JtgYWzmmF8ecLjKME3jVC

For some reason, the operation errors are being interpreted as rpc.Errors but should be parsed as ResultError. I'm trying to figure out why this is.

In an effort to start debugging the issue, I stripped down the block to basic components plus the operation with the errors. Just using a simple parse to work out the issue, if I can:

-- badblock.go
package main

import (
    "encoding/json"
    "io/ioutil"
    "os"
    "github.com/goat-systems/go-tezos/v3/rpc"
    log "github.com/sirupsen/logrus"
)

func main() {
    // load test json block
    jsonFile, err := os.Open("bad_parse.json")
    if err != nil {
        panic(err)
    }
    defer jsonFile.Close()

    byteValue, _ := ioutil.ReadAll(jsonFile)

    var b rpc.Block
    err = json.Unmarshal(byteValue, &b)
    if err != nil {
        log.WithError(err).Fatal("Error Unmarshaling Block")
    }

    log.Println(b)
    log.Info("Exiting")
}

Here's the contents of bad_parse.json:

{
  "protocol": "PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb",
  "chain_id": "NetXjD3HPJJjmcd",
  "hash": "BMess41PKzQXeJ3tt6NQVz4TG7hgF2JtgYWzmmF8ecLjKME3jVC",
  "header": {
    "level": 800912,
    "proto": 2,
    "predecessor": "BL5qNHsPQjE21xTGtL4g2365VaWN34peQ1fWsG3aGrU8vucrSXa",
    "timestamp": "2020-10-13T14:21:16Z",
    "validation_pass": 4,
    "operations_hash": "LLoabCTa4nN8wsK1zSBHd2e6dwSQNXdeiVyjKDrPLdQq7ytRAWXZ2",
    "fitness": [
      "01",
      "00000000000c388f"
    ],
    "context": "CoVoXg1NMGdG77kjgkDUfXFekVBCaXywH2Y5eN7MWNr41HUzmYXT",
    "priority": 0,
    "proof_of_work_nonce": "ee87c704a8b70000",
    "signature": "sigYkwbFePhWjMqdGvYu6wUqXFD9dLWQrV6MykTAfW3v7a6s9x6xKRVBmKY3tighA9grCtbqoFymiCD1n6jzmrwFG2vMr8kK"
  },
  "metadata": {
    "protocol": "PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb",
    "next_protocol": "PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb",
    "test_chain_status": {
      "status": "not_running"
    },
    "max_operations_ttl": 60,
    "max_operation_data_length": 16384,
    "max_block_header_length": 238,
    "max_operation_list_length": [
      {
        "max_size": 32768,
        "max_op": 32
      },
      {
        "max_size": 32768
      },
      {
        "max_size": 135168,
        "max_op": 132
      },
      {
        "max_size": 524288
      }
    ],
    "baker": "tz1f8Ybid6x2pMvJGauz3Zi8enkABSSkGPn8",
    "level": {
      "level": 800912,
      "level_position": 800911,
      "cycle": 391,
      "cycle_position": 143,
      "voting_period": 391,
      "voting_period_position": 143,
      "expected_commitment": false
    },
    "voting_period_kind": "proposal",
    "nonce_hash": null,
    "consumed_gas": "100071",
    "deactivated": [],
    "balance_updates": [
      {
        "kind": "contract",
        "contract": "tz1f8Ybid6x2pMvJGauz3Zi8enkABSSkGPn8",
        "change": "-512000000"
      },
      {
        "kind": "freezer",
        "category": "deposits",
        "delegate": "tz1f8Ybid6x2pMvJGauz3Zi8enkABSSkGPn8",
        "cycle": 391,
        "change": "512000000"
      },
      {
        "kind": "freezer",
        "category": "rewards",
        "delegate": "tz1f8Ybid6x2pMvJGauz3Zi8enkABSSkGPn8",
        "cycle": 391,
        "change": "40000000"
      }
    ]
  },
  "operations": [
    [],
    [],
    [],
    [
      {
        "protocol": "PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb",
        "chain_id": "NetXjD3HPJJjmcd",
        "hash": "ooaAQT1KXstZK8Ej9UNeJ2xSZHaUfycZYXuwiStUm7zEL7xMvPu",
        "branch": "BLEtngzxCVntrBJMCRYiQb9Ni8CkG2LJ9PGv9rfX9Qf5J6zaYLS",
        "contents": [
          {
            "kind": "origination",
            "source": "tz1Myayxsk3EycijxBn4iEYEFW4aoGVQfjTH",
            "fee": "100000",
            "counter": "2608885",
            "gas_limit": "900000",
            "storage_limit": "20000",
            "balance": "0",
            "metadata": {
              "balance_updates": [
                {
                  "kind": "contract",
                  "contract": "tz1Myayxsk3EycijxBn4iEYEFW4aoGVQfjTH",
                  "change": "-100000"
                },
                {
                  "kind": "freezer",
                  "category": "fees",
                  "delegate": "tz1f8Ybid6x2pMvJGauz3Zi8enkABSSkGPn8",
                  "cycle": 391,
                  "change": "100000"
                }
              ],
              "operation_result": {
                "status": "failed",
                "errors": [
                  {
                    "kind": "permanent",
                    "id": "proto.006-PsCARTHA.michelson_v1.ill_typed_data",
                    "expected_type": {
                      "prim": "pair"
                    },
                    "ill_typed_expression": {
                      "prim": "Pair"
                    }
                  },
                  {
                    "kind": "permanent",
                    "id": "proto.006-PsCARTHA.invalidSyntacticConstantError",
                    "location": 14,
                    "expectedForm": {
                      "prim": "pair"
                    },
                    "wrongExpression": {
                      "prim": "Pair",
                      "args": [
                        {
                          "string": "Contract_0"
                        },
                        {
                          "int": "0"
                        }
                      ]
                    }
                  },
                  {
                    "kind": "permanent",
                    "id": "proto.006-PsCARTHA.contract.invalid_contract_notation",
                    "notation": "Contract_0"
                  }
                ]
              }
            }
          }
        ],
        "signature": "sigvuYTuQrLTXtMSbrPAVwGhp6ePPVkcN4yyfsbbR2SgWCKqmVW7gNqqJ2z8vDb7wTfDJHvq7jTy72nuQf1xPD4gkvmqPhXX"
      }
    ]
  ]
}
utdrmac commented 3 years ago

Found the issue https://github.com/goat-systems/go-tezos/blob/6fecc940c49c88c9f7e7f600d3417f74e31b2252/rpc/block.go#L464 ^^This should be of type []ResultError

There seem to be two "OperationResults" type structs: https://github.com/goat-systems/go-tezos/blob/6fecc940c49c88c9f7e7f600d3417f74e31b2252/rpc/block.go#L211 https://github.com/goat-systems/go-tezos/blob/6fecc940c49c88c9f7e7f600d3417f74e31b2252/rpc/block.go#L456

They appear to be 99% the same, but are different with the 'Errors' member types.

These two structs probably need to be merged?

utdrmac commented 3 years ago

Actually, I think I found the more correct issue. There exists type Errors []Error in client.go, but many of the structs in block.go use []Errors which is a slice of slices. By changing every struct in block.go to use []Error the above operation is parsing correctly now.

DefinitelyNotAGoat commented 3 years ago

@utdrmac Yeah this was a problem with no using the ResultErrors struct. It will be fixed when I merge v4. Thanks for reporting!