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