ERC20 Event Listener in Rust Programming - rust

I am trying to program an erc20 event listener using Rust programming language and trying to get events from a particular block to the latest block. But even though I have specified from the block it just gives an output from the latest block, not from the specified block. Below is my code:
use hex_literal::hex;
use std::time;
use web3::{
contract::{Contract, Options},
futures::{future, StreamExt},
types::{FilterBuilder, Address},
};
use std::str::FromStr;
use web3::types::{BlockNumber, U64};
#[tokio::main]
async fn main() -> web3::contract::Result<()> {
let web3 = web3::Web3::new(web3::transports::WebSocket::new("wss:").await?);
let filter = FilterBuilder::default()
// this is BSC: TokenHub
.from_block(BlockNumber::Number(U64::from(15255440)))
.address(vec![Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()])
/* .topics(
// this is 'Transfer (index_topic_1 address from, index_topic_2 address to, uint256 value)' event
// use https://emn178.github.io/online-tools/keccak_256.html, and type in 'Transfer(address,address,uint256)'
// it will return result hash as used in next line
Some(vec![hex!("d282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e").into()]),
None,
None,
None,
)*/
.build();
let sub = web3.eth_subscribe().subscribe_logs(filter).await?;
sub.for_each(|log| {
println!("{:?}", log);
future::ready(())
}).await;
Ok(())
}
Output is like:
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000007933790a4f00000099e9001629d9fe7775b800, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 65, 133, 173, 92, 182, 64, 0]),
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xdd603b88c60e5b5a1aaebedb53d38df626b72d6ec2a11530cf50a8957082f404),
transaction_index: Some(0),
log_index: Some(0),
transaction_log_index: None,
log_type: None,
removed: Some(false) })
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x0000000000000000000000002dfa8a0413255ecca71
3b6d1c1e28e634e021478, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 76,81, 115, 63, 131, 0, 0]),
block_hash:Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xcf4b91c9c465e8c2b22126ad580e92988162bf51be7520e5cc6dfe9f878faa06),
transaction_index: Some(1),
log_index: Some(4),
transaction_log_index: None,
log_type: None,
removed: Some(false) })
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2,
topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34, 0x000000000000000000000000007933790a4f00000099e9001629d9fe7775b800],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 170, 83, 197, 89, 150, 0, 0]),
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xd45f5b511cf832b407182751c451c32b27a203d8ab60136f9719cf61523b0e9a),
transaction_index: Some(2),
log_index: Some(9),
transaction_log_index: None,
log_type: None, removed: Some(false) })
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2,
topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000000000000035b5e5ad9019092c665357240f594e, 0x0000000000000000000000002f8ac927aa94293461c75406e90ec0ccfb2748d9],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 90,
119, 187, 148, 242, 21, 156]),
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xd36df58ffe146202ffb9ef2e29e64380991a674c820bb8a1f6a776b9468baa86),
transaction_index: Some(3),
log_index: Some(12),
transaction_log_index: None,
log_type: None,
removed: Some(false) })

I got to the conclusion that the above code gives the output of the event that will happen. So when we add from_block: 15255440, the event has already happened. And in that particular block, no other event is going to happen. So you will get the output from the latest block.
To test this in code add this:
let filter = FilterBuilder::default()
// this is BSC: TokenHub
.from_block(BlockNumber::Number(U64::from(15257125)))
.to_block(BlockNumber::Number(U64::from(15257130)))
.address(vec![Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()])
.build();
Keep in mind to replace 15257125(i.e from_block) and 15257130(i.e. to_block) with the future block which has not been mined yet. And when that block will be mined you will get events between that two blocks.

Related

Understanding the config file of paraphrase mpnet base v2?

Here is the config file of the paraphrase mpnet transformer model and I would like to understand the meaning with examples of the hidden_size and num_hidden_layers parameters.
{
"_name_or_path": "old_models/paraphrase-mpnet-base-v2/0_Transformer",
"architectures": [
"MPNetModel"
],
"attention_probs_dropout_prob": 0.1,
"bos_token_id": 0,
"eos_token_id": 2,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-05,
"max_position_embeddings": 514,
"model_type": "mpnet",
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 1,
"relative_attention_num_buckets": 32,
"transformers_version": "4.7.0",
"vocab_size": 30527
}

When training jukebox on notebooks, why is our training data is stuck at 0 for Sample length and min duration?

Hps data:
/home/dotty/anaconda3/envs/JukVersionRoy/jukebox/train.py --hps=small_vqvae --name=small_vqvae --sample_length=9999999999999 --bs=2 --audio_files_dir=/home/dotty/anaconda3/envs/JukVersionRoy/jukebox/my-training-data --labels=False --train --aug_shift --aug_blend
Here
/
HPS ***************
{'rcall_command': '<unknown_rcall_command>', 'git_commit': '<unknown_git_commit>', 'name': '', 'debug_mem': False, 'debug_eval_files': False, 'debug_speed': False, 'debug_iters': 100, 'debug_batch': False, 'debug_grad_accum': False, 'debug_inputs': False, 'local_path': '', 'local_logdir': 'logs', 'max_len': 24, 'max_log': 32, 'save': True, 'save_iters': 20000, 'seed': 0, 'prior': False, 'log_steps': 100, 'func': '', 'audio_files_dir': '', 'finetune': '', 'english_only': False, 'bs': 1, 'bs_sample': 1, 'nworkers': 1, 'aug_shift': False, 'aug_blend': False, 'train_test_split': 0.9, 'train_shrink_factor': 1.0, 'test_shrink_factor': 1.0, 'p_unk': 0.1, 'min_duration': None, 'max_duration': None, 'n_tokens': 0, 'n_vocab': 0, 'use_tokens': False, 'curr_epoch': -1, 'restore_vqvae': '', 'levels': 2, 'downs_t': (1, 1), 'strides_t': (2, 2), 'hvqvae_multipliers': None, 'revival_threshold': 1.0, 'emb_width': 64, 'l_bins': 512, 'l_mu': 0.99, 'commit': 1.0, 'spectral': 0.0, 'multispectral': 1.0, 'loss_fn': 'l2', 'linf_k': 2048, 'lmix_l1': 0.0, 'lmix_l2': 0.0, 'lmix_linf': 0.0, 'use_bottleneck': True, 'depth': 3, 'width': 128, 'm_conv': 1.0, 'dilation_growth_rate': 1, 'dilation_cycle': None, 'vqvae_reverse_decoder_dilation': True, 'restore_prior': '', 'restore_prior_ddp': False, 'max_bow_genre_size': None, 'y_bins': 0, 'level': 0, 'cond_levels': None, 't_bins': 64, 'y_cond_as_bias': False, 'copy_input': False, 'merged_decoder': False, 'single_enc_dec': False, 'alignment_layer': None, 'alignment_head': None, 'n_ctx': 1024, 'prior_depth': 3, 'prior_width': 128, 'heads': 1, 'attn_order': 0, 'blocks': None, 'spread': None, 'attn_dropout': 0.0, 'resid_dropout': 0.0, 'emb_dropout': 0.0, 'zero_out': False, 'res_scale': False, 'pos_init': False, 'init_scale': 1.0, 'm_attn': 0.25, 'm_mlp': 1.0, 'c_res': 0, 'c_attn': 0, 'c_mlp': 0, 'cond_depth': 3, 'cond_width': 128, 'cond_m_conv': 1.0, 'cond_zero_out': False, 'cond_res_scale': False, 'cond_dilation_growth_rate': 1, 'cond_dilation_cycle': None, 'cond_c_res': 0, 'primed_chunk_size': None, 'selected_artists': '', 'temp_top': 1.0, 'temp_rest': 0.99, 'sample_length_in_seconds': 24, 'total_sample_length_in_seconds': 240, 'prime_loss_fraction': 0.1, 'restore_decoder': '', 'prime_depth': 3, 'prime_width': 128, 'prime_heads': 1, 'prime_attn_order': 0, 'prime_blocks': None, 'prime_spread': None, 'prime_attn_dropout': 0.0, 'prime_resid_dropout': 0.0, 'prime_emb_dropout': 0.0, 'prime_zero_out': False, 'prime_res_scale': False, 'prime_pos_init': False, 'prime_init_scale': 1.0, 'prime_m_attn': 0.25, 'prime_m_mlp': 1.0, 'prime_c_res': 0, 'prime_c_attn': 0, 'prime_c_mlp': 0, 'prime_rel_attn': False, 'prime_posemb_timescale': 10000, 'epochs': 10000, 'lr': 0.0003, 'clip': 1.0, 'beta1': 0.9, 'beta2': 0.999, 'ignore_grad_norm': 0, 'weight_decay': 0.0, 'eps': 1e-08, 'lr_warmup': 100.0, 'lr_decay': 10000000000.0, 'lr_gamma': 1.0, 'lr_scale': 1.0, 'lr_use_linear_decay': False, 'lr_start_linear_decay': 0, 'lr_use_cosine_decay': False, 'fp16': False, 'fp16_params': False, 'fp16_loss_scale': None, 'fp16_scale_window': 1000.0, 'fp16_opt': False, 'labels': True, 'labels_v3': False, 'dump': False, 'ema': True, 'ema_fused': True, 'cpu_ema': False, 'cpu_ema_freq': 100, 'reset_best_loss': False, 'reset_step': False, 'reset_opt': False, 'reset_shd': False, 'train': False, 'test': False, 'sample': False, 'sampler': 'ancestral', 'codes_logdir': '', 'date': None, 'labeller': 'top_genres', 'label_line': 0, 'iters_before_update': 1, 'grad_accum_iters': 0, 'mu': None, 'piped': False, 'pipe_depth': 8, 'break_train': 10000000000.0, 'break_test': 10000000000.0, 'exit_train': 10000000000.0, 'n_fft': 1024, 'hop_length': 256, 'window_size': 1024, 'sr': 44100, 'channels': 2, 'wav': '', 'n_inps': 1, 'n_hops': 2, 'n_segment': 1, 'n_total_segment': 1, 'n_segment_each': 1, 'prime_chunks': 4, 'sample_length': 0, 'sample_hop_length': 30000, 'max_silence_pad_length': 0, 'ignore_boundaries': False, 'use_nonrelative_specloss': True, 'multispec_loss_n_fft': (2048, 1024, 512), 'multispec_loss_hop_length': (240, 120, 50), 'multispec_loss_window_size': (1200, 600, 240), 'bucket': 128, 'ngpus': 16, 'argv': '/home/dotty/anaconda3/envs/JukVersionRoy/jukebox/train.py --hps=small_vqvae --name=small_vqvae --sample_length=9999999999999 --bs=2 --audio_files_dir=/home/dotty/anaconda3/envs/JukVersionRoy/jukebox/my-training-data --labels=False --train --aug_shift --aug_blend'}
SAMPLE LENGTH:
0
SAMPLE RATE:
44100
SAMPLE LENGTH / SAMPLE RATE:
0.0
MIN DURATION:
0
AssertionError Traceback (most recent call last)
/tmp/ipykernel_14312/1841762803.py in <module>
325
326
--> 327 run()
/tmp/ipykernel_14312/1841762803.py in run(hps, port, **kwargs)
283 print("HPS ***************")
284 print(hps)
--> 285 data_processor = DataProcessor(hps)
286
287 # Setup models
~/anaconda3/envs/JukVersionRoy/jukebox/data/data_processor.py in init(self, hps)
24 class DataProcessor():
25 def init(self, hps):
---> 26 self.dataset = FilesAudioDataset(hps)
27 duration = 1 if hps.prior else 600
28 hps.bandwidth = calculate_bandwidth(self.dataset, hps, duration=duration)
~/anaconda3/envs/JukVersionRoy/jukebox/data/files_dataset.py in init(self, hps)
24 print("MIN DURATION:")
25 print(self.min_duration)
---> 26 assert hps.sample_length / hps.sr < self.min_duration, f'Sample length {hps.sample_length} per sr {hps.sr} ({hps.sample_length / hps.sr:.2f}) should be shorter than min duration {self.min_duration}'
27 self.aug_shift = hps.aug_shift
28 self.labels = hps.labels
AssertionError: Sample length 0 per sr 44100 (0.00) should be shorter than min duration 0
Our files are not being read in, as the sample stayed at 44100 when we changed the files being read in. we went through all the files dataset.py, dataprocessor.py and audio_utils.py and could not finds where exactly the input files is being registered. Our path is correct. We are trying to sample length and min duration to be read in correctly.

Shared memory `shmctl`: Permissions denied

I am trying to use shared memory in a process without using sudo.
My minimal example:
fn main() {
const KEY:i32 = 453845;
const SIZE:usize = 1024*1024;
unsafe {
let shmid = libc::shmget(KEY, SIZE, libc::IPC_CREAT);
dbg!(shmid);
dbg!(*libc::__errno_location());
let mut description: MaybeUninit<libc::shmid_ds> = MaybeUninit::uninit();
let result = libc::shmctl(KEY, libc::IPC_STAT, description.as_mut_ptr());
dbg!(description.assume_init_read());
dbg!(result);
dbg!(*libc::__errno_location());
}
}
produces the output:
[shared-memory-allocator/src/lib.rs:561] shmid = 1015817
[shared-memory-allocator/src/lib.rs:565] description.assume_init_read() = shmid_ds {
shm_perm: ipc_perm {
__key: 0,
uid: 0,
gid: 0,
cuid: 0,
cgid: 0,
mode: 0,
__pad1: 0,
__seq: 0,
__pad2: 0,
__unused1: 0,
__unused2: 0,
},
shm_segsz: 0,
shm_atime: 0,
shm_dtime: 0,
shm_ctime: 0,
shm_cpid: 0,
shm_lpid: 0,
shm_nattch: 0,
__unused4: 0,
__unused5: 0,
}
[shared-memory-allocator/src/lib.rs:566] result = -1
[shared-memory-allocator/src/lib.rs:567] *libc::__errno_location() = 22
Which is to note that it errors with error code 22 e.g. Permission Denied.
This seems like a simple use case, but checking https://linux.die.net/man/2/shmctl and https://linux.die.net/man/2/shmget has no description of permissions. Looking at https://www.ibm.com/docs/en/zos/2.1.0?topic=functions-shmget-get-shared-memory-segment it does note you can pass permission bits.
But if I change it to
let permissions = (libc::S_IRGRP | libc::S_IROTH | libc::S_IRUSR | libc::S_IWGRP | libc::S_IWOTH | libc::S_IWUSR) as i32;
let shmid = libc::shmget(KEY, SIZE, libc::IPC_CREAT | permissions);
The result is shmget now fails:
[shared-memory-allocator/src/lib.rs:562] shmid = -1
[shared-memory-allocator/src/lib.rs:563] *libc::__errno_location() = 13

Unrecognized configuration class <class 'transformers.models.bert.configuration_bert.BertConfig'> for this kind of AutoModel: AutoModelForSeq2SeqLM

Model type should be one of BartConfig, PLBartConfig, BigBirdPegasusConfig, M2M100Config, LEDConfig, BlenderbotSmallConfig, MT5Config, T5Config, PegasusConfig, MarianConfig, MBartConfig, BartConfig, BlenderbotConfig, FSMTConfig, XLMProphetNetConfig, ProphetNetConfig, EncoderDecoderConfig.
I am trying to load a fine-tuned Bert model for machine translation using AutoModelForSeq2SeqLM but it can't recognize the configuration class.
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer
model = AutoModelForSeq2SeqLM.from_pretrained('/content/drive/MyDrive/Models/CSE498')
Config File
{
"_name_or_path": "ckiplab/albert-tiny-chinese",
"architectures": [
"BertForMaskedLM"
],
"attention_probs_dropout_prob": 0.0,
"bos_token_id": 101,
"classifier_dropout": null,
"classifier_dropout_prob": 0.1,
"down_scale_factor": 1,
"embedding_size": 128,
"eos_token_id": 102,
"gap_size": 0,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.0,
"hidden_size": 312,
"initializer_range": 0.02,
"inner_group_num": 1,
"intermediate_size": 1248,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "bert",
"net_structure_type": 0,
"num_attention_heads": 12,
"num_hidden_groups": 1,
"num_hidden_layers": 4,
"num_memory_blocks": 0,
"pad_token_id": 0,
"position_embedding_type": "absolute",
"tokenizer_class": "BertTokenizerFast",
"torch_dtype": "float32",
"transformers_version": "4.18.0",
"type_vocab_size": 2,
"use_cache": true,
"vocab_size": 30522
}
This is because BERT itself is not a seq2seq model. You can consider using a pre-trained BART instead.

Yubikey 5 NFC: get a "packed" attestation statement

With the following javascript request:
navigator.credentials.create({
publicKey: {
// random, cryptographically secure, at least 16 bytes
challenge: new Uint8Array(16),
// relying party
rp: {
id: 'localhost',
name: 'My website'
},
user: {
id: new Uint8Array(16),
name: 'Tang',
displayName: 'Tang'
},
pubKeyCredParams: [
{
type: "public-key", alg: -7
}
],
attestation: "direct"
}
})
a FIDO2-compatible Yubikey 5 NFC systematically returns a "fido-u2f" attestation statement:
%{
"attStmt" => %{
"sig" => <<48, 69, 2, 33, 0, 132, 31, 225, 91, 58, 61, 190, 47, 66, 168, 8,
177, 18, 136, 106, 100, 219, 54, 52, 255, 103, 106, 156, 230, 141, 240,
82, 130, 167, 204, 128, 100, 2, 32, 61, 159, 126, 9, 244, 55, 100, 123,
169, ...>>,
"x5c" => [
<<48, 130, 2, 188, 48, 130, 1, 164, 160, 3, 2, 1, 2, 2, 4, 3, 173, 240,
18, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 11, 5, 0, 48, 46, 49,
44, 48, 42, 6, 3, 85, 4, 3, 19, ...>>
]
},
"authData" => <<73, 150, 13, 229, 136, 14, 140, 104, 116, 52, 23, 15, 100,
118, 96, 91, 143, 228, 174, 185, 162, 134, 50, 199, 153, 92, 243, 186, 131,
29, 151, 99, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...>>,
"fmt" => "fido-u2f"
}
How to receive a FIDO2 "packed" attestation statement instead?
Per the current spec/standard I don't think you (acting as a Relying Party) can "select" which attestation statement format you receive from the Authenticator (i.e. "the device"). That is a decision made by the Authenticator.
I think MacBook Pro TouchID platform authenticator via Chrome Desktop is sending "packed" attestation statements, if that helps.
There is no way to select the attestation using such simple keys. To test my implementation for both attestations I simply bought two different keys one from Yibico and one from Nitrokey. Yubico sends fido-u2f, while the Nitrokey sends packed attestations.
And if someone likes to know, this is how I implemented that:
let verifyAuthenticatorAttestationResponse = (webAuthnResponse) => {
let attestationBuffer =
base64url.toBuffer(webAuthnResponse.response.attestationObject);
let ctapMakeCredResp = cbor.decodeAllSync(attestationBuffer)[0];
let authrDataStruct = parseMakeCredAuthData(ctapMakeCredResp.authData);
let response = {'verified': false };
if(ctapMakeCredResp.fmt === 'fido-u2f' || ctapMakeCredResp.fmt === 'packed') {
if(!(authrDataStruct.flags & U2F_USER_PRESENTED))
throw new Error('User was NOT presented durring authentication!');
let clientDataHash =
hash(base64url.toBuffer(webAuthnResponse.response.clientDataJSON))
let publicKey = COSEECDHAtoPKCS(authrDataStruct.COSEPublicKey)
let PEMCertificate = ASN1toPEM(ctapMakeCredResp.attStmt.x5c[0]);
let signature = ctapMakeCredResp.attStmt.sig;
let signatureBase;
if(ctapMakeCredResp.fmt === 'fido-u2f') {
signatureBase = Buffer.concat([Buffer.from([0x00]), authrDataStruct.rpIdHash, clientDataHash, authrDataStruct.credID, publicKey]);
} else {
signatureBase = Buffer.concat([ctapMakeCredResp.authData, clientDataHash]);
}
response.verified = verifySignature(signature, signatureBase, PEMCertificate)
if(response.verified) {
response.authrInfo = {
fmt: `${ctapMakeCredResp.fmt}`,
publicKey: base64url.encode(publicKey),
counter: authrDataStruct.counter,
credID: base64url.encode(authrDataStruct.credID)
}
}
}
return response
}
By default the browser might select U2F. Try forcing UV by setting it to "required". This will force browser to use FIDO2 as U2F does not support UV.

Resources