eos icon indicating copy to clipboard operation
eos copied to clipboard

get_table_rows API returns empty for data type array

Open andriantolie opened this issue 9 years ago • 6 comments

I'm trying to write a smart contract, and I have the following struct

  struct Game {
    Game() {};
    Game(AccountName challenger, AccountName host):challenger(challenger), host(host), turn(host), winner(N(none)) {
      // Initialize board
      for(int i = 0; i < 9; i++) {
        board[i] = N(-);
      }
    };
    AccountName   challenger; // use challenger as the key
    AccountName   host;
    AccountName   turn; // host/ challenger
    AccountName   winner; // none/ draw/ host/ challenger
    uint64_t      board[9]; // use 1d array since 2d array is not yet supported
  };

And the following data in the .abi

{
  "types": [{
      "newTypeName": "AccountName",
      "type": "Name"
    }
  ],
  "structs": [{
      "name": "game",
      "base": "",
      "fields": {
        "challenger": "AccountName",
        "host": "AccountName",
        "turn": "AccountName",
        "winner": "AccountName",
        "board": "UInt64[]"
      }
    }],
  "tables": [{
        "table": "game",
        "type": "game",
        "indextype": "i64",
        "keynames" : ["challenger"],
        "keytypes" : ["AccountName"]
      }
  ]
}

After constructing the Game object, I try to retrieve the data through eosc get table, however I got empty array instead of array of '-' as the following:

{
  "rows": [{
      "challenger": "inita",
      "host": "initb",
      "turn": "initb",
      "winner": "",
      "board": []
    }
  ],
  "more": false
}

Anybody, has ever face the same issue?

andriantolie avatar Oct 13 '17 10:10 andriantolie

I think you need to specify the board length like this in your struct. uint8_t board_len = 9;

https://gist.github.com/nanonano7/44ea822dff6d6a347ea594ca7e2129fc#gistcomment-2220096

nanonano7 avatar Oct 13 '17 11:10 nanonano7

There must be something wrong. Show me the code of how you store Game struct on db, so maybe I can help.

On 13 Oct 2017, at 6:10 PM, Andrianto Lie [email protected] wrote:

I'm trying to write a smart contract, and I have the following struct

struct Game { Game() {}; Game(AccountName challenger, AccountName host):challenger(challenger), host(host), turn(host), winner(N(none)) { // Initialize board for(int i = 0; i < 9; i++) { board[i] = N(-); } }; AccountName challenger; // use challenger as the key AccountName host; AccountName turn; // host/ challenger AccountName winner; // none/ draw/ host/ challenger uint64_t board[9]; // use 1d array since 2d array is not yet supported }; And the following data in the .abi

{ "types": [{ "newTypeName": "AccountName", "type": "Name" } ], "structs": [{ "name": "game", "base": "", "fields": { "challenger": "AccountName", "host": "AccountName", "turn": "AccountName", "winner": "AccountName", "board": "UInt64[]" } }], "tables": [{ "table": "game", "type": "game", "indextype": "i64", "keynames" : ["challenger"], "keytypes" : ["AccountName"] } ] } After constructing the Game object, I try to retrieve the data through eosc get table, however I got empty array as the following:

{ "rows": [{ "challenger": "inita", "host": "initb", "turn": "initb", "winner": "", "board": [] } ], "more": false } Anybody, has ever face the same issue?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/EOSIO/eos/issues/564, or mute the thread https://github.com/notifications/unsubscribe-auth/Ad6ZPimvL_VWq8_a2HkbVvyhUO4HrmPfks5srzclgaJpZM4P4RD8.

ghost avatar Oct 13 '17 11:10 ghost

@nanonano7 Oh yeah, the trick works! I need to specify the board_len before board inside the struct but not inside the abi. This needs to be documented somewhere... Thanks! @learnforpractice The problem is already solved, thanks for the support :) I'm trying to make a tic-tac-toe game on top of eos blockchain https://github.com/andriantolie/tic_tac_toe. If you have some time to spend, comments are really welcomed :)

andriantolie avatar Oct 16 '17 10:10 andriantolie

Well, if if the size of board >= 128, the length bytes is >=2, so it will still be a problem.

See code in libraries/fc/include/fc/io/raw.hpp

template<typename Stream> inline void pack( Stream& s, const unsigned_int& v ) {
  uint64_t val = v.value;
  do {
    uint8_t b = uint8_t(val) & 0x7f;
    val >>= 7;
    b |= ((val > 0) << 7);
    s.write((char*)&b,1);//.put(b);
  }while( val );
}

template inline void unpack( Stream& s, signed_int& vi ) { uint32_t v = 0; char b = 0; int by = 0; do { s.get(b); v |= uint32_t(uint8_t(b) & 0x7f) << by; by += 7; } while( uint8_t(b) & 0x80 ); vi.value = ((v>>1) ^ (v>>31)) + (v&0x01); vi.value = v&0x01 ? vi.value : -vi.value; vi.value = -vi.value; }

// std::vector<char>
template<typename Stream> inline void pack( Stream& s, const std::vector<char>& value ) {
  fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
  if( value.size() )
    s.write( &value.front(), (uint32_t)value.size() );
}
template<typename Stream> inline void unpack( Stream& s, std::vector<char>& value ) {
  unsigned_int size; fc::raw::unpack( s, size );
  FC_ASSERT( size.value < MAX_ARRAY_ALLOC_SIZE );
  value.resize(size.value);
  if( value.size() )
    s.read( value.data(), value.size() );
}

So if length>=128, you have to unpack the length bytes manually.

I think the eos team should think about add this function to the eoslib.

On 16 Oct 2017, at 6:24 PM, Andrianto Lie [email protected] wrote:

@nanonano7 https://github.com/nanonano7 Oh yeah, the trick works! I need to specify the board_len before board inside the struct but not inside the abi. This needs to be documented somewhere... Thanks! @learnforpractice https://github.com/learnforpractice The problem is already solved, thanks for the support :) I'm trying to make a tic-tac-toe game on top of eos blockchain https://github.com/andriantolie/tic_tac_toe https://github.com/andriantolie/tic_tac_toe. If you have some time to spend, comments are really welcomed :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EOSIO/eos/issues/564#issuecomment-336845242, or mute the thread https://github.com/notifications/unsubscribe-auth/Ad6ZPhcRHBc5Wwnmk7JQdzJ7VbkEu3ERks5ssy7AgaJpZM4P4RD8.

ghost avatar Oct 16 '17 13:10 ghost

There is a error in the previous post, the length is unsigned, not signed,so the unpack method is as below:

template<typename Stream> inline void unpack( Stream& s, unsigned_int& vi ) {
  uint64_t v = 0; char b = 0; uint8_t by = 0;
  do {
      s.get(b);
      v |= uint32_t(uint8_t(b) & 0x7f) << by;
      by += 7;
  } while( uint8_t(b) & 0x80 );
  vi.value = static_cast<uint32_t>(v);
}

On 16 Oct 2017, at 9:13 PM, john rok [email protected] wrote:

Well, if if the size of board >= 128, the length bytes is 2, so it will still be a problem.

See code in libraries/fc/include/fc/io/raw.hpp

template<typename Stream> inline void pack( Stream& s, const unsigned_int& v ) {
  uint64_t val = v.value;
  do {
    uint8_t b = uint8_t(val) & 0x7f;
    val >>= 7;
    b |= ((val > 0) << 7);
    s.write((char*)&b,1);//.put(b);
  }while( val );
}

template inline void unpack( Stream& s, signed_int& vi ) { uint32_t v = 0; char b = 0; int by = 0; do { s.get(b); v |= uint32_t(uint8_t(b) & 0x7f) << by; by += 7; } while( uint8_t(b) & 0x80 ); vi.value = ((v>>1) ^ (v>>31)) + (v&0x01); vi.value = v&0x01 ? vi.value : -vi.value; vi.value = -vi.value; }

// std::vector<char>
template<typename Stream> inline void pack( Stream& s, const std::vector<char>& value ) {
  fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
  if( value.size() )
    s.write( &value.front(), (uint32_t)value.size() );
}
template<typename Stream> inline void unpack( Stream& s, std::vector<char>& value ) {
  unsigned_int size; fc::raw::unpack( s, size );
  FC_ASSERT( size.value < MAX_ARRAY_ALLOC_SIZE );
  value.resize(size.value);
  if( value.size() )
    s.read( value.data(), value.size() );
}

So if length>=128, you have to unpack the length bytes manually.

I think the eos team should think about add this function to the eoslib.

On 16 Oct 2017, at 6:24 PM, Andrianto Lie <[email protected] mailto:[email protected]> wrote:

@nanonano7 https://github.com/nanonano7 Oh yeah, the trick works! I need to specify the board_len before board inside the struct but not inside the abi. This needs to be documented somewhere... Thanks! @learnforpractice https://github.com/learnforpractice The problem is already solved, thanks for the support :) I'm trying to make a tic-tac-toe game on top of eos blockchain https://github.com/andriantolie/tic_tac_toe https://github.com/andriantolie/tic_tac_toe. If you have some time to spend, comments are really welcomed :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EOSIO/eos/issues/564#issuecomment-336845242, or mute the thread https://github.com/notifications/unsubscribe-auth/Ad6ZPhcRHBc5Wwnmk7JQdzJ7VbkEu3ERks5ssy7AgaJpZM4P4RD8.

ghost avatar Oct 16 '17 13:10 ghost

Yes, you are right, it will cause problem when length >=128. Let's open another issue to increase visibility

andriantolie avatar Oct 18 '17 07:10 andriantolie