modify Transaction data structure

pull/2/head
Minh Doan 7 years ago
parent d10ba966ab
commit 3a49816e6a
  1. 5
      benchmark_node.go
  2. 24
      block.go
  3. 28
      transaction.go

@ -1,8 +1,6 @@
package main package main
import ( import (
"./consensus"
"./p2p"
"bufio" "bufio"
"flag" "flag"
"fmt" "fmt"
@ -11,6 +9,9 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"./consensus"
"./p2p"
) )
// Consts // Consts

@ -3,31 +3,27 @@ package main
import ( import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"strconv"
"time" "time"
) )
// Constants
const (
TOTAL_COINS = 21000000
)
// Block keeps block headers. // Block keeps block headers.
type Block struct { type Block struct {
Timestamp int64 Timestamp int64
utxoPool []UTXOPool Transactions []*Transaction
PrevBlockHash []byte PrevBlockHash []byte
Hash []byte Hash []byte
} }
//SetHash calculates and sets block hash. // HashTransactions returns a hash of the transactions in the block
func (b *Block) SetHash() { func (b *Block) HashTransactions() []byte {
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10)) var txHashes [][]byte
// headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{}) var txHash [32]byte
headers := bytes.Join([][]byte{b.PrevBlockHash, timestamp}, []byte{})
hash := sha256.Sum256(headers)
b.Hash = hash[:] for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.ID)
}
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
return txHash[:]
} }
// NewBlock creates and returns Block. // NewBlock creates and returns Block.

@ -5,32 +5,24 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/gob" "encoding/gob"
"log" "log"
"strconv"
"strings"
) )
// Transaction represents a Bitcoin transaction // Transaction represents a Bitcoin transaction
type Transaction struct { type Transaction struct {
ID []byte ID []byte
inputAddresss []string Vin []TXInput
outputAddress []string Vout []TXOutput
value []int
} }
func (tx *Transaction) Parse(data string) { type TXOutput struct {
items := strings.Split(data, ",") Addresss string
for _, value := range items { Value int
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 type TXInput struct {
Txid []byte
outIndex int
Address string
} }
// SetID sets ID of a transaction // SetID sets ID of a transaction

Loading…
Cancel
Save