Add keccak-secp256k1 instruction (#11839)

* Implement keccak-secp256k1 instruction

Verifies eth addreses with ecrecover function

* Move secp256k1 test
This commit is contained in:
sakridge
2020-09-15 18:23:21 -07:00
committed by GitHub
parent 7237e7065f
commit 3930cb865a
25 changed files with 732 additions and 52 deletions

View File

@ -42,6 +42,7 @@ solana-sdk = { path = "../sdk", version = "1.4.0" }
solana-sdk-macro-frozen-abi = { path = "../sdk/macro-frozen-abi", version = "1.4.0" }
solana-stake-program = { path = "../programs/stake", version = "1.4.0" }
solana-vote-program = { path = "../programs/vote", version = "1.4.0" }
solana-secp256k1-program = { path = "../programs/secp256k1", version = "1.4.0" }
symlink = "0.1.0"
tar = "0.4.28"
tempfile = "3.1.0"

View File

@ -16,7 +16,7 @@ use rand::{thread_rng, Rng};
use rayon::slice::ParallelSliceMut;
use solana_sdk::{
account::Account,
clock::Slot,
clock::{Epoch, Slot},
fee_calculator::FeeCalculator,
genesis_config::ClusterType,
hash::Hash,
@ -44,6 +44,9 @@ pub struct Accounts {
/// my slot
pub slot: Slot,
/// my epoch
pub epoch: Epoch,
/// Single global AccountsDB
pub accounts_db: Arc<AccountsDB>,
@ -70,17 +73,19 @@ impl Accounts {
pub fn new(paths: Vec<PathBuf>, cluster_type: &ClusterType) -> Self {
Self {
slot: 0,
epoch: 0,
accounts_db: Arc::new(AccountsDB::new(paths, cluster_type)),
account_locks: Mutex::new(HashSet::new()),
readonly_locks: Arc::new(RwLock::new(Some(HashMap::new()))),
}
}
pub fn new_from_parent(parent: &Accounts, slot: Slot, parent_slot: Slot) -> Self {
pub fn new_from_parent(parent: &Accounts, slot: Slot, parent_slot: Slot, epoch: Epoch) -> Self {
let accounts_db = parent.accounts_db.clone();
accounts_db.set_hash(slot, parent_slot);
Self {
slot,
epoch,
accounts_db,
account_locks: Mutex::new(HashSet::new()),
readonly_locks: Arc::new(RwLock::new(Some(HashMap::new()))),
@ -90,6 +95,7 @@ impl Accounts {
pub(crate) fn new_empty(accounts_db: AccountsDB) -> Self {
Self {
slot: 0,
epoch: 0,
accounts_db: Arc::new(accounts_db),
account_locks: Mutex::new(HashSet::new()),
readonly_locks: Arc::new(RwLock::new(Some(HashMap::new()))),
@ -293,7 +299,13 @@ impl Accounts {
.cloned(),
};
let fee = if let Some(fee_calculator) = fee_calculator {
fee_calculator.calculate_fee(tx.message())
fee_calculator.calculate_fee(
tx.message(),
solana_sdk::secp256k1::get_fee_config(
self.accounts_db.cluster_type.unwrap(),
self.epoch,
),
)
} else {
return (Err(TransactionError::BlockhashNotFound), hash_age_kind);
};
@ -993,7 +1005,7 @@ mod tests {
);
let fee_calculator = FeeCalculator::new(10);
assert_eq!(fee_calculator.calculate_fee(tx.message()), 10);
assert_eq!(fee_calculator.calculate_fee(tx.message(), None), 10);
let loaded_accounts =
load_accounts_with_fee(tx, &accounts, &fee_calculator, &mut error_counters);

View File

@ -418,7 +418,7 @@ pub struct AccountsDB {
stats: AccountsStats,
cluster_type: Option<ClusterType>,
pub cluster_type: Option<ClusterType>,
}
#[derive(Debug, Default)]

View File

@ -569,11 +569,15 @@ impl Bank {
parent.freeze();
assert_ne!(slot, parent.slot());
let epoch_schedule = parent.epoch_schedule;
let epoch = epoch_schedule.get_epoch(slot);
let rc = BankRc {
accounts: Arc::new(Accounts::new_from_parent(
&parent.rc.accounts,
slot,
parent.slot(),
epoch,
)),
parent: RwLock::new(Some(parent.clone())),
slot,
@ -581,8 +585,6 @@ impl Bank {
let src = StatusCacheRc {
status_cache: parent.src.status_cache.clone(),
};
let epoch_schedule = parent.epoch_schedule;
let epoch = epoch_schedule.get_epoch(slot);
let fee_rate_governor =
FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count());
@ -2057,7 +2059,10 @@ impl Bank {
};
let fee_calculator = fee_calculator.ok_or(TransactionError::BlockhashNotFound)?;
let fee = fee_calculator.calculate_fee(tx.message());
let fee = fee_calculator.calculate_fee(
tx.message(),
solana_sdk::secp256k1::get_fee_config(self.cluster_type(), self.epoch()),
);
let message = tx.message();
match *res {
@ -8387,7 +8392,8 @@ mod tests {
.map(|_| bank.process_stale_slot_with_budget(0, force_to_return_alive_account))
.collect::<Vec<_>>();
consumed_budgets.sort();
assert_eq!(consumed_budgets, vec![0, 1, 9]);
// consumed_budgets represents the count of alive accounts in the three slots 0,1,2
assert_eq!(consumed_budgets, vec![0, 1, 10]);
}
#[test]

View File

@ -63,6 +63,14 @@ pub fn get_builtins(cluster_type: ClusterType) -> Vec<(Builtin, Epoch)> {
)]);
}
let secp256k1_builtin = Builtin::new(
"secp256k1_program",
solana_sdk::secp256k1_program::id(),
Entrypoint::Program(solana_secp256k1_program::process_instruction),
);
let secp_epoch = solana_sdk::secp256k1::is_enabled_epoch(cluster_type);
builtins.push((secp256k1_builtin, secp_epoch));
builtins
}
@ -101,6 +109,9 @@ mod tests {
#[test]
fn test_get_builtins() {
let mock_program_id =
Pubkey::from_str("7saCc6X5a2syoYANA5oUUnPZLcLMfKoSjiDhFU5fbpoK").unwrap();
let (mut genesis_config, _mint_keypair) = create_genesis_config(100_000);
genesis_config.cluster_type = ClusterType::Testnet;
let bank0 = Arc::new(Bank::new(&genesis_config));
@ -145,7 +156,7 @@ mod tests {
solana_config_program::id(),
solana_stake_program::id(),
solana_vote_program::id(),
Pubkey::from_str("7saCc6X5a2syoYANA5oUUnPZLcLMfKoSjiDhFU5fbpoK").unwrap(),
mock_program_id,
]
);
@ -157,7 +168,7 @@ mod tests {
solana_config_program::id(),
solana_stake_program::id(),
solana_vote_program::id(),
Pubkey::from_str("7saCc6X5a2syoYANA5oUUnPZLcLMfKoSjiDhFU5fbpoK").unwrap(),
mock_program_id,
]
);
@ -169,7 +180,7 @@ mod tests {
solana_config_program::id(),
solana_stake_program::id(),
solana_vote_program::id(),
Pubkey::from_str("7saCc6X5a2syoYANA5oUUnPZLcLMfKoSjiDhFU5fbpoK").unwrap(),
mock_program_id,
]
);
}