Skip to content

StateDB: commit() does not allow to create new account #2578

@cakevm

Description

@cakevm

I try to insert a new account into the StateDB, like I would for the CacheDB, but I get an error All accounts should be present inside cache.

StateDB has insert_account(...) and CacheDB has insert_account_info(...), but I would like to use both just based on the traits Database + DatabaseCommit. For that reason I am using commit(...) for changing the state. If the error is an intended behavior feel free to close this issue.

Here the error:

let this_account = self
.accounts
.get_mut(&address)
.expect("All accounts should be present inside cache");

Here is a test that fails for me:

#[test]
fn test_state_db_commit_created_account() {
    let address = Address::with_last_byte(42);
    let db = CacheDB::new(EmptyDB::default());
    let mut state = State::builder().with_database(db).build();
    
    let account = Account {
        info: AccountInfo::default(),
        transaction_id: 0,
        storage: Default::default(),
        status: state::AccountStatus::Touched.union(state::AccountStatus::Created),
    };
    state.commit(HashMap::from_iter([(address, account)]));

    assert!(state.basic(address).unwrap().is_some());
}

For CacheDB it works:

#[test]
fn test_cache_db_commit_created_account() {
    let address = Address::with_last_byte(42);
    let mut db = CacheDB::new(EmptyDB::default());
    
    let account = Account {
        info: AccountInfo::default(),
        transaction_id: 0,
        storage: Default::default(),
        status: state::AccountStatus::Touched.union(state::AccountStatus::Created),
    };
    db.commit(HashMap::from_iter([(address, account)]));

    assert!(db.basic(address).unwrap().is_some());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions