1.What is chainbase used for?
we can find the answer in :
about chainbase
2.when we add a new object (i.e., row) to the table by Multi-Index such as
class addressbook: contract {
struct address {
uint64_t account_name;
string first_name;
string last_name;
string street;
string city;
string state;
uint64_t primary_key() const { return account_name; }
EOSLIB_SERIALIZE( address, (account_name)(first_name)(last_name)(street)(city)(state) )
};
public:
addressbook(account_name self):contract(self) {}
typedef eosio::multi_index< N(address), address > address_index;
void myaction() {
address_index addresses(_self, _self); // code, scope
// add to table, first argument is account to bill for storage
addresses.emplace(_self, [&](auto& address) {
address.account_name = N(dan);
address.first_name = "Daniel";
address.last_name = "Larimer";
address.street = "1 EOS Way";
address.city = "Blacksburg";
address.state = "VA";
});
}
}
when we use emplace call the funtion db_store_i64 ,then we can find db_store_i64 in apply_context.cpp
int apply_context::db_store_i64( uint64_t code, uint64_t scope, uint64_t table, const account_name& payer, uint64_t id, const char* buffer, size_t buffer_size ) {
// require_write_lock( scope );
const auto& tab = find_or_create_table( code, scope, table, payer );
auto tableid = tab.id;
EOS_ASSERT( payer != account_name(), invalid_table_payer, "must specify a valid account to pay for new record" );
const auto& obj = db.create<key_value_object>( [&]( auto& o ) {
o.t_id = tableid;
o.primary_key = id;
o.value.resize( buffer_size );
o.payer = payer;
memcpy( o.value.data(), buffer, buffer_size );
});
db.modify( tab, [&]( auto& t ) {
++t.count;
});
db is a object of chianbase ,after a series operate the data has inserted to the db,then we can get the info how we use or update our RAM used by this function update_db_usage
void apply_context::update_db_usage( const account_name& payer, int64_t delta ) {
if( delta > 0 ) {
if( !(privileged || payer == account_name(receiver)) ) {
require_authorization( payer );
}
}
trx_context.add_ram_usage(payer, delta);
}
above all is all i know about chainbase.