Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created July 2, 2018 11:14
Show Gist options
  • Save mratsim/ce0ba64f89a6bfd625990f73c4ff8237 to your computer and use it in GitHub Desktop.
Save mratsim/ce0ba64f89a6bfd625990f73c4ff8237 to your computer and use it in GitHub Desktop.
# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import random, sequtils, strformat
type
Op = enum
A
B
C
Halt
proc baseA() {.inline.}=
echo "gas: ", 10
proc baseB() {.inline.}=
echo "gas: ", 20
proc baseC() {.inline.}=
echo "gas: ", 30
# Fork changes just C
proc tangerineC() {.inline.}=
echo "gas: ", 5000
template callFork(prefix, enumOp: untyped) =
`prefix enumOp`()
proc baseVM(instructions: seq[Op]) =
var pc = 0
while true:
{.computedGoto.}
let instr = instructions[pc]
case instr
of A:
callFork(base, A)
of B:
callFork(base, B)
of C:
callFork(base, C)
of Halt:
break
inc(pc)
proc tangerineVM(instructions: seq[Op]) {.inline.}=
var pc = 0
while true:
{.computedGoto.}
let instr = instructions[pc]
case instr
of A:
callFork(base, A) # <-- in the real case the ident would be added by a macro/template
of B:
callFork(base, B)
of C:
callFork(tangerine, C) # <-- I only change what is needed
of Halt:
break
inc(pc)
proc process(start_block, end_block: int) =
for blk in start_block..end_block:
let length = rand(2..10)
var instructions = newSeqWith(length, rand(0..2).Op)
instructions.add Halt
echo &"Block: {blk}, instructions: {instructions}"
case blk:
# This case statement is easily predictable since it doesn't change for months
of {0..10}:
echo "using FrontierVM"
baseVM(instructions)
echo "block done\n"
else:
echo "using TangerineVM"
tangerineVM(instructions)
echo "block done\n"
randomize()
process(0, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment