4

According to the types.h all string-related data-types (or at least those where i would presume that they receive strings) like account_name, token_name and so on are declared as uint64_t and i am curious why it is so?

I stumbled upon this code from EOS-repo:

struct contract {
   contract( account_name n ):_self(n){}
   account_name _self;
};

and looked up the type account_name which is defined in types.h:

typedef uint64_t account_name;
typedef uint64_t permission_name;
typedef uint64_t token_name;
....

At a first glance i thought that strings maybe stored only once per lifetime in the blockchain database to reduce its size and that they will receive something like an ID as uint64 to reuse in further contracts. But then i found from a future related perspective that the range of an uint64 may not fit all needs over upcoming decades. Also the lookup could take some time from up to a specific size.

At a second glance i think it could simply be a hash of this string. If so, will the appropriate string stored as a full string elsewhere?

Any clarification is very appreciated.

Also, if my assumption is right, i'd like to ask how to convert a string into such an uint64-based number within code internally (or which hash-function will be used).

Thanks in advance!

2

2 Answers 2

4

They are not strings, but rather base32 string representations of the uint64s. That’s why they are limited to 12 chars length and only the characters a-z, 1-5 and .

When ever you see the preprocessor function N(something) being called is actually doing the conversion https://github.com/EOSIO/eos/blob/9be89106da1d6a1543eb19dd0f3b96a53e286088/libraries/chain/include/eosio/chain/name.hpp#L21

1
4

The account name is converted into an integer uint64 using the function string_to_name defined in types.hpp file

static constexpr uint64_t string_to_name( const char* str ) {
   uint32_t len = 0;
   while( str[len] ) ++len;
   uint64_t value = 0;
   for( uint32_t i = 0; i <= 12; ++i ) {
      uint64_t c = 0;
      if( i < len && i <= 12 ) c = uint64_t(char_to_symbol( str[i] ));
      if( i < 12 ) {
         c &= 0x1f;
         c <<= 64-5*(i+1);
      }
      else {
         c &= 0x0f;
      }
      value |= c;
   }
   return value;
}

static constexpr  char char_to_symbol( char c ) {
   if( c >= 'a' && c <= 'z' )
      return (c - 'a') + 6;
   if( c >= '1' && c <= '5' )
      return (c - '1') + 1;
   return 0;
}
1
  • Hi, please format code as code block by indenting it with 4 spaces (or press Ctrl+K or the {} button in the editor) and don't use <code> tags. You can also insert inline code with backticks. Bold formatting is applied so: **bold** Commented May 16, 2018 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.