diff --git a/Cargo.lock b/Cargo.lock index cf43fa0e8..12b2ab088 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12317,7 +12317,7 @@ checksum = "4e69bf016dc406eff7d53a7d3f7cf1c2e72c82b9088aac1118591e36dd2cd3e9" dependencies = [ "bitcoin_hashes 0.13.0", "rand", - "rand_core 0.6.4", + "rand_core 0.5.1", "serde", "unicode-normalization", ] @@ -12812,6 +12812,7 @@ dependencies = [ "bls12_381", "cid 0.11.1", "clap", + "hex", "jsonrpsee", "mater", "parity-scale-codec", @@ -14904,7 +14905,6 @@ dependencies = [ "sp-core 34.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", "sp-runtime 39.0.1", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", - "storage-proofs-core", ] [[package]] diff --git a/cli/polka-storage-provider/client/Cargo.toml b/cli/polka-storage-provider/client/Cargo.toml index fcde79f97..4fad6816f 100644 --- a/cli/polka-storage-provider/client/Cargo.toml +++ b/cli/polka-storage-provider/client/Cargo.toml @@ -21,6 +21,7 @@ bls12_381 = { workspace = true } cid = { workspace = true, features = ["std"] } clap = { workspace = true, features = ["derive"] } codec = { workspace = true } +hex = { workspace = true } jsonrpsee = { workspace = true, features = ["http-client", "macros", "server", "ws-client"] } sc-cli = { workspace = true } serde = { workspace = true } diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index ab8525176..f320f85f9 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -5,6 +5,7 @@ use std::{ str::FromStr, }; +use codec::Encode; use mater::CarV2Reader; use polka_storage_proofs::{ porep::{self, sealer::Sealer}, @@ -16,7 +17,11 @@ use primitives_commitment::{ piece::{PaddedPieceSize, PieceInfo}, Commitment, CommitmentError, }; -use primitives_proofs::{derive_prover_id, RegisteredPoStProof, RegisteredSealProof}; +use primitives_proofs::{ + derive_prover_id, + randomness::{draw_randomness, DomainSeparationTag}, + RegisteredPoStProof, RegisteredSealProof, SectorNumber, +}; use storagext::multipair::{MultiPairArgs, MultiPairSigner}; use subxt::tx::Signer; @@ -72,6 +77,15 @@ pub enum ProofsCommand { /// Directory where the proof files and the sector will be put. Defaults to the current directory. #[arg(short, long)] output_path: Option, + /// Sector number + #[arg(long)] + sector_id: u32, + /// The height at which we draw the randomness for deriving a sealed cid. + #[arg(long)] + seal_randomness_height: u64, + /// Precommit block number + #[arg(long)] + pre_commit_block_number: u64, }, /// Generates PoSt verifying key and proving parameters for zk-SNARK workflows (submit windowed PoSt) #[clap(name = "post-params")] @@ -203,17 +217,42 @@ impl ProofsCommand { commp, output_path, cache_directory, + sector_id, + seal_randomness_height, + pre_commit_block_number, } => { let Some(signer) = Option::::from(signer_key) else { return Err(UtilsCommandError::NoSigner)?; }; - let prover_id = derive_prover_id(signer.account_id()); - // Those are hardcoded for the showcase only. - // They should come from Storage Provider Node, precommits and other information. - let sector_id = 77.into(); - let ticket = [12u8; 32]; - let seed = [13u8; 32]; + let sector_number = SectorNumber::try_from(sector_id) + .map_err(|_| UtilsCommandError::InvalidSectorId)?; + + let entropy = signer.account_id().encode(); + println!("Entropy: {}", hex::encode(&entropy)); + + let ticket = get_randomness( + DomainSeparationTag::SealRandomness, + seal_randomness_height, + &entropy, + ); + println!( + "[{seal_randomness_height}] Ticket randomness: {}", + hex::encode(ticket) + ); + + // The number added is configured in runtime: + // https://github.com/eigerco/polka-storage/blob/18207759d7c6c175916d5bed70246d94a8f028f4/runtime/src/configs/mod.rs#L360 + let interactive_block_number = pre_commit_block_number + 10; + let seed = get_randomness( + DomainSeparationTag::InteractiveSealChallengeSeed, + interactive_block_number, + &entropy, + ); + println!( + "[{interactive_block_number}] Seed randomness: {}", + hex::encode(seed) + ); let output_path = if let Some(output_path) = output_path { output_path @@ -273,6 +312,9 @@ impl ProofsCommand { .create_sector(vec![(piece_file, piece_info)], unsealed_sector) .map_err(|e| UtilsCommandError::GeneratePoRepError(e))?; + let prover_id = derive_prover_id(signer.account_id()); + println!("Prover ID: {}", hex::encode(prover_id)); + println!("Precommitting..."); let precommit = sealer .precommit_sector( @@ -280,7 +322,7 @@ impl ProofsCommand { unsealed_sector_path, &sealed_sector_path, prover_id, - sector_id, + sector_number, ticket, &piece_infos, ) @@ -293,7 +335,7 @@ impl ProofsCommand { &cache_directory, &sealed_sector_path, prover_id, - sector_id, + sector_number, ticket, Some(seed), precommit, @@ -310,8 +352,10 @@ impl ProofsCommand { .clone() .try_into() .expect("converstion between rust-fil-proofs and polka-storage-proofs to work"); - proof_scale_file.write_all(&codec::Encode::encode(&proof_scale))?; + let scale_encoded_proof = codec::Encode::encode(&proof_scale); + proof_scale_file.write_all(&scale_encoded_proof)?; + println!("Proof as HEX: {}", hex::encode(scale_encoded_proof)); println!("Wrote proof to {}", proof_scale_filename.display()); } ProofsCommand::GeneratePoStParams { @@ -446,6 +490,8 @@ pub enum UtilsCommandError { InvalidPieceCommP(String, cid::Error), #[error("invalid piece type, error: {1}")] InvalidPieceType(String, CommitmentError), + #[error("invalid sector id")] + InvalidSectorId, #[error("file {0} is invalid CARv2 file {1}")] InvalidCARv2(PathBuf, mater::Error), #[error("no signer key was provider")] @@ -465,3 +511,13 @@ fn file_with_extension( .map_err(|e| UtilsCommandError::FileCreateError(new_path.clone(), e))?; Ok((new_path, file)) } + +fn get_randomness( + personalization: DomainSeparationTag, + block_number: u64, + entropy: &[u8], +) -> [u8; 32] { + // This randomness digest is hardcoded because it's always same on testnet. + let digest = [0u8; 32]; + draw_randomness(&digest, personalization, block_number, &entropy) +} diff --git a/lib/polka-storage-proofs/src/porep/sealer.rs b/lib/polka-storage-proofs/src/porep/sealer.rs index 790627125..21e35bdf7 100644 --- a/lib/polka-storage-proofs/src/porep/sealer.rs +++ b/lib/polka-storage-proofs/src/porep/sealer.rs @@ -217,7 +217,7 @@ impl Sealer { unsealed_sector, sealed_sector, prover_id, - sector_id.into(), + storage_proofs_core::sector::SectorId::from(u64::from(sector_id)), ticket, &piece_infos, )?; @@ -278,7 +278,7 @@ impl Sealer { cache_path, replica_path, prover_id, - sector_id.into(), + storage_proofs_core::sector::SectorId::from(u64::from(sector_id)), ticket, seed, pre_commit.into(), diff --git a/lib/polka-storage-proofs/src/post/mod.rs b/lib/polka-storage-proofs/src/post/mod.rs index e34eb2d2c..b58d3a67b 100644 --- a/lib/polka-storage-proofs/src/post/mod.rs +++ b/lib/polka-storage-proofs/src/post/mod.rs @@ -77,7 +77,7 @@ pub fn generate_window_post>( let mut replicas = BTreeMap::new(); for replica in partition_replicas { replicas.insert( - replica.sector_id.into(), + storage_proofs_core::sector::SectorId::from(u64::from(replica.sector_id)), PrivateReplicaInfo::::new( replica.replica_path, replica.comm_r, diff --git a/maat/generate_porep_proof.sh b/maat/generate_porep_proof.sh new file mode 100755 index 000000000..8d20c50f8 --- /dev/null +++ b/maat/generate_porep_proof.sh @@ -0,0 +1,22 @@ +# Clear cache before run +CACHE_FOLDER="/tmp/psp-cache" +rm -r "$CACHE_FOLDER" +mkdir "$CACHE_FOLDER" + +PROVIDER="//Charlie" +CAR_FILE="../examples/test-data-big.car" +SECTOR_CID="baga6ea4seaqbfhdvmk5qygevit25ztjwl7voyikb5k2fqcl2lsuefhaqtukuiii" +PARAMS_PATH="../2KiB.porep.params" +SECTOR_ID=1 +SEAL_RANDOMNESS_HEIGHT=20 +PRE_COMMIT_BLOCK_NUMBER=30 + +polka-storage-provider-client proofs porep \ +--sr25519-key "$PROVIDER" \ +--proof-parameters-path "$PARAMS_PATH" \ +--cache-directory "$CACHE_FOLDER" \ +--sector-id "$SECTOR_ID" \ +--seal-randomness-height "$SEAL_RANDOMNESS_HEIGHT" \ +--pre-commit-block-number "$PRE_COMMIT_BLOCK_NUMBER" \ +"$CAR_FILE" \ +"$SECTOR_CID" diff --git a/maat/tests/real_world.rs b/maat/tests/real_world.rs index 3073e207c..360a253bd 100644 --- a/maat/tests/real_world.rs +++ b/maat/tests/real_world.rs @@ -221,15 +221,10 @@ where // the randomness for itself, when checking the proof in later stage. let seal_randomness_height = 20; - // The randomness received at used to derive the - // sealed cid: [162, 13, 84, 200, 249, 99, 34, 176, 119, 98, 24, 201, 104, - // 246, 249, 160, 8, 202, 132, 1, 205, 231, 49, 145, 195, 28, 231, 104, 45, - // 13, 151, 107] - // The unsealed_cid, randomness above and some other things were used to // generate this sealed_cid. let sealed_cid = - Cid::try_from("bagboea4b5abcah2xpzls5hsuyngd5onktkgxfdsltfgroxk722oi72ipbo6jiwig").unwrap(); + Cid::try_from("bagboea4b5abcaqolcsygu5o756srf7l4pzzagml5r3wa3o6ahoo5vixummsev6rf").unwrap(); let sectors_pre_commit_info = vec![SectorPreCommitInfo { seal_proof: primitives_proofs::RegisteredSealProof::StackedDRG2KiBV1P1, @@ -280,15 +275,16 @@ async fn prove_commit_sector( // // Proofs were generated by using `polka-storage-provider-client proofs porep` command. let proof = match pre_commit_block_number { - 21 => "ac7b0de077236494913f6c5a0af70b1a5ee7d2c423be0a3555a964a9afde64ce712b23f8ed2d3a6e30e9a0323a9e3c5a9171ec98fcc2cd0ab8110e0e1aa764b627015342eb252d90d2b05816a6e5456876d71fb7e8fb066cceb8bbab8fb1fb1c17c42bf359939a3064a28d78c0258f3b2b7cb6813ab7adaf259d2b0a48c8a5b29b8417f8bea3e56ab14d4999e077f22fa9536d9311ca05ab786de590685effc313be15f52438cdb09fe96ba9917c1c201914edd17b1c9c20f618fa872c7f5845", - 22 => "a329f2b90dfd87d1d5e9c30e1cfd298491d0334c2307259d4b4b4a261246ae1f46c0120b1af1201415e43facb1c0943d9087a80922b1362f045b715d089678f54c76deee4d12c71935b6ee01ef04c770175f6471e81296793941ed75d7c60b92005cc877c255d3c0c78d530b42e04b1e1b3349a5a912bffeca9a06d229077fb8f6c08e4b3947e5d8f370b35d8bfbeb65b2da6ecdadc5b0addf6186232f5f559f6cf1fa9f93813da4360a9b1554e13b49ed58a4ea91f8399701e94ebc26bfea12", - 23 => "985a6b3cd7564ad2a80f086aa2d8f332654c41773571babf24e66584ecfe2a76d5100146c4808767fbb9b50d51de962fa1db255430fb9faa5d0011c5b8b7ea0d5904fc6f75c8cb20efb266d2af867f02a30a78bc2f026f956f7ad71ba25c9e3414aa566f98749e7c9424b8b250d63b33a29972ec6eea1d22cf0e9d2662905c4042f236cc741f07b7611392d17d3cfd0fa0202cc9fcd3137433e66ad73649ee0bfb1b97ffdb1bd5ab66704ac30ab7ea769eb3e8be12401724f9ee5f740cbcd0e8", - 24 => "b0f777807d02c16794e6dc8ca2a27aa97aa88f7590bee9c6f8f5da388ed4a7d8adbd2f5c0cfbfeb23aaf8e4b9de47b65a9ac7c52a89c97d280afcda2fc4459a2f87d74501d6008bc87a3f40f760811edeb48d29fc4113867bea569ae5b1485e9112e77a74fec5789542fbaea69fec79152ae6b7dbcf4520ef98df426afa11c5afda21b09dc00f7153fdc5760fcd2c52586eea468ff43a6ceeeb64cc62f15371bbdaee7b7c72ac6277587b3a32638ffae19b9c7af9a80299dbe956e520119be8a", - 25 => "87c8128bc80baf631102b8f4b60b98fd80d2ea1b6804c2c399ee313aa709147663c8a51ad5c2d7c80005272792e1e223a96d8675ccfe8b5ed1097e32a77bef8f0c1b6b61a5fa77ef9faebeb566561267273a7a4b9f73634b968ca1315b85dc630880a04eae5cc20482752f20ee8d388b429e31adc4c519547fef6d2eff88aad9d6e8a1110e6c7462cc5ab9cbc84f3d4ba9654e9e34bf83960dd3cb73684ca42db1471f643aa38e3b98d3a587b523139b4b9ba1b22004af6a443226c440d8255f", - 26 => "8558c970b0ae521c8af16b76f8ac85aa484670cbada153fffc7341515946b19083d8bb7fa03cbc63031c70770511a170abc5f0f1b3083e82108271e3d2b63d8751997696804893caddd1fbfd5f4d5fbf66f0f8df34b18b1dcb0787d70efddde713b9b331fc669d775b7a9ccd07b4fbd9418af78368989fcd503446ca47cb35631cdc4f64ca56d9715cd918d74b4ca01f81b1ea2b4868cc7f7736afd314b8eaeb6f04d9dd996fc181210ece0f3c34ea8588eb1f53f734345309bb800fdc31256d", - 27 => "8d978855236bdaf82c44ba1f0b8ec6451c8c8beda6324f7ff85e15b86be8bdfcb5790d5a8902114f187f9e8c59afe0f891a01fea8b25c583f1f4759da3240982565d99a5d63297d6a084e29b26333577e0a1d37d6a817ca5ae0ab5a18629057115b9c5601db47d262fb97946488119e527099dcfda0c0e598dc03d1875ce2cefc96260805627670605f66c461f66b04688ce66bb0dab8eec4f123fc0c50340eb4c48dbfbd70a82db65661541c8273dc39ddebdc64a5c59e708fcbb870a5711b4", - 28 => "90c459ab1cb5300faa8bf75653e455a04e8dea8e5d320e33b0e338272a4800d97995ea1985a3e21f791c15158de38b5499506bc5d4edccb0164b878842ac31fb6e412709f31d77a7bb61227c13d648326cfed86ca5c468bb0d9033a9e3c48fb5082cdb36a5e63f94341ed3b855846b1ca5970334847911b7fcc9ca7dcba4596a060854e7fb8c5dcd3d7abf8e480333658b87641e13444ac740dfd7724f5696790faee4c7c9fe2cd1568fb5d6386bc70e82078030d07857ab80086b494f8aa6cc", - 29 => "b5dd7ae4c6a3bcf093afed88a3a23ea1783f9ee85cbd93d24d725580017c7f502e75bcca06ad866e576b3f7fd312ff5bb24909a79f99f416873b2c154eda37646229e5062e5fcba4f996a5d86ef36064cfd76f2cd7c09a6d67541852fe2b6f351620f18bf43c59d6b4c34a6a6c62103383bfb6fe374773cd64eea21980fc33b7cd29ab521902f0cf822240622936a341afc85c0928e6b035f0b44f470d87e8974bc86356abf4326ee2d0914a2633c53fcb3ab84f439a4615f4621c883d974a72", + 21 => "a0fc39bb0ac6986d56126fb445a7fa38cca95969db1320caf7bb7c0ad7f9d11f02050c9157669ceb95d44015d7da741aa32f2456eb312d76b863652c6f16a7c3805bb5a25368c59ec4b257394936113e3b93ff2b67211819d0452363b4d37f0416c6b08cadfb22edb20eea6898829631da5523c1fb98804dc5645e9b4c75dc6152f0019517863040463fe351de9c630c8367cc5b9ce8257dd7f6c784152a29199620d697b8fabae05463a6b70ce4bbdc1b16a8d8805951a4cc1ea36c14ba406f", + 22 => "9250a154bc2d75b4c7349f5a6f3da85d5bae71ced091953cf195a8305808e8b87c81d8598dc4b8f1fc9f51a74020267882dd8a0c56aa70f8323fbc1689b09172c2cb7e1782c39fdfcba66a8498b945d33d432e461da06cb23960d5757f8638c3090e97a69e4d66121ef32eeb104d76386ece0bb492258e83e503c5e648c21895d50e458467aebf50c7a9a504ff0d84adb1935a72b82dcadb9826c1f82f5f04094608b2983b48663139697c5583166a02b513609e5755c6613e53ee41f1e45210", + 23 => "a7af1f176e978b97760e6fd703505d885cba6b7dfd60a1f03aecdb52ca3ead6f9b576f8e4b0d4e1bad46a5ee36da3106abd16d6c121d5bfe5bb1a3bbad9f06bfdbcac85caa866179986c81383e9947243a91e83c897d21b4fb0f0c90a5a6dacb04c83ae4bd57595a8b052208e44fadbac7670e17eb470cef0724f838e1c2617f0f0333877398193510bb132520bbe64e964df36bb6e7c2d7e2bd688ad4033d9d010e74be448c1a4b26e83dbf2d3a1235e954e1d9ff9645b8f7948112c960521c", + 24 => "924a59f396c18c26641266366f54ca708c82913b1c91d72285627da22e06c1948aef7895e5d4128d442b20d0e43c91aca40ac8ce0af73f58b3de13a691af1036ed4b477925bad88983cd6e3f685d1d0c43262a3927d3c47380b712a2a1523a4909156fdfee9c3f7e6100cfab9a12936634c09001e5a738975b3109209e3c17d2f0af0d147093ca43481940ec5112431fa84eb003b4d812748cba47bf6632a783c98d684ca297eb3ca67a854f474a12118db57356d319ec2d4be6884f82f2b924", + 25 => "a86c1a73703450e67fef89c5df975ebd79add9a5ecad76f00f3836dea3cd779a3b2206d75ead4044d9d873e470079a408b3bdefdb86a7fc69c57d76f2ef7a4ffb235ca0451d2b5521419bf7cda32916bc10b49c7c0d5d22ade8d33824ec19662112b38bc6dad836a96c5e15ceb7012b920778f984b0425fc442375bcd4c1edcf6def3090b052ece59396763f1e2410b8981c729a63eaa303165ab6adf3fd46cc866790116a5121c71d4c2f6a95e3f94ec4a0229f3fb3aa8667bebfa8579c1a4f", + 26 => "84ee71add7f7fe6f8e0e8da5730400e50e58025cb96fa1e62afca58d46d29c0da7315a6f21c46b05210bd0cf83f6310e9775f3ed2b8586c96c9438dfb801b141b6decb1be026874ebdd24b1ba68ed434f0e89ef654297135d8a27f5eabaab2a911d7015ddd33d8f55aa11e77ddb81afe83a0f06b8421bdc1de659f5c73e033083499a021b858b3725afe967dfd40a7a6a04c6e44112d15d2b8e7a2048c8cdbbcb5d60f6116a0263a5aa7cf9fa67ff736609379973ecbc5300f1b30bc527eaba8", + 27 => "8219438b09845020fc56367326c63e66c1b720c740ff544ac8a3f9d0f726056d443f95fbaf9b9ea70da81ddddc946ad4a28cd8b7653d827a823c0b55932e78318ff5e64c92b00723bd402a83071ebae4ffcca785e0b3846895585ac6be50b75d0470b5783caf883a58a8db37bea3d79a3f8860d9b48010ec79db1a8e0ff7628e09a6fa29cf1ab570f470bcf71db62f579413b5e630a34215d5cf7e2d309fdddd911f1f8f658282439ea764f641d8120aaf0c5c97577f7fead1dc2f9029563aa5", + 28 => "9536835a80b7b6c0f94437a3f89e53034c2fbe778bebdb9fca0bb2a39a72164547da62737874c140ccc26eddf4ae691a8a686228c555eb2a836213518fc94797e9c0187bda5099754283285a3966d99a5a8bf253fe0a9b503c49fbf6ac3d8947143068e8dcde7f945b20c33bac57cddfb2b8305e1e78208db11699860328cb15f44e8c5ebca8f68e3c6a46d45f09f24799d8292a132d6566b2c71abd716c3805171cf8d8a3e670c5b295f8fd37080e2b9fde077daefa3d502ffacdafeeab42c8", + 29 => "8949e95f5d9e4145911ce1d09dda93d640e7677970a008af46f79eef4ff678cdf433aa8bb0675bb1d09b200648b46b9c816bd67cd7be6d9492a5502583e6053f87c2a495583146e81f4564ea730474a1555dd7ab5c0969352514817b7a04fd02079e67233bb1b3735997ccf037f73c7cb6635a027c89d1a6618e1fb79759cfe2fbdc42feb6c0b8def03a3e601362bf97a69c7e120b6e7d4ab11eb064bb94f9364edfae01fcc25ffdcde2b059fc1ced270be3954505557a70b3b02c46f1c030ca", + 30 => "9412416b5fb1bccc10df03ac866245ae9689eae2d7859193a4b423780f1e7ac0c0fa4fa523ce85330229b13d5d691538976678f716579cda00bdab1e127134c5ab86afd73080dda4eff06bbf97cc59a8ca2185a123fe097e0488ae1dc37ada26079442de0044295f77b252a5fad0588d95157cf35055540b5980830b214038628a8f815b0777c9b848703693890fd393a7bc740d76aca92a0306587bf796399f39bd0061b9dc7044973e7aabc4fc46c4fb5525e9188040027df26554c15e2530", _ => { panic!("Proof not generated for {pre_commit_block_number}"); } diff --git a/primitives/proofs/Cargo.toml b/primitives/proofs/Cargo.toml index 6102661c8..df9e1f702 100644 --- a/primitives/proofs/Cargo.toml +++ b/primitives/proofs/Cargo.toml @@ -20,7 +20,6 @@ sp-std = { workspace = true, default-features = false } clap = { workspace = true, features = ["derive"], optional = true } serde = { workspace = true, features = ["derive"], optional = true } -storage-proofs-core = { optional = true, workspace = true } [dev-dependencies] serde_json = { workspace = true, default-features = true } @@ -31,4 +30,4 @@ workspace = true [features] clap = ["dep:clap", "std"] default = ["std"] -std = ["cid/scale-codec", "codec/std", "dep:storage-proofs-core", "scale-info/std", "sp-core/std", "sp-runtime/std", "sp-std/std"] +std = ["cid/scale-codec", "codec/std", "scale-info/std", "sp-core/std", "sp-runtime/std", "sp-std/std"] diff --git a/primitives/proofs/src/types.rs b/primitives/proofs/src/types.rs index 8ebcd003d..8099825fa 100644 --- a/primitives/proofs/src/types.rs +++ b/primitives/proofs/src/types.rs @@ -155,13 +155,6 @@ impl Display for SectorNumber { } } -#[cfg(feature = "std")] -impl From for storage_proofs_core::sector::SectorId { - fn from(sector_number: SectorNumber) -> Self { - Self::from(sector_number.0 as u64) - } -} - /// Byte representation of the entity that was signing the proof. /// It must match the ProverId used for Proving. pub type ProverId = [u8; 32]; diff --git a/runtime/src/configs/mod.rs b/runtime/src/configs/mod.rs index 55694e3c4..e881700ab 100644 --- a/runtime/src/configs/mod.rs +++ b/runtime/src/configs/mod.rs @@ -446,20 +446,27 @@ impl pallet_randomness::Config for Runtime { #[cfg(feature = "testnet")] mod randomness_source_testnet { + use codec::Decode; use frame_support::traits::Randomness; use frame_system::{pallet_prelude::BlockNumberFor, Config, Pallet}; - use sp_runtime::traits::Hash; + use sp_runtime::traits::TrailingZeroInput; use sp_std::marker::PhantomData; - /// Randomness source that always returns same random value. + /// Randomness source that always returns same random value based on the + /// subject used. + /// /// ! USE THIS ONLY IN TESTNET ! pub struct PredictableRandomnessSource(PhantomData); - impl Randomness> for PredictableRandomnessSource { - fn random(_: &[u8]) -> (T::Hash, BlockNumberFor) { + impl Randomness> for PredictableRandomnessSource + where + Output: Decode + Default, + T: Config, + { + fn random(subject: &[u8]) -> (Output, BlockNumberFor) { ( - T::Hashing::hash(&[]), + Output::decode(&mut TrailingZeroInput::new(subject)).unwrap_or_default(), // This means the the randomness can be used immediately. - >::block_number(), + Pallet::::block_number(), ) } }