• 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

48.81
/vochain/transaction/admin_tx.go
1
package transaction
2

3
import (
4
        "fmt"
5

6
        "go.vocdoni.io/dvote/crypto/ethereum"
7
        "go.vocdoni.io/dvote/log"
8
        "go.vocdoni.io/dvote/vochain/transaction/vochaintx"
9
        "go.vocdoni.io/proto/build/go/models"
10
)
11

12
// AdminTxCheck is an abstraction of ABCI checkTx for an admin transaction
13
func (t *TransactionHandler) AdminTxCheck(vtx *vochaintx.Tx) (ethereum.Address, error) {
224✔
14
        if vtx.SignedBody == nil || vtx.Signature == nil || vtx.Tx == nil {
224✔
15
                return ethereum.Address{}, ErrNilTx
×
16
        }
×
17
        tx := vtx.Tx.GetAdmin()
224✔
18

224✔
19
        // check vtx.Signature available and extract address
224✔
20
        if vtx.Signature == nil || tx == nil || vtx.SignedBody == nil {
224✔
21
                return ethereum.Address{}, fmt.Errorf("missing signature or transaction body")
×
22
        }
×
23
        pubKey, err := ethereum.PubKeyFromSignature(vtx.SignedBody, vtx.Signature)
224✔
24
        if err != nil {
224✔
25
                return ethereum.Address{}, fmt.Errorf("cannot extract public key from signature: %w", err)
×
26
        }
×
27
        addr, err := ethereum.AddrFromPublicKey(pubKey)
224✔
28
        if err != nil {
224✔
29
                return ethereum.Address{}, fmt.Errorf("cannot extract address from public key: %w", err)
×
30
        }
×
31
        log.Debugw("checking admin tx", "addr", addr.Hex(), "tx", log.FormatProto(tx))
224✔
32

224✔
33
        switch tx.Txtype {
224✔
34
        case models.TxType_ADD_PROCESS_KEYS, models.TxType_REVEAL_PROCESS_KEYS:
224✔
35
                if tx.ProcessId == nil {
224✔
36
                        return ethereum.Address{}, fmt.Errorf("missing processId on adminTx")
×
37
                }
×
38

39
                // check process exists
40
                process, err := t.state.Process(tx.ProcessId, false)
224✔
41
                if err != nil {
224✔
UNCOV
42
                        return ethereum.Address{}, err
×
UNCOV
43
                }
×
44
                if process == nil {
224✔
45
                        return ethereum.Address{}, fmt.Errorf("process with id (%x) does not exist", tx.ProcessId)
×
46
                }
×
47

48
                // check if process actually requires keys
49
                if !process.EnvelopeType.EncryptedVotes && !process.EnvelopeType.Anonymous {
224✔
50
                        return ethereum.Address{}, fmt.Errorf("process does not require keys")
×
51
                }
×
52

53
                // check if sender authorized (a current validator)
54
                validator, err := t.state.Validator(addr, false)
224✔
55
                if validator == nil || err != nil {
224✔
56
                        if err != nil {
×
57
                                return ethereum.Address{}, err
×
58
                        }
×
59
                        return ethereum.Address{}, fmt.Errorf(
×
60
                                "not a validator, unauthorized to execute admin tx key management, address: %s", addr.Hex())
×
61
                }
62

63
                // if validator keyIndex is zero, it is disabled
64
                if validator.KeyIndex == 0 {
224✔
65
                        return ethereum.Address{}, fmt.Errorf("validator key management is disabled")
×
66
                }
×
67

68
                // check keyIndex is not nil
69
                if tx.KeyIndex == nil {
224✔
70
                        return ethereum.Address{}, fmt.Errorf("missing keyIndex on adminTx")
×
71
                }
×
72

73
                // check keyIndex in the transaction is correct for the validator
74
                if *tx.KeyIndex != validator.KeyIndex {
224✔
75
                        return ethereum.Address{}, fmt.Errorf("transaction key index does not match with validator index")
×
76
                }
×
77

78
                // get current timestamp
79
                currentTime, err := t.state.Timestamp(false)
224✔
80
                if err != nil {
224✔
81
                        return ethereum.Address{}, err
×
82
                }
×
83

84
                // Specific checks
85
                switch tx.Txtype {
224✔
86
                case models.TxType_ADD_PROCESS_KEYS:
112✔
87
                        // process is not canceled
112✔
88
                        if process.Status == models.ProcessStatus_CANCELED ||
112✔
89
                                process.Status == models.ProcessStatus_ENDED ||
112✔
90
                                process.Status == models.ProcessStatus_RESULTS {
112✔
91
                                return ethereum.Address{}, fmt.Errorf("cannot add process keys to a %s process", process.Status)
×
92
                        }
×
93
                        if len(process.EncryptionPublicKeys[tx.GetKeyIndex()]) > 0 {
112✔
94
                                return ethereum.Address{}, fmt.Errorf("keys for process %x already added", tx.ProcessId)
×
95
                        }
×
96
                        // check included keys and keyindex are valid
97
                        if err := checkAddProcessKeys(tx, process); err != nil {
112✔
98
                                return ethereum.Address{}, err
×
99
                        }
×
100
                case models.TxType_REVEAL_PROCESS_KEYS:
112✔
101
                        // check process is finished
112✔
102
                        if currentTime < process.StartTime+process.Duration &&
112✔
103
                                !(process.Status == models.ProcessStatus_ENDED) {
112✔
104
                                return ethereum.Address{}, fmt.Errorf("cannot reveal keys before the process is finished or not in ENDED status")
×
105
                        }
×
106
                        if len(process.EncryptionPrivateKeys[tx.GetKeyIndex()]) > 0 {
112✔
107
                                return ethereum.Address{}, fmt.Errorf("keys for process %x already revealed", tx.ProcessId)
×
108
                        }
×
109
                        // check the keys are valid
110
                        if err := checkRevealProcessKeys(tx, process); err != nil {
112✔
111
                                return ethereum.Address{}, err
×
112
                        }
×
113
                }
114
        default:
×
115
                return ethereum.Address{}, fmt.Errorf("tx not supported")
×
116
        }
117
        return ethereum.Address(addr), nil
224✔
118
}
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