This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_hash_valid?(hash, difficulty) | |
prefix = "0" * difficulty | |
hash.starts_with?(prefix) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# provider.tf | |
# Specify the provider and access details | |
provider "aws" { | |
shared_credentials_file = "$HOME/.aws/credentials" | |
profile = "default" | |
region = var.aws_region | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# network.tf | |
# Fetch AZs in the current region | |
data "aws_availability_zones" "available" { | |
} | |
resource "aws_vpc" "main" { | |
cidr_block = "172.17.0.0/16" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ecs.tf | |
resource "aws_ecs_cluster" "main" { | |
name = "cb-cluster" | |
} | |
data "template_file" "cb_app" { | |
template = file("./templates/ecs/cb_app.json.tpl") | |
vars = { |