Skip to content

Commit

Permalink
Merge #1768
Browse files Browse the repository at this point in the history
1768: Store delegation reward account balances in the database r=KtorZ a=rvl

### Issue Number

ADP-302 / #1750 / #1759

### Overview

Turns out querying reward account balances can be slow.
This is not good if it's done when listing/getting wallets.
Better to do these queries in a separate thread and cache the results in the database.

- [x] Add new database table for the latest known wallet reward account balance.
- [x] Use the database when getting the reward account balance.
- [x] Add a network layer method to watch the node tip.
- [x] Update the account balance in each wallet when the node tip changes (asynchronously).
- [x] Store reward account balance in db as it arrives.
- [x] Adapt Jörmungandr network layer


Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: KtorZ <[email protected]>
Co-authored-by: Johannes Lund <[email protected]>
  • Loading branch information
4 people authored Jun 29, 2020
2 parents 7ab53ab + c869327 commit 5c7fa3e
Show file tree
Hide file tree
Showing 35 changed files with 448 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .buildkite/push-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fi

advance_branch "$this_branch" "$head"

common_ref=$(git merge-base "$this_branch" "$other_branch" || true)
common_ref=$(git merge-base "origin/$this_branch" "origin/$other_branch" || true)

if [ -n "$common_ref" ]; then
advance_branch "$common_branch" "$common_ref"
Expand Down
1 change: 1 addition & 0 deletions lib/byron/src/Cardano/Wallet/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ serveWallet
(DefaultFieldValues $ getActiveSlotCoefficient gp)
databaseDir
Server.newApiLayer walletEngineTracer params nl' tl db
Server.idleWorker
where
gp@GenesisParameters
{ getGenesisBlockHash
Expand Down
4 changes: 3 additions & 1 deletion lib/byron/src/Cardano/Wallet/Byron/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import Cardano.Wallet.Api.Server
, getTransaction
, getUTxOsStatistics
, getWallet
, idleWorker
, liftHandler
, listAddresses
, listTransactions
Expand Down Expand Up @@ -177,7 +178,8 @@ server byron icarus ntp =
SomeIcarusWallet x -> postIcarusWallet icarus x
SomeTrezorWallet x -> postTrezorWallet icarus x
SomeLedgerWallet x -> postLedgerWallet icarus x
SomeAccount x -> postAccountWallet icarus mkLegacyWallet IcarusKey x
SomeAccount x ->
postAccountWallet icarus mkLegacyWallet IcarusKey idleWorker x
)
:<|> (\wid -> withLegacyLayer wid
(byron , deleteWallet byron wid)
Expand Down
5 changes: 5 additions & 0 deletions lib/byron/src/Cardano/Wallet/Byron/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ withNetworkLayer tr np addrInfo versionData action = do
, postTx = _postTx localTxSubmissionQ
, stakeDistribution = _stakeDistribution
, getAccountBalance = _getAccountBalance
, watchNodeTip = _watchNodeTip
}
where
gp@W.GenesisParameters
Expand Down Expand Up @@ -295,9 +296,13 @@ withNetworkLayer tr np addrInfo versionData action = do
case result of
SubmitSuccess -> pure ()
SubmitFail err -> throwE $ ErrPostTxBadRequest $ T.pack (show err)

_stakeDistribution =
notImplemented "stakeDistribution"

_watchNodeTip =
notImplemented "watchNodeTip"

-- | Type representing a network client running two mini-protocols to sync
-- from the chain and, submit transactions.
type NetworkClient m = OuroborosApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ spec = do
it "NETWORK_PARAMS - Able to fetch network parameters" $ \ctx -> do
r <- request @ApiNetworkParameters ctx Link.getNetworkParams Default Empty
expectResponseCode @IO HTTP.status200 r
let Right d = Quantity <$> mkPercentage (3 % 100)
let Right d = Quantity <$> mkPercentage (3 % 4)
verify r
[ expectField (#decentralizationLevel) (`shouldBe` d) ]
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Data.Quantity
import Data.Text.Class
( toText )
import Test.Hspec
( SpecWith, describe, it, shouldBe, shouldSatisfy, xit )
( SpecWith, describe, it, shouldBe, shouldSatisfy )
import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
Expand All @@ -65,7 +65,6 @@ import Test.Integration.Framework.DSL
, fixturePassphrase
, fixtureWallet
, fixtureWalletWith
, getFromResponse
, getSlotParams
, joinStakePool
, mkEpochInfo
Expand Down Expand Up @@ -265,7 +264,7 @@ spec = do
[ expectField #delegation (`shouldBe` notDelegating [])
]

xit "STAKE_POOLS_JOIN_04 - Rewards accumulate and stop" $ \ctx -> do
it "STAKE_POOLS_JOIN_04 - Rewards accumulate and stop" $ \ctx -> do
w <- fixtureWallet ctx
pool:_ <- map (view #id) . snd
<$> unsafeRequest @[ApiStakePool] ctx (Link.listStakePools arbitraryStake) Empty
Expand Down Expand Up @@ -293,37 +292,38 @@ spec = do
(.> (Quantity 0))
]

-- Quit a pool
quitStakePool @n ctx (w, fixturePassphrase) >>= flip verify
[ expectResponseCode HTTP.status202
, expectField (#status . #getApiT) (`shouldBe` Pending)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
]
eventually "Certificates are inserted after quiting a pool" $ do
let ep = Link.listTransactions @'Shelley w
request @[ApiTransaction n] ctx ep Default Empty >>= flip verify
[ expectListField 0
(#direction . #getApiT) (`shouldBe` Outgoing)
, expectListField 0
(#status . #getApiT) (`shouldBe` InLedger)
, expectListField 1
(#direction . #getApiT) (`shouldBe` Outgoing)
, expectListField 1
(#status . #getApiT) (`shouldBe` InLedger)
]

-- Check that rewards have stopped flowing.
waitForNextEpoch ctx
waitForNextEpoch ctx
reward <- getFromResponse (#balance . #getApiT . #reward) <$>
request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty

waitForNextEpoch ctx
request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty >>= flip verify
[ expectField
(#balance . #getApiT . #reward)
(`shouldBe` reward)
]
-- TODO: Check if we can enable this
-- -- Quit a pool
-- quitStakePool @n ctx (w, fixturePassphrase) >>= flip verify
-- [ expectResponseCode HTTP.status202
-- , expectField (#status . #getApiT) (`shouldBe` Pending)
-- , expectField (#direction . #getApiT) (`shouldBe` Outgoing)
-- ]
-- eventually "Certificates are inserted after quiting a pool" $ do
-- let ep = Link.listTransactions @'Shelley w
-- request @[ApiTransaction n] ctx ep Default Empty >>= flip verify
-- [ expectListField 0
-- (#direction . #getApiT) (`shouldBe` Outgoing)
-- , expectListField 0
-- (#status . #getApiT) (`shouldBe` InLedger)
-- , expectListField 1
-- (#direction . #getApiT) (`shouldBe` Outgoing)
-- , expectListField 1
-- (#status . #getApiT) (`shouldBe` InLedger)
-- ]
--
-- -- Check that rewards have stopped flowing.
-- waitForNextEpoch ctx
-- waitForNextEpoch ctx
-- reward <- getFromResponse (#balance . #getApiT . #reward) <$>
-- request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty
--
-- waitForNextEpoch ctx
-- request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty >>= flip verify
-- [ expectField
-- (#balance . #getApiT . #reward)
-- (`shouldBe` reward)
-- ]

describe "STAKE_POOLS_JOIN_01x - Fee boundary values" $ do
it "STAKE_POOLS_JOIN_01x - \
Expand Down
29 changes: 15 additions & 14 deletions lib/core/src/Cardano/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) )
import Cardano.DB.Sqlite.Delete
( DeleteSqliteDatabaseLog )
import Cardano.Wallet.Logging
( BracketLog, bracketTracer )
import Control.Concurrent.MVar
( newMVar, withMVarMasked )
import Control.Exception
Expand All @@ -65,7 +67,7 @@ import Control.Monad.Logger
import Control.Retry
( constantDelay, limitRetriesByCumulativeDelay, recovering )
import Control.Tracer
( Tracer, traceWith )
( Tracer, contramap, traceWith )
import Data.Aeson
( ToJSON (..) )
import Data.Function
Expand Down Expand Up @@ -214,23 +216,23 @@ startSqliteBackend
-> Tracer IO DBLog
-> Maybe FilePath
-> IO (Either MigrationError SqliteContext)
startSqliteBackend manualMigration autoMigration trace fp = do
(backend, connection) <-
createSqliteBackend trace fp manualMigration (queryLogFunc trace)
lock <- newMVar ()
let traceRun = traceWith trace . MsgRun
startSqliteBackend manualMigration autoMigration tr fp = do
(unsafeBackend, connection) <-
createSqliteBackend tr fp manualMigration (queryLogFunc tr)
lock <- newMVar unsafeBackend
let observe :: IO a -> IO a
observe = bracket_ (traceRun False) (traceRun True)
observe = bracketTracer (contramap MsgRun tr)
let runQuery :: SqlPersistT IO a -> IO a
runQuery cmd = withMVarMasked lock $ const $ observe $ runSqlConn cmd backend
runQuery cmd = withMVarMasked lock $ \backend ->
observe $ runSqlConn cmd backend
autoMigrationResult <-
withForeignKeysDisabled trace connection
withForeignKeysDisabled tr connection
$ runQuery (runMigrationQuiet autoMigration)
& tryJust (matchMigrationError @PersistException)
& tryJust (matchMigrationError @SqliteException)
& fmap join
traceWith trace $ MsgMigrations $ fmap length autoMigrationResult
let ctx = SqliteContext backend runQuery fp trace
traceWith tr $ MsgMigrations $ fmap length autoMigrationResult
let ctx = SqliteContext unsafeBackend runQuery fp tr
case autoMigrationResult of
Left e -> do
destroyDBLayer ctx
Expand Down Expand Up @@ -361,7 +363,7 @@ sqliteConnStr = maybe ":memory:" T.pack
data DBLog
= MsgMigrations (Either MigrationError Int)
| MsgQuery Text Severity
| MsgRun Bool
| MsgRun BracketLog
| MsgConnStr Text
| MsgClosing (Maybe FilePath)
| MsgWillOpenDB (Maybe FilePath)
Expand Down Expand Up @@ -463,8 +465,7 @@ instance ToText DBLog where
MsgMigrations (Left err) ->
"Failed to migrate the database: " <> getMigrationErrorMessage err
MsgQuery stmt _ -> stmt
MsgRun False -> "Running database action - Start"
MsgRun True -> "Running database action - Finish"
MsgRun b -> "Running database action - " <> toText b
MsgWillOpenDB fp -> "Will open db at " <> (maybe "in-memory" T.pack fp)
MsgConnStr connStr -> "Using connection string: " <> connStr
MsgClosing fp -> "Closing database ("+|fromMaybe "in-memory" fp|+")"
Expand Down
Loading

0 comments on commit 5c7fa3e

Please sign in to comment.