-
Notifications
You must be signed in to change notification settings - Fork 658
/
utxo_id.go
124 lines (101 loc) · 2.86 KB
/
utxo_id.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avax
import (
"bytes"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
var (
errNilUTXOID = errors.New("nil utxo ID is not valid")
errMalformedUTXOIDString = errors.New("unexpected number of tokens in string")
errFailedDecodingUTXOIDTxID = errors.New("failed decoding UTXOID TxID")
errFailedDecodingUTXOIDIndex = errors.New("failed decoding UTXOID index")
_ verify.Verifiable = (*UTXOID)(nil)
)
type UTXOID struct {
// Serialized:
TxID ids.ID `serialize:"true" json:"txID"`
OutputIndex uint32 `serialize:"true" json:"outputIndex"`
// Symbol is false if the UTXO should be part of the DB
Symbol bool `json:"-"`
// id is the unique ID of a UTXO, it is calculated from TxID and OutputIndex
id ids.ID
}
// InputSource returns the source of the UTXO that this input is spending
func (utxo *UTXOID) InputSource() (ids.ID, uint32) {
return utxo.TxID, utxo.OutputIndex
}
// InputID returns a unique ID of the UTXO that this input is spending
func (utxo *UTXOID) InputID() ids.ID {
if utxo.id == ids.Empty {
utxo.id = utxo.TxID.Prefix(uint64(utxo.OutputIndex))
}
return utxo.id
}
// Symbolic returns if this is the ID of a UTXO in the DB, or if it is a
// symbolic input
func (utxo *UTXOID) Symbolic() bool {
return utxo.Symbol
}
func (utxo *UTXOID) String() string {
return fmt.Sprintf("%s:%d", utxo.TxID, utxo.OutputIndex)
}
// UTXOIDFromString attempts to parse a string into a UTXOID
func UTXOIDFromString(s string) (*UTXOID, error) {
ss := strings.Split(s, ":")
if len(ss) != 2 {
return nil, errMalformedUTXOIDString
}
txID, err := ids.FromString(ss[0])
if err != nil {
return nil, fmt.Errorf("%w: %s", errFailedDecodingUTXOIDTxID, err)
}
idx, err := strconv.ParseUint(ss[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("%w: %s", errFailedDecodingUTXOIDIndex, err)
}
return &UTXOID{
TxID: txID,
OutputIndex: uint32(idx),
}, nil
}
func (utxo *UTXOID) Verify() error {
switch {
case utxo == nil:
return errNilUTXOID
default:
return nil
}
}
type innerSortUTXOIDs []*UTXOID
func (utxos innerSortUTXOIDs) Less(i, j int) bool {
iID, iIndex := utxos[i].InputSource()
jID, jIndex := utxos[j].InputSource()
switch bytes.Compare(iID[:], jID[:]) {
case -1:
return true
case 0:
return iIndex < jIndex
default:
return false
}
}
func (utxos innerSortUTXOIDs) Len() int {
return len(utxos)
}
func (utxos innerSortUTXOIDs) Swap(i, j int) {
utxos[j], utxos[i] = utxos[i], utxos[j]
}
func SortUTXOIDs(utxos []*UTXOID) {
sort.Sort(innerSortUTXOIDs(utxos))
}
func IsSortedAndUniqueUTXOIDs(utxos []*UTXOID) bool {
return utils.IsSortedAndUnique(innerSortUTXOIDs(utxos))
}