Skip to content

Proof of Work and Validations setup #4

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 16 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions github/workflows/ci.yml

This file was deleted.

53 changes: 53 additions & 0 deletions internal/blockchain/proof_of_work.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
package blockchain

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

const targetBits = 24

var maxNonce = math.MaxInt64

const Difficulty = 16

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

// Prepare pow data, this function hashes the data and returns it

func (pow *ProofOfWork) prepareData(nonce int) []byte {
data := bytes.Join(
[][]byte{
pow.block.prevBlockHash,
pow.block.Data,
[]byte(strconv.FormatInt(pow.block.Timestamp, 10)),
[]byte(strconv.FormatInt(targetBits, 10)),
[]byte(strconv.FormatInt(int64(nonce), 10)),
},
[]byte{},
)

return data
}

func (pow *ProofOfWork) RunProofOfWork() (int, []byte) {
var hashInt big.Int
var hash [32]byte
nonce := 0

if pow.block == nil {
pow.block.Data = NewGenesisBlock().Data
} else {
temp_block := pow.block
temp_block.SetHash()
pow.block = temp_block
}

for nonce < maxNonce {
data := pow.prepareData(nonce)
hash = sha256.Sum256(data)

hashInt.SetBytes(hash[:])
if hashInt.Cmp(&pow.T) == -1 {
break
} else {
nonce++
}
}

return nonce, hash[:]
}

func NewProofOfWork(b *Block) *ProofOfWork {
target := big.NewInt(1)
target.Lsh(target, uint(256-Difficulty))
Expand Down
57 changes: 46 additions & 11 deletions internal/blockchain/validantions.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package blockchain

import (
"crypto/sha256"
"fmt"
"math/big"
"reflect"
"time"
)

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

/*
func ValidateBlockPredicate(b *Block) bool {
func ValidateBlockPredicate(pow *ProofOfWork) bool {
var hashInt big.Int

data := pow.prepareData(pow.block.Counter)
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])

validation := hashInt.Cmp(&pow.T) == -1
return validation
}
*/

// Simple Content Validation Predicate implementation from backbone protocol
func ContentValidatePredicate(x *Blockchain) bool {
Expand Down Expand Up @@ -61,18 +68,46 @@
return data
}

/*func ChainValidationPredicate(c *Blockchain) bool {
// Function to validate the chain
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()
if b {
temp_chain := c.blocks[len(c.blocks)-1]
s_ := sha256.Sum256(temp_chain.Data)
proof_ := &ProofOfWork{temp_chain, *big.NewInt(1)}

for i := true; i; b = false || c == nil {
if ValidateBlockPredicate(proof_) && reflect.DeepEqual(temp_chain.Hash, s_) {
s_ = [32]byte{}
copy(s_[:], temp_chain.Data)
c.blocks = c.blocks[:len(c.blocks)-1]
} else {
b = false

Check failure on line 86 in internal/blockchain/validantions.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to b (ineffassign)
}
}
}
return b
}

// Function to find the best chain

for i := true; i; b = false{
if
func MaxChain(c [][]Blockchain) []*Block {
temp_chain := []*Block{}

for i := 1; i < len(c); i++ {
if ChainValidationPredicate(&c[i][0]) {
temp_chain = maxBlocks(temp_chain, c[i][0].blocks)
}
}

return temp_chain
}

// compare blockchains length
func maxBlocks(a, b []*Block) []*Block {
if len(a) > len(b) {
return a
}
return b
}
*/
Loading