• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vocdoni / vocdoni-node / 11856589598

15 Nov 2024 12:49PM UTC coverage: 62.207% (-0.08%) from 62.286%
11856589598

push

github

altergui
Merge branch 'origin/main' into release-lts-1

29 of 56 new or added lines in 10 files covered. (51.79%)

6 existing lines in 3 files now uncovered.

16837 of 27066 relevant lines covered (62.21%)

38382.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.63
/tree/arbo/utils.go
1
package arbo
2

3
import (
4
        "fmt"
5
        "math"
6
        "math/big"
7

8
        "go.vocdoni.io/dvote/db"
9
)
10

11
// SwapEndianness swaps the order of the bytes in the byte slice.
12
func SwapEndianness(b []byte) []byte {
1,371,000✔
13
        o := make([]byte, len(b))
1,371,000✔
14
        for i := range b {
41,311,558✔
15
                o[len(b)-1-i] = b[i]
39,940,558✔
16
        }
39,940,558✔
17
        return o
1,371,000✔
18
}
19

20
// BigIntToBytes converts a *big.Int into a byte array in Little-Endian
21
//
22
// Deprecated: use BigIntToBytesLE or BigIntToBytesBE
UNCOV
23
func BigIntToBytes(blen int, bi *big.Int) []byte {
×
NEW
24
        return BigIntToBytesLE(blen, bi)
×
NEW
25
}
×
26

27
// BigIntToBytesLE converts a *big.Int into a byte array in Little-Endian
28
func BigIntToBytesLE(blen int, bi *big.Int) []byte {
483,489✔
29
        // TODO make the length depending on the tree.hashFunction.Len()
483,489✔
30
        b := make([]byte, blen)
483,489✔
31
        copy(b, SwapEndianness(bi.Bytes()))
483,489✔
32
        return b
483,489✔
33
}
483,489✔
34

35
// BigIntToBytesBE converts a *big.Int into a byte array in Big-Endian
NEW
36
func BigIntToBytesBE(blen int, bi *big.Int) []byte {
×
NEW
37
        // TODO make the length depending on the tree.hashFunction.Len()
×
NEW
38
        b := make([]byte, blen)
×
NEW
39
        copy(b, bi.Bytes())
×
NEW
40
        return b
×
NEW
41
}
×
42

43
// BytesToBigInt converts a byte array in Little-Endian representation into
44
// *big.Int
45
//
46
// Deprecated: use BytesLEToBigInt or BytesBEToBigInt
UNCOV
47
func BytesToBigInt(b []byte) *big.Int {
×
NEW
48
        return BytesLEToBigInt(b)
×
NEW
49
}
×
50

51
// BytesLEToBigInt converts a byte array in Little-Endian representation into
52
// *big.Int
53
func BytesLEToBigInt(b []byte) *big.Int {
886,309✔
54
        return new(big.Int).SetBytes(SwapEndianness(b))
886,309✔
55
}
886,309✔
56

57
// BytesBEToBigInt converts a byte array in Big-Endian representation into
58
// *big.Int
NEW
59
func BytesBEToBigInt(b []byte) *big.Int {
×
NEW
60
        return new(big.Int).SetBytes(b)
×
NEW
61
}
×
62

63
// newLeafValue takes a key & value from a leaf, and computes the leaf hash,
64
// which is used as the leaf key. And the value is the concatenation of the
65
// inputted key & value. The output of this function is used as key-value to
66
// store the leaf in the DB.
67
// [     1 byte   |     1 byte    | N bytes | M bytes ]
68
// [ type of node | length of key |   key   |  value  ]
69
func newLeafValue(hashFunc HashFunction, k, v []byte) ([]byte, []byte, error) {
217,092✔
70
        if err := checkKeyValueLen(k, v); err != nil {
217,092✔
71
                return nil, nil, err
×
72
        }
×
73
        leafKey, err := hashFunc.Hash(k, v, []byte{1})
217,092✔
74
        if err != nil {
217,092✔
75
                return nil, nil, err
×
76
        }
×
77
        var leafValue []byte
217,092✔
78
        leafValue = append(leafValue, byte(PrefixValueLeaf))
217,092✔
79
        leafValue = append(leafValue, byte(len(k)))
217,092✔
80
        leafValue = append(leafValue, k...)
217,092✔
81
        leafValue = append(leafValue, v...)
217,092✔
82
        return leafKey, leafValue, nil
217,092✔
83
}
84

85
// ReadLeafValue reads from a byte array the leaf key & value
86
func ReadLeafValue(b []byte) ([]byte, []byte) {
418,105✔
87
        if len(b) < PrefixValueLen {
438,656✔
88
                return []byte{}, []byte{}
20,551✔
89
        }
20,551✔
90

91
        kLen := b[1]
397,554✔
92
        if len(b) < PrefixValueLen+int(kLen) {
397,554✔
93
                return []byte{}, []byte{}
×
94
        }
×
95
        k := b[PrefixValueLen : PrefixValueLen+kLen]
397,554✔
96
        v := b[PrefixValueLen+kLen:]
397,554✔
97
        return k, v
397,554✔
98
}
99

100
// keyPathFromKey returns the keyPath and checks that the key is not bigger
101
// than maximum key length for the tree maxLevels size.
102
// This is because if the key bits length is bigger than the maxLevels of the
103
// tree, two different keys that their difference is at the end, will collision
104
// in the same leaf of the tree (at the max depth).
105
func keyPathFromKey(maxLevels int, k []byte) ([]byte, error) {
593,904✔
106
        maxKeyLen := int(math.Ceil(float64(maxLevels) / float64(8)))
593,904✔
107
        if len(k) > maxKeyLen {
593,914✔
108
                return nil, fmt.Errorf("len(k) can not be bigger than ceil(maxLevels/8), where"+
10✔
109
                        " len(k): %d, maxLevels: %d, max key len=ceil(maxLevels/8): %d. Might need"+
10✔
110
                        " a bigger tree depth (maxLevels>=%d) in order to input keys of length %d",
10✔
111
                        len(k), maxLevels, maxKeyLen, len(k)*8, len(k))
10✔
112
        }
10✔
113
        keyPath := make([]byte, maxKeyLen)
593,894✔
114
        copy(keyPath, k)
593,894✔
115
        return keyPath, nil
593,894✔
116
}
117

118
// checkKeyValueLen checks the key length and value length. This method is used
119
// when adding single leafs and also when adding a batch. The limits of lengths
120
// used are derived from the encoding of tree dumps: 1 byte to define the
121
// length of the keys (2^8-1 bytes length)), and 2 bytes to define the length
122
// of the values (2^16-1 bytes length).
123
func checkKeyValueLen(k, v []byte) error {
467,748✔
124
        if len(k) > maxUint8 {
467,748✔
125
                return fmt.Errorf("len(k)=%v, can not be bigger than %v",
×
126
                        len(k), maxUint8)
×
127
        }
×
128
        if len(v) > maxUint16 {
467,748✔
129
                return fmt.Errorf("len(v)=%v, can not be bigger than %v",
×
130
                        len(v), maxUint16)
×
131
        }
×
132
        return nil
467,748✔
133
}
134

135
// deleteNodes removes the nodes in the keys slice from the database.
136
func deleteNodes(wTx db.WriteTx, keys [][]byte) error {
164,086✔
137
        for _, k := range keys {
2,002,406✔
138
                if err := wTx.Delete(k); err != nil {
1,838,320✔
139
                        return err
×
140
                }
×
141
        }
142
        return nil
164,086✔
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc