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. 32
      transaction.go

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

@ -3,31 +3,27 @@ package main
import (
"bytes"
"crypto/sha256"
"strconv"
"time"
)
// Constants
const (
TOTAL_COINS = 21000000
)
// Block keeps block headers.
type Block struct {
Timestamp int64
utxoPool []UTXOPool
Transactions []*Transaction
PrevBlockHash []byte
Hash []byte
}
//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{})
headers := bytes.Join([][]byte{b.PrevBlockHash, timestamp}, []byte{})
hash := sha256.Sum256(headers)
// HashTransactions returns a hash of the transactions in the block
func (b *Block) HashTransactions() []byte {
var txHashes [][]byte
var txHash [32]byte
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.

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

Loading…
Cancel
Save