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

1
.gitignore vendored

@ -1,5 +1,6 @@
# IdeaIDE
.idea
.vscode
# Executables
*.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
},
}

@ -21,6 +21,6 @@ func main() {
}
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
for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.id)
txHashes = append(txHashes, tx.ID)
}
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
return txHash[:]

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

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

Loading…
Cancel
Save