alok 7 years ago
commit b13d9127e7
  1. 1
      .gitignore
  2. 14
      .vscode/settings.json
  3. 20
      aws-code/dial.go
  4. 34
      aws-code/listen.go
  5. 2
      blockchain/block.go
  6. 18
      blockchain/blockchain.go
  7. 24
      blockchain/transaction.go

1
.gitignore vendored

@ -1,5 +1,6 @@
# IdeaIDE # IdeaIDE
.idea .idea
.vscode
# Executables # Executables
*.exe *.exe

@ -0,0 +1,14 @@
{
"workbench.colorTheme": "Solarized Light",
"npm.enableScriptExplorer": true,
"window.zoomLevel": 1,
"editor.tabCompletion": true,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false,
"go.autocompleteUnimportedPackages": true,
"[go]": {
"editor.insertSpaces": true,
"editor.formatOnSave": true
},
}

@ -1,19 +1,19 @@
package main package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
) )
func main() { func main() {
conn, err := net.Dial("tcp", ":9000") conn, err := net.Dial("tcp", ":9000")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer conn.Close() defer conn.Close()
bs, _ := ioutil.ReadAll(conn) bs, _ := ioutil.ReadAll(conn)
fmt.Println(string(bs)) fmt.Println(string(bs))
} }

@ -1,26 +1,26 @@
package main package main
import ( import (
"fmt" "fmt"
"io" "io"
"net" "net"
"time" "time"
) )
func main() { func main() {
ln, err := net.Listen("tcp", "localhost:9000") ln, err := net.Listen("tcp", "localhost:9000")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer ln.Close() defer ln.Close()
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
panic(err) panic(err)
} }
io.WriteString(conn, fmt.Sprint("Hello World\n", time.Now(), "\n")) io.WriteString(conn, fmt.Sprint("Hello World\n", time.Now(), "\n"))
conn.Close() conn.Close()
} }
} }

@ -44,7 +44,7 @@ func (b *Block) HashTransactions() []byte {
var txHash [32]byte var txHash [32]byte
for _, tx := range b.Transactions { for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.id) txHashes = append(txHashes, tx.ID)
} }
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{})) txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
return txHash[:] return txHash[:]

@ -21,19 +21,19 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
BreakTransaction: BreakTransaction:
for _, tx := range block.Transactions { for _, tx := range block.Transactions {
txID := hex.EncodeToString(tx.id) txID := hex.EncodeToString(tx.ID)
idx := -1 idx := -1
if spentTXOs[txID] != nil { if spentTXOs[txID] != nil {
idx = 0 idx = 0
} }
for outIdx, txOutput := range tx.txOutput { for outIdx, txOutput := range tx.TxOutput {
if idx >= 0 && spentTXOs[txID][idx] == outIdx { if idx >= 0 && spentTXOs[txID][idx] == outIdx {
idx++ idx++
continue continue
} }
if txOutput.address == address { if txOutput.Address == address {
unspentTXs = append(unspentTXs, *tx) unspentTXs = append(unspentTXs, *tx)
continue BreakTransaction continue BreakTransaction
} }
@ -49,8 +49,8 @@ func (bc *Blockchain) FindUTXO(address string) []TXOutput {
unspentTXs := bc.FindUnspentTransactions(address) unspentTXs := bc.FindUnspentTransactions(address)
for _, tx := range unspentTXs { for _, tx := range unspentTXs {
for _, txOutput := range tx.txOutput { for _, txOutput := range tx.TxOutput {
if txOutput.address == address { if txOutput.Address == address {
UTXOs = append(UTXOs, txOutput) UTXOs = append(UTXOs, txOutput)
break break
} }
@ -68,11 +68,11 @@ func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map
Work: Work:
for _, tx := range unspentTXs { for _, tx := range unspentTXs {
txID := hex.EncodeToString(tx.id) txID := hex.EncodeToString(tx.ID)
for outIdx, txOutput := range tx.txOutput { for outIdx, txOutput := range tx.TxOutput {
if txOutput.address == address && accumulated < amount { if txOutput.Address == address && accumulated < amount {
accumulated += txOutput.value accumulated += txOutput.Value
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx) unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)
if accumulated >= amount { if accumulated >= amount {

@ -11,26 +11,26 @@ import (
// Transaction represents a Bitcoin transaction // Transaction represents a Bitcoin transaction
type Transaction struct { type Transaction struct {
id []byte ID []byte
txInput []TXInput TxInput []TXInput
txOutput []TXOutput TxOutput []TXOutput
} }
// TXOutput is the struct of transaction output in a transaction. // TXOutput is the struct of transaction output in a transaction.
type TXOutput struct { type TXOutput struct {
value int Value int
address string Address string
} }
// TXInput is the struct of transaction input in a transaction. // TXInput is the struct of transaction input in a transaction.
type TXInput struct { type TXInput struct {
txID []byte TxID []byte
txOutputIndex int TxOutputIndex int
address string Address string
} }
// defaultCoinbaseValue is the default value of coinbase transaction. // DefaultCoinbaseValue is the default value of coinbase transaction.
const defaultCoinbaseValue = 10 const DefaultCoinbaseValue = 10
// SetID sets ID of a transaction // SetID sets ID of a transaction
func (tx *Transaction) SetID() { func (tx *Transaction) SetID() {
@ -43,7 +43,7 @@ func (tx *Transaction) SetID() {
log.Panic(err) log.Panic(err)
} }
hash = sha256.Sum256(encoded.Bytes()) hash = sha256.Sum256(encoded.Bytes())
tx.id = hash[:] tx.ID = hash[:]
} }
// NewCoinbaseTX creates a new coinbase transaction // NewCoinbaseTX creates a new coinbase transaction
@ -53,7 +53,7 @@ func NewCoinbaseTX(to, data string) *Transaction {
} }
txin := TXInput{[]byte{}, -1, data} txin := TXInput{[]byte{}, -1, data}
txout := TXOutput{defaultCoinbaseValue, to} txout := TXOutput{DefaultCoinbaseValue, to}
tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}} tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}}
tx.SetID() tx.SetID()

Loading…
Cancel
Save