Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PaymentServer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Administrator
PaymentServer
Commits
405ae318
Commit
405ae318
authored
5 years ago
by
Ramakrishnan Muthukrishnan
Browse files
Options
Downloads
Patches
Plain Diff
refactor code to accomodate SQLite based voucher database
parent
4e01a543
No related branches found
Branches containing commit
No related tags found
1 merge request
!26
Initial implementation of Persistence using sqlite
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/PaymentServer/Main.hs
+7
-1
7 additions, 1 deletion
src/PaymentServer/Main.hs
src/PaymentServer/Persistence.hs
+18
-27
18 additions, 27 deletions
src/PaymentServer/Persistence.hs
with
25 additions
and
28 deletions
src/PaymentServer/Main.hs
+
7
−
1
View file @
405ae318
...
...
@@ -25,7 +25,7 @@ import Network.Wai.Middleware.RequestLogger
)
import
PaymentServer.Persistence
(
memory
,
Database
(
Memory
,
SQLite3
)
,
getDBConnection
)
import
PaymentServer.Issuer
(
trivialIssue
...
...
@@ -64,6 +64,11 @@ data Issuer =
|
Ristretto
deriving
(
Show
,
Eq
,
Ord
,
Read
)
data
Database
=
Memory
|
SQLite3
deriving
(
Show
,
Eq
,
Ord
,
Read
)
data
ServerConfig
=
ServerConfig
{
issuer
::
Issuer
,
signingKey
::
Maybe
Text
...
...
@@ -111,6 +116,7 @@ main =
getDatabase
ServerConfig
{
database
,
databasePath
}
=
case
(
database
,
databasePath
)
of
(
Memory
,
Nothing
)
->
Right
memory
(
SQLite3
,
Just
path
)
->
Right
(
getDBConnection
path
)
_
->
Left
"invalid options"
in
do
config
<-
execParser
opts
...
...
This diff is collapsed.
Click to expand it.
src/PaymentServer/Persistence.hs
+
18
−
27
View file @
405ae318
...
...
@@ -4,9 +4,8 @@ module PaymentServer.Persistence
(
Voucher
,
Fingerprint
,
RedeemError
(
NotPaid
,
AlreadyRedeemed
)
,
Database
(
Memory
,
SQLite3
)
,
VoucherDatabase
(
payForVoucher
,
redeemVoucher
)
,
Memory
VoucherDatabase
,
VoucherDatabase
State
(
MemoryDB
,
SQLiteDB
)
,
memory
,
getDBConnection
-- * for testing
...
...
@@ -50,11 +49,6 @@ data RedeemError =
|
AlreadyRedeemed
deriving
(
Show
,
Eq
)
data
Database
=
Memory
|
SQLite3
deriving
(
Show
,
Eq
,
Ord
,
Read
)
-- | A fingerprint cryptographically identifies a redemption of a voucher.
-- When a voucher is redeemed, a number of random tokens are received
-- alongside it. These tokens are signed to create ZKAPs to return to the
...
...
@@ -87,7 +81,7 @@ class VoucherDatabase d where
-- in-memory. The state does not outlive the process which creates it (nor
-- even the MemoryVoucherDatabase value). This is primarily useful for
-- testing.
data
Memory
VoucherDatabase
=
data
VoucherDatabase
State
=
MemoryDB
{
-- | A set of vouchers which have been paid for.
paid
::
IORef
(
Set
.
Set
Voucher
)
...
...
@@ -95,11 +89,13 @@ data MemoryVoucherDatabase =
-- redemption.
,
redeemed
::
IORef
(
Map
.
Map
Voucher
Fingerprint
)
}
|
SQLiteDB
{
conn
::
Sqlite
.
Connection
}
instance
VoucherDatabase
Memory
VoucherDatabase
where
instance
VoucherDatabase
VoucherDatabase
State
where
payForVoucher
MemoryDB
{
paid
=
paid
,
redeemed
=
redeemed
}
voucher
=
do
modifyIORef
paid
(
Set
.
insert
voucher
)
return
()
payForVoucher
SQLiteDB
{
conn
=
conn
}
voucher
=
insertVoucher
conn
voucher
redeemVoucher
MemoryDB
{
paid
=
paid
,
redeemed
=
redeemed
}
voucher
fingerprint
=
do
unpaid
<-
Set
.
notMember
voucher
<$>
readIORef
paid
...
...
@@ -115,26 +111,14 @@ instance VoucherDatabase MemoryVoucherDatabase where
return
$
Right
()
else
return
$
Left
AlreadyRedeemed
-- | Create a new, empty MemoryVoucherDatabase.
memory
::
IO
MemoryVoucherDatabase
memory
=
do
paid
<-
newIORef
mempty
redeemed
<-
newIORef
mempty
return
$
MemoryDB
paid
redeemed
instance
VoucherDatabase
Sqlite
.
Connection
where
-- payForVoucher :: Sqlite.Connection -> Voucher -> IO ()
payForVoucher
=
insertVoucher
-- redeemVoucher :: Sqlite.Connection -> Voucher -> Fingerprint -> IO (Either RedeemError ())
redeemVoucher
dbConn
voucher
fingerprint
=
do
unpaid
<-
isVoucherUnpaid
dbConn
voucher
existingFingerprint
<-
getVoucherFingerprint
dbConn
voucher
redeemVoucher
SQLiteDB
{
conn
=
conn
}
voucher
fingerprint
=
do
unpaid
<-
isVoucherUnpaid
conn
voucher
existingFingerprint
<-
getVoucherFingerprint
conn
voucher
case
(
unpaid
,
existingFingerprint
)
of
(
True
,
_
)
->
return
$
Left
NotPaid
(
False
,
[]
)
->
do
insertVoucherAndFingerprint
dbC
onn
voucher
fingerprint
insertVoucherAndFingerprint
c
onn
voucher
fingerprint
return
$
Right
()
(
False
,
[
fingerprint'
])
->
if
fingerprint
==
fingerprint'
then
...
...
@@ -143,6 +127,13 @@ instance VoucherDatabase Sqlite.Connection where
return
$
Left
AlreadyRedeemed
-- | Create a new, empty MemoryVoucherDatabase.
memory
::
IO
VoucherDatabaseState
memory
=
do
paid
<-
newIORef
mempty
redeemed
<-
newIORef
mempty
return
$
MemoryDB
paid
redeemed
instance
FromRow
Fingerprint
where
fromRow
=
Sqlite
.
field
...
...
@@ -164,9 +155,9 @@ insertVoucherAndFingerprint :: Sqlite.Connection -> Voucher -> Fingerprint -> IO
insertVoucherAndFingerprint
dbConn
voucher
fingerprint
=
Sqlite
.
execute
dbConn
"INSERT INTO redeemed (voucher_id, fingerprint) VALUES ((SELECT id FROM vouchers_new WHERE name = ?), ?)"
(
voucher
,
fingerprint
)
getDBConnection
::
Text
->
IO
Sqlite
.
Connection
getDBConnection
::
Text
->
IO
VoucherDatabaseState
getDBConnection
name
=
do
dbConn
<-
Sqlite
.
open
(
unpack
name
)
Sqlite
.
execute_
dbConn
"CREATE TABLE IF NOT EXISTS vouchers (id INTEGER PRIMARY KEY, name TEXT UNIQUE)"
Sqlite
.
execute_
dbConn
"CREATE TABLE IF NOT EXISTS redeemed (id INTEGER PRIMARY KEY, voucher_id INTEGER, fingerprint TEXT, FOREIGN KEY (voucher_id) REFERENCES vouchers(id))"
return
dbConn
return
$
SQLiteDB
dbConn
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment