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"
)
// Block keeps block headers
// Constants
const (
TOTAL_COINS = 21000000
)
// Block keeps block headers.
type Block struct {
Timestamp int64
utxoPool []UTXOPool
@ -15,7 +20,7 @@ type Block struct {
Hash []byte
}
//SetHash calculates and sets block hash
//SetHash calculates and sets block hash.
func (b *Block) SetHash() {
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
// headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
@ -25,7 +30,7 @@ func (b *Block) SetHash() {
b.Hash = hash[:]
}
// NewBlock creates and returns Block
// NewBlock creates and returns Block.
func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), utxoPool, prevBlockHash, []byte{}}
@ -33,7 +38,10 @@ func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block {
return block
}
// NewGenesisBlock creates and returns genesis Block
// NewGenesisBlock creates and returns genesis 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"
"encoding/gob"
"log"
"strconv"
"strings"
)
const subsidy = 10
// Transaction represents a Bitcoin transaction
type Transaction struct {
ID []byte
data string
ID []byte
inputAddresss []string
outputAddress []string
value []int
}
func (tx *Transaction) Parse() {
strings.Split("a,b,c", ",")
func (tx *Transaction) Parse(data string) {
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

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

Loading…
Cancel
Save