parent
e61d01eaa8
commit
5e7ac26db9
@ -0,0 +1,37 @@ |
||||
package main |
||||
|
||||
// UTXOPool is the data structure to store the current balance.
|
||||
type UTXOPool struct { |
||||
utxos map[string]int |
||||
} |
||||
|
||||
// func (utxoPool *UTXOPool) handleTransaction(transaction Transaction, receiver string) {
|
||||
// if !isValidTransaction(transaction) {
|
||||
// return
|
||||
// }
|
||||
// // utxoPool[]
|
||||
// }
|
||||
|
||||
// func (utxoPool *UTXOPool) isValidTransaction(transaction Transaction) {
|
||||
// const { inputPublicKey, amount, fee } = transaction
|
||||
// const utxo = this.utxos[inputPublicKey]
|
||||
// return utxo !== undefined && utxo.amount >= (amount + fee) && amount > 0
|
||||
// }
|
||||
|
||||
// func (utxoPool *UTXOPool) handleTransaction(transaction, feeReceiver) {
|
||||
// if (!this.isValidTransaction(transaction))
|
||||
// return
|
||||
// const inputUTXO = this.utxos[transaction.inputPublicKey];
|
||||
// inputUTXO.amount -= transaction.amount
|
||||
// inputUTXO.amount -= transaction.fee
|
||||
// if (inputUTXO.amount === 0)
|
||||
// delete this.utxos[transaction.inputPublicKey]
|
||||
// this.addUTXO(transaction.outputPublicKey, transaction.amount)
|
||||
// this.addUTXO(feeReceiver, transaction.fee)
|
||||
// }
|
||||
|
||||
// func (utxoPool *UTXOPool) isValidTransaction(transaction Transaction) {
|
||||
// const { inputPublicKey, amount, fee } = transaction
|
||||
// const utxo = utxoPool.utxos[inputPublicKey]
|
||||
// return utxo !== undefined && utxo.amount >= (amount + fee) && amount > 0
|
||||
// }
|
@ -0,0 +1,35 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/sha256" |
||||
"encoding/gob" |
||||
"log" |
||||
"strings" |
||||
) |
||||
|
||||
const subsidy = 10 |
||||
|
||||
// Transaction represents a Bitcoin transaction
|
||||
type Transaction struct { |
||||
ID []byte |
||||
data string |
||||
} |
||||
|
||||
func (tx *Transaction) Parse() { |
||||
strings.Split("a,b,c", ",") |
||||
} |
||||
|
||||
// SetID sets ID of a transaction
|
||||
func (tx *Transaction) SetID() { |
||||
var encoded bytes.Buffer |
||||
var hash [32]byte |
||||
|
||||
enc := gob.NewEncoder(&encoded) |
||||
err := enc.Encode(tx) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
hash = sha256.Sum256(encoded.Bytes()) |
||||
tx.ID = hash[:] |
||||
} |
@ -0,0 +1,25 @@ |
||||
package main |
||||
|
||||
import "testing" |
||||
|
||||
func TestConvertIntoMap(t *testing.T) { |
||||
data := "minh:3,mike:2" |
||||
res := ConvertIntoMap(data) |
||||
if len(res) != 2 { |
||||
t.Errorf("Result should have 2 pairs (key, value)") |
||||
} |
||||
if val, ok := res["minh"]; !ok { |
||||
t.Errorf("Result should contain key minh") |
||||
} else { |
||||
if res["minh"] != 3 { |
||||
t.Errorf("Value of minh should be 3") |
||||
} |
||||
} |
||||
if val, ok := res["mike"]; !ok { |
||||
t.Errorf("Result should contain key mike") |
||||
} else { |
||||
if res["minh"] != 3 { |
||||
t.Errorf("Value of minh should be 2") |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue