Skip to content

Instantly share code, notes, and snippets.

View jdaviderb's full-sized avatar
👨‍💻
Creating

Jorge Hernandez jdaviderb

👨‍💻
Creating
View GitHub Profile
@jdaviderb
jdaviderb / test.rs
Created December 22, 2023 04:45
Rust Cast Low level
pub fn cast_to_parts<T>(content: &T) -> &[u8] {
let ptr = content as *const T as *const u8;
let len = mem::size_of::<T>();
unsafe { std::slice::from_raw_parts(ptr, len) }
}
pub fn cast_to<T>(value: &[u8]) -> &T {
let ptr = value.as_ptr() as *const T;
unsafe { &*ptr }
}
@jdaviderb
jdaviderb / vmmap.c
Created August 22, 2023 02:56 — forked from bazad/vmmap.c
A simple vmmap implementation for macOS.
// Brandon Azad (@_bazad)
#include <assert.h>
#include <errno.h>
#include <mach/mach.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@jdaviderb
jdaviderb / privatekeysolana.js
Created October 2, 2022 00:55 — forked from Xavier59/privatekeysolana.js
Convert Solana private key from/to base58/uint8array
// exporting from a bs58 private key to an Uint8Array
// == from phantom private key to solana cli id.json key file
// npm install bs58 @solana/web3.js
const web3 = require("@solana/web3.js");
const bs58 = require('bs58');
let secretKey = bs58.decode("[base58 private key here]");
console.log(`[${web3.Keypair.fromSecretKey(secretKey).secretKey}]`);
// exporting back from Uint8Array to bs58 private key
@jdaviderb
jdaviderb / gist:9773aedc2c7aaf83653295c4c95ebec3
Created September 18, 2022 18:31
How solana calculate Clock::get()?.unix_timestamp;
> It's strongly desired by some users to have a network-controlled best effort timestamp associated with the production time of each block. One such example is https://en.bitcoin.it/wiki/Block_timestamp
>
> Some important properties of this timestamp are:
>
> 1. Drift of no worse than a couple hours from the actual block production time
> 2. Replaying the ledger at a later time results in the same block production time
> 3. The block production time is not controlled by a single centralized oracle, but ideally is a function that uses inputs from all validators
>
> A rough proposal would be to require validators to submit their current UTC time every N minutes. A median time is then constructed from the latest submission and block times are then computed as `last_median_time + slot_offset * 400ms` until the next median time is constructed.
>
@jdaviderb
jdaviderb / example.rs
Created August 13, 2022 23:17
rust zero copy serialization
fn serialize(value: &[i32]) -> &[u8] {
let len = value.len() * 4;
unsafe { std::slice::from_raw_parts(value.as_ptr().cast(), len) }
}
fn deserialize(bytes: &[u8]) -> Vec<i32> {
assert!(bytes.len() % 4 == 0);
let mut vec = Vec::<i32>::with_capacity(bytes.len() / 4);
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), vec.as_mut_ptr().cast(), bytes.len());
@jdaviderb
jdaviderb / solana-helpers.sh
Created August 3, 2022 22:45
anchor devnet helper
# Set Envs
export ANCHOR_WALLET="/Users/YOUR_USER/.config/solana/id.json"
# export ANCHOR_PROVIDER_URL="http://127.0.0.1:8899"
export ANCHOR_PROVIDER_URL="https://api.devnet.solana.com"
# airdrop wallet
solana airdrop 2 $(solana-keygen pubkey $ANCHOR_WALLET) --url https://api.devnet.solana.com && solana airdrop 2 $(solana-keygen pubkey $ANCHOR_WALLET) --url https://api.devnet.solana.com
# deploy
@jdaviderb
jdaviderb / s3-medata.sh
Last active August 2, 2022 02:47
S3 Metadata
# Get S3 metadata
aws s3 ls s3://YOUR_S3_BUCKET --recursive --summarize --human-readable
# Group files by extension
aws s3 ls s3://YOUR_S3_BUCKET --recursive \
| grep -v -E '^.+/$' \
| awk '{na=split($NF, a, "."); tot[a[na]] += $3; num[a[na]]++;} END {for (e in tot) printf "%15d %6d %s\n", tot[e], num[e], e};'
#!/bin/bash
sudo yum install gcc -y
sudo yum install openssl-devel -y
sudo yum install zlib-devel -y
sudo yum install mlocate -y
sudo yum install autoconf -y
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.2p1.tar.gz
tar zxvf openssh-8.2p1.tar.gz
cd openssh-8.2p1 && ./configure && make && sudo make install
@jdaviderb
jdaviderb / update jenkins ubuntu.md
Created May 18, 2022 17:54
Update Jenkins on ubuntu

Update the installation

cd /usr/share/jenkins
sudo service jenkins stop
sudo mv jenkins.war jenkins.war.old
sudo wget https://updates.jenkins-ci.org/latest/jenkins.war
sudo service jenkins start
@jdaviderb
jdaviderb / query.sql
Created May 9, 2022 00:20
mysql show databases by size
SELECT table_schema AS "Data Base Name", sum( data_length + index_length ) / 1024 / 1024 AS "Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema;