Skip to content

Instantly share code, notes, and snippets.

@joewagner
joewagner / Sharded mongodb environment on localhost
Last active May 7, 2024 13:33
Bash shell script that sets up a sharded mongodb cluster on a single machine. Handy for testing or development when a sharded deployment is required. Notice that this will remove everything in the data/config and data/shard directories. If you are using those for something else, you may want to edit this...
# clean everything up
echo "killing mongod and mongos"
killall mongod
killall mongos
echo "removing data files"
rm -rf data/config
rm -rf data/shard*
# For mac make sure rlimits are high enough to open all necessary connections
ulimit -n 2048
@joewagner
joewagner / bulk-insert.mjs
Created March 2, 2023 01:10
An example of helpers for Tableland bulk inserts that will need to be split into multiple transactions.
// Dependencies: @tableland/sdk, @tableland/local
// Run `npx local-tableland` and then run this script in another window
import { Database, helpers } from "@tableland/sdk";
import { Wallet, getDefaultProvider, utils } from "ethers";
// NOTE: using 34000 here to give a 1000 byte padding for the `insert into table values` and
// in case the sql parser increases the size, which is unlikely but possible.
// If you wanted to limit for gas you'd have to kinda guess a value here.
const BYTES_LIMIT = 34000
@joewagner
joewagner / policy-upgrades-testing.sh
Created February 28, 2023 23:10
List of commands to manually setup, deploy, upgrade a tableland registry. This enables testing the affects of existing policy contracts.
# you will want to run the hardhat node in a separate terminal
# putting this here as a reminder
cd evm-tableland
npx hardhat node
# Add the following to the `networks` prop in hardhat.config.ts
# "local-tableland": {
# url: "http://127.0.0.1:8545",
# accounts: [
# "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
@joewagner
joewagner / plog alias
Created November 2, 2022 01:06
Create a git alias that pretty logs
alias.plog log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=iso --branches
@joewagner
joewagner / muller-kahan.js
Created October 10, 2022 15:05
Javascript Muller Kahan
// This is a test designed by muller and kahan that demonstrates how
// the calculation of a known convergence returns incorrectly because
// of the mathematics of floating point numbers
const max = 21
let u = 2.0;
let v = -4.0;
let w;
for (let i = 3; i < max; i++) {
w = 111.0 - 1130.0/v + 3000.0/(v*u);
@joewagner
joewagner / Filecoin vanilla block inspector
Last active October 28, 2021 23:29
Filecoin vanilla block inspector
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chain Height</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
<!--
Make sure your Docker daemon is running
@joewagner
joewagner / brute force limit folding a paper
Created December 28, 2019 05:10
copy paste this into a javascript shell and you'll get 33.33333333333333
var lim = function (num) {
if (num === void 0) num = 100;
var series = [100, 50, 25];
var next = function () {
var lastTerm = series[series.length - 1];
var secondToLastTerm = series[series.length - 2];
// notice that, since we started above with the first three terms, that the fliping back and forth between adding
// and subtracting is handled here ↓, i.e. `secondToLastTerm - lastTerm` is alternating negative and positive
const express = require("express");
const session = require("express-session");
const cors = require("cors");
const app = express();
app.use(cors({
origin: ["http://localhost:3000"], credentials: true
}
));
@joewagner
joewagner / optimization-status.js
Created February 2, 2016 20:06
Demonstrates v8 optimization for reassigning vs. not reassigning `arguments` object
// reassigning an argument
function argReassign1(a) {
a = a || {};
}
// not reassigning an argument
function argReassign2(a) {
if (a === void 0) var a = {};
}
@joewagner
joewagner / catchVersionErr.js
Last active December 22, 2015 11:48
Helper function to catch version errors when trying to update an Array field in a mongoose Model
// --- Dependancies ---
var _ = require('underscore');
var mongoose = require('mongoose');
// Any function that makes an update to a mongoose model that has an Array field can
// use this to catch version errors, and retry the update at most two times
var catchVersionErr = function () {
var updateFunc, callback, context, args = _.toArray(arguments);
context = args.shift();