Skip to content

Commit

Permalink
Merge pull request #269 from Concordium/get-account-info-by-index
Browse files Browse the repository at this point in the history
Fixes #259: Expose foundation account address
  • Loading branch information
rimbi authored Apr 8, 2022
2 parents cf8f2e6 + 4d6ab9e commit 56a2e64
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
which they returned previously.
- Fix issue #244: Collector to keep querying. Remove the parameter for maximum allowed
times a gRPC call can fail and keeps `node-collector` querying forever.
- `GetAccountInfo` endpoint supports querying the account via the account index.

## concordium-node 3.0.1

Expand Down
15 changes: 3 additions & 12 deletions concordium-consensus/src/Concordium/External.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import qualified Data.FixedByteString as FBS
import Concordium.Afgjort.Finalize.Types (FinalizationInstance (FinalizationInstance))
import Concordium.Birk.Bake
import Concordium.Constants.Time (defaultEarlyBlockThreshold, defaultMaxBakingDelay)
import Concordium.Crypto.ByteStringHelpers
import Concordium.GlobalState
import Concordium.GlobalState.Persistent.LMDB (addDatabaseVersion)
import Concordium.GlobalState.Persistent.TreeState (InitException (..))
Expand Down Expand Up @@ -891,15 +890,6 @@ decodeBlockHash blockcstr = readMaybe <$> peekCString blockcstr
decodeAccountAddress :: CString -> IO (Either String AccountAddress)
decodeAccountAddress acctstr = addressFromBytes <$> BS.packCString acctstr

-- |Decode a null-terminated string as either an account address (base-58) or a
-- credential registration ID (base-16).
decodeAccountAddressOrCredId :: CString -> IO (Maybe (Either CredentialRegistrationID AccountAddress))
decodeAccountAddressOrCredId str = do
bs <- BS.packCString str
return $ case addressFromBytes bs of
Left _ -> Left <$> bsDeserializeBase16 bs
Right acc -> Just $ Right acc

-- |Decode an instance address from a null-terminated JSON-encoded string.
decodeInstanceAddress :: CString -> IO (Maybe ContractAddress)
decodeInstanceAddress inststr = AE.decodeStrict <$> BS.packCString inststr
Expand Down Expand Up @@ -1071,8 +1061,9 @@ getModuleList cptr blockcstr = do
getAccountInfo :: StablePtr ConsensusRunner -> CString -> CString -> IO CString
getAccountInfo cptr blockcstr acctcstr = do
mblock <- decodeBlockHash blockcstr
maccount <- decodeAccountAddressOrCredId acctcstr
case (mblock, maccount) of
acctbs <- BS.packCString acctcstr
let account = decodeAccountIdentifier acctbs
case (mblock, account) of
(Just bh, Just acct) -> jsonQuery cptr (Q.getAccountInfo bh acct)
_ -> jsonCString AE.Null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ instance (IsProtocolVersion pv, Monad m) => BS.BlockStateQuery (PureBlockStateMo
Nothing -> return Nothing
Just ai -> return $ (ai, ) <$> bs ^? blockAccounts . Accounts.indexedAccount ai

{-# INLINE getAccountByIndex #-}
getAccountByIndex bs ai =
return $ (ai, ) <$> bs ^? blockAccounts . Accounts.indexedAccount ai

{-# INLINE getBakerAccount #-}
getBakerAccount bs (BakerId ai) =
return $ bs ^? blockAccounts . Accounts.indexedAccount ai
Expand Down
5 changes: 5 additions & 0 deletions concordium-consensus/src/Concordium/GlobalState/BlockState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class AccountOperations m => BlockStateQuery m where
-- |Query an account by the id of the credential that belonged to it.
getAccountByCredId :: BlockState m -> CredentialRegistrationID -> m (Maybe (AccountIndex, Account m))

-- |Query an account by the account index that belonged to it.
getAccountByIndex :: BlockState m -> AccountIndex -> m (Maybe (AccountIndex, Account m))

-- |Get the contract state from the contract table of the state instance.
getContractInstance :: BlockState m -> ContractAddress -> m (Maybe Instance)

Expand Down Expand Up @@ -644,6 +647,7 @@ instance (Monad (t m), MonadTrans t, BlockStateQuery m) => BlockStateQuery (MGST
getAccount s = lift . getAccount s
accountExists s = lift . accountExists s
getAccountByCredId s = lift . getAccountByCredId s
getAccountByIndex s = lift . getAccountByIndex s
getBakerAccount s = lift . getBakerAccount s
getContractInstance s = lift . getContractInstance s
getModuleList = lift . getModuleList
Expand Down Expand Up @@ -674,6 +678,7 @@ instance (Monad (t m), MonadTrans t, BlockStateQuery m) => BlockStateQuery (MGST
{-# INLINE getAccount #-}
{-# INLINE accountExists #-}
{-# INLINE getAccountByCredId #-}
{-# INLINE getAccountByIndex #-}
{-# INLINE getBakerAccount #-}
{-# INLINE getContractInstance #-}
{-# INLINE getModuleList #-}
Expand Down
11 changes: 11 additions & 0 deletions concordium-consensus/src/Concordium/GlobalState/Paired.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ instance (Monad m, C.HasGlobalStateContext (PairGSContext lc rc) r, BlockStateQu
return Nothing
(Nothing, _) -> error $ "Cannot get account with credid " ++ show cid ++ " in left implementation"
(_, Nothing) -> error $ "Cannot get account with credid " ++ show cid ++ " in right implementation"
getAccountByIndex (ls, rs) idx = do
a1 <- coerceBSML (getAccountByIndex ls idx)
a2 <- coerceBSMR (getAccountByIndex rs idx)
case (a1, a2) of
(Just (ai1, a1'), Just (ai2, a2')) ->
assert ((getHash a1' :: H.Hash) == getHash a2' && ai1 == ai2) $
return $ Just (ai1, (a1', a2'))
(Nothing, Nothing) ->
return Nothing
(Nothing, _) -> error $ "Cannot get account by index " ++ show idx ++ " in left implementation"
(_, Nothing) -> error $ "Cannot get account by index " ++ show idx ++ " in right implementation"
getBakerAccount (ls, rs) bid = do
a1 <- coerceBSML (getBakerAccount ls bid)
a2 <- coerceBSMR (getBakerAccount rs bid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ doGetAccountByCredId pbs cid = do
bsp <- loadPBS pbs
Accounts.getAccountByCredId cid (bspAccounts bsp)

doGetAccountByIndex :: (IsProtocolVersion pv, MonadBlobStore m) => PersistentBlockState pv -> AccountIndex -> m (Maybe (AccountIndex, PersistentAccount pv))
doGetAccountByIndex pbs idx = do
bsp <- loadPBS pbs
fmap (idx, ) <$> Accounts.indexedAccount idx (bspAccounts bsp)

doGetAccountIndex :: (IsProtocolVersion pv, MonadBlobStore m) => PersistentBlockState pv -> AccountAddress -> m (Maybe AccountIndex)
doGetAccountIndex pbs addr = do
Expand Down Expand Up @@ -1276,6 +1280,7 @@ instance (IsProtocolVersion pv, PersistentState r m) => BlockStateQuery (Persist
getAccount = doGetAccount . hpbsPointers
accountExists = doGetAccountExists . hpbsPointers
getAccountByCredId = doGetAccountByCredId . hpbsPointers
getAccountByIndex = doGetAccountByIndex . hpbsPointers
getContractInstance = doGetInstance . hpbsPointers
getModuleList = doGetModuleList . hpbsPointers
getAccountList = doAccountList . hpbsPointers
Expand Down
9 changes: 6 additions & 3 deletions concordium-consensus/src/Concordium/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -459,19 +459,22 @@ getModuleList :: BlockHash -> MVR gsconf finconf (Maybe [ModuleRef])
getModuleList = liftSkovQueryBlock $ BS.getModuleList <=< blockState

-- |Get the details of an account in the block state.
-- The account can be given either via an address, or via a credential registration id.
-- The account can be given via an address, an account index or a credential registration id.
-- In the latter case we lookup the account the credential is associated with, even if it was
-- removed from the account.
getAccountInfo ::
BlockHash ->
Either CredentialRegistrationID AccountAddress ->
AccountIdentifier ->
MVR gsconf finconf (Maybe AccountInfo)
getAccountInfo blockHash acct =
join
<$> liftSkovQueryBlock
( \bp -> do
bs <- blockState bp
macc <- either (BS.getAccountByCredId bs) (BS.getAccount bs) acct
macc <- case acct of
AccAddress addr -> BS.getAccount bs addr
AccIndex idx -> BS.getAccountByIndex bs idx
CredRegID crid -> BS.getAccountByCredId bs crid
forM macc $ \(aiAccountIndex, acc) -> do
aiAccountNonce <- BS.getAccountNonce acc
aiAccountAmount <- BS.getAccountAmount acc
Expand Down

0 comments on commit 56a2e64

Please sign in to comment.