holochain-open-dev / holoom

Tools for weaving blockchain data into holochain
2 stars 1 forks source link

Input becomes output on invalid jq program #59

Closed 8e8b2c closed 2 months ago

8e8b2c commented 2 months ago

Given the following recipe:

{
  "trusted_authors": "!!CUT!!",
  "arguments": [],
  "instructions": [
    [
      "match_ids_final_name",
      {
        "type": "Constant",
        "value": "\"championship/61ce72bb-2955-429e-ac19-9bdd098ce6cf/match_ids_final\""
      }
    ],
    [
      "match_ids",
      {
        "type": "GetLatestDocWithIdentifier",
        "var_name": "match_ids_final_name"
      }
    ],
    [
      "match_stats_names",
      {
        "type": "Jq",
        "input_var_names": {
          "type": "Single",
          "var_name": "match_ids"
        },
        "program": "map(\"match-stats/\\(.)\")"
      }
    ],
    [
      "all_match_stats",
      {
        "type": "GetDocsListedByVar",
        "var_name": "match_stats_names"
      }
    ],
    [
      "external_id",
      {
        "type": "GetLatestCallerExternalId"
      }
    ],
    [
      "$return",
      {
        "type": "Jq",
        "input_var_names": {
          "type": "List",
          "var_names": [
            "all_match_stats",
            "external_id"
          ]
        },
        "program": "!!SEE BELOW!!"
      }
    ]
  ]
}

with invalid jq program:

def pick_player_stats:
    . as $team |
    $team.players | map(
        {
            id: .player_id,
            kills: .player_stats.Kills | tonumber,
            team_wins: $team."team_stats"."Team Win" | tonumber
        }
    );

.all_match_stats as $all_match_stats |
.external_id.external_id as $player_id |

[$all_match_stats[] | select(has("rounds"))] |
[.[].rounds] | 
add | 
[.[].teams] |
add |
map(pick_player_stats) |
add |
group_by(.id) |
map({
    id: .[0].id,
    kills: [.[].kills] | add,
    team_wins: [.[].team_wins] | add,
}) |
group_by(.team_wins) |
map(sort_by(.kills)) |
add |
reverse |
map(.id) |
index($player_id) |
[
    "06f05b59d3b20000", # 50% as 10e18 hex
    "03782dace9d90000", # 25%
    "0214e8348c4f0000", # 15%
    "016345785d8a0000"  # 10%
][.] as $percentage |

[$ticket_number, $percentage, "1B5f0EaE225491eE0b629A2f70761E3288f18E08"]

(Note that the final array contains the var $ticket_number that hasn't been declared.) The output of this recipe when executed by an agent (whose external id wasn't present in the match data) was the full input to the program. I.e.

{ all_match_stats, external_id }
8e8b2c commented 2 months ago

With the missing $ticket_number removed, the program execution fails with jq execution error: cannot index [\"06f05b59d3b20000\",\"03782dace9d90000\",\"0214e8348c4f0000\",\"016345785d8a0000\"] with null, which seems correct, as the agent's external ID isn't present.

8e8b2c commented 2 months ago

Creation of this jq program should have been prevented because it doesn't compile.

8e8b2c commented 2 months ago

Fix included in https://github.com/holochain-open-dev/holoom/pull/57