add transaction, util, utxo

pull/2/head
Minh Doan 7 years ago
parent 7824d77df1
commit d10ba966ab
  1. 18
      block.go
  2. 25
      transaction.go
  3. 6
      utils.go

@ -7,7 +7,12 @@ import (
"time" "time"
) )
// Block keeps block headers // Constants
const (
TOTAL_COINS = 21000000
)
// Block keeps block headers.
type Block struct { type Block struct {
Timestamp int64 Timestamp int64
utxoPool []UTXOPool utxoPool []UTXOPool
@ -15,7 +20,7 @@ type Block struct {
Hash []byte Hash []byte
} }
//SetHash calculates and sets block hash //SetHash calculates and sets block hash.
func (b *Block) SetHash() { func (b *Block) SetHash() {
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10)) timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
// headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{}) // headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
@ -25,7 +30,7 @@ func (b *Block) SetHash() {
b.Hash = hash[:] b.Hash = hash[:]
} }
// NewBlock creates and returns Block // NewBlock creates and returns Block.
func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block { func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), utxoPool, prevBlockHash, []byte{}} block := &Block{time.Now().Unix(), utxoPool, prevBlockHash, []byte{}}
@ -33,7 +38,10 @@ func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block {
return block return block
} }
// NewGenesisBlock creates and returns genesis Block // NewGenesisBlock creates and returns genesis Block.
func NewGenesisBlock() *Block { func NewGenesisBlock() *Block {
return NewBlock([]UTXOPool{}, []byte{}) genesisUTXOPool := UTXOPool{}
genesisUTXOPool.utxos["genesis"] = TOTAL_COINS
return NewBlock(genesisUTXOPool, []byte{})
} }

@ -5,19 +5,32 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/gob" "encoding/gob"
"log" "log"
"strconv"
"strings" "strings"
) )
const subsidy = 10
// Transaction represents a Bitcoin transaction // Transaction represents a Bitcoin transaction
type Transaction struct { type Transaction struct {
ID []byte ID []byte
data string inputAddresss []string
outputAddress []string
value []int
} }
func (tx *Transaction) Parse() { func (tx *Transaction) Parse(data string) {
strings.Split("a,b,c", ",") items := strings.Split(data, ",")
for _, value := range items {
pair := strings.Split(value, " ")
if len(pair) == 3 {
intValue, err := strconv.Atoi(pair[2])
if err != nil {
tx.inputAddress = append(tx.inputAddresss, strings.Trim(pair[0]))
tx.outputAddress = append(tx.outputAddress, strings.Trim(pair[1]))
tx.value = append(tx.value, intValue)
}
}
}
return res
} }
// SetID sets ID of a transaction // SetID sets ID of a transaction

@ -57,9 +57,9 @@ func ConvertIntoMap(data string) map[string]int {
var res = map[string]int var res = map[string]int
items := strings.Split(data, ",") items := strings.Split(data, ",")
for _, value := range items { for _, value := range items {
pair := strings.Split(value, ":") pair := strings.Split(value, " ")
if len(pair) == 2 { if len(pair) == 3 {
intValue, err := strconv.Atoi(pair[1]) intValue, err := strconv.Atoi(pair[2])
if err != nil { if err != nil {
pair[0] = strings.Trim(pair[0]) pair[0] = strings.Trim(pair[0])
res[pair[0]] = intValue res[pair[0]] = intValue

Loading…
Cancel
Save