arnaucube
arnaucube/slowlorisdbPublic Self-hosted repository
slowlorisdb/core/tx.go
core/tx.goRaw
 1package core
 2
 3import "crypto/ecdsa"
 4
 5type Input struct {
 6	TxId  Hash
 7	Vout  int // index of the output from the TxId
 8	Value uint64
 9}
10
11type Output struct {
12	Value uint64
13}
14
15// Tx holds the data structure of a transaction
16type Tx struct {
17	From       *ecdsa.PublicKey
18	To         *ecdsa.PublicKey
19	InputCount uint64
20	Inputs     []Input
21	Outputs    []Output
22	Signature  []byte
23}
24
25func NewTx(from, to *ecdsa.PublicKey, in []Input, out []Output) *Tx {
26	tx := &Tx{
27		From:       from,
28		To:         to,
29		InputCount: uint64(len(in)),
30		Inputs:     in,
31		Outputs:    out,
32	}
33	return tx
34}
35
36// CheckTx checks if the transaction is consistent
37func CheckTx(tx *Tx) bool {
38	// TODO check that inputs and outputs are not empty
39
40	// check that inputs == outputs
41	totalIn := 0
42	for _, in := range tx.Inputs {
43		totalIn = totalIn + int(in.Value)
44	}
45	totalOut := 0
46	for _, out := range tx.Outputs {
47		totalOut = totalOut + int(out.Value)
48	}
49	if totalIn < totalOut {
50		return false
51	}
52
53	// TODO check signature
54	return true
55}