From eba7c0290f954605fe428386e316f10464f0fb05 Mon Sep 17 00:00:00 2001 From: Markus Legner Date: Mon, 19 Aug 2024 10:48:11 +0200 Subject: [PATCH] fix(contracts): updated and reformatted contracts (#108) --- contracts/blob_store/Move.lock | 16 +- contracts/blob_store/Move.toml | 2 +- contracts/blob_store/sources/blob.move | 147 +++++------ contracts/blob_store/sources/blob_events.move | 26 +- .../blob_store/sources/bls_aggregate.move | 97 ++++---- contracts/blob_store/sources/committee.move | 64 +++-- contracts/blob_store/sources/e2etest.move | 20 +- contracts/blob_store/sources/encoding.move | 2 +- contracts/blob_store/sources/redstuff.move | 40 +-- .../sources/storage_accounting.move | 78 +++--- .../blob_store/sources/storage_node.move | 39 +-- .../blob_store/sources/storage_resource.move | 84 +++---- contracts/blob_store/sources/system.move | 211 +++++++--------- .../blob_store/sources/tests/blob_tests.move | 235 +++++++++++------- .../blob_store/sources/tests/bls_tests.move | 8 +- .../sources/tests/epoch_change_tests.move | 65 +++-- .../sources/tests/invalid_tests.move | 11 +- .../sources/tests/ringbuffer_tests.move | 13 +- .../sources/tests/storage_resource_tests.move | 40 +-- 19 files changed, 553 insertions(+), 645 deletions(-) diff --git a/contracts/blob_store/Move.lock b/contracts/blob_store/Move.lock index fde526a7..0b6f01a9 100644 --- a/contracts/blob_store/Move.lock +++ b/contracts/blob_store/Move.lock @@ -2,7 +2,7 @@ [move] version = 2 -manifest_digest = "537E35587055E526E85CD04C400536AFFEC5CC0BE833EB4B5FAC245850C34C96" +manifest_digest = "C461A25DAED3234921DF6DD2B4AE93FF11BA907C5A5CF469400757C595C15B68" deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082" dependencies = [ { name = "Sui" }, @@ -10,25 +10,17 @@ dependencies = [ [[move.package]] name = "MoveStdlib" -source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet-v1.29.2", subdir = "crates/sui-framework/packages/move-stdlib" } +source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet-v1.31.1", subdir = "crates/sui-framework/packages/move-stdlib" } [[move.package]] name = "Sui" -source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet-v1.29.2", subdir = "crates/sui-framework/packages/sui-framework" } +source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet-v1.31.1", subdir = "crates/sui-framework/packages/sui-framework" } dependencies = [ { name = "MoveStdlib" }, ] [move.toolchain-version] -compiler-version = "1.29.2" +compiler-version = "1.30.1" edition = "2024.beta" flavor = "sui" - -[env] - -[env.testnet] -chain-id = "4c78adac" -original-published-id = "0x8bf57168d5acb0e83efa49905a326d0d624ff147955106d94fb09b6abdaa7bbc" -latest-published-id = "0x8bf57168d5acb0e83efa49905a326d0d624ff147955106d94fb09b6abdaa7bbc" -published-version = "1" diff --git a/contracts/blob_store/Move.toml b/contracts/blob_store/Move.toml index 132d2b0a..4855e94f 100644 --- a/contracts/blob_store/Move.toml +++ b/contracts/blob_store/Move.toml @@ -3,7 +3,7 @@ name = "blob_store" edition = "2024.beta" [dependencies] -Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "testnet-v1.29.2" } +Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "testnet-v1.31.1" } [addresses] blob_store = "0x0" diff --git a/contracts/blob_store/sources/blob.move b/contracts/blob_store/sources/blob.move index b3a84c55..85877a80 100644 --- a/contracts/blob_store/sources/blob.move +++ b/contracts/blob_store/sources/blob.move @@ -6,14 +6,15 @@ module blob_store::blob { use sui::hash; use blob_store::committee::{Self, CertifiedMessage}; - use blob_store::system::{Self, System}; + use blob_store::system::System; use blob_store::storage_resource::{ Storage, start_epoch, end_epoch, storage_size, fuse_periods, - destroy}; + destroy, + }; use blob_store::encoding; use blob_store::blob_events::{emit_blob_registered, emit_blob_certified}; @@ -21,13 +22,13 @@ module blob_store::blob { const BLOB_CERT_MSG_TYPE: u8 = 1; // Error codes - const ERROR_INVALID_MSG_TYPE: u64 = 1; - const ERROR_RESOURCE_BOUNDS: u64 = 2; - const ERROR_RESOURCE_SIZE: u64 = 3; - const ERROR_WRONG_EPOCH: u64 = 4; - const ERROR_ALREADY_CERTIFIED: u64 = 5; - const ERROR_INVALID_BLOB_ID: u64 = 6; - const ERROR_NOT_CERTIFIED : u64 = 7; + const EInvalidMsgType: u64 = 1; + const EResourceBounds: u64 = 2; + const EResourceSize: u64 = 3; + const EWrongEpoch: u64 = 4; + const EAlreadyCertified: u64 = 5; + const EInvalidBlobId: u64 = 6; + const ENotCertified: u64 = 7; // Object definitions @@ -39,33 +40,33 @@ module blob_store::blob { blob_id: u256, size: u64, erasure_code_type: u8, - certified: option::Option, // Store the epoch first certified + certified_epoch: option::Option, // Store the epoch first certified storage: Storage, } // Accessor functions - public fun stored_epoch(b: &Blob) : u64 { + public fun stored_epoch(b: &Blob): u64 { b.stored_epoch } - public fun blob_id(b: &Blob) : u256 { + public fun blob_id(b: &Blob): u256 { b.blob_id } - public fun size(b: &Blob) : u64 { + public fun size(b: &Blob): u64 { b.size } - public fun erasure_code_type(b: &Blob) : u8 { + public fun erasure_code_type(b: &Blob): u8 { b.erasure_code_type } - public fun certified(b: &Blob) : &Option { - &b.certified + public fun certified_epoch(b: &Blob): &Option { + &b.certified_epoch } - public fun storage(b: &Blob) : &Storage { + public fun storage(b: &Blob): &Storage { &b.storage } @@ -76,8 +77,7 @@ module blob_store::blob { } /// Derive the blob_id for a blob given the root_hash, erasure_code_type and size. - public fun derive_blob_id(root_hash: u256, erasure_code_type: u8, size : u64) : u256 { - + public fun derive_blob_id(root_hash: u256, erasure_code_type: u8, size: u64): u256 { let blob_id_struct = BlobIdDerivation { erasure_code_type, size, @@ -87,7 +87,7 @@ module blob_store::blob { let serialized = bcs::to_bytes(&blob_id_struct); let encoded = hash::blake2b256(&serialized); let mut decoder = bcs::new(encoded); - let blob_id = bcs::peel_u256(&mut decoder); + let blob_id = decoder.peel_u256(); blob_id } @@ -102,28 +102,28 @@ module blob_store::blob { size: u64, erasure_code_type: u8, ctx: &mut TxContext, - ) : Blob { - + ): Blob { let id = object::new(ctx); - let stored_epoch = system::epoch(sys); + let stored_epoch = sys.epoch(); // Check resource bounds. - assert!(stored_epoch >= start_epoch(&storage), ERROR_RESOURCE_BOUNDS); - assert!(stored_epoch < end_epoch(&storage), ERROR_RESOURCE_BOUNDS); + assert!(stored_epoch >= start_epoch(&storage), EResourceBounds); + assert!(stored_epoch < end_epoch(&storage), EResourceBounds); // check that the encoded size is less than the storage size let encoded_size = encoding::encoded_blob_length( size, erasure_code_type, - system::n_shards(sys) + sys.n_shards(), ); - assert!(encoded_size <= storage_size(&storage), ERROR_RESOURCE_SIZE); + assert!(encoded_size <= storage_size(&storage), EResourceSize); // Cryptographically verify that the Blob ID authenticates // both the size and fe_type. - assert!(derive_blob_id(root_hash, erasure_code_type, size) == blob_id, - ERROR_INVALID_BLOB_ID); - + assert!( + derive_blob_id(root_hash, erasure_code_type, size) == blob_id, + EInvalidBlobId, + ); // Emit register event emit_blob_registered( @@ -141,10 +141,9 @@ module blob_store::blob { size, // erasure_code_type, - certified: option::none(), + certified_epoch: option::none(), storage, } - } public struct CertifiedBlobMessage has drop { @@ -154,28 +153,21 @@ module blob_store::blob { /// Construct the certified blob message, note that constructing /// implies a certified message, that is already checked. - public fun certify_blob_message( - message: CertifiedMessage - ) : CertifiedBlobMessage { - + public fun certify_blob_message(message: CertifiedMessage): CertifiedBlobMessage { // Assert type is correct - assert!(committee::intent_type(&message) == BLOB_CERT_MSG_TYPE, - ERROR_INVALID_MSG_TYPE); + assert!(message.intent_type() == BLOB_CERT_MSG_TYPE, EInvalidMsgType); // The certified blob message contain a blob_id : u256 - let epoch = committee::cert_epoch(&message); - let message_body = committee::into_message(message); + let epoch = message.cert_epoch(); + let message_body = message.into_message(); let mut bcs_body = bcs::new(message_body); - let blob_id = bcs::peel_u256(&mut bcs_body); + let blob_id = bcs_body.peel_u256(); // On purpose we do not check that nothing is left in the message // to allow in the future for extensibility. - CertifiedBlobMessage { - epoch, - blob_id, - } + CertifiedBlobMessage { epoch, blob_id } } /// Certify that a blob will be available in the storage system until the end epoch of the @@ -184,22 +176,21 @@ module blob_store::blob { sys: &System, message: CertifiedBlobMessage, blob: &mut Blob, - ){ - + ) { // Check that the blob is registered in the system - assert!(blob_id(blob) == message.blob_id, ERROR_INVALID_BLOB_ID); + assert!(blob_id(blob) == message.blob_id, EInvalidBlobId); // Check that the blob is not already certified - assert!(!option::is_some(&blob.certified), ERROR_ALREADY_CERTIFIED); + assert!(!blob.certified_epoch.is_some(), EAlreadyCertified); // Check that the message is from the current epoch - assert!(message.epoch == system::epoch(sys), ERROR_WRONG_EPOCH); + assert!(message.epoch == sys.epoch(), EWrongEpoch); // Check that the storage in the blob is still valid - assert!(message.epoch < end_epoch(storage(blob)), ERROR_RESOURCE_BOUNDS); + assert!(message.epoch < end_epoch(storage(blob)), EResourceBounds); // Mark the blob as certified - blob.certified = option::some(message.epoch); + blob.certified_epoch = option::some(message.epoch); // Emit certified event emit_blob_certified( @@ -219,23 +210,19 @@ module blob_store::blob { message: vector, ) { let certified_msg = committee::verify_quorum_in_epoch( - system::current_committee(sys), + sys.current_committee(), signature, members, - message + message, ); let certified_blob_msg = certify_blob_message(certified_msg); certify_with_certified_msg(sys, certified_blob_msg, blob); } /// After the period of validity expires for the blob we can destroy the blob resource. - public fun destroy_blob( - sys: &System, - blob: Blob, - ){ - - let current_epoch = system::epoch(sys); - assert!(current_epoch >= end_epoch(storage(&blob)), ERROR_RESOURCE_BOUNDS); + public fun destroy_blob(sys: &System, blob: Blob) { + let current_epoch = sys.epoch(); + assert!(current_epoch >= end_epoch(storage(&blob)), EResourceBounds); // Destroy the blob let Blob { @@ -244,38 +231,34 @@ module blob_store::blob { blob_id: _, size: _, erasure_code_type: _, - certified: _, + certified_epoch: _, storage, } = blob; - object::delete(id); + id.delete(); destroy(storage); } /// Extend the period of validity of a blob with a new storage resource. /// The new storage resource must be the same size as the storage resource /// used in the blob, and have a longer period of validity. - public fun extend( - sys: &System, - blob: &mut Blob, - extension: Storage){ - + public fun extend(sys: &System, blob: &mut Blob, extension: Storage) { // We only extend certified blobs within their period of validity // with storage that extends this period. First we check for these // conditions. // Assert this is a certified blob - assert!(option::is_some(&blob.certified), ERROR_NOT_CERTIFIED); + assert!(blob.certified_epoch.is_some(), ENotCertified); // Check the blob is within its availability period - assert!(system::epoch(sys) < end_epoch(storage(blob)), ERROR_RESOURCE_BOUNDS); + assert!(sys.epoch() < end_epoch(storage(blob)), EResourceBounds); // Check that the extension is valid, and the end // period of the extension is after the current period. - assert!(end_epoch(&extension) > end_epoch(storage(blob)), ERROR_RESOURCE_BOUNDS); + assert!(end_epoch(&extension) > end_epoch(storage(blob)), EResourceBounds); // Note: if the amounts do not match there will be an abort here. - fuse_periods(&mut blob.storage , extension); + fuse_periods(&mut blob.storage, extension); // Emit certified event // @@ -283,11 +266,10 @@ module blob_store::blob { // reconfiguration this is the committee that has a quorum that hold the // resource. emit_blob_certified( - *option::borrow(&blob.certified), + *option::borrow(&blob.certified_epoch), blob.blob_id, end_epoch(storage(blob)), ); - } // Testing Functions @@ -301,29 +283,22 @@ module blob_store::blob { blob_id: _, size: _, erasure_code_type: _, - certified: _, + certified_epoch: _, storage, } = b; - object::delete(id); + id.delete(); destroy(storage); } #[test_only] // Accessor for blob - public fun message_blob_id(m: &CertifiedBlobMessage) : u256 { + public fun message_blob_id(m: &CertifiedBlobMessage): u256 { m.blob_id } #[test_only] - public fun certified_blob_message_for_testing( - epoch: u64, - blob_id: u256, - ) : CertifiedBlobMessage { - CertifiedBlobMessage { - epoch, - blob_id, - } + public fun certified_blob_message_for_testing(epoch: u64, blob_id: u256): CertifiedBlobMessage { + CertifiedBlobMessage { epoch, blob_id } } - } diff --git a/contracts/blob_store/sources/blob_events.move b/contracts/blob_store/sources/blob_events.move index 1ecea7b3..6b87dafb 100644 --- a/contracts/blob_store/sources/blob_events.move +++ b/contracts/blob_store/sources/blob_events.move @@ -37,32 +37,14 @@ module blob_store::blob_events { erasure_code_type: u8, end_epoch: u64, ) { - event::emit(BlobRegistered { - epoch, - blob_id, - size, - erasure_code_type, - end_epoch, - }); + event::emit(BlobRegistered { epoch, blob_id, size, erasure_code_type, end_epoch }); } - public(package) fun emit_blob_certified( - epoch: u64, - blob_id: u256, - end_epoch: u64, - ) { - event::emit(BlobCertified { - epoch, - blob_id, - end_epoch, - }); + public(package) fun emit_blob_certified(epoch: u64, blob_id: u256, end_epoch: u64) { + event::emit(BlobCertified { epoch, blob_id, end_epoch }); } public(package) fun emit_invalid_blob_id(epoch: u64, blob_id: u256) { - event::emit(InvalidBlobID { - epoch, - blob_id, - }); + event::emit(InvalidBlobID { epoch, blob_id }); } - } diff --git a/contracts/blob_store/sources/bls_aggregate.move b/contracts/blob_store/sources/bls_aggregate.move index a8591de5..8568dcc6 100644 --- a/contracts/blob_store/sources/bls_aggregate.move +++ b/contracts/blob_store/sources/bls_aggregate.move @@ -1,17 +1,19 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 +// editorconfig-checker-disable-file + module blob_store::bls_aggregate { use sui::group_ops::Self; use sui::bls12381::{Self, bls12381_min_pk_verify}; - use blob_store::storage_node::{Self, StorageNodeInfo}; + use blob_store::storage_node::StorageNodeInfo; // Error codes - const ERROR_TOTAL_MEMBER_ORDER: u64 = 0; - const ERROR_SIG_VERIFICATION: u64 = 1; - const ERROR_NOT_ENOUGH_STAKE: u64 = 2; - const ERROR_INCORRECT_COMMITTEE: u64 = 3; + const ETotalMemberOrder: u64 = 0; + const ESigVerification: u64 = 1; + const ENotEnoughStake: u64 = 2; + const EIncorrectCommittee: u64 = 3; /// This represents a BLS signing committee. public struct BlsCommittee has store, drop { @@ -22,51 +24,26 @@ module blob_store::bls_aggregate { } /// Constructor - public fun new_bls_committee( - members: vector - ) : BlsCommittee { - + public fun new_bls_committee(members: vector): BlsCommittee { // Compute the total number of shards let mut n_shards = 0; let mut i = 0; - while (i < vector::length(&members)) { - let added_weight = storage_node::weight(vector::borrow(&members, i)); - assert!(added_weight > 0, ERROR_INCORRECT_COMMITTEE); + while (i < members.length()) { + let added_weight = members[i].weight(); + assert!(added_weight > 0, EIncorrectCommittee); n_shards = n_shards + added_weight; i = i + 1; }; - assert!(n_shards != 0, ERROR_INCORRECT_COMMITTEE); + assert!(n_shards != 0, EIncorrectCommittee); - BlsCommittee { - members, - n_shards - } + BlsCommittee { members, n_shards } } /// Returns the number of shards held by the committee. - public fun n_shards(self: &BlsCommittee) : u16 { + public fun n_shards(self: &BlsCommittee): u16 { self.n_shards } - #[test_only] - /// Test committee - public fun new_bls_committee_for_testing() : BlsCommittee { - // Pk corresponding to secret key scalar(117) - let pub_key_bytes = vector[ - 149, 234, 204, 58, 220, 9, 200, 39, - 89, 63, 88, 30, 142, 45, 224, 104, - 191, 76, 245, 208, 192, 235, 41, 229, - 55, 47, 13, 35, 54, 71, 136, 238, - 15, 155, 235, 17, 44, 138, 126, 156, - 47, 12, 114, 4, 51, 112, 92, 240, - ]; - let storage_node = storage_node::new_for_testing(pub_key_bytes, 100); - BlsCommittee { - members: vector[storage_node], - n_shards: 100, - } - } - /// Verify an aggregate BLS signature is a certificate in the epoch, and return the type of /// certificate and the bytes certified. The `signers` vector is an increasing list of indexes /// into the `members` vector of the committee. If there is a certificate, the function @@ -76,8 +53,7 @@ module blob_store::bls_aggregate { signature: &vector, signers: &vector, message: &vector, - ) : u16 - { + ): u16 { // Use the signers flags to construct the key and the weights. // Lower bound for the next `member_index` to ensure they are monotonically increasing @@ -87,15 +63,15 @@ module blob_store::bls_aggregate { let mut aggregate_key = bls12381::g1_identity(); let mut aggregate_weight = 0; - while (i < vector::length(signers)) { - let member_index = (*vector::borrow(signers, i) as u64); - assert!(member_index >= min_next_member_index, ERROR_TOTAL_MEMBER_ORDER); + while (i < signers.length()) { + let member_index = signers[i] as u64; + assert!(member_index >= min_next_member_index, ETotalMemberOrder); min_next_member_index = member_index + 1; // Bounds check happens here - let member = vector::borrow(&self.members, member_index); - let key = storage_node::public_key(member); - let weight = storage_node::weight(member); + let member = &self.members[member_index]; + let key = member.public_key(); + let weight = member.weight(); aggregate_key = bls12381::g1_add(&aggregate_key, key); aggregate_weight = aggregate_weight + weight; @@ -106,18 +82,35 @@ module blob_store::bls_aggregate { // The expression below is the solution to the inequality: // n_shards = 3 f + 1 // stake >= 2f + 1 - assert!(3 * (aggregate_weight as u64) >= 2 * (self.n_shards as u64) + 1, - ERROR_NOT_ENOUGH_STAKE); + assert!( + 3 * (aggregate_weight as u64) >= 2 * (self.n_shards as u64) + 1, + ENotEnoughStake, + ); // Verify the signature let pub_key_bytes = group_ops::bytes(&aggregate_key); - assert!(bls12381_min_pk_verify( - signature, - pub_key_bytes, - message), ERROR_SIG_VERIFICATION); + assert!( + bls12381_min_pk_verify( + signature, + pub_key_bytes, + message, + ), + ESigVerification, + ); (aggregate_weight as u16) - } + + #[test_only] + use blob_store::storage_node::Self; + + #[test_only] + /// Test committee + public fun new_bls_committee_for_testing(): BlsCommittee { + // Pk corresponding to secret key scalar(117) + let pub_key_bytes = x"95eacc3adc09c827593f581e8e2de068bf4cf5d0c0eb29e5372f0d23364788ee0f9beb112c8a7e9c2f0c720433705cf0"; + let storage_node = storage_node::new_for_testing(pub_key_bytes, 100); + BlsCommittee { members: vector[storage_node], n_shards: 100 } + } } diff --git a/contracts/blob_store/sources/committee.move b/contracts/blob_store/sources/committee.move index caa0b5f4..04116952 100644 --- a/contracts/blob_store/sources/committee.move +++ b/contracts/blob_store/sources/committee.move @@ -7,8 +7,8 @@ module blob_store::committee { const APP_ID: u8 = 3; // Errors - const ERROR_INCORRECT_APP_ID: u64 = 0; - const ERROR_INCORRECT_EPOCH: u64 = 1; + const EIncorrectAppId: u64 = 0; + const EIncorrectEpoch: u64 = 1; #[test_only] use blob_store::bls_aggregate::new_bls_committee_for_testing; @@ -31,11 +31,11 @@ module blob_store::committee { /// signal that the new epoch has started. public struct Committee has store { epoch: u64, - bls_committee : BlsCommittee, + bls_committee: BlsCommittee, } /// Get the epoch of the committee. - public fun epoch(self: &Committee) : u64 { + public fun epoch(self: &Committee): u64 { self.epoch } @@ -44,18 +44,18 @@ module blob_store::committee { /// A constructor for the capability to create committees /// This is only accessible through friend modules. - public(package) fun create_committee_cap() : CreateCommitteeCap { + public(package) fun create_committee_cap(): CreateCommitteeCap { CreateCommitteeCap {} } /// Returns the number of shards held by the committee. - public fun n_shards(self: &Committee) : u16 { + public fun n_shards(self: &Committee): u16 { bls_aggregate::n_shards(&self.bls_committee) } #[test_only] /// A constructor for the capability to create committees for tests - public fun create_committee_cap_for_tests() : CreateCommitteeCap { + public fun create_committee_cap_for_tests(): CreateCommitteeCap { CreateCommitteeCap {} } @@ -65,7 +65,7 @@ module blob_store::committee { _cap: &CreateCommitteeCap, epoch: u64, members: vector, - ) : Committee { + ): Committee { // Make BlsCommittee let bls_committee = new_bls_committee(members); @@ -73,18 +73,13 @@ module blob_store::committee { } #[test_only] - public fun committee_for_testing( - epoch: u64, - ) : Committee { + public fun committee_for_testing(epoch: u64): Committee { let bls_committee = new_bls_committee_for_testing(); Committee { epoch, bls_committee } } #[test_only] - public fun committee_for_testing_with_bls( - epoch: u64, - bls_committee: BlsCommittee, - ) : Committee { + public fun committee_for_testing_with_bls(epoch: u64, bls_committee: BlsCommittee): Committee { Committee { epoch, bls_committee } } @@ -103,33 +98,33 @@ module blob_store::committee { cert_epoch: u64, stake_support: u16, message: vector, - ) : CertifiedMessage { + ): CertifiedMessage { CertifiedMessage { intent_type, intent_version, cert_epoch, stake_support, message } } // Make accessors for the CertifiedMessage - public fun intent_type(self: &CertifiedMessage) : u8 { + public fun intent_type(self: &CertifiedMessage): u8 { self.intent_type } - public fun intent_version(self: &CertifiedMessage) : u8 { + public fun intent_version(self: &CertifiedMessage): u8 { self.intent_version } - public fun cert_epoch(self: &CertifiedMessage) : u64 { + public fun cert_epoch(self: &CertifiedMessage): u64 { self.cert_epoch } - public fun stake_support(self: &CertifiedMessage) : u16 { + public fun stake_support(self: &CertifiedMessage): u16 { self.stake_support } - public fun message(self: &CertifiedMessage) : &vector { + public fun message(self: &CertifiedMessage): &vector { &self.message } // Deconstruct into the vector of message bytes - public fun into_message(self: CertifiedMessage) : vector { + public fun into_message(self: CertifiedMessage): vector { self.message } @@ -143,24 +138,27 @@ module blob_store::committee { signature: vector, members: vector, message: vector, - ) : CertifiedMessage { - - let stake_support = - verify_certificate(&committee.bls_committee, &signature, &members, &message); + ): CertifiedMessage { + let stake_support = verify_certificate( + &committee.bls_committee, + &signature, + &members, + &message, + ); // Here we BCS decode the header of the message to check intents, epochs, etc. let mut bcs_message = bcs::new(message); - let intent_type = bcs::peel_u8(&mut bcs_message); - let intent_version = bcs::peel_u8(&mut bcs_message); + let intent_type = bcs_message.peel_u8(); + let intent_version = bcs_message.peel_u8(); - let intent_app = bcs::peel_u8(&mut bcs_message); - assert!(intent_app == APP_ID, ERROR_INCORRECT_APP_ID); + let intent_app = bcs_message.peel_u8(); + assert!(intent_app == APP_ID, EIncorrectAppId); - let cert_epoch = bcs::peel_u64(&mut bcs_message); - assert!(cert_epoch == epoch(committee), ERROR_INCORRECT_EPOCH); + let cert_epoch = bcs_message.peel_u64(); + assert!(cert_epoch == epoch(committee), EIncorrectEpoch); - let message = bcs::into_remainder_bytes(bcs_message); + let message = bcs_message.into_remainder_bytes(); CertifiedMessage { intent_type, intent_version, cert_epoch, stake_support, message } } diff --git a/contracts/blob_store/sources/e2etest.move b/contracts/blob_store/sources/e2etest.move index ef6c4a5d..e42c6cd7 100644 --- a/contracts/blob_store/sources/e2etest.move +++ b/contracts/blob_store/sources/e2etest.move @@ -1,12 +1,11 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -module blob_store::e2e_test { - +module blob_store::e2e_test { use blob_store::committee::{Self, CreateCommitteeCap}; use blob_store::storage_node; - public struct CommitteeCapHolder has key, store { + public struct CommitteeCapHolder has key, store { id: UID, cap: CreateCommitteeCap, } @@ -17,11 +16,10 @@ module blob_store::e2e_test { /// Create a committee given a capability and a list of storage nodes public fun make_committee( - cap : &CommitteeCapHolder, + cap: &CommitteeCapHolder, epoch: u64, - storage_nodes: vector) - : committee::Committee { - + storage_nodes: vector, + ): committee::Committee { committee::create_committee( &cap.cap, epoch, @@ -30,17 +28,13 @@ module blob_store::e2e_test { } fun init(ctx: &mut TxContext) { - // Create a committee caps let committee_cap = committee::create_committee_cap(); // We send the wrapped cap to the creator of the package transfer::public_transfer( - CommitteeCapHolder { - id: object::new(ctx), - cap: committee_cap, - }, - tx_context::sender(ctx), + CommitteeCapHolder { id: object::new(ctx), cap: committee_cap }, + ctx.sender(), ); } } diff --git a/contracts/blob_store/sources/encoding.move b/contracts/blob_store/sources/encoding.move index 2d58c389..7b67c375 100644 --- a/contracts/blob_store/sources/encoding.move +++ b/contracts/blob_store/sources/encoding.move @@ -12,7 +12,7 @@ module blob_store::encoding { /// Computes the encoded length of a blob given its unencoded length, encoding type /// and number of shards `n_shards`. - public fun encoded_blob_length(unencoded_length: u64, encoding_type: u8, n_shards: u16) : u64 { + public fun encoded_blob_length(unencoded_length: u64, encoding_type: u8, n_shards: u16): u64 { // Currently only supports a single encoding type assert!(encoding_type == RED_STUFF_ENCODING, EInvalidEncoding); redstuff::encoded_blob_length(unencoded_length, n_shards) diff --git a/contracts/blob_store/sources/redstuff.move b/contracts/blob_store/sources/redstuff.move index 00525248..ca0208ca 100644 --- a/contracts/blob_store/sources/redstuff.move +++ b/contracts/blob_store/sources/redstuff.move @@ -2,24 +2,20 @@ // SPDX-License-Identifier: Apache-2.0 module blob_store::redstuff { - // The length of a hash used for the Red Stuff metadata const DIGEST_LEN: u64 = 32; // The length of a blob id in the stored metadata const BLOB_ID_LEN: u64 = 32; - // Errors - const EInvalidDataLength: u64 = 0; - - /// Computes the encoded length of a blob for the Red Stuff encoding, given its /// unencoded size and the number of shards. The output length includes the /// size of the metadata hashes and the blob ID. - public(package) fun encoded_blob_length(unencoded_length: u64, n_shards: u16) : u64 { - let slivers_size = ((source_symbols_primary(n_shards) as u64) + public(package) fun encoded_blob_length(unencoded_length: u64, n_shards: u16): u64 { + let slivers_size = (source_symbols_primary(n_shards) as u64 + (source_symbols_secondary(n_shards) as u64)) * (symbol_size(unencoded_length, n_shards) as u64); + (n_shards as u64) * (slivers_size + metadata_size(n_shards)) } @@ -35,35 +31,35 @@ module blob_store::redstuff { /// The total number of source symbols given `n_shards`. fun n_source_symbols(n_shards: u16): u64 { - (source_symbols_primary(n_shards) as u64) * - (source_symbols_secondary(n_shards) as u64) + (source_symbols_primary(n_shards) as u64) * (source_symbols_secondary(n_shards) as u64) } /// Computes the symbol size given the `unencoded_length` and number of shards /// `n_shards`. If the resulting symbols would be larger than a `u16`, this /// results in an Error. - fun symbol_size(unencoded_length: u64, n_shards: u16) : u16 { - assert!(unencoded_length > 0, EInvalidDataLength); + fun symbol_size(mut unencoded_length: u64, n_shards: u16): u16 { + if (unencoded_length == 0) { + unencoded_length = 1; + }; let n_symbols = n_source_symbols(n_shards); ((unencoded_length - 1) / n_symbols + 1) as u16 } /// The size of the metadata, i.e. sliver root hashes and blob_id. - fun metadata_size(n_shards: u16) : u64 { + fun metadata_size(n_shards: u16): u64 { (n_shards as u64) * DIGEST_LEN * 2 + BLOB_ID_LEN } - /// Returns the decoding safety limit. fun decoding_safety_limit(n_shards: u16): u16 { // These ranges are chosen to ensure that the safety limit is at most 20% of f, // up to a safety limit of 5. - min_u16(max_byzantine(n_shards)/5, 5) + min_u16(max_byzantine(n_shards) / 5, 5) } /// Maximum number of byzantine shards, given `n_shards`. fun max_byzantine(n_shards: u16): u16 { - (n_shards - 1)/3 + (n_shards - 1) / 3 } fun min_u16(a: u16, b: u16): u16 { @@ -83,13 +79,17 @@ module blob_store::redstuff { #[test] fun test_encoded_size() { - assert_encoded_size(1, 10, 10*((4+7) + 10*2*32 + 32)); - assert_encoded_size(1, 1000, 1000*((329+662) + 1000*2*32 + 32)); - assert_encoded_size((4*7)*100, 10, 10*((4+7)*100 + 10*2*32 + 32)); - assert_encoded_size((329*662)*100, 1000, 1000*((329+662)*100 + 1000*2*32 + 32)); + assert_encoded_size(1, 10, 10 * ((4 + 7) + 10 * 2 * 32 + 32)); + assert_encoded_size(1, 1000, 1000 * ((329 + 662) + 1000 * 2 * 32 + 32)); + assert_encoded_size((4 * 7) * 100, 10, 10 * ((4 + 7) * 100 + 10 * 2 * 32 + 32)); + assert_encoded_size( + (329 * 662) * 100, + 1000, + 1000 * ((329 + 662) * 100 + 1000 * 2 * 32 + 32), + ); } - #[test,expected_failure] + #[test] fun test_zero_size() { //test should fail here encoded_blob_length(0, 10); diff --git a/contracts/blob_store/sources/storage_accounting.move b/contracts/blob_store/sources/storage_accounting.move index 06cea3b7..f971f1bf 100644 --- a/contracts/blob_store/sources/storage_accounting.move +++ b/contracts/blob_store/sources/storage_accounting.move @@ -5,7 +5,7 @@ module blob_store::storage_accounting { use sui::balance::{Self, Balance}; // Errors - const ERROR_INDEX_OUT_OF_BOUNDS : u64 = 3; + const EIndexOutOfBounds: u64 = 3; /// Holds information about a future epoch, namely how much /// storage needs to be reclaimed and the rewards to be distributed. @@ -20,82 +20,70 @@ module blob_store::storage_accounting { epoch: u64, storage_to_reclaim: u64, rewards_to_distribute: Balance, - ) : FutureAccounting { - FutureAccounting { - epoch, - storage_to_reclaim, - rewards_to_distribute, - } + ): FutureAccounting { + FutureAccounting { epoch, storage_to_reclaim, rewards_to_distribute } } /// Accessor for epoch, read-only - public fun epoch(accounting: &FutureAccounting) : u64 { + public fun epoch(accounting: &FutureAccounting): u64 { *&accounting.epoch } /// Accessor for storage_to_reclaim, mutable. - public fun storage_to_reclaim( - accounting: &mut FutureAccounting) - : u64 { + public fun storage_to_reclaim(accounting: &mut FutureAccounting): u64 { accounting.storage_to_reclaim } /// Increase storage to reclaim public fun increase_storage_to_reclaim( - accounting: &mut FutureAccounting, - amount: u64) { + accounting: &mut FutureAccounting, + amount: u64, + ) { accounting.storage_to_reclaim = accounting.storage_to_reclaim + amount; } /// Accessor for rewards_to_distribute, mutable. public fun rewards_to_distribute( - accounting: &mut FutureAccounting) - : &mut Balance { + accounting: &mut FutureAccounting, + ): &mut Balance { &mut accounting.rewards_to_distribute } /// Destructor for FutureAccounting, when empty. - public fun delete_empty_future_accounting( - self: FutureAccounting, - ) { + public fun delete_empty_future_accounting(self: FutureAccounting) { let FutureAccounting { epoch: _, storage_to_reclaim: _, rewards_to_distribute, } = self; - balance::destroy_zero(rewards_to_distribute) + rewards_to_distribute.destroy_zero() } #[test_only] - public fun burn_for_testing( - self: FutureAccounting, - ) { + public fun burn_for_testing(self: FutureAccounting) { let FutureAccounting { epoch: _, storage_to_reclaim: _, rewards_to_distribute, } = self; - balance::destroy_for_testing(rewards_to_distribute); + rewards_to_distribute.destroy_for_testing(); } /// A ring buffer holding future accounts for a continuous range of epochs. public struct FutureAccountingRingBuffer has store { - current_index : u64, - length : u64, - ring_buffer : vector>, + current_index: u64, + length: u64, + ring_buffer: vector>, } /// Constructor for FutureAccountingRingBuffer - public fun ring_new( - length: u64, - ) : FutureAccountingRingBuffer { - - let mut ring_buffer : vector> = vector::empty(); + public fun ring_new(length: u64): FutureAccountingRingBuffer { + let mut ring_buffer: vector> = vector::empty(); let mut i = 0; while (i < length) { - vector::push_back(&mut ring_buffer, FutureAccounting { + ring_buffer.push_back(FutureAccounting { epoch: i, storage_to_reclaim: 0, rewards_to_distribute: balance::zero(), @@ -103,43 +91,39 @@ module blob_store::storage_accounting { i = i + 1; }; - FutureAccountingRingBuffer { - current_index: 0, - length: length, - ring_buffer: ring_buffer, - } + FutureAccountingRingBuffer { current_index: 0, length: length, ring_buffer: ring_buffer } } /// Lookup an entry a number of epochs in the future. public fun ring_lookup_mut( self: &mut FutureAccountingRingBuffer, epochs_in_future: u64, - ) : &mut FutureAccounting { - + ): &mut FutureAccounting { // Check for out-of-bounds access. - assert!(epochs_in_future < self.length, ERROR_INDEX_OUT_OF_BOUNDS); + assert!(epochs_in_future < self.length, EIndexOutOfBounds); let actual_index = (epochs_in_future + self.current_index) % self.length; - vector::borrow_mut(&mut self.ring_buffer, actual_index) + &mut self.ring_buffer[actual_index] } public fun ring_pop_expand( - self: &mut FutureAccountingRingBuffer) - : FutureAccounting { - + self: &mut FutureAccountingRingBuffer, + ): FutureAccounting { // Get current epoch let current_index = self.current_index; - let current_epoch = vector::borrow_mut(&mut self.ring_buffer, current_index).epoch; + let current_epoch = self.ring_buffer[current_index].epoch; // Expand the ring buffer - vector::push_back(&mut self.ring_buffer, FutureAccounting { + self + .ring_buffer + .push_back(FutureAccounting { epoch: current_epoch + self.length, storage_to_reclaim: 0, rewards_to_distribute: balance::zero(), }); // Now swap remove the current element and increment the current_index - let accounting = vector::swap_remove(&mut self.ring_buffer, current_index); + let accounting = self.ring_buffer.swap_remove(current_index); self.current_index = (current_index + 1) % self.length; accounting diff --git a/contracts/blob_store/sources/storage_node.move b/contracts/blob_store/sources/storage_node.move index 9e3a3b33..bbd6b9f1 100644 --- a/contracts/blob_store/sources/storage_node.move +++ b/contracts/blob_store/sources/storage_node.move @@ -3,11 +3,12 @@ module blob_store::storage_node { use std::string::String; - #[test_only] - use std::string; use sui::group_ops::Element; use sui::bls12381::{G1, g1_from_bytes}; + // Error codes + const EInvalidNetworkPublicKey: u64 = 1; + /// Represents a storage node and its meta-data. /// /// Creation and deletion of storage node info is an @@ -17,6 +18,7 @@ module blob_store::storage_node { name: String, network_address: String, public_key: Element, + network_public_key: vector, shard_ids: vector, } @@ -25,45 +27,50 @@ module blob_store::storage_node { name: String, network_address: String, public_key: vector, + network_public_key: vector, shard_ids: vector, - ) : StorageNodeInfo { + ): StorageNodeInfo { + assert!(network_public_key.length() == 32, EInvalidNetworkPublicKey); StorageNodeInfo { name, network_address, public_key: g1_from_bytes(&public_key), + network_public_key, shard_ids } } - public fun public_key(self: &StorageNodeInfo) : &Element { + public fun public_key(self: &StorageNodeInfo): &Element { &self.public_key } - public fun shard_ids(self: &StorageNodeInfo) : &vector { + public fun network_public_key(self: &StorageNodeInfo): &vector { + &self.network_public_key + } + + public fun shard_ids(self: &StorageNodeInfo): &vector { &self.shard_ids } - public fun weight(self: &StorageNodeInfo) : u16 { - (vector::length(&self.shard_ids) as u16) + public fun weight(self: &StorageNodeInfo): u16 { + self.shard_ids.length() as u16 } #[test_only] /// Create a storage node with dummy name & address - public fun new_for_testing( - public_key: vector, - weight: u16, - ) : StorageNodeInfo { + public fun new_for_testing(public_key: vector, weight: u16): StorageNodeInfo { let mut i: u16 = 0; - let mut shard_ids = vector::empty(); + let mut shard_ids = vector[]; while (i < weight) { - vector::push_back(&mut shard_ids, i); + shard_ids.push_back(i); i = i + 1; }; StorageNodeInfo { - name: string::utf8(b"node"), - network_address: string::utf8(b"127.0.0.1"), + name: b"node".to_string(), + network_address: b"127.0.0.1".to_string(), public_key: g1_from_bytes(&public_key), - shard_ids + network_public_key: x"820e2b273530a00de66c9727c40f48be985da684286983f398ef7695b8a44677", + shard_ids, } } } diff --git a/contracts/blob_store/sources/storage_resource.move b/contracts/blob_store/sources/storage_resource.move index 7ef89980..7e7fc1a0 100644 --- a/contracts/blob_store/sources/storage_resource.move +++ b/contracts/blob_store/sources/storage_resource.move @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 module blob_store::storage_resource { - const EInvalidEpoch: u64 = 0; const EIncompatibleEpochs: u64 = 1; const EIncompatibleAmount: u64 = 2; @@ -15,15 +14,15 @@ module blob_store::storage_resource { storage_size: u64, } - public fun start_epoch(self: &Storage) : u64 { + public fun start_epoch(self: &Storage): u64 { self.start_epoch } - public fun end_epoch(self: &Storage) : u64 { + public fun end_epoch(self: &Storage): u64 { self.end_epoch } - public fun storage_size(self: &Storage) : u64 { + public fun storage_size(self: &Storage): u64 { self.storage_size } @@ -35,13 +34,8 @@ module blob_store::storage_resource { end_epoch: u64, storage_size: u64, ctx: &mut TxContext, - ) : Storage { - Storage { - id: object::new(ctx), - start_epoch, - end_epoch, - storage_size, - } + ): Storage { + Storage { id: object::new(ctx), start_epoch, end_epoch, storage_size } } /// Split the storage object into two based on `split_epoch` @@ -52,10 +46,10 @@ module blob_store::storage_resource { storage: &mut Storage, split_epoch: u64, ctx: &mut TxContext, - ) : Storage { + ): Storage { assert!( split_epoch >= storage.start_epoch && split_epoch <= storage.end_epoch, - EInvalidEpoch + EInvalidEpoch, ); let end_epoch = storage.end_epoch; storage.end_epoch = split_epoch; @@ -71,11 +65,7 @@ module blob_store::storage_resource { /// /// `storage` is modified to cover `split_size` and a new object covering /// `storage.storage_size - split_size` is created. - public fun split_by_size( - storage: &mut Storage, - split_size: u64, - ctx: &mut TxContext, - ) : Storage { + public fun split_by_size(storage: &mut Storage, split_size: u64, ctx: &mut TxContext): Storage { let storage_size = storage.storage_size - split_size; storage.storage_size = split_size; Storage { @@ -87,19 +77,16 @@ module blob_store::storage_resource { } /// Fuse two storage objects that cover adjacent periods with the same storage size. - public fun fuse_periods( - first: &mut Storage, - second: Storage, - ) { + public fun fuse_periods(first: &mut Storage, second: Storage) { let Storage { - id, - start_epoch: second_start, - end_epoch: second_end, - storage_size: second_size, - } = second; - object::delete(id); + id, + start_epoch: second_start, + end_epoch: second_end, + storage_size: second_size, + } = second; + id.delete(); assert!(first.storage_size == second_size, EIncompatibleAmount); - if(first.end_epoch == second_start) { + if (first.end_epoch == second_start) { first.end_epoch = second_end; } else { assert!(first.start_epoch == second_end, EIncompatibleEpochs); @@ -108,30 +95,24 @@ module blob_store::storage_resource { } /// Fuse two storage objects that cover the same period - public fun fuse_amount( - first: &mut Storage, - second: Storage, - ) { + public fun fuse_amount(first: &mut Storage, second: Storage) { let Storage { - id, - start_epoch: second_start, - end_epoch: second_end, - storage_size: second_size, - } = second; - object::delete(id); + id, + start_epoch: second_start, + end_epoch: second_end, + storage_size: second_size, + } = second; + id.delete(); assert!( first.start_epoch == second_start && first.end_epoch == second_end, - EIncompatibleEpochs + EIncompatibleEpochs, ); first.storage_size = first.storage_size + second_size; } /// Fuse two storage objects that either cover the same period /// or adjacent periods with the same storage size. - public fun fuse( - first: &mut Storage, - second: Storage, - ) { + public fun fuse(first: &mut Storage, second: Storage) { if (first.start_epoch == second.start_epoch) { // Fuse by storage_size fuse_amount(first, second); @@ -148,25 +129,18 @@ module blob_store::storage_resource { end_epoch: u64, storage_size: u64, ctx: &mut TxContext, - ) : Storage { - Storage { - id: object::new(ctx), - start_epoch, - end_epoch, - storage_size, - } + ): Storage { + Storage { id: object::new(ctx), start_epoch, end_epoch, storage_size } } /// Destructor for [Storage] objects - public fun destroy( - storage: Storage, - ) { + public fun destroy(storage: Storage) { let Storage { id, start_epoch: _, end_epoch: _, storage_size: _, } = storage; - object::delete(id); + id.delete(); } } diff --git a/contracts/blob_store/sources/system.move b/contracts/blob_store/sources/system.move index 9f2e3f66..50927f89 100644 --- a/contracts/blob_store/sources/system.move +++ b/contracts/blob_store/sources/system.move @@ -2,8 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 module blob_store::system { - use sui::balance::{Self}; - use sui::coin::{Self, Coin}; + use sui::coin::Coin; use sui::event; use sui::table::{Self, Table}; use sui::bcs; @@ -14,31 +13,30 @@ module blob_store::system { use blob_store::blob_events::emit_invalid_blob_id; // Errors - const ERROR_INCORRECT_COMMITTEE : u64 = 0; - const ERROR_SYNC_EPOCH_CHANGE : u64 = 1; - const ERROR_INVALID_PERIODS_AHEAD : u64 = 2; - const ERROR_STORAGE_EXCEEDED : u64 = 3; - const ERROR_INVALID_MSG_TYPE : u64 = 4; - const ERROR_INVALID_ID_EPOCH : u64 = 5; + const EIncorrectCommittee: u64 = 0; + const ESyncEpochChange: u64 = 1; + const EInvalidPeriodsAhead: u64 = 2; + const EStorageExceeded: u64 = 3; + const EInvalidMsgType: u64 = 4; + const EInvalidIdEpoch: u64 = 5; // Message types: const EPOCH_DONE_MSG_TYPE: u8 = 0; - const INVALID_BLOB_ID_MSG_TYPE : u8 = 2; + const INVALID_BLOB_ID_MSG_TYPE: u8 = 2; // Epoch status values #[allow(unused_const)] - const EPOCH_STATUS_DONE : u8 = 0; + const EPOCH_STATUS_DONE: u8 = 0; #[allow(unused_const)] - const EPOCH_STATUS_SYNC : u8 = 1; + const EPOCH_STATUS_SYNC: u8 = 1; /// The maximum number of periods ahead we allow for storage reservations. /// This number is a placeholder, and assumes an epoch is a week, /// and therefore 2 x 52 weeks = 2 years. - const MAX_PERIODS_AHEAD : u64 = 104; - + const MAX_PERIODS_AHEAD: u64 = 104; // Keep in sync with the same constant in `crates/walrus-sui/utils.rs`. - const BYTES_PER_UNIT_SIZE : u64 = 1_024; + const BYTES_PER_UNIT_SIZE: u64 = 1_024; // Event types @@ -58,47 +56,35 @@ module blob_store::system { #[allow(unused_field)] public struct System has key, store { - id: UID, - /// The current committee, with the current epoch. /// The option is always Some, but need it for swap. current_committee: Option, - /// When we first enter the current epoch we SYNC, /// and then we are DONE after a cert from a quorum. epoch_status: u8, - // Some accounting - total_capacity_size : u64, - used_capacity_size : u64, - + total_capacity_size: u64, + used_capacity_size: u64, /// The price per unit size of storage. price_per_unit_size: u64, - /// Tables about the future and the past. past_committees: Table, future_accounting: FutureAccountingRingBuffer, } /// Get epoch. Uses the committee to get the epoch. - public fun epoch( - self: &System - ) : u64 { + public fun epoch(self: &System): u64 { committee::epoch(option::borrow(&self.current_committee)) } /// Accessor for total capacity size. - public fun total_capacity_size( - self: &System - ) : u64 { + public fun total_capacity_size(self: &System): u64 { self.total_capacity_size } /// Accessor for used capacity size. - public fun used_capacity_size( - self: &System - ) : u64 { + public fun used_capacity_size(self: &System): u64 { self.used_capacity_size } @@ -110,10 +96,9 @@ module blob_store::system { first_committee: Committee, capacity: u64, price: u64, - ctx: &mut TxContext - ) : System { - - assert!(committee::epoch(&first_committee) == 0, ERROR_INCORRECT_COMMITTEE); + ctx: &mut TxContext, + ): System { + assert!(first_committee.epoch() == 0, EIncorrectCommittee); // We emit both sync and done events for the first epoch. event::emit(EpochChangeSync { @@ -121,9 +106,7 @@ module blob_store::system { total_capacity_size: capacity, used_capacity_size: 0, }); - event::emit(EpochChangeDone { - epoch: 0, - }); + event::emit(EpochChangeDone { epoch: 0 }); System { id: object::new(ctx), @@ -145,23 +128,19 @@ module blob_store::system { first_committee: Committee, capacity: u64, price: u64, - ctx: &mut TxContext + ctx: &mut TxContext, ) { - let sys : System = new(first_committee, capacity, price, ctx); + let sys: System = new(first_committee, capacity, price, ctx); transfer::share_object(sys); } /// An accessor for the current committee. - public fun current_committee( - self: &System - ) : &Committee { - option::borrow(&self.current_committee) + public fun current_committee(self: &System): &Committee { + self.current_committee.borrow() } - public fun n_shards( - self: &System - ) : u16 { - committee::n_shards(current_committee(self)) + public fun n_shards(self: &System): u16 { + current_committee(self).n_shards() } /// Update epoch to next epoch, and also update the committee, price and capacity. @@ -170,35 +149,30 @@ module blob_store::system { new_committee: Committee, new_capacity: u64, new_price: u64, - ) : FutureAccounting { - + ): FutureAccounting { // Must be in DONE state to move epochs. This is the way. - assert!(self.epoch_status == EPOCH_STATUS_DONE, ERROR_SYNC_EPOCH_CHANGE); + assert!(self.epoch_status == EPOCH_STATUS_DONE, ESyncEpochChange); // Check new committee is valid, the existence of a committee for the next epoch // is proof that the time has come to move epochs. let old_epoch = epoch(self); let new_epoch = old_epoch + 1; - assert!(committee::epoch(&new_committee) == new_epoch, ERROR_INCORRECT_COMMITTEE); - let old_committee = option::swap(&mut self.current_committee, new_committee); + assert!(new_committee.epoch() == new_epoch, EIncorrectCommittee); + let old_committee = self.current_committee.swap(new_committee); // Add the old committee to the past_committees table. - table::add(&mut self.past_committees, old_epoch, old_committee); + self.past_committees.add(old_epoch, old_committee); // Update the system object. self.total_capacity_size = new_capacity; self.price_per_unit_size = new_price; self.epoch_status = EPOCH_STATUS_SYNC; - let mut accounts_old_epoch = storage_accounting::ring_pop_expand( - &mut self.future_accounting - ); - assert!(storage_accounting::epoch(&accounts_old_epoch) == old_epoch, - ERROR_SYNC_EPOCH_CHANGE); + let mut accounts_old_epoch = self.future_accounting.ring_pop_expand(); + assert!(accounts_old_epoch.epoch() == old_epoch, ESyncEpochChange); // Update storage based on the accounts data. - self.used_capacity_size = self.used_capacity_size - - storage_accounting::storage_to_reclaim(&mut accounts_old_epoch); + self.used_capacity_size = self.used_capacity_size - accounts_old_epoch.storage_to_reclaim(); // Emit Sync event. event::emit(EpochChangeSync { @@ -217,30 +191,31 @@ module blob_store::system { periods_ahead: u64, mut payment: Coin, ctx: &mut TxContext, - ) : (Storage, Coin) { - + ): (Storage, Coin) { // Check the period is within the allowed range. - assert!(periods_ahead > 0, ERROR_INVALID_PERIODS_AHEAD); - assert!(periods_ahead <= MAX_PERIODS_AHEAD, ERROR_INVALID_PERIODS_AHEAD); + assert!(periods_ahead > 0, EInvalidPeriodsAhead); + assert!(periods_ahead <= MAX_PERIODS_AHEAD, EInvalidPeriodsAhead); // Check capacity is available. - assert!(self.used_capacity_size + storage_amount <= self.total_capacity_size, - ERROR_STORAGE_EXCEEDED); + assert!( + self.used_capacity_size + storage_amount <= self.total_capacity_size, + EStorageExceeded, + ); // Pay rewards for each future epoch into the future accounting. - let storage_units = (storage_amount + BYTES_PER_UNIT_SIZE - 1)/BYTES_PER_UNIT_SIZE; + let storage_units = (storage_amount + BYTES_PER_UNIT_SIZE - 1) / BYTES_PER_UNIT_SIZE; let period_payment_due = self.price_per_unit_size * storage_units; - let coin_balance = coin::balance_mut(&mut payment); + let coin_balance = payment.balance_mut(); let mut i = 0; while (i < periods_ahead) { - let accounts = storage_accounting::ring_lookup_mut(&mut self.future_accounting, i); + let accounts = self.future_accounting.ring_lookup_mut(i); // Distribute rewards - let rewards_balance = storage_accounting::rewards_to_distribute(accounts); + let rewards_balance = accounts.rewards_to_distribute(); // Note this will abort if the balance is not enough. - let epoch_payment = balance::split(coin_balance, period_payment_due); - balance::join(rewards_balance, epoch_payment); + let epoch_payment = coin_balance.split(period_payment_due); + rewards_balance.join(epoch_payment); i = i + 1; }; @@ -249,24 +224,23 @@ module blob_store::system { self.used_capacity_size = self.used_capacity_size + storage_amount; // Account the space to reclaim in the future. - let final_account = storage_accounting::ring_lookup_mut( - &mut self.future_accounting, periods_ahead - 1); - storage_accounting::increase_storage_to_reclaim(final_account, storage_amount); + let final_account = self.future_accounting.ring_lookup_mut(periods_ahead - 1); + final_account.increase_storage_to_reclaim(storage_amount); let self_epoch = epoch(self); - (storage_resource::create_storage( - self_epoch, - self_epoch + periods_ahead, - storage_amount, - ctx, - ), - payment) + ( + storage_resource::create_storage( + self_epoch, + self_epoch + periods_ahead, + storage_amount, + ctx, + ), + payment, + ) } #[test_only] - public fun set_done_for_testing( - self: &mut System - ) { + public fun set_done_for_testing(self: &mut System) { self.epoch_status = EPOCH_STATUS_DONE; } @@ -281,47 +255,35 @@ module blob_store::system { /// Construct the certified sync done message, note that constructing /// implies a certified message, that is already checked. - public fun certify_sync_done_message( - message: committee::CertifiedMessage - ) : CertifiedSyncDone { - + public fun certify_sync_done_message(message: committee::CertifiedMessage): CertifiedSyncDone { // Assert type is correct - assert!(committee::intent_type(&message) == EPOCH_DONE_MSG_TYPE, - ERROR_INVALID_MSG_TYPE); + assert!(message.intent_type() == EPOCH_DONE_MSG_TYPE, EInvalidMsgType); // The SyncDone message has no payload besides the epoch. // Which happens to already be parsed in the header of the // certified message. - CertifiedSyncDone { epoch: committee::cert_epoch(&message) } + CertifiedSyncDone { epoch: message.cert_epoch() } } // make a test only certified message. #[test_only] - public fun make_sync_done_message_for_testing( - epoch: u64 - ) : CertifiedSyncDone { + public fun make_sync_done_message_for_testing(epoch: u64): CertifiedSyncDone { CertifiedSyncDone { epoch } } /// Use the certified message to advance the epoch status to DONE. - public fun sync_done_for_epoch( - system: &mut System, - message: CertifiedSyncDone, - ) - { + public fun sync_done_for_epoch(system: &mut System, message: CertifiedSyncDone) { // Assert the epoch is correct. - assert!(message.epoch == epoch(system), ERROR_SYNC_EPOCH_CHANGE); + assert!(message.epoch == epoch(system), ESyncEpochChange); // Assert we are in the sync state. - assert!(system.epoch_status == EPOCH_STATUS_SYNC, ERROR_SYNC_EPOCH_CHANGE); + assert!(system.epoch_status == EPOCH_STATUS_SYNC, ESyncEpochChange); // Move to done state. system.epoch_status = EPOCH_STATUS_DONE; - event::emit(EpochChangeDone { - epoch: message.epoch, - }); + event::emit(EpochChangeDone { epoch: message.epoch }); } // The logic to register an invalid Blob ID @@ -335,29 +297,28 @@ module blob_store::system { } // read the blob id - public fun invalid_blob_id( - self: &CertifiedInvalidBlobID - ) : u256 { + public fun invalid_blob_id(self: &CertifiedInvalidBlobID): u256 { self.blob_id } /// Construct the certified invalid Blob ID message, note that constructing /// implies a certified message, that is already checked. public fun invalid_blob_id_message( - message: committee::CertifiedMessage - ) : CertifiedInvalidBlobID { - + message: committee::CertifiedMessage, + ): CertifiedInvalidBlobID { // Assert type is correct - assert!(committee::intent_type(&message) == INVALID_BLOB_ID_MSG_TYPE, - ERROR_INVALID_MSG_TYPE); + assert!( + message.intent_type() == INVALID_BLOB_ID_MSG_TYPE, + EInvalidMsgType, + ); // The InvalidBlobID message has no payload besides the blob_id. // The certified blob message contain a blob_id : u256 - let epoch = committee::cert_epoch(&message); - let message_body = committee::into_message(message); + let epoch = message.cert_epoch(); + let message_body = message.into_message(); let mut bcs_body = bcs::new(message_body); - let blob_id = bcs::peel_u256(&mut bcs_body); + let blob_id = bcs_body.peel_u256(); // This output is provided as a service in case anything else needs to rely on // certified invalid blob ID information in the future. But out base design only @@ -373,15 +334,14 @@ module blob_store::system { system: &System, message: CertifiedInvalidBlobID, ) { - // Assert the epoch is correct. let epoch = message.epoch; - assert!(epoch == epoch(system), ERROR_INVALID_ID_EPOCH); + assert!(epoch == epoch(system), EInvalidIdEpoch); // Emit the event about a blob id being invalid here. emit_invalid_blob_id( epoch, - message.blob_id + message.blob_id, ); } @@ -393,19 +353,18 @@ module blob_store::system { signature: vector, members: vector, message: vector, - ) : u256 { - let committee = option::borrow(&system.current_committee); + ): u256 { + let committee = system.current_committee.borrow(); - let certified_message = committee::verify_quorum_in_epoch( - committee, + let certified_message = committee.verify_quorum_in_epoch( signature, members, - message); + message, + ); let invalid_blob_message = invalid_blob_id_message(certified_message); let blob_id = invalid_blob_message.blob_id; inner_declare_invalid_blob_id(system, invalid_blob_message); blob_id } - } diff --git a/contracts/blob_store/sources/tests/blob_tests.move b/contracts/blob_store/sources/tests/blob_tests.move index 8c45c561..75a834d4 100644 --- a/contracts/blob_store/sources/tests/blob_tests.move +++ b/contracts/blob_store/sources/tests/blob_tests.move @@ -3,7 +3,6 @@ #[test_only] module blob_store::blob_tests { - use sui::coin; use sui::bcs; @@ -15,17 +14,16 @@ module blob_store::blob_tests { use blob_store::blob; use blob_store::storage_node; - use blob_store::storage_resource::{ - split_by_epoch, - destroy}; + use blob_store::storage_resource::{split_by_epoch, destroy}; const RED_STUFF: u8 = 0; + const NETWORK_PUBLIC_KEY: vector = + x"820e2b273530a00de66c9727c40f48be985da684286983f398ef7695b8a44677"; public struct TESTWAL has store, drop {} #[test] - public fun test_blob_register_happy_path() : system::System { - + public fun test_blob_register_happy_path(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -35,12 +33,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -51,9 +53,8 @@ module blob_store::blob_tests { system } - #[test, expected_failure(abort_code=blob::ERROR_RESOURCE_SIZE)] - public fun test_blob_insufficient_space() : system::System { - + #[test, expected_failure(abort_code=blob::EResourceSize)] + public fun test_blob_insufficient_space(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -63,12 +64,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs - TOO LITTLE SPACE let (storage, fake_coin) = system::reserve_space( - &mut system, 5000, 3, fake_coin, &mut ctx); + &mut system, + 5000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -80,8 +85,7 @@ module blob_store::blob_tests { } #[test] - public fun test_blob_certify_happy_path() : system::System { - + public fun test_blob_certify_happy_path(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -91,12 +95,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -108,7 +116,7 @@ module blob_store::blob_tests { blob::certify_with_certified_msg(&system, certify_message, &mut blob1); // Assert certified - assert!(option::is_some(blob::certified(&blob1)), 0); + assert!(option::is_some(blob::certified_epoch(&blob1)), 0); coin::burn_for_testing(fake_coin); blob::drop_for_testing(blob1); @@ -116,8 +124,7 @@ module blob_store::blob_tests { } #[test] - public fun test_blob_certify_single_function() : system::System { - + public fun test_blob_certify_single_function(): system::System { let mut ctx = tx_context::dummy(); // Derive blob ID and root_hash from bytes @@ -135,7 +142,8 @@ module blob_store::blob_tests { 119, 174, 25, 167, 128, 57, 96, 1, 163, 56, 61, 132, 191, 35, 44, 18, 231, 224, 79, 178, 85, 51, 69, 53, - 214, 95, 198, 203, 56, 221, 111, 83]; + 214, 95, 198, 203, 56, 221, 111, 83 + ]; let mut encode = bcs::new(blob_id_vec); let blob_id = bcs::peel_u256(&mut encode); @@ -166,14 +174,17 @@ module blob_store::blob_tests { // Create storage node // Pk corresponding to secret key scalar(117) - let public_key = vector[149, 234, 204, 58, 220, 9, 200, 39, 89, 63, 88, 30, 142, 45, + let public_key = vector[ + 149, 234, 204, 58, 220, 9, 200, 39, 89, 63, 88, 30, 142, 45, 224, 104, 191, 76, 245, 208, 192, 235, 41, 229, 55, 47, 13, 35, 54, 71, 136, 238, 15, - 155, 235, 17, 44, 138, 126, 156, 47, 12, 114, 4, 51, 112, 92, 240]; + 155, 235, 17, 44, 138, 126, 156, 47, 12, 114, 4, 51, 112, 92, 240 + ]; let storage_node = storage_node::create_storage_node_info( string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, - vector[0, 1, 2, 3, 4, 5] + NETWORK_PUBLIC_KEY, + vector[0, 1, 2, 3, 4, 5], ); // Create a new committee @@ -181,8 +192,7 @@ module blob_store::blob_tests { let committee = committee::create_committee(&cap, 0, vector[storage_node]); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( @@ -190,7 +200,7 @@ module blob_store::blob_tests { 1_000_000, 3, fake_coin, - &mut ctx + &mut ctx, ); // Register a Blob @@ -201,23 +211,22 @@ module blob_store::blob_tests { root_hash, 10000, RED_STUFF, - &mut ctx + &mut ctx, ); // Set certify blob::certify(&system, &mut blob1, signature, vector[0], confirmation); // Assert certified - assert!(option::is_some(blob::certified(&blob1)), 0); + assert!(option::is_some(blob::certified_epoch(&blob1)), 0); coin::burn_for_testing(fake_coin); blob::drop_for_testing(blob1); system } - #[test, expected_failure(abort_code=blob::ERROR_WRONG_EPOCH)] - public fun test_blob_certify_bad_epoch() : system::System { - + #[test, expected_failure(abort_code=blob::EWrongEpoch)] + public fun test_blob_certify_bad_epoch(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -227,12 +236,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -249,9 +262,8 @@ module blob_store::blob_tests { system } - #[test, expected_failure(abort_code=blob::ERROR_INVALID_BLOB_ID)] - public fun test_blob_certify_bad_blob_id() : system::System { - + #[test, expected_failure(abort_code=blob::EInvalidBlobId)] + public fun test_blob_certify_bad_blob_id(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -261,12 +273,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -283,9 +299,8 @@ module blob_store::blob_tests { system } - #[test, expected_failure(abort_code=blob::ERROR_RESOURCE_BOUNDS)] - public fun test_blob_certify_past_epoch() : system::System { - + #[test, expected_failure(abort_code=blob::EResourceBounds)] + public fun test_blob_certify_past_epoch(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -295,12 +310,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -333,8 +352,7 @@ module blob_store::blob_tests { } #[test] - public fun test_blob_happy_destroy() : system::System { - + public fun test_blob_happy_destroy(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -344,12 +362,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -383,9 +405,8 @@ module blob_store::blob_tests { system } - #[test, expected_failure(abort_code=blob::ERROR_RESOURCE_BOUNDS)] - public fun test_blob_unhappy_destroy() : system::System { - + #[test, expected_failure(abort_code=blob::EResourceBounds)] + public fun test_blob_unhappy_destroy(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -395,12 +416,16 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -421,10 +446,11 @@ module blob_store::blob_tests { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]); + ] + ); - let message = blob::certify_blob_message(msg); - assert!(blob::message_blob_id(&message) == 0xAA, 0); + let message = blob::certify_blob_message(msg); + assert!(blob::message_blob_id(&message) == 0xAA, 0); } #[test, expected_failure] @@ -435,15 +461,15 @@ module blob_store::blob_tests { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]); + ], + ); - let message = blob::certify_blob_message(msg); - assert!(blob::message_blob_id(&message) == 0xAA, 0); + let message = blob::certify_blob_message(msg); + assert!(blob::message_blob_id(&message) == 0xAA, 0); } #[test] - public fun test_blob_extend_happy_path() : system::System { - + public fun test_blob_extend_happy_path(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -453,20 +479,28 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Get a longer storage period let (mut storage_long, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 5, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 5, + fake_coin, + &mut ctx, + ); // Split by period - let trailing_storage = - split_by_epoch(&mut storage_long, 3, &mut ctx); + let trailing_storage = split_by_epoch(&mut storage_long, 3, &mut ctx); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -480,7 +514,7 @@ module blob_store::blob_tests { blob::extend(&system, &mut blob1, trailing_storage); // Assert certified - assert!(option::is_some(blob::certified(&blob1)), 0); + assert!(option::is_some(blob::certified_epoch(&blob1)), 0); destroy(storage_long); coin::burn_for_testing(fake_coin); @@ -489,8 +523,7 @@ module blob_store::blob_tests { } #[test, expected_failure] - public fun test_blob_extend_bad_period() : system::System { - + public fun test_blob_extend_bad_period(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -500,20 +533,28 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Get a longer storage period let (mut storage_long, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 5, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 5, + fake_coin, + &mut ctx, + ); // Split by period - let trailing_storage = - split_by_epoch(&mut storage_long, 4, &mut ctx); + let trailing_storage = split_by_epoch(&mut storage_long, 4, &mut ctx); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -532,9 +573,8 @@ module blob_store::blob_tests { system } - #[test,expected_failure(abort_code=blob::ERROR_RESOURCE_BOUNDS)] - public fun test_blob_unhappy_extend() : system::System { - + #[test,expected_failure(abort_code=blob::EResourceBounds)] + public fun test_blob_unhappy_extend(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -544,20 +584,28 @@ module blob_store::blob_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000000000, 5, &mut ctx); + let mut system: system::System = system::new(committee, 1000000000, 5, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 3, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 3, + fake_coin, + &mut ctx, + ); // Get a longer storage period let (mut storage_long, fake_coin) = system::reserve_space( - &mut system, 1_000_000, 5, fake_coin, &mut ctx); + &mut system, + 1_000_000, + 5, + fake_coin, + &mut ctx, + ); // Split by period - let trailing_storage = - split_by_epoch(&mut storage_long, 3, &mut ctx); + let trailing_storage = split_by_epoch(&mut storage_long, 3, &mut ctx); // Register a Blob let blob_id = blob::derive_blob_id(0xABC, RED_STUFF, 5000); @@ -596,5 +644,4 @@ module blob_store::blob_tests { coin::burn_for_testing(fake_coin); system } - } diff --git a/contracts/blob_store/sources/tests/bls_tests.move b/contracts/blob_store/sources/tests/bls_tests.move index 8e356215..27a39aa5 100644 --- a/contracts/blob_store/sources/tests/bls_tests.move +++ b/contracts/blob_store/sources/tests/bls_tests.move @@ -64,7 +64,7 @@ module blob_store::bls_tests { } - #[test, expected_failure(abort_code = bls_aggregate::ERROR_SIG_VERIFICATION) ] + #[test, expected_failure(abort_code = bls_aggregate::ESigVerification) ] public fun test_add_members_error(): BlsCommittee { let pk0 = vector[166, 14, 117, 25, 14, 98, 182, 165, 65, 66, 209, 71, 40, 154, 115, 92, 76, 225, 26, 157, 153, 117, 67, 218, 83, 154, 61, 181, 125, 239, 94, 216, 59, 164, 11, 116, 229, 80, 101, 240, 43, 53, 170, 29, 80, 76, 64, 75]; let pk1 = vector[174, 18, 3, 148, 89, 198, 4, 145, 103, 43, 106, 98, 130, 53, 93, 135, 101, 186, 98, 114, 56, 127, 185, 26, 62, 150, 4, 250, 42, 129, 69, 12, 241, 107, 135, 11, 180, 70, 252, 58, 62, 10, 24, 127, 255, 111, 137, 69]; @@ -98,7 +98,7 @@ module blob_store::bls_tests { } - #[test, expected_failure(abort_code = bls_aggregate::ERROR_SIG_VERIFICATION) ] + #[test, expected_failure(abort_code = bls_aggregate::ESigVerification) ] public fun test_incorrect_signature_error(): BlsCommittee { let pk0 = vector[166, 14, 117, 25, 14, 98, 182, 165, 65, 66, 209, 71, 40, 154, 115, 92, 76, 225, 26, 157, 153, 117, 67, 218, 83, 154, 61, 181, 125, 239, 94, 216, 59, 164, 11, 116, 229, 80, 101, 240, 43, 53, 170, 29, 80, 76, 64, 75]; let pk1 = vector[174, 18, 3, 148, 89, 198, 4, 145, 103, 43, 106, 98, 130, 53, 93, 135, 101, 186, 98, 114, 56, 127, 185, 26, 62, 150, 4, 250, 42, 129, 69, 12, 241, 107, 135, 11, 180, 70, 252, 58, 62, 10, 24, 127, 255, 111, 137, 69]; @@ -133,7 +133,7 @@ module blob_store::bls_tests { } - #[test, expected_failure(abort_code = bls_aggregate::ERROR_TOTAL_MEMBER_ORDER) ] + #[test, expected_failure(abort_code = bls_aggregate::ETotalMemberOrder) ] public fun test_duplicate_member_error(): BlsCommittee { let pk0 = vector[166, 14, 117, 25, 14, 98, 182, 165, 65, 66, 209, 71, 40, 154, 115, 92, 76, 225, 26, 157, 153, 117, 67, 218, 83, 154, 61, 181, 125, 239, 94, 216, 59, 164, 11, 116, 229, 80, 101, 240, 43, 53, 170, 29, 80, 76, 64, 75]; let pk1 = vector[174, 18, 3, 148, 89, 198, 4, 145, 103, 43, 106, 98, 130, 53, 93, 135, 101, 186, 98, 114, 56, 127, 185, 26, 62, 150, 4, 250, 42, 129, 69, 12, 241, 107, 135, 11, 180, 70, 252, 58, 62, 10, 24, 127, 255, 111, 137, 69]; @@ -168,7 +168,7 @@ module blob_store::bls_tests { } - #[test, expected_failure(abort_code = bls_aggregate::ERROR_NOT_ENOUGH_STAKE) ] + #[test, expected_failure(abort_code = bls_aggregate::ENotEnoughStake) ] public fun test_incorrect_stake_error(): BlsCommittee { let pk0 = vector[166, 14, 117, 25, 14, 98, 182, 165, 65, 66, 209, 71, 40, 154, 115, 92, 76, 225, 26, 157, 153, 117, 67, 218, 83, 154, 61, 181, 125, 239, 94, 216, 59, 164, 11, 116, 229, 80, 101, 240, 43, 53, 170, 29, 80, 76, 64, 75]; let pk1 = vector[174, 18, 3, 148, 89, 198, 4, 145, 103, 43, 106, 98, 130, 53, 93, 135, 101, 186, 98, 114, 56, 127, 185, 26, 62, 150, 4, 250, 42, 129, 69, 12, 241, 107, 135, 11, 180, 70, 252, 58, 62, 10, 24, 127, 255, 111, 137, 69]; diff --git a/contracts/blob_store/sources/tests/epoch_change_tests.move b/contracts/blob_store/sources/tests/epoch_change_tests.move index ac525266..c74d577c 100644 --- a/contracts/blob_store/sources/tests/epoch_change_tests.move +++ b/contracts/blob_store/sources/tests/epoch_change_tests.move @@ -12,15 +12,14 @@ module blob_store::epoch_change_tests { use blob_store::storage_resource as sr; // Keep in sync with the same constant in `blob_store::system` - const BYTES_PER_UNIT_SIZE : u64 = 1_024; + const BYTES_PER_UNIT_SIZE: u64 = 1_024; public struct TESTWAL has store, drop {} - // ------------- TESTS -------------------- + // ------------- TESTS -------------------- #[test] - public fun test_use_system() : system::System { - + public fun test_use_system(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -30,8 +29,12 @@ module blob_store::epoch_change_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1_000 * BYTES_PER_UNIT_SIZE, 2, &mut ctx); + let mut system: system::System = system::new( + committee, + 1_000 * BYTES_PER_UNIT_SIZE, + 2, + &mut ctx, + ); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space( @@ -39,7 +42,7 @@ module blob_store::epoch_change_tests { 10 * BYTES_PER_UNIT_SIZE, 3, fake_coin, - &mut ctx + &mut ctx, ); sr::destroy(storage); @@ -58,7 +61,7 @@ module blob_store::epoch_change_tests { &mut system, committee, 1_000 * BYTES_PER_UNIT_SIZE, - 3 + 3, ); assert!(balance::value(sa::rewards_to_distribute(&mut epoch_accounts)) == 20, 0); @@ -68,7 +71,7 @@ module blob_store::epoch_change_tests { 5 * BYTES_PER_UNIT_SIZE, 1, fake_coin, - &mut ctx + &mut ctx, ); sr::destroy(storage); // The value of the coin should be 40 - 3 x 5 @@ -84,7 +87,7 @@ module blob_store::epoch_change_tests { &mut system, committee, 1_000 * BYTES_PER_UNIT_SIZE, - 3 + 3, ); assert!(balance::value(sa::rewards_to_distribute(&mut epoch_accounts)) == 35, 0); sa::burn_for_testing(epoch_accounts); @@ -98,7 +101,7 @@ module blob_store::epoch_change_tests { &mut system, committee, 1_000 * BYTES_PER_UNIT_SIZE, - 3 + 3, ); assert!(balance::value(sa::rewards_to_distribute(&mut epoch_accounts)) == 20, 0); sa::burn_for_testing(epoch_accounts); @@ -113,7 +116,7 @@ module blob_store::epoch_change_tests { &mut system, committee, 1_000 * BYTES_PER_UNIT_SIZE, - 3 + 3, ); assert!(balance::value(sa::rewards_to_distribute(&mut epoch_accounts)) == 0, 0); sa::burn_for_testing(epoch_accounts); @@ -126,9 +129,8 @@ module blob_store::epoch_change_tests { system } - #[test, expected_failure(abort_code=system::ERROR_SYNC_EPOCH_CHANGE)] - public fun test_move_sync_err_system() : system::System { - + #[test, expected_failure(abort_code=system::ESyncEpochChange)] + public fun test_move_sync_err_system(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -138,8 +140,7 @@ module blob_store::epoch_change_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000, 2, &mut ctx); + let mut system: system::System = system::new(committee, 1000, 2, &mut ctx); // Advance epoch -- to epoch 1 let committee = committee::committee_for_testing(1); @@ -157,9 +158,8 @@ module blob_store::epoch_change_tests { system } - #[test, expected_failure(abort_code=system::ERROR_STORAGE_EXCEEDED)] - public fun test_fail_capacity_system() : system::System { - + #[test, expected_failure(abort_code=system::EStorageExceeded)] + public fun test_fail_capacity_system(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -169,8 +169,7 @@ module blob_store::epoch_change_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000, 2, &mut ctx); + let mut system: system::System = system::new(committee, 1000, 2, &mut ctx); // Get some space for a few epochs let (storage, fake_coin) = system::reserve_space(&mut system, 10, 3, fake_coin, &mut ctx); @@ -192,8 +191,7 @@ module blob_store::epoch_change_tests { } #[test] - public fun test_sync_done_happy() : system::System { - + public fun test_sync_done_happy(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -203,8 +201,7 @@ module blob_store::epoch_change_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000, 2, &mut ctx); + let mut system: system::System = system::new(committee, 1000, 2, &mut ctx); // Advance epoch -- to epoch 1 let committee = committee::committee_for_testing(1); @@ -228,9 +225,8 @@ module blob_store::epoch_change_tests { system } - #[test, expected_failure(abort_code=system::ERROR_SYNC_EPOCH_CHANGE)] - public fun test_sync_done_unhappy() : system::System { - + #[test, expected_failure(abort_code=system::ESyncEpochChange)] + public fun test_sync_done_unhappy(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -240,8 +236,7 @@ module blob_store::epoch_change_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000, 2, &mut ctx); + let mut system: system::System = system::new(committee, 1000, 2, &mut ctx); // Advance epoch -- to epoch 1 let committee = committee::committee_for_testing(1); @@ -259,9 +254,8 @@ module blob_store::epoch_change_tests { system } - #[test, expected_failure(abort_code=system::ERROR_SYNC_EPOCH_CHANGE)] - public fun test_twice_unhappy() : system::System { - + #[test, expected_failure(abort_code=system::ESyncEpochChange)] + public fun test_twice_unhappy(): system::System { let mut ctx = tx_context::dummy(); // A test coin. @@ -271,8 +265,7 @@ module blob_store::epoch_change_tests { let committee = committee::committee_for_testing(0); // Create a new system object - let mut system : system::System = system::new(committee, - 1000, 2, &mut ctx); + let mut system: system::System = system::new(committee, 1000, 2, &mut ctx); // Advance epoch -- to epoch 1 let committee = committee::committee_for_testing(1); diff --git a/contracts/blob_store/sources/tests/invalid_tests.move b/contracts/blob_store/sources/tests/invalid_tests.move index 2018a898..90765d54 100644 --- a/contracts/blob_store/sources/tests/invalid_tests.move +++ b/contracts/blob_store/sources/tests/invalid_tests.move @@ -11,6 +11,8 @@ module blob_store::invalid_tests { use blob_store::storage_node; use blob_store::storage_accounting as sa; + const NETWORK_PUBLIC_KEY: vector = + x"820e2b273530a00de66c9727c40f48be985da684286983f398ef7695b8a44677"; public struct TESTWAL has store, drop {} @@ -43,6 +45,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); @@ -78,6 +81,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); @@ -99,6 +103,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); let committee = committee::create_committee(&cap, epoch, vector[storage_node]); @@ -150,6 +155,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); @@ -171,6 +177,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); let committee = committee::create_committee(&cap, epoch, vector[storage_node]); @@ -216,7 +223,7 @@ module blob_store::invalid_tests { } - #[test, expected_failure(abort_code=system::ERROR_INVALID_ID_EPOCH)] + #[test, expected_failure(abort_code=system::EInvalidIdEpoch)] public fun test_system_invalid_id_wrong_epoch() : system::System { let mut ctx = tx_context::dummy(); @@ -231,6 +238,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); @@ -252,6 +260,7 @@ module blob_store::invalid_tests { string::utf8(b"node"), string::utf8(b"127.0.0.1"), public_key, + NETWORK_PUBLIC_KEY, vector[0, 1, 2, 3, 4, 5] ); let committee = committee::create_committee(&cap, epoch, vector[storage_node]); diff --git a/contracts/blob_store/sources/tests/ringbuffer_tests.move b/contracts/blob_store/sources/tests/ringbuffer_tests.move index 91afc062..6e820d19 100644 --- a/contracts/blob_store/sources/tests/ringbuffer_tests.move +++ b/contracts/blob_store/sources/tests/ringbuffer_tests.move @@ -11,10 +11,8 @@ module blob_store::ringbuffer_tests { // ------------- TESTS -------------------- #[test] - public fun test_basic_ring_buffer() : FutureAccountingRingBuffer{ - - let mut buffer : FutureAccountingRingBuffer - = sa::ring_new(3); + public fun test_basic_ring_buffer(): FutureAccountingRingBuffer { + let mut buffer: FutureAccountingRingBuffer = sa::ring_new(3); assert!(sa::epoch(sa::ring_lookup_mut(&mut buffer, 0)) == 0, 100); assert!(sa::epoch(sa::ring_lookup_mut(&mut buffer, 1)) == 1, 100); @@ -36,14 +34,11 @@ module blob_store::ringbuffer_tests { } #[test, expected_failure] - public fun test_oob_fail_ring_buffer() : FutureAccountingRingBuffer{ - - let mut buffer : FutureAccountingRingBuffer - = sa::ring_new(3); + public fun test_oob_fail_ring_buffer(): FutureAccountingRingBuffer { + let mut buffer: FutureAccountingRingBuffer = sa::ring_new(3); sa::epoch(sa::ring_lookup_mut(&mut buffer, 3)); buffer } - } diff --git a/contracts/blob_store/sources/tests/storage_resource_tests.move b/contracts/blob_store/sources/tests/storage_resource_tests.move index f6c1fd5b..5a0b64cd 100644 --- a/contracts/blob_store/sources/tests/storage_resource_tests.move +++ b/contracts/blob_store/sources/tests/storage_resource_tests.move @@ -3,7 +3,6 @@ #[test_only] module blob_store::storage_resource_tests { - use blob_store::storage_resource::{ fuse, split_by_epoch, @@ -19,29 +18,37 @@ module blob_store::storage_resource_tests { }; #[test] - public fun test_split_epoch(){ + public fun test_split_epoch() { let ctx = &mut tx_context::dummy(); let storage_amount = 5_000_000; let mut storage = create_for_test(0, 10, storage_amount, ctx); let new_storage = split_by_epoch(&mut storage, 7, ctx); - assert!(start_epoch(&storage) == 0 && end_epoch(&storage) == 7 - && start_epoch(&new_storage) == 7 && end_epoch(&new_storage) == 10, 0); assert!( - storage_size(&storage) == storage_amount - && storage_size(&new_storage) == storage_amount, - 0 + start_epoch(&storage) == 0 && end_epoch(&storage) == 7 && + start_epoch(&new_storage) == 7 && + end_epoch(&new_storage) == 10, + 0, + ); + assert!( + storage_size(&storage) == storage_amount && + storage_size(&new_storage) == storage_amount, + 0, ); destroy(storage); destroy(new_storage); } #[test] - public fun test_split_size(){ + public fun test_split_size() { let ctx = &mut tx_context::dummy(); let mut storage = create_for_test(0, 10, 5_000_000, ctx); let new_storage = split_by_size(&mut storage, 1_000_000, ctx); - assert!(start_epoch(&storage) == 0 && end_epoch(&storage) == 10 - && start_epoch(&new_storage) == 0 && end_epoch(&new_storage) == 10, 0); + assert!( + start_epoch(&storage) == 0 && end_epoch(&storage) == 10 && + start_epoch(&new_storage) == 0 && + end_epoch(&new_storage) == 10, + 0, + ); assert!(storage_size(&storage) == 1_000_000 && storage_size(&new_storage) == 4_000_000, 0); destroy(storage); destroy(new_storage); @@ -49,7 +56,7 @@ module blob_store::storage_resource_tests { #[test] #[expected_failure(abort_code=EInvalidEpoch)] - public fun test_split_epoch_invalid_end(){ + public fun test_split_epoch_invalid_end() { let ctx = &mut tx_context::dummy(); let mut storage = create_for_test(0, 10, 5_000_000, ctx); let new_storage = split_by_epoch(&mut storage, 11, ctx); @@ -59,7 +66,7 @@ module blob_store::storage_resource_tests { #[test] #[expected_failure(abort_code=EInvalidEpoch)] - public fun test_split_epoch_invalid_start(){ + public fun test_split_epoch_invalid_start() { let ctx = &mut tx_context::dummy(); let mut storage = create_for_test(1, 10, 5_000_000, ctx); let new_storage = split_by_epoch(&mut storage, 0, ctx); @@ -68,7 +75,7 @@ module blob_store::storage_resource_tests { } #[test] - public fun test_fuse_size(){ + public fun test_fuse_size() { let ctx = &mut tx_context::dummy(); let mut first = create_for_test(0, 10, 1_000_000, ctx); let second = create_for_test(0, 10, 2_000_000, ctx); @@ -79,7 +86,7 @@ module blob_store::storage_resource_tests { } #[test] - public fun test_fuse_epochs(){ + public fun test_fuse_epochs() { let ctx = &mut tx_context::dummy(); let mut first = create_for_test(0, 5, 1_000_000, ctx); let second = create_for_test(5, 10, 1_000_000, ctx); @@ -98,7 +105,7 @@ module blob_store::storage_resource_tests { #[test] #[expected_failure(abort_code=EIncompatibleAmount)] - public fun test_fuse_incompatible_size(){ + public fun test_fuse_incompatible_size() { let ctx = &mut tx_context::dummy(); let mut first = create_for_test(0, 5, 1_000_000, ctx); let second = create_for_test(5, 10, 2_000_000, ctx); @@ -108,12 +115,11 @@ module blob_store::storage_resource_tests { #[test] #[expected_failure(abort_code=EIncompatibleEpochs)] - public fun test_fuse_incompatible_epochs(){ + public fun test_fuse_incompatible_epochs() { let ctx = &mut tx_context::dummy(); let mut first = create_for_test(0, 6, 1_000_000, ctx); let second = create_for_test(5, 10, 1_000_000, ctx); fuse(&mut first, second); destroy(first); } - }