Skip to content
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

Adding the basic features #1

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .github/workflows/build_go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ jobs:

- name: Test
run: go test -v ./...

- name: Run golangci-lint
uses: golangci/[email protected]

58 changes: 58 additions & 0 deletions .github/workflows/coverage_go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
name: Update coverage badge
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.

- name: Setup go
uses: actions/setup-go@v2
with:
go-version: '1.14.4'

- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Run Test
run: |
go test -v ./... -covermode=count -coverprofile=coverage.out
go tool cover -func=coverage.out -o=coverage.out

- name: Go Coverage Badge # Pass the `coverage.out` output to this action
uses: tj-actions/[email protected]
with:
filename: coverage.out

- name: Verify Changed files
uses: tj-actions/[email protected]
id: verify-changed-files
with:
files: README.md

- name: Commit changes
if: steps.verify-changed-files.outputs.files_changed == 'true'
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add README.md
git commit -m "chore: Updated coverage badge."

- name: Push changes
if: steps.verify-changed-files.outputs.files_changed == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ github.token }}
branch: ${{ github.head_ref }}
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Logs
logs.txt

# Temporary files
tmp/*

# VSCode
.vscode/*

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
91 changes: 91 additions & 0 deletions blockchain/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package blockchain

import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/json"
"strconv"
"strings"

logging "github.com/MVRetailManager/MVInventoryChain/logging"
)

type Block struct {
Index int
UnixTimeStamp int64
Hash []byte
PreviousHash []byte
Nonce int
Difficulty int
Transaction []*Transaction
}

func NewBlock(index int, unixTimeStamp int64, previousHash []byte, transaction []*Transaction) *Block {
b := &Block{
Index: index,
UnixTimeStamp: unixTimeStamp,
Hash: make([]byte, 32),
PreviousHash: previousHash,
Nonce: 0,
Difficulty: 1,
Transaction: transaction,
}

logging.BlocksLogger.Printf("Block Initialised: %v\n", b)

b.Mine()

return b
}

func (b *Block) Mine() {
for !b.validateHash(b.calculateHash()) {
b.Nonce++
b.Hash = b.calculateHash()
}

logging.BlocksLogger.Printf("Block Mined:\n Nonce: %v\nHash: %v\n", b.Nonce, b.Hash)
}

func (b *Block) calculateHash() []byte {
hash := sha256.Sum256([]byte(strconv.Itoa(b.Index) + strconv.FormatInt(b.UnixTimeStamp, 10) + string(b.PreviousHash) + strconv.Itoa(b.Nonce) + strconv.Itoa(b.Difficulty) + transactionToString(b.Transaction)))
return hash[:]
}

func (b *Block) validateHash(hash []byte) bool {
prefix := strings.Repeat("0", b.Difficulty)
return strings.HasPrefix(string(hash), prefix)
}

func first(n []byte, _ error) []byte {
return n
}

func transactionToString(transaction []*Transaction) string {
return string(first(json.Marshal(transaction)))
}

func (bc *Block) Serialize() []byte {
var res bytes.Buffer

encoder := gob.NewEncoder(&res)

err := encoder.Encode(bc)

HandleError(err)

return res.Bytes()
}

func Deserialize(data []byte) *Block {
var block Block

decoder := gob.NewDecoder(bytes.NewReader(data))

err := decoder.Decode(&block)

HandleError(err)

return &block
}
Loading