Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
sakia
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor 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
clients
python
sakia
Commits
726968d4
Commit
726968d4
authored
9 years ago
by
inso
Browse files
Options
Downloads
Patches
Plain Diff
Handle rollbacks in the cache
parent
83b5f7ce
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cutecoin/core/net/api/bma/access.py
+32
-4
32 additions, 4 deletions
src/cutecoin/core/net/api/bma/access.py
with
32 additions
and
4 deletions
src/cutecoin/core/net/api/bma/access.py
+
32
−
4
View file @
726968d4
from
PyQt5.QtCore
import
QObject
,
pyqtSlot
from
PyQt5.QtNetwork
import
QNetworkReply
from
ucoinpy.api.bma
import
blockchain
from
ucoinpy.documents
import
Block
,
BlockId
from
.....tools.exceptions
import
NoPeerAvailable
from
.....
import
__version__
import
logging
...
...
@@ -25,6 +26,7 @@ class BmaAccess(QObject):
"""
super
().
__init__
()
self
.
_data
=
data
self
.
_rollback_to
=
None
self
.
_pending_requests
=
{}
self
.
_network
=
network
...
...
@@ -50,11 +52,12 @@ class BmaAccess(QObject):
:param dict data: The cache in json format
"""
data
=
{}
for
entry
in
json_data
:
for
entry
in
json_data
[
'
entries
'
]
:
key
=
entry
[
'
key
'
]
cache_key
=
(
key
[
0
],
key
[
1
],
key
[
2
],
key
[
3
],
key
[
4
])
data
[
cache_key
]
=
entry
[
'
value
'
]
self
.
_data
=
data
self
.
_rollback_to
=
json_data
[
'
rollback
'
]
def
jsonify
(
self
):
"""
...
...
@@ -67,7 +70,8 @@ class BmaAccess(QObject):
for
d
in
data
:
entries
.
append
({
'
key
'
:
d
,
'
value
'
:
data
[
d
]})
return
entries
return
{
'
rollback
'
:
self
.
_rollback_to
,
'
entries
'
:
entries
}
@staticmethod
def
_gen_cache_key
(
request
,
req_args
,
get_args
):
...
...
@@ -99,13 +103,21 @@ class BmaAccess(QObject):
Get data from the cache
:param request: The requested data
:param cache_key: The key
:r
eturn:
:r
type: tuple[bool, dict]
"""
cache_key
=
BmaAccess
.
_gen_cache_key
(
request
,
req_args
,
get_args
)
if
cache_key
in
self
.
_data
.
keys
():
cached_data
=
self
.
_data
[
cache_key
]
need_reload
=
True
if
str
(
request
)
in
BmaAccess
.
__saved_requests
\
# If we detected a rollback
# We reload if we don't know if this block changed or not
if
self
.
_rollback_to
:
if
request
is
blockchain
.
Block
:
if
get_args
[
"
number
"
]
>=
self
.
_rollback_to
:
need_reload
=
True
if
request
is
blockchain
.
Parameters
and
self
.
_rollback_to
==
0
:
need_reload
=
True
elif
str
(
request
)
in
BmaAccess
.
__saved_requests
\
or
cached_data
[
'
metadata
'
][
'
block_hash
'
]
==
self
.
_network
.
current_blockid
.
sha_hash
:
need_reload
=
False
ret_data
=
cached_data
[
'
value
'
]
...
...
@@ -114,6 +126,20 @@ class BmaAccess(QObject):
ret_data
=
None
return
need_reload
,
ret_data
def
_update_rollback
(
self
,
request
,
req_args
,
get_args
,
data
):
"""
Update the rollback
:param class request: A bma request class calling for data
:param dict req_args: Arguments to pass to the request constructor
:param dict get_args: Arguments to pass to the request __get__ method
:param dict data: Json data got from the blockchain
"""
if
self
.
_rollback_to
and
request
is
blockchain
.
Block
:
if
get_args
[
'
number
'
]
>=
self
.
_rollback_to
:
cache_key
=
BmaAccess
.
_gen_cache_key
(
request
,
req_args
,
get_args
)
if
cache_key
in
self
.
_data
and
self
.
_data
[
cache_key
][
'
value
'
][
'
hash
'
]
==
data
[
'
hash
'
]:
self
.
_rollback_to
=
get_args
[
'
number
'
]
def
_update_cache
(
self
,
request
,
req_args
,
get_args
,
data
):
"""
Update data in cache and returns True if cached data changed
...
...
@@ -124,6 +150,8 @@ class BmaAccess(QObject):
:return: True if data changed
:rtype: bool
"""
self
.
_update_rollback
(
request
,
req_args
,
get_args
,
data
)
cache_key
=
BmaAccess
.
_gen_cache_key
(
request
,
req_args
,
get_args
)
if
cache_key
not
in
self
.
_data
:
self
.
_data
[
cache_key
]
=
{
'
metadata
'
:
{},
...
...
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