Browse Source

Add Blockchain

golang
garritfra 6 years ago
parent
commit
bf9ad1afab
  1. 24
      .vscode/launch.json
  2. 6
      Block.go
  3. 35
      Blockchain.go
  4. 11
      main.go

24
.vscode/launch.json vendored

@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}"
},
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}

6
Block.go

@ -0,0 +1,6 @@
package main
type Block struct {
hash []byte
previousHash []byte
}

35
Blockchain.go

@ -0,0 +1,35 @@
package main
import (
"crypto/sha256"
"fmt"
)
// Blockchain struct
type Blockchain struct {
blocks []Block
}
func (bc *Blockchain) addBlock(block Block) {
bc.blocks = append(bc.blocks, block)
}
func newBlockchain() Blockchain {
blockchain := Blockchain{blocks: make([]Block, 0)}
genesisBlock := generateGenesisBlock()
blockchain.addBlock(genesisBlock)
return blockchain
}
func generateGenesisBlock() Block {
block := new(Block)
hasher := sha256.New()
s := fmt.Sprintf("%v", block)
sum := hasher.Sum([]byte(s))
block.hash = sum
return *block
}

11
main.go

@ -1,7 +1,14 @@
package main
import "fmt"
import (
"fmt"
)
func main() {
fmt.Printf("hello, world\n")
blockchain := newBlockchain()
for _, v := range blockchain.blocks {
fmt.Println(v.hash)
}
}

Loading…
Cancel
Save