Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

5380 support iteration over scopes & tables in cleos #5486

Merged
merged 4 commits into from
Sep 10, 2018

Conversation

taokayan
Copy link
Contributor

@taokayan taokayan commented Aug 30, 2018

#5380 This allow user to off-chain iterate over scopes and tables for a given contract.

Adds new /v1/chain/get_table_by_scope rpc.

Usage: ./cleos get scope [OPTIONS] contract

Positionals:
contract TEXT The contract who owns the table (required)

Options:
-t,--table TEXT The name of the table as filter
-l,--limit UINT The maximum number of rows to return
-L,--lower TEXT lower bound of scope
-U,--upper TEXT upper bound of scope

for example: to iterate over all accounts that has EOS balance table:

./cleos get scope eosio.token -t accounts -l 4
{
  "rows": [{
      "code": "eosio.token",
      "scope": "a11111111111",
      "table": "accounts",
      "payer": "eosio",
      "count": 1
    },{
      "code": "eosio.token",
      "scope": "a123",
      "table": "accounts",
      "payer": "eosio",
      "count": 1
    },{
      "code": "eosio.token",
      "scope": "a22222222222",
      "table": "accounts",
      "payer": "eosio",
      "count": 1
    },{
      "code": "eosio.token",
      "scope": "a33333333333",
      "table": "accounts",
      "payer": "eosio",
      "count": 1
    }
  ],
  "more": true
}

uint32_t count;
};
struct get_table_by_scope_result {
vector<fc::variant> rows; ///< one row per item, either encoded as hex String or JSON object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a variant? Also the comment seems wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix this accordingly.

("lower_bound",lower)
("upper_bound",upper)
("limit",limit)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have some tests for this and the other get table functionality. Not required for this PR, but something to do when you have some time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. let me make some tests for "get table" related functionality.

@heifner
Copy link
Contributor

heifner commented Sep 5, 2018

New features should target develop branch not release/1.2.x branch

@taokayan taokayan changed the base branch from release/1.2.x to develop September 6, 2018 08:23
};
struct get_table_by_scope_result {
vector<get_table_by_scope_result_row> rows;
bool more = false; ///< true if last element in data is not the end and sizeof data() < limit
Copy link
Contributor

@heifner heifner Sep 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to move away from true/false for more. Instead fill it with the next value to pass to lower bound if there is more. See, for example, get_producers or get_scheduled_transactions. Would be nice to also make that change for get table row as well.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not {next : "nextitem", more: 12345} or is there an other way to get the length of a table? I got referenced to this issue from here #5543

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasperfish there's no way to know the remaining number unless you iterate all of them.

Copy link
Contributor

@UMU618 UMU618 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUG issue

decltype(idx.upper_bound(boost::make_tuple(0, 0, 0))) upper;

if (p.lower_bound.size()) {
uint64_t scope = convert_to_type<uint64_t>(p.lower_bound, "lower_bound scope");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is a bug!
If I pass '313313313313' to lower_bound, then it's 313313313313 (0x48F2EDC221), SHOULD be 1749138884762583600 (0x1846308C6118C230)
1111111.bank is 595056260428703488 (0x084210842039A700)

313313313313 < N(1111111.bank) < N(313313313313)

You can reproduce BUG by:

`
cleos -u https://mainnet.meet.one --print-request get scope eosio.token -t accounts -l 1 -L 313313313313
REQUEST:

POST /v1/chain/get_table_by_scope HTTP/1.0
Host: mainnet.meet.one
content-length: 118
Accept: /
Connection: close

{
"code": "eosio.token",
"table": "accounts",
"lower_bound": "313313313313",
"upper_bound": "",
"limit": 1
}

{
"rows": [{
"code": "eosio.token",
"scope": "1111111.bank",
"table": "accounts",
"payer": "1111111.bank",
"count": 1
}
],
"more": "111111111111"
}
`

Copy link
Contributor Author

@taokayan taokayan Sep 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a bug. The value of scope will be read as uint64_t first, then be read as account_name if failed to be read as uint64_t, as for different contracts the meaning of scopes may defer. If you want to read it as account_name always, you need to use cleos -u https://mainnet.meet.one --print-request get scope eosio.token -t accounts -l 1 -L " 313313313313"

@noprom
Copy link
Contributor

noprom commented Aug 24, 2020

Is it possible to get a contract scope in contract?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants