Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the admin-site-enhancements domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hide-my-wp domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the unlimited-elements-for-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wp-security-audit-log domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 27

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 35

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/betterdocs-pro/includes/Admin/Analytics.php on line 25

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2905

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2908

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2909

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2910

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2911

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2919
ذخیره‌گاه (Storage) | پراناتک

ذخیره‌گاه (Storage)

فهرست محتوا

ذخیره‌گاه یه نگاشت از کلید (key) به مقداره (value). تعداد کلیدها تو تمام موارد کاربردی بی نهایته. هر مقدار تا 32 بایت میتونه باشه. و اول کار با صفر مقدار دهی میشن.

ذخیره‌گاه شبیه SSD کامپیوتر شماست. حافظه غیر فراره.

منبع: https://docs.alchemy.com/docs/smart-contract-storage-layout

ما ذخیره‌گاه رو به عنوان دیکشنری در نظر میگیریم.

class KeyValue:
    def __init__(self): self.storage = {}
    
    def load (self, key)       : return self.storage[key]
    def store(self, key, value): self.storage[key] = value

گرم/سرد #

هزینه دسترسی به اسلات های گرم و سرد ذخیره‌گاه متفاوته. یک اسلات زمانی گرم در نظر گرفته می‌شه که قبلاً به اون دسترسی پیدا کرده باشیم. در غیر این صورت، بهش میگیم سرد. دسترسی به یک اسلات سرد، هزینه گس (gas) بیشتری نسبت به دسترسی به یک اسلات گرم داره.

ما این منطق رو با ردیابی یه حافظه کش (cache) پیاده سازی می‌کنیم. وقتی یه اسلات حافظه رو بارگذاری می‌کنیم، کلید اونو توی کش ذخیره می‌کنیم. اگه کلیدی توی کش وجود داشته باشه، میگیم که گرمه.

class Storage(KeyValue):
    def __init__(self):
        super().__init__()
        self.cache = []
        
    def load(self, key):
        warm = True if key in self.cache else False
        if not warm: self.cache.append(key)
        if key not in self.storage: return 0x00
        return warm, super().load(key)
storage = Storage()

ما 420 و توی اسلات شماره 1 ه ذخیره‌گاه ذخیره میکنیم.

storage.store(1, 420)

دقت کنین که اولین باری که چیزی از اسلات 1 بازیابی می‌کنیم، سرد ه.

storage.load(1)
(False, 420)

الان اسلات شماره1 گرم شد.

storage.load(1)
(True, 420)

خوندن یه مقدار تصادفی از ذخیره‌گاه که قبلاً مقدارههی نشده، عدد صفر را برمی‌گردونه و باعث ایجاد خطا نمی‌شه.

storage.load(42069)
0