The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/blockchain/transaction.go

157 lines
4.4 KiB

package blockchain
7 years ago
import (
7 years ago
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"fmt"
7 years ago
"log"
"math"
)
var (
// zeroHash is the zero value for a Hash and is defined as
// a package level variable to avoid the need to create a new instance
// every time a check is needed.
6 years ago
zeroHash TxID
7 years ago
)
type Transaction struct {
ID [32]byte // 32 byte hash
7 years ago
TxInput []TXInput
TxOutput []TXOutput
Proofs []CrossShardTxProof // The proofs for crossShard tx unlock-to-commit/abort
7 years ago
}
// TXOutput is the struct of transaction output in a transaction.
type TXOutput struct {
7 years ago
Value int
Address string
6 years ago
ShardID uint32 // The Id of the shard where this UTXO belongs
}
6 years ago
type TxID = [32]byte
6 years ago
// Output defines a data type that is used to track previous
// transaction outputs.
6 years ago
// TxID is the transaction id
6 years ago
// Index is the index of the transaction ouput in the previous transaction
type OutPoint struct {
6 years ago
TxID TxID
Index uint32
6 years ago
}
// NewOutPoint returns a new transaction outpoint point with the
6 years ago
// provided txID and index.
func NewOutPoint(txID *TxID, index uint32) *OutPoint {
6 years ago
return &OutPoint{
6 years ago
TxID: *txID,
6 years ago
Index: index,
}
}
// TXInput is the struct of transaction input (a UTXO) in a transaction.
type TXInput struct {
6 years ago
PreviousOutPoint OutPoint
Address string
ShardID uint32 // The Id of the shard where this UTXO belongs
}
// NewTXInput returns a new transaction input with the provided
// previous outpoint point, output address and shardID
func NewTXInput(prevOut *OutPoint, address string, shardID uint32) *TXInput {
return &TXInput{
PreviousOutPoint: *prevOut,
Address: address,
ShardID: shardID,
}
7 years ago
}
// The proof of accept or reject in the cross shard transaction locking phase.
// This is created by the shard leader, filled with proof signatures after consensus, and returned back to the client.
// One proof structure is only tied to one shard. Therefore, the utxos in the proof are all with the same shard.
type CrossShardTxProof struct {
Accept bool // false means proof-of-reject, true means proof-of-accept
TxID [32]byte // Id of the transaction which this proof is on
TxInput []TXInput // The list of Utxo that this proof is on. They should be in the same shard.
BlockHash [32]byte // The hash of the block where the proof is registered
// Signatures
}
// This is a internal data structure that doesn't go across network
type CrossShardTxAndProof struct {
Transaction *Transaction // The cross shard tx
Proof *CrossShardTxProof // The proof
}
7 years ago
// DefaultCoinbaseValue is the default value of coinbase transaction.
const DefaultCoinbaseValue = 1000
// SetID sets ID of a transaction (32 byte hash of the whole transaction)
func (tx *Transaction) SetID() {
7 years ago
var encoded bytes.Buffer
var hash [32]byte
7 years ago
7 years ago
enc := gob.NewEncoder(&encoded)
err := enc.Encode(tx)
if err != nil {
log.Panic(err)
}
hash = sha256.Sum256(encoded.Bytes())
tx.ID = hash
}
// NewCoinbaseTX creates a new coinbase transaction
6 years ago
func NewCoinbaseTX(to, data string, shardID uint32) *Transaction {
if data == "" {
data = fmt.Sprintf("Reward to '%s'", to)
}
6 years ago
txin := NewTXInput(NewOutPoint(&TxID{}, math.MaxUint32), to, shardID)
6 years ago
txout := TXOutput{DefaultCoinbaseValue, to, shardID}
tx := Transaction{[32]byte{}, []TXInput{*txin}, []TXOutput{txout}, nil}
tx.SetID()
return &tx
}
// Used for debuging.
func (txInput *TXInput) String() string {
6 years ago
res := fmt.Sprintf("TxID: %v, ", hex.EncodeToString(txInput.PreviousOutPoint.TxID[:]))
6 years ago
res += fmt.Sprintf("TxOutputIndex: %v, ", txInput.PreviousOutPoint.Index)
res += fmt.Sprintf("Address: %v, ", txInput.Address)
6 years ago
res += fmt.Sprintf("Shard Id: %v", txInput.ShardID)
return res
}
// Used for debuging.
func (txOutput *TXOutput) String() string {
res := fmt.Sprintf("Value: %v, ", txOutput.Value)
res += fmt.Sprintf("Address: %v", txOutput.Address)
return res
}
// Used for debuging.
func (proof *CrossShardTxProof) String() string {
res := fmt.Sprintf("Accept: %v, ", proof.Accept)
return res
}
// Used for debuging.
func (tx *Transaction) String() string {
res := fmt.Sprintf("ID: %v\n", hex.EncodeToString(tx.ID[:]))
res += fmt.Sprintf("TxInput:\n")
for id, value := range tx.TxInput {
res += fmt.Sprintf("%v: %v\n", id, value.String())
}
res += fmt.Sprintf("TxOutput:\n")
for id, value := range tx.TxOutput {
res += fmt.Sprintf("%v: %v\n", id, value.String())
}
for id, value := range tx.Proofs {
res += fmt.Sprintf("%v: %v\n", id, value.String())
}
return res
7 years ago
}