Skip to content

Commit

Permalink
Upgrade to new contract format
Browse files Browse the repository at this point in the history
  • Loading branch information
moshababo committed Jul 12, 2024
1 parent e9b7c36 commit 5a1a259
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 167 deletions.
22 changes: 8 additions & 14 deletions core/node/consensus/src/storage/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,23 @@ impl VMWriter {
owner: Address,
nodes: &[&[Token]],
) -> Address {
let consensus_authority_bytecode = read_bytecode("contracts/l2-contracts/artifacts-zk/contracts/ConsensusAuthority.sol/ConsensusAuthority.json");
let consensus_authority_contract = load_contract("contracts/l2-contracts/artifacts-zk/contracts/ConsensusAuthority.sol/ConsensusAuthority.json");
let validator_registry_bytecode = read_bytecode("contracts/l2-contracts/artifacts-zk/contracts/ValidatorRegistry.sol/ValidatorRegistry.json");
let attester_registry_bytecode = read_bytecode("contracts/l2-contracts/artifacts-zk/contracts/AttesterRegistry.sol/AttesterRegistry.json");
let registry_bytecode = read_bytecode("contracts/l2-contracts/artifacts-zk/contracts/ConsensusRegistry.sol/ConsensusRegistry.json");
let registry_contract = load_contract("contracts/l2-contracts/artifacts-zk/contracts/ConsensusRegistry.sol/ConsensusRegistry.json");

let mut txs: Vec<Transaction> = vec![];
let deploy_tx = self.account.get_deploy_tx_with_factory_deps(
&consensus_authority_bytecode,
&registry_bytecode,
Some(&[Token::Address(owner)]),
vec![validator_registry_bytecode, attester_registry_bytecode],
vec![],
TxType::L2,
);
txs.push(deploy_tx.tx);
for node in nodes {
let tx = self.gen_tx_add(&consensus_authority_contract, deploy_tx.address, node);
let tx = self.gen_tx_add(&registry_contract, deploy_tx.address, node);
txs.push(tx);
}
txs.push(
self.gen_tx_set_validator_committee(deploy_tx.address, &consensus_authority_contract),
);
txs.push(
self.gen_tx_set_attester_committee(deploy_tx.address, &consensus_authority_contract),
);
txs.push(self.gen_tx_set_validator_committee(deploy_tx.address, &registry_contract));
txs.push(self.gen_tx_set_attester_committee(deploy_tx.address, &registry_contract));

self.node.push_block(&txs).await;
self.pool
Expand Down Expand Up @@ -298,7 +292,7 @@ impl VMWriter {
value: Default::default(),
factory_deps: vec![],
},
Some(fee(1_000_000)),
Some(fee(10_000_000)),
)
}
}
12 changes: 3 additions & 9 deletions core/node/consensus/src/storage/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn test_vm_reader() {
}
let nodes_ref: Vec<&[Token]> = nodes.iter().map(|v| v.as_slice()).collect();
let nodes_slice: &[&[Token]] = nodes_ref.as_slice();
let consensus_authority_address = writer
let registry_address = writer
.deploy_and_add_nodes(ctx, account.address, nodes_slice)
.await;

Expand All @@ -48,14 +48,8 @@ async fn test_vm_reader() {
)
.await;
let block_id = BlockId::Number(BlockNumber::Pending);
let mut reader = super::vm_reader::VMReader::new(
ctx,
block_id,
pool.clone(),
tx_sender.clone(),
consensus_authority_address,
)
.await;
let mut reader =
super::vm_reader::VMReader::new(pool.clone(), tx_sender.clone(), registry_address);

let validators = reader.read_validator_committee(ctx, block_id).await;
assert_eq!(validators.len(), num_nodes);
Expand Down
193 changes: 49 additions & 144 deletions core/node/consensus/src/storage/vm_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,125 +18,57 @@ use crate::storage::ConnectionPool;
pub struct VMReader {
pool: ConnectionPool,
tx_sender: TxSender,
consensus_authority_contract: Contract,
validator_registry_address: Option<Address>,
validator_registry_contract: Contract,
attester_registry_address: Option<Address>,
attester_registry_contract: Contract,
registry_contract: Contract,
registry_address: Address,
}

impl VMReader {
pub async fn new(
ctx: &Ctx,
block_id: BlockId,
pool: ConnectionPool,
tx_sender: TxSender,
consensus_authority_address: Address,
) -> Self {
let consensus_authority_contract = load_contract("contracts/l2-contracts/artifacts-zk/contracts/ConsensusAuthority.sol/ConsensusAuthority.json");
let validator_registry_contract = load_contract("contracts/l2-contracts/artifacts-zk/contracts/ValidatorRegistry.sol/ValidatorRegistry.json");
let attester_registry_contract = load_contract("contracts/l2-contracts/artifacts-zk/contracts/AttesterRegistry.sol/AttesterRegistry.json");

let mut reader = Self {
pub fn new(pool: ConnectionPool, tx_sender: TxSender, registry_address: Address) -> Self {
let registry_contract = load_contract("contracts/l2-contracts/artifacts-zk/contracts/ConsensusRegistry.sol/ConsensusRegistry.json");
Self {
pool,
tx_sender,
consensus_authority_contract,
validator_registry_address: None,
validator_registry_contract,
attester_registry_address: None,
attester_registry_contract,
};

reader.validator_registry_address = Some(
reader
.read_address(
ctx,
block_id,
consensus_authority_address,
reader
.consensus_authority_contract
.function("validatorRegistry")
.unwrap()
.clone(),
)
.await,
);

reader.attester_registry_address = Some(
reader
.read_address(
ctx,
block_id,
consensus_authority_address,
reader
.consensus_authority_contract
.function("attesterRegistry")
.unwrap()
.clone(),
)
.await,
);

reader
registry_contract,
registry_address,
}
}

pub async fn read_validator_committee(
&mut self,
ctx: &Ctx,
block_id: BlockId,
) -> Vec<CommitteeValidator> {
let mut validators = vec![];
let num_committee_validators = self.read_num_committee_validators(ctx, block_id).await;
for i in 0..num_committee_validators {
let mut committee = vec![];
let validator_committee_size = self.read_validator_committee_size(ctx, block_id).await;
for i in 0..validator_committee_size {
let committee_validator = self.read_committee_validator(ctx, block_id, i).await;
let validator = self
.read_validator(ctx, block_id, committee_validator.0)
.await;

validators.push(CommitteeValidator {
node_owner: committee_validator.0,
weight: committee_validator.1,
pub_key: committee_validator.2,
pop: validator.2,
})
committee.push(committee_validator)
}

validators
committee
}

pub async fn read_attester_committee(
&mut self,
ctx: &Ctx,
block_id: BlockId,
) -> Vec<CommitteeAttester> {
let mut attesters = vec![];
let num_committee_attesters = self.read_num_committee_attesters(ctx, block_id).await;
for i in 0..num_committee_attesters {
let committee_attester = self.read_committee_attester(ctx, block_id, i).await;
let attester = self
.read_attester(ctx, block_id, committee_attester.1)
.await;

attesters.push(CommitteeAttester {
node_owner: committee_attester.1,
weight: committee_attester.0,
pub_key: attester.1,
})
let mut committee = vec![];
let attester_committee_size = self.read_attester_committee_size(ctx, block_id).await;
for i in 0..attester_committee_size {
let committee_validator = self.read_committee_attester(ctx, block_id, i).await;
committee.push(committee_validator)
}
attesters
committee
}

async fn read_num_committee_validators(&mut self, ctx: &Ctx, block_id: BlockId) -> usize {
async fn read_validator_committee_size(&mut self, ctx: &Ctx, block_id: BlockId) -> usize {
let func = self
.validator_registry_contract
.function("numCommitteeValidators")
.registry_contract
.function("validatorCommitteeSize")
.unwrap()
.clone();

let tx = self.gen_l2_call_tx(
self.validator_registry_address.unwrap(),
func.short_signature().to_vec(),
);
let tx = self.gen_l2_call_tx(self.registry_address, func.short_signature().to_vec());

let res = self.eth_call(ctx, block_id, tx).await;

Expand All @@ -147,16 +79,13 @@ impl VMReader {
.as_usize()
}

async fn read_num_committee_attesters(&mut self, ctx: &Ctx, block_id: BlockId) -> usize {
async fn read_attester_committee_size(&mut self, ctx: &Ctx, block_id: BlockId) -> usize {
let func = self
.attester_registry_contract
.function("numCommitteeAttesters")
.registry_contract
.function("attesterCommitteeSize")
.unwrap()
.clone();
let tx = self.gen_l2_call_tx(
self.attester_registry_address.unwrap(),
func.short_signature().to_vec(),
);
let tx = self.gen_l2_call_tx(self.registry_address, func.short_signature().to_vec());

let res = self.eth_call(ctx, block_id, tx).await;
func.decode_output(&res).unwrap()[0]
Expand All @@ -166,44 +95,19 @@ impl VMReader {
.as_usize()
}

async fn read_validator(
&mut self,
ctx: &Ctx,
block_id: BlockId,
node_owner: zksync_types::ethabi::Address,
) -> (usize, Vec<u8>, Vec<u8>, bool) {
let func = self
.validator_registry_contract
.function("validators")
.unwrap()
.clone();
let tx = self.gen_l2_call_tx(
self.validator_registry_address.unwrap(),
func.encode_input(&[Token::Address(node_owner)]).unwrap(),
);
let res = self.eth_call(ctx, block_id, tx).await;
let tokens = func.decode_output(&res).unwrap();
(
tokens[0].clone().into_uint().unwrap().as_usize(),
tokens[1].clone().into_bytes().unwrap(),
tokens[2].clone().into_bytes().unwrap(),
tokens[3].clone().into_bool().unwrap(),
)
}

async fn read_attester(
&mut self,
ctx: &Ctx,
block_id: BlockId,
node_owner: Address,
) -> (usize, Vec<u8>, bool) {
let func = self
.attester_registry_contract
.registry_contract
.function("attesters")
.unwrap()
.clone();
let tx = self.gen_l2_call_tx(
self.attester_registry_address.unwrap(),
self.registry_address,
func.encode_input(&[Token::Address(node_owner)]).unwrap(),
);
let res = self.eth_call(ctx, block_id, tx).await;
Expand All @@ -220,51 +124,52 @@ impl VMReader {
ctx: &Ctx,
block_id: BlockId,
idx: usize,
) -> (Address, usize, Vec<u8>) {
) -> CommitteeValidator {
let func = self
.validator_registry_contract
.function("committee")
.registry_contract
.function("validatorCommittee")
.unwrap()
.clone();
let tx = self.gen_l2_call_tx(
self.validator_registry_address.unwrap(),
self.registry_address,
func.encode_input(&[Token::Uint(zksync_types::U256::from(idx))])
.unwrap(),
);

let res = self.eth_call(ctx, block_id, tx).await;
let tokens = func.decode_output(&res).unwrap();
(
tokens[0].clone().into_address().unwrap(),
tokens[1].clone().into_uint().unwrap().as_usize(),
tokens[2].clone().into_bytes().unwrap(),
)
CommitteeValidator {
node_owner: tokens[0].clone().into_address().unwrap(),
weight: tokens[1].clone().into_uint().unwrap().as_usize(),
pub_key: tokens[2].clone().into_bytes().unwrap(),
pop: tokens[3].clone().into_bytes().unwrap(),
}
}

async fn read_committee_attester(
&mut self,
ctx: &Ctx,
block_id: BlockId,
idx: usize,
) -> (usize, Address, Vec<u8>) {
) -> CommitteeAttester {
let func = self
.attester_registry_contract
.function("committee")
.registry_contract
.function("attesterCommittee")
.unwrap()
.clone();
let tx = self.gen_l2_call_tx(
self.attester_registry_address.unwrap(),
self.registry_address,
func.encode_input(&[Token::Uint(zksync_types::U256::from(idx))])
.unwrap(),
);

let res = self.eth_call(ctx, block_id, tx).await;
let tokens = func.decode_output(&res).unwrap();
(
tokens[0].clone().into_uint().unwrap().as_usize(),
tokens[1].clone().into_address().unwrap(),
tokens[2].clone().into_bytes().unwrap(),
)
CommitteeAttester {
weight: tokens[0].clone().into_uint().unwrap().as_usize(),
node_owner: tokens[1].clone().into_address().unwrap(),
pub_key: tokens[2].clone().into_bytes().unwrap(),
}
}

async fn read_address(
Expand Down Expand Up @@ -335,7 +240,7 @@ pub struct CommitteeValidator {

#[derive(Debug, Default)]
pub struct CommitteeAttester {
pub node_owner: Address,
pub weight: usize,
pub node_owner: Address,
pub pub_key: Vec<u8>,
}

0 comments on commit 5a1a259

Please sign in to comment.