Skip to content

Instantly share code, notes, and snippets.

View bradford-hamilton's full-sized avatar

Bradford Lamson-Scribner bradford-hamilton

View GitHub Profile
def calculate_hash(block)
plain_text = "
#{block[:index]}
#{block[:timestamp]}
#{block[:data]}
#{block[:prev_hash]}
#{block[:nonce]}
"
sha256 = OpenSSL::Digest.new("SHA256")
sha256.update(plain_text)
def is_hash_valid?(hash, difficulty)
prefix = "0" * difficulty
hash.starts_with?(prefix)
end
post "/new-block" do |env|
data = env.params.json["data"].as(String)
new_block = Block.generate(blockchain[blockchain.size - 1], data)
if Block.is_valid?(new_block, blockchain[blockchain.size - 1])
blockchain << new_block
puts "\n"
p new_block
puts "\n"
def is_valid?(new_block, old_block)
if old_block[:index] + 1 != new_block[:index]
return false
elsif old_block[:hash] != new_block[:prev_hash]
return false
elsif self.calculate_hash(new_block) != new_block[:hash]
return false
end
true
# provider.tf
# Specify the provider and access details
provider "aws" {
shared_credentials_file = "$HOME/.aws/credentials"
profile = "default"
region = var.aws_region
}
# variables.tf
variable "aws_region" {
description = "The AWS region things are created in"
default = "us-west-2"
}
variable "ecs_task_execution_role_name" {
description = "ECS task execution role name"
default = "myEcsTaskExecutionRole"
# network.tf
# Fetch AZs in the current region
data "aws_availability_zones" "available" {
}
resource "aws_vpc" "main" {
cidr_block = "172.17.0.0/16"
}
# security.tf
# ALB Security Group: Edit to restrict access to the application
resource "aws_security_group" "lb" {
name = "cb-load-balancer-security-group"
description = "controls access to the ALB"
vpc_id = aws_vpc.main.id
ingress {
protocol = "tcp"
# alb.tf
resource "aws_alb" "main" {
name = "cb-load-balancer"
subnets = aws_subnet.public.*.id
security_groups = [aws_security_group.lb.id]
}
resource "aws_alb_target_group" "app" {
name = "cb-target-group"
# ecs.tf
resource "aws_ecs_cluster" "main" {
name = "cb-cluster"
}
data "template_file" "cb_app" {
template = file("./templates/ecs/cb_app.json.tpl")
vars = {