- From a LevelDB perspective, where are tokens stored? Is it part of account info? Or is it only part of the smart contract?
LevelDB is key/value storage then the key will depends on the smart contract address and the address of the token holder in the balance mapping. We can say that token balance is part of smart contract's state and the account is only the key in the smart contract balance mapping.
For detail you can refer to this link
https://medium.com/aigang-network/how-to-read-ethereum-contract-storage-44252c8af925
- If it is only part of the contract, how does block explorer know what
tokens each account has?
Blockexplorer will watch events or reading tnx's receipt/logs of relevant token smart contract's transactions to record the token balance of each account/address.
- Anyone can point me to the relevant implementation files in aleth
impl?
This is the implement code to read the data by specific key
// GetState returns a value in account storage.
func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
value, exists := self.cachedStorage[key]
if exists {
return value
}
// Load from DB in case it is missing.
enc, err := self.getTrie(db).TryGet(key[:])
if err != nil {
self.setError(err)
return common.Hash{}
}
if len(enc) > 0 {
_, content, _, err := rlp.Split(enc)
if err != nil {
self.setError(err)
}
value.SetBytes(content)
}
self.cachedStorage[key] = value
return value
}