Skip to content

Commit 706012c

Browse files
authored
Introduce GasKey to the trie. (#13687)
See near/NEPs#611 This PR adds the GasKey trie key. In subsequent PRs we will introduce a new transaction type to accept gas key transactions, and to implement the gas key actions.
1 parent dcfb6b9 commit 706012c

File tree

16 files changed

+709
-19
lines changed

16 files changed

+709
-19
lines changed

chain/chain/src/resharding/flat_storage_resharder.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use near_primitives::trie_key::col::{self};
1919
use near_primitives::trie_key::trie_key_parsers::{
2020
parse_account_id_from_access_key_key, parse_account_id_from_account_key,
2121
parse_account_id_from_contract_code_key, parse_account_id_from_contract_data_key,
22-
parse_account_id_from_received_data_key, parse_account_id_from_trie_key_with_separator,
22+
parse_account_id_from_gas_key_key, parse_account_id_from_received_data_key,
23+
parse_account_id_from_trie_key_with_separator,
2324
};
2425
use near_primitives::types::{AccountId, BlockHeight};
2526
use near_store::adapter::StoreAdapter;
@@ -764,6 +765,13 @@ fn shard_split_handle_key_value(
764765
store_update,
765766
parse_account_id_from_access_key_key,
766767
)?,
768+
col::GAS_KEY => copy_kv_to_child(
769+
&split_params,
770+
key,
771+
value,
772+
store_update,
773+
parse_account_id_from_gas_key_key,
774+
)?,
767775
col::RECEIVED_DATA => copy_kv_to_child(
768776
&split_params,
769777
key,

chain/chain/src/store/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,19 @@ impl ChainStore {
666666
}
667667
changes
668668
}
669+
StateChangesRequest::SingleGasKeyChanges { keys } => {
670+
let mut changes = StateChanges::new();
671+
for key in keys {
672+
let data_key = trie_key_parsers::get_raw_prefix_for_gas_key(
673+
&key.account_id,
674+
&key.public_key,
675+
);
676+
let storage_key = KeyForStateChanges::from_raw_key(block_hash, &data_key);
677+
let changes_per_key_prefix = storage_key.find_iter(&store);
678+
changes.extend(StateChanges::from_gas_key_changes(changes_per_key_prefix)?);
679+
}
680+
changes
681+
}
669682
StateChangesRequest::AllAccessKeyChanges { account_ids } => {
670683
let mut changes = StateChanges::new();
671684
for account_id in account_ids {
@@ -676,6 +689,16 @@ impl ChainStore {
676689
}
677690
changes
678691
}
692+
StateChangesRequest::AllGasKeyChanges { account_ids } => {
693+
let mut changes = StateChanges::new();
694+
for account_id in account_ids {
695+
let data_key = trie_key_parsers::get_raw_prefix_for_gas_keys(account_id);
696+
let storage_key = KeyForStateChanges::from_raw_key(block_hash, &data_key);
697+
let changes_per_key_prefix = storage_key.find_iter(&store);
698+
changes.extend(StateChanges::from_gas_key_changes(changes_per_key_prefix)?);
699+
}
700+
changes
701+
}
679702
StateChangesRequest::ContractCodeChanges { account_ids } => {
680703
let mut changes = StateChanges::new();
681704
for account_id in account_ids {

chain/jsonrpc/openapi/openapi.json

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4826,6 +4826,29 @@
48264826
},
48274827
"type": "object"
48284828
},
4829+
"GasKeyView": {
4830+
"properties": {
4831+
"balance": {
4832+
"format": "uint128",
4833+
"minimum": 0,
4834+
"type": "integer"
4835+
},
4836+
"num_nonces": {
4837+
"format": "uint32",
4838+
"minimum": 0,
4839+
"type": "integer"
4840+
},
4841+
"permission": {
4842+
"$ref": "#/components/schemas/AccessKeyPermissionView"
4843+
}
4844+
},
4845+
"required": [
4846+
"num_nonces",
4847+
"balance",
4848+
"permission"
4849+
],
4850+
"type": "object"
4851+
},
48294852
"GenesisConfig": {
48304853
"properties": {
48314854
"avg_hidden_validator_seats_per_shard": {
@@ -10694,6 +10717,27 @@
1069410717
],
1069510718
"type": "object"
1069610719
},
10720+
{
10721+
"properties": {
10722+
"changes_type": {
10723+
"enum": [
10724+
"single_gas_key_changes"
10725+
],
10726+
"type": "string"
10727+
},
10728+
"keys": {
10729+
"items": {
10730+
"$ref": "#/components/schemas/AccountWithPublicKey"
10731+
},
10732+
"type": "array"
10733+
}
10734+
},
10735+
"required": [
10736+
"changes_type",
10737+
"keys"
10738+
],
10739+
"type": "object"
10740+
},
1069710741
{
1069810742
"properties": {
1069910743
"account_ids": {
@@ -10715,6 +10759,27 @@
1071510759
],
1071610760
"type": "object"
1071710761
},
10762+
{
10763+
"properties": {
10764+
"account_ids": {
10765+
"items": {
10766+
"$ref": "#/components/schemas/AccountId"
10767+
},
10768+
"type": "array"
10769+
},
10770+
"changes_type": {
10771+
"enum": [
10772+
"all_gas_key_changes"
10773+
],
10774+
"type": "string"
10775+
}
10776+
},
10777+
"required": [
10778+
"changes_type",
10779+
"account_ids"
10780+
],
10781+
"type": "object"
10782+
},
1071810783
{
1071910784
"properties": {
1072010785
"account_ids": {
@@ -11979,6 +12044,112 @@
1197912044
],
1198012045
"type": "object"
1198112046
},
12047+
{
12048+
"properties": {
12049+
"change": {
12050+
"properties": {
12051+
"account_id": {
12052+
"$ref": "#/components/schemas/AccountId"
12053+
},
12054+
"gas_key": {
12055+
"$ref": "#/components/schemas/GasKeyView"
12056+
},
12057+
"public_key": {
12058+
"$ref": "#/components/schemas/PublicKey"
12059+
}
12060+
},
12061+
"required": [
12062+
"account_id",
12063+
"public_key",
12064+
"gas_key"
12065+
],
12066+
"type": "object"
12067+
},
12068+
"type": {
12069+
"enum": [
12070+
"gas_key_update"
12071+
],
12072+
"type": "string"
12073+
}
12074+
},
12075+
"required": [
12076+
"type",
12077+
"change"
12078+
],
12079+
"type": "object"
12080+
},
12081+
{
12082+
"properties": {
12083+
"change": {
12084+
"properties": {
12085+
"account_id": {
12086+
"$ref": "#/components/schemas/AccountId"
12087+
},
12088+
"index": {
12089+
"format": "uint32",
12090+
"minimum": 0,
12091+
"type": "integer"
12092+
},
12093+
"nonce": {
12094+
"format": "uint64",
12095+
"minimum": 0,
12096+
"type": "integer"
12097+
},
12098+
"public_key": {
12099+
"$ref": "#/components/schemas/PublicKey"
12100+
}
12101+
},
12102+
"required": [
12103+
"account_id",
12104+
"public_key",
12105+
"index",
12106+
"nonce"
12107+
],
12108+
"type": "object"
12109+
},
12110+
"type": {
12111+
"enum": [
12112+
"gas_key_nonce_update"
12113+
],
12114+
"type": "string"
12115+
}
12116+
},
12117+
"required": [
12118+
"type",
12119+
"change"
12120+
],
12121+
"type": "object"
12122+
},
12123+
{
12124+
"properties": {
12125+
"change": {
12126+
"properties": {
12127+
"account_id": {
12128+
"$ref": "#/components/schemas/AccountId"
12129+
},
12130+
"public_key": {
12131+
"$ref": "#/components/schemas/PublicKey"
12132+
}
12133+
},
12134+
"required": [
12135+
"account_id",
12136+
"public_key"
12137+
],
12138+
"type": "object"
12139+
},
12140+
"type": {
12141+
"enum": [
12142+
"gas_key_deletion"
12143+
],
12144+
"type": "string"
12145+
}
12146+
},
12147+
"required": [
12148+
"type",
12149+
"change"
12150+
],
12151+
"type": "object"
12152+
},
1198212153
{
1198312154
"properties": {
1198412155
"change": {

core/primitives-core/src/account.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hash::CryptoHash;
22
use crate::serialize::dec_format;
3-
use crate::types::{Balance, Nonce, StorageUsage};
3+
use crate::types::{Balance, Nonce, NonceIndex, StorageUsage};
44
use borsh::{BorshDeserialize, BorshSerialize};
55
pub use near_account_id as id;
66
use near_account_id::AccountId;
@@ -481,6 +481,31 @@ impl AccessKey {
481481
}
482482
}
483483

484+
/// Gas key is like an access key, except it stores a balance separately, and transactions signed
485+
/// with it deduct their cost from the gas key balance instead of the account balance.
486+
#[derive(
487+
BorshSerialize,
488+
BorshDeserialize,
489+
PartialEq,
490+
Eq,
491+
Hash,
492+
Clone,
493+
Debug,
494+
serde::Serialize,
495+
serde::Deserialize,
496+
ProtocolSchema,
497+
)]
498+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
499+
pub struct GasKey {
500+
/// The number of nonces this gas key has.
501+
pub num_nonces: NonceIndex,
502+
/// The balance of the gas key.
503+
pub balance: Balance,
504+
/// Defines the permissions for this gas key.
505+
/// If this is a `FunctionCallPermission`, the allowance must be None (unlimited).
506+
pub permission: AccessKeyPermission,
507+
}
508+
484509
/// Defines permissions for AccessKey
485510
#[derive(
486511
BorshSerialize,

core/primitives-core/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub type StorageUsage = u64;
1818
pub type StorageUsageChange = i64;
1919
/// Nonce for transactions.
2020
pub type Nonce = u64;
21+
/// Nonce index for gas keys.
22+
pub type NonceIndex = u32;
2123
/// Height of the block.
2224
pub type BlockHeight = u64;
2325
/// Height of the epoch.

0 commit comments

Comments
 (0)