Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cartesapp refactor add relay wallet #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from . import setup, cartridge, replay, scoreboard
# TODO: use settings file instead of init
8 changes: 5 additions & 3 deletions app/cartridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from cartesi.abi import String, Bytes, Bytes32, UInt

from cartesapp.storage import Entity, helpers, seed
from cartesapp.manager import query, mutation, get_metadata, event, output, add_output, emit_event, contract_call
from cartesapp.context import get_metadata
from cartesapp.input import query, mutation
from cartesapp.output import event, output, add_output, emit_event, contract_call

from .riv import riv_get_cartridge_info, riv_get_cartridge_screenshot, riv_get_cartridges_path, riv_get_cover, riv_get_cartridge_outcard
from .setup import AppSettings
from .settings import AppSettings

LOGGER = logging.getLogger(__name__)

Expand All @@ -26,7 +28,7 @@
class Cartridge(Entity):
id = helpers.PrimaryKey(str, 64)
name = helpers.Required(str, index=True, unique=True)
user_address = helpers.Required(str, 66)
user_address = helpers.Required(str, 42)
info = helpers.Optional(helpers.Json, lazy=True)
created_at = helpers.Required(int)
cover = helpers.Optional(bytes, lazy=True)
Expand Down
24 changes: 24 additions & 0 deletions app/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from enum import Enum

class ScoreType(Enum):
default = 0
scoreboard = 1
tournament = 2


class GameplayHash:
cartridge_replays = {}
def __new__(cls):
return cls

@classmethod
def add(cls, cartridge_id, replay_hash):
if cls.cartridge_replays.get(cartridge_id) is None: cls.cartridge_replays[cartridge_id] = {}
cls.cartridge_replays[cartridge_id][replay_hash] = True

@classmethod
def check(cls, cartridge_id, replay_hash):
return cls.cartridge_replays.get(cartridge_id) is None \
or cls.cartridge_replays[cartridge_id].get(replay_hash) is None \
or cls.cartridge_replays[cartridge_id][replay_hash] == False
9 changes: 6 additions & 3 deletions app/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

from cartesi.abi import String, Bytes, Bytes32, Int, UInt

from cartesapp.storage import helpers # TODO: create repo to avoid this relative import hassle
from cartesapp.manager import mutation, get_metadata, add_output, event, emit_event, contract_call # TODO: create repo to avoid this relative import hassle
from cartesapp.storage import helpers
from cartesapp.context import get_metadata
from cartesapp.input import mutation
from cartesapp.output import add_output, event, emit_event, contract_call
from cartesapp.utils import bytes2str

from .setup import AppSettings, ScoreType, GameplayHash
from .settings import AppSettings
from .riv import replay_log
from .common import ScoreType, GameplayHash

LOGGER = logging.getLogger(__name__)

Expand Down
11 changes: 7 additions & 4 deletions app/scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from cartesi.abi import String, Bytes, Bytes32, Int, UInt

from cartesapp.storage import Entity, helpers, seed
from cartesapp.manager import mutation, query, get_metadata, output, add_output, event, emit_event, contract_call
from cartesapp.context import get_metadata
from cartesapp.input import mutation, query
from cartesapp.output import output, add_output, event, emit_event, contract_call
from cartesapp.utils import hex2bytes, str2bytes, bytes2str

from .setup import AppSettings, ScoreType, GameplayHash
from .settings import AppSettings
from .riv import replay_log, riv_get_cartridge_outcard
from .cartridge import Cartridge
from .common import ScoreType, GameplayHash

LOGGER = logging.getLogger(__name__)

Expand All @@ -28,7 +31,7 @@ class Scoreboard(Entity):
id = helpers.PrimaryKey(str, 64)
name = helpers.Required(str, index=True, unique=True)
cartridge_id = helpers.Required(str, 64, index=True)
created_by = helpers.Required(str, 66)
created_by = helpers.Required(str, 42)
created_at = helpers.Required(int)
args = helpers.Optional(str)
in_card = helpers.Optional(bytes)
Expand All @@ -38,7 +41,7 @@ class Scoreboard(Entity):

class Score(Entity):
id = helpers.PrimaryKey(int, auto=True)
user_address = helpers.Required(str, 66, index=True)
user_address = helpers.Required(str, 42, index=True)
timestamp = helpers.Required(int)
score = helpers.Required(int)
scoreboard = helpers.Required(Scoreboard, index=True)
Expand Down
17 changes: 17 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# App Framework settings

# Files with definitions to import
FILES = ['setup','cartridge','replay','scoreboard'] # * Required

# Index outputs in inspect indexer queries
INDEX_OUTPUTS = True # Defaul: False

ENABLE_DAPP_RELAY = False # Defaul: False

ENABLE_WALLET = False # Defaul: False (required to set ENABLE_DAPP_RELAY)


class AppSettings:
rivemu_path = None
cartridges_path = "cartridges"
scoreboard_ttl = 7776000 # 90 days
36 changes: 2 additions & 34 deletions app/setup.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
import os
from enum import Enum
from cartesapp.manager import setup, setting

# TODO: use settings file instead of init
@setting()
class FrameworkSettings:
index_outputs = True

class AppSettings:
rivemu_path = None
cartridges_path = "cartridges"
scoreboard_ttl = 7776000 # 90 days
from cartesapp.manager import setup
from .settings import AppSettings

@setup()
def setup_rivemu():
AppSettings.rivemu_path = os.getenv('RIVEMU_PATH')


class ScoreType(Enum):
default = 0
scoreboard = 1
tournament = 2


class GameplayHash:
cartridge_replays = {}
def __new__(cls):
return cls

@classmethod
def add(cls, cartridge_id, replay_hash):
if cls.cartridge_replays.get(cartridge_id) is None: cls.cartridge_replays[cartridge_id] = {}
cls.cartridge_replays[cartridge_id][replay_hash] = True

@classmethod
def check(cls, cartridge_id, replay_hash):
return cls.cartridge_replays.get(cartridge_id) is None \
or cls.cartridge_replays[cartridge_id].get(replay_hash) is None \
or cls.cartridge_replays[cartridge_id][replay_hash] == False
50 changes: 50 additions & 0 deletions cartesapp/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

from cartesi import Rollup, RollupData, RollupMetadata


###
# Context

class Context(object):
rollup: Rollup | None = None
metadata: RollupMetadata | None = None
module: str | None = None
n_reports: int = 0
n_notices: int = 0
n_vouchers: int = 0
configs = None
dapp_address: str | None = None


def __new__(cls):
return cls

@classmethod
def set_context(cls, rollup: Rollup, metadata: RollupMetadata, module: str, **kwargs):
cls.rollup = rollup
cls.metadata = metadata
cls.module = module
cls.n_reports = 0
cls.n_notices = 0
cls.n_vouchers = 0
cls.configs = kwargs

@classmethod
def clear_context(cls):
cls.rollup = None
cls.metadata = None
cls.module = None
cls.n_reports: 0
cls.n_notices = 0
cls.n_vouchers = 0
cls.configs = None


###
# Helpers

def get_metadata() -> RollupMetadata:
return Context.metadata

def get_dapp_address() -> str | None:
return Context.dapp_address
Empty file added cartesapp/indexer/__init__.py
Empty file.
118 changes: 118 additions & 0 deletions cartesapp/indexer/output_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from pydantic import BaseModel
from typing import Optional, List

from cartesapp.storage import Entity, helpers
from cartesapp.input import query
from cartesapp.output import output, add_output


###
# Indexer model and methods

class Output(Entity):
id = helpers.PrimaryKey(int, auto=True)
output_type = helpers.Required(str) # helpers.Required(OutputType)
msg_sender = helpers.Required(str, 42, lazy=True, index=True)
block_number = helpers.Required(int, lazy=True)
timestamp = helpers.Required(int, lazy=True, index=True)
epoch_index = helpers.Required(int, lazy=True)
input_index = helpers.Required(int)
output_index = helpers.Required(int)
output_module = helpers.Required(str)
output_class = helpers.Required(str)
tags = helpers.Set("OutputTag")

class OutputTag(Entity):
id = helpers.PrimaryKey(int, auto=True)
name = helpers.Required(str, index=True)
output = helpers.Required(Output, index=True)


def add_output_index(metadata,output_type,output_index,output_module,output_class,tags=None):
o = Output(
output_type = output_type.name.lower(),
output_class = output_class,
output_module = output_module,
msg_sender = metadata.msg_sender.lower(),
block_number = metadata.block_number,
timestamp = metadata.timestamp,
epoch_index = metadata.epoch_index,
input_index = metadata.input_index,
output_index = output_index
)
if tags is not None:
for tag in tags:
t = OutputTag(
name = tag,
output = o
)

def get_output_indexes(**kwargs):
tags = kwargs.get('tags')

output_query = Output.select()

tag_query = OutputTag.select()

if tags is not None and len(tags) > 0:
tag_query = tag_query.filter(lambda t: t.name in tags)

if kwargs.get('module') is not None:
output_query = output_query.filter(lambda o: o.output_module == kwargs.get('module').lower())
if kwargs.get('output_type') is not None:
output_query = output_query.filter(lambda o: o.output_type == kwargs.get('output_type').lower())
if kwargs.get('msg_sender') is not None:
output_query = output_query.filter(lambda o: o.msg_sender == kwargs.get('msg_sender').lower())
if kwargs.get('timestamp_gte') is not None:
output_query = output_query.filter(lambda o: o.timestamp >= kwargs.get('timestamp_gte'))
if kwargs.get('timestamp_lte') is not None:
output_query = output_query.filter(lambda o: o.timestamp <= kwargs.get('timestamp_lte'))
if kwargs.get('input_index') is not None:
output_query = output_query.filter(lambda o: o.input_index == kwargs.get('input_index'))

if tags is not None and len(tags) > 0:
query = helpers.distinct(
[o.output_type,o.output_module,o.output_class,o.input_index,o.output_index]
for o in output_query for t in tag_query if t.output == o and helpers.count(t) == len(tags)
)
else:
query = helpers.distinct(
[o.output_type,o.output_module,o.output_class,o.input_index,o.output_index]
for o in output_query for t in tag_query if t.output == o
)

return query.fetch()




class IndexerPayload(BaseModel):
tags: Optional[List[str]]
output_type: Optional[str]
msg_sender: Optional[str]
timestamp_gte: Optional[int]
timestamp_lte: Optional[int]
module: Optional[str]
input_index: Optional[int]

class OutputIndex(BaseModel):
output_type: str
module: str
class_name: str
input_index: int
output_index: int


@output(module_name='indexer')
class IndexerOutput(BaseModel):
data: List[OutputIndex]

@query(module_name='indexer')
def indexer_query(payload: IndexerPayload) -> bool:
out = get_output_indexes(**payload.dict())

output_inds = [OutputIndex(output_type=r[0],module=r[1],class_name=r[2],input_index=r[3],output_index=r[4]) for r in out]

add_output(IndexerOutput(data=output_inds))

return True
Loading