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

Saving Table with FixedString32 #475

Closed
michaeljyeates opened this issue Sep 19, 2017 · 16 comments
Closed

Saving Table with FixedString32 #475

michaeljyeates opened this issue Sep 19, 2017 · 16 comments
Labels
Milestone

Comments

@michaeljyeates
Copy link

I am writing a simple test contract which saves a struct with (Name,char[32]). Saving seems to work ok but when I try to read nothing is returned and eosc get table dbtest dbtest test fails with the following error. Attached is a simple test case, I think the length of 40 is correct so I am not sure why it has a negative over figure

1377035ms thread-0 main.cpp:906 main ] Failed with error: 10 assert_exception: Assert Exception
status_code == 200: Error
: 8 out_of_range_exception: Out of Range
read datastream of length 40 over by -30
{"method":"read","len":40,"over":-30}
thread-0 datastream.cpp:6 throw_datastream_range_error

{"msg":"8 out_of_range_exception: Out of Range\nread datastream of length 40 over by -30\n    {\"method\":\"read\",\"len\":40,\"over\":-30}\n    thread-0  datastream.cpp:6 throw_datastream_range_error"}
thread-0  httpc.cpp:113 call

{"server":"localhost","port":8888,"path":"/v1/chain/get_table_rows","postdata":{"json":true,"scope":"dbtest","code":"dbtest","table":"test"}}
thread-0  httpc.cpp:117 call

dbtest.zip

@liderako
Copy link

Can you change the value in the table?

@michaeljyeates
Copy link
Author

Yes, if you look at the build.sh script it sets the value but then I cannot read it back out (maybe just my lack of c++ skills), the eosc command still asserts so I cannot verify that anything was written

@liderako
Copy link

maybe type char, doesn't work.
Do you know that error?

3234004ms thread-0 producer_plugin.cpp:199 block_production_loo ] Got exception while generating block:
10 assert_exception: Assert Exception
itr != index.end():
{}
thread-0 chain_controller.cpp:112 get_recent_transaction

Output in eosd window

Maybe. At you too she will be.

@michaeljyeates
Copy link
Author

Yes I get that error every time a message is sent but it doesnt seem to break anything

@heifner
Copy link
Contributor

heifner commented Sep 19, 2017

The get_recent_transaction is issue #429.

@pavybez
Copy link

pavybez commented Sep 20, 2017

@michaeljyeates can you attach your implementation so that I can debug?

Edit: Just saw your attachment.

@pavybez pavybez self-assigned this Sep 20, 2017
@pavybez
Copy link

pavybez commented Sep 20, 2017

Can you modify your abi file as below and check again please? I changed the keyname in table section from "teststring" to "owner". I will continue debugging it tomorrow.

{ "types": [{ "newTypeName": "AccountName", "type": "Name" } ], "structs": [{ "name": "teststring", "base": "", "fields": { "owner": "Name", "name": "FixedString32" } } ], "actions": [{ "action": "store", "type": "teststring" },{ "action": "get", "type": "teststring" } ], "tables": [{ "table": "test", "type": "teststring", "indextype": "i64", "keynames" : ["owner"], "keytypes" : ["Name"] } ] }

@michaeljyeates
Copy link
Author

I tried changing that, same problem

@michaeljyeates
Copy link
Author

Is there a way to delete the database or remove all entries?

@elmato
Copy link
Contributor

elmato commented Sep 20, 2017 via email

@elmato
Copy link
Contributor

elmato commented Sep 21, 2017

After some debugging here is a quick fix for your contract.

In dbtest.abi keynames of the table should be

"keynames" : ["owner"],

That change is needed to later query the tables using eosc

./eosc get table dbtest dbtest test

In dbtest.hpp TestString type should be

struct PACKED(TestString) {
     AccountName       owner;
     char              name[33];
};
static_assert( sizeof(TestString) == sizeof(uint64_t)+33, "unexpected packing" );

since the first byte of a serialized FixedString32 indicates its length.

Also in dbtest.hpp the call to Tests::get should be

Tests::get( owner, teststr );

otherwise you will be calling

static bool get( Record& r, uint64_t s = scope ) {
   return impl::load_primary( s, code, table, &r, sizeof(Record) ) == sizeof(Record);
}

and a tx_missing_scope will arise since you were using owner as the scope to read from.

And finally in dbtest.cpp

print("Store string", (const char *)(teststr.name+1), "\n");
...
print("GOT teststring", (const char *)(teststr_row.name+1), "\n");

Note: This is a quick fix to make your contract work, in future versions the handling of types like FixedString32 inside the contract code will be easier.

@elmato
Copy link
Contributor

elmato commented Sep 21, 2017

Here is the output from ./eosc get table dbtest dbtest test

{
  "rows": [{
      "owner": "dbtest",
      "name": "0123456789ABCDEF0123456789ABCDEF"
    },{
      "owner": "iiii",
      "name": "0123456789ABCDEF0123456789ABCDEF"
    },{
      "owner": "xx",
      "name": "0123456789ABCDEF0123456789ABCDEF"
    },{
      "owner": "yy",
      "name": "0123456789ABCDEF0123456789ABCDEF"
    }
  ],
  "more": false
}

@pavybez pavybez closed this as completed Sep 21, 2017
@michaeljyeates
Copy link
Author

I am getting this error now

./dbtest.hpp:15:5: error: static_assert failed "unexpected packing"

Attached is my current version
dbtest.zip

@elmato
Copy link
Contributor

elmato commented Sep 22, 2017

Are you using PACKED when declaring TestString?

struct PACKED(TestString) {
     AccountName       owner;
     char              name[33];
};

@michaeljyeates
Copy link
Author

That worked, thanks!

@thomasbcox thomasbcox added this to the EOS Dawn 1.1 milestone Nov 9, 2017
@gpmn
Copy link

gpmn commented May 30, 2018

thanks this post.
I get the same issue today. following this post's suggestion, I update my struct, then my contract works fine.
e.g., (schema1)(schema2) is missed in wrong version.

 // @abi table                                                                                                                      
 struct cont{
      uint64_t             key;
      name                 issuer;
      uint64_t             schema1;
      uint64_t             schema2;
      std::string          link;                                                                                           
      auto primary_key()const {return key;}

      EOSLIB_SERIALIZE(cont, (key)(issuer)(link))
      // should change to the following:
      // EOSLIB_SERIALIZE(cont, (key)(issuer)(schema1)(schema2)(link))

}

nksanthosh added a commit that referenced this issue Aug 3, 2020
This CLI11 update from v1.9.0 to v1.9.1 provides the following improvements: 
- Support relative inclusion [#475](CLIUtils/CLI11#475)
- Fix cases where spaces in paths could break CMake support [#471](CLIUtils/CLI11#471)
- Fix an issue with string conversion [#421](CLIUtils/CLI11#421)
- Cross-compiling improvement for Conan.io [#430](CLIUtils/CLI11#430)
- Fix option group default propagation [#450](CLIUtils/CLI11#450)
- Fix for C++20 [#459](CLIUtils/CLI11#459)
- Support compiling with RTTI off [#461](CLIUtils/CLI11#461)
dimas1185 pushed a commit that referenced this issue Aug 4, 2020
This CLI11 update from v1.9.0 to v1.9.1 provides the following improvements: 
- Support relative inclusion [#475](CLIUtils/CLI11#475)
- Fix cases where spaces in paths could break CMake support [#471](CLIUtils/CLI11#471)
- Fix an issue with string conversion [#421](CLIUtils/CLI11#421)
- Cross-compiling improvement for Conan.io [#430](CLIUtils/CLI11#430)
- Fix option group default propagation [#450](CLIUtils/CLI11#450)
- Fix for C++20 [#459](CLIUtils/CLI11#459)
- Support compiling with RTTI off [#461](CLIUtils/CLI11#461)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants