diff --git a/block.go b/block.go index 9af9cac92..d1b2a8035 100644 --- a/block.go +++ b/block.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{}) } diff --git a/transaction.go b/transaction.go index 0e68efc2e..fe04ff6e8 100644 --- a/transaction.go +++ b/transaction.go @@ -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 diff --git a/utils.go b/utils.go index e3813f87a..6356ca08c 100644 --- a/utils.go +++ b/utils.go @@ -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