-
Notifications
You must be signed in to change notification settings - Fork 8
/
Utils.go
35 lines (31 loc) · 827 Bytes
/
Utils.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
package apollo
import (
"sort"
"github.com/Salvionied/apollo/serialization/UTxO"
)
func SortUtxos(utxos []UTxO.UTxO) []UTxO.UTxO {
res := make([]UTxO.UTxO, len(utxos))
copy(res, utxos)
for i := 0; i < len(res); i++ {
for j := i + 1; j < len(res); j++ {
if res[i].Output.GetAmount().Less(res[j].Output.GetAmount()) {
res[i], res[j] = res[j], res[i]
}
}
}
return res
}
func SortInputs(inputs []UTxO.UTxO) []UTxO.UTxO {
hashes := make([]string, 0)
relationMap := map[string]UTxO.UTxO{}
for _, utxo := range inputs {
hashes = append(hashes, string(utxo.Input.String()))
relationMap[string(utxo.Input.String())] = utxo
}
sort.Strings(hashes)
sorted_inputs := make([]UTxO.UTxO, 0)
for _, hash := range hashes {
sorted_inputs = append(sorted_inputs, relationMap[hash])
}
return sorted_inputs
}