-
Notifications
You must be signed in to change notification settings - Fork 47
/
txrecords.go
143 lines (119 loc) · 3.32 KB
/
txrecords.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package txrecords
import (
"bytes"
"encoding/binary"
"time"
"github.com/dusk-network/dusk-blockchain/pkg/core/data/ipc/transactions"
"github.com/sirupsen/logrus"
)
// Direction is an enum which tells us whether a transaction is
// incoming or outgoing.
type Direction uint8
const (
// In identifies incoming transaction.
In Direction = iota
// Out identifies outgoing transaction.
Out
)
// TxMeta is a convenience structure which groups fields common to the TxView
// and the TxRecord.
type TxMeta struct {
Height uint64
Direction Direction
Timestamp int64
}
// TxRecord encapsulates the data stored on the DB related to a transaction such as block
// height, direction, etc. The fields related to the ContractCall are embedded
// inside the Transaction carrying the information about the `Fee` (gas) and the
// `Amount` (which can be zero for smart contract calls not related to transfer
// o value).
type TxRecord struct {
TxMeta
Transaction transactions.ContractCall
}
// TxView is the UX-friendly structure to be served to the UI requesting them.
type TxView struct {
TxMeta
Type transactions.TxType
Amount uint64
Fee uint64
Timelock uint64
Hash []byte
Data []byte
Obfuscated bool
}
// New creates a TxRecord.
func New(call transactions.ContractCall, height uint64, direction Direction) *TxRecord {
return &TxRecord{
Transaction: call,
TxMeta: TxMeta{
Direction: direction,
Timestamp: time.Now().Unix(),
Height: height,
},
}
}
// View returns a UI consumable representation of a TXRecord.
func (t TxRecord) View() TxView {
h, err := t.Transaction.CalculateHash()
if err != nil {
logrus.Panic(err)
}
view := TxView{
TxMeta: t.TxMeta,
Type: t.Transaction.Type(),
Hash: h,
}
view.Fee, err = t.Transaction.Fee()
if err != nil {
logrus.WithError(err).Warn("failed to decode fee")
}
// switch tx := t.Transaction.(type) {
// case *transactions.BidTransaction:
// view.Timelock = tx.ExpirationHeight
// case *transactions.StakeTransaction:
// view.Timelock = tx.ExpirationHeight
// case *transactions.Transaction:
// view.Data = tx.Data
// }
return view
}
// Encode the TxRecord into a buffer.
func Encode(b *bytes.Buffer, t *TxRecord) error {
if err := binary.Write(b, binary.LittleEndian, t.Direction); err != nil {
return err
}
if err := binary.Write(b, binary.LittleEndian, t.Timestamp); err != nil {
return err
}
if err := binary.Write(b, binary.LittleEndian, t.Height); err != nil {
return err
}
if err := transactions.Marshal(b, t.Transaction); err != nil {
return err
}
return nil
}
// Decode a TxRecord from a buffer.
func Decode(b *bytes.Buffer, t *TxRecord) error {
if err := binary.Read(b, binary.LittleEndian, &t.Direction); err != nil {
return err
}
if err := binary.Read(b, binary.LittleEndian, &t.Timestamp); err != nil {
return err
}
if err := binary.Read(b, binary.LittleEndian, &t.Height); err != nil {
return err
}
call := new(transactions.Transaction)
if err := transactions.Unmarshal(b, call); err != nil {
return err
}
t.Transaction = *call
return nil
}