Skip to content

Blockchain setup #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 21, 2024
23 changes: 22 additions & 1 deletion internal/blockchain/block.go
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
package blockchain
package main

import (
"bytes"
"crypto/sha256"
"strconv"
)

type Block struct {
Timestamp int64
Data []byte // Transactions
prevBlockHash []byte
Hash []byte
Counter int // Nonce
}

func (b *Block) SetHash() {
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
headers := bytes.Join([][]byte{b.prevBlockHash, b.Data, timestamp}, []byte{})
hash := sha256.Sum256(headers)
b.Hash = hash[:]
}
5 changes: 5 additions & 0 deletions internal/blockchain/blockchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type Blockchain struct {
blocks []*Block
}
21 changes: 21 additions & 0 deletions internal/blockchain/proof_of_work.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"math/big"
)

const Difficulty = 16

type ProofOfWork struct {
block *Block
T big.Int
}

func NewProofOfWork(b *Block) *ProofOfWork {
target := big.NewInt(1)
target.Lsh(target, uint(256-Difficulty))

pow := &ProofOfWork{b, *target}

return pow
}
78 changes: 78 additions & 0 deletions internal/blockchain/validantions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"fmt"
"reflect"
"time"
)

type Receive func() string
type Input func() string

/*
func ValidateBlockPredicate(b *Block) bool {

}
*/

// Simple Content Validation Predicate implementation from backbone protocol
func ContentValidatePredicate(x *Blockchain) bool {

if len(x.blocks) == 0 {
return false
}

for i := range x.blocks {
if i == 0 {
continue
}
if reflect.DeepEqual(x.blocks[i].Hash, x.blocks[i-1].Hash) {
return false
}
}
return true
}

func InputContributionFunction(data []byte, cr *Blockchain, round int, input Input, receive Receive) {

input_data := input()
receive_data := receive()

concat_data := input_data + receive_data
// creating new block

newBlock := &Block{time.Now().Unix(), []byte(concat_data), cr.blocks[len(cr.blocks)-1].Hash, []byte{}, round}
cr.blocks = append(cr.blocks, newBlock)

if !ContentValidatePredicate(cr) {
fmt.Println("Content Validation Failed")
cr.blocks = cr.blocks[:len(cr.blocks)-1]
} else {
fmt.Println("Content Validation Passed")
}
}

// Function to read the chain
func ChainReadFunction(c *Blockchain) string {
data := ""
for i := range c.blocks {
data += string(c.blocks[i].Data)
}
return data
}

/*func ChainValidationPredicate(c *Blockchain) bool {
b := ContentValidatePredicate(c)

if b && c != nil {
index := rand.Intn(len(c.blocks))
temp_chain := c
c.blocks[index].SetHash()

for i := true; i; b = false{
if
}
}
return b
}
*/
Loading