summaryrefslogtreecommitdiff
path: root/node/netprio.go
blob: f3ac6a488faded6777d2a5d63afeafdc3ed0ebbd (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package node

import (
	"encoding/base64"
	"fmt"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/account"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/protocol"
)

const netPrioChallengeSize = 32

const netPrioChallengeSizeBase64Encoded = 44 // 32 * (4/3) rounded up to nearest multiple of 4 -> 44

type netPrioResponse struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"`

	Nonce string `codec:"Nonce,allocbound=netPrioChallengeSizeBase64Encoded"`
}

type netPrioResponseSigned struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"`

	Response netPrioResponse
	Round    basics.Round
	Sender   basics.Address
	Sig      crypto.OneTimeSignature
}

func (npr netPrioResponse) ToBeHashed() (protocol.HashID, []byte) {
	return protocol.NetPrioResponse, protocol.Encode(&npr)
}

// NewPrioChallenge implements the network.NetPrioScheme interface
func (node *AlgorandFullNode) NewPrioChallenge() string {
	var rand [netPrioChallengeSize]byte
	crypto.RandBytes(rand[:])
	return base64.StdEncoding.EncodeToString(rand[:])
}

// MakePrioResponse implements the network.NetPrioScheme interface
func (node *AlgorandFullNode) MakePrioResponse(challenge string) []byte {
	if !node.config.AnnounceParticipationKey {
		return nil
	}

	rs := netPrioResponseSigned{
		Response: netPrioResponse{
			Nonce: challenge,
		},
	}

	// Find the participation key that has the highest weight in the
	// latest round.
	var maxWeight uint64
	var maxPart account.ParticipationRecordForRound

	latest := node.ledger.LastRound()
	proto, err := node.ledger.ConsensusParams(latest)
	if err != nil {
		return nil
	}

	// Use the participation key for 2 rounds in the future, so that
	// it's unlikely to be deleted from underneath of us.
	voteRound := latest + 2
	for _, part := range node.accountManager.Keys(voteRound) {
		parent := part.Account
		data, err := node.ledger.LookupAgreement(latest, parent)
		if err != nil {
			continue
		}

		weight := data.MicroAlgosWithRewards.ToUint64()
		if weight > maxWeight {
			maxPart = part
			maxWeight = weight
		}
	}

	if maxWeight == 0 {
		return nil
	}

	signer := maxPart.VotingSigner()
	ephID := basics.OneTimeIDForRound(voteRound, signer.KeyDilution(proto.DefaultKeyDilution))

	rs.Round = voteRound
	rs.Sender = maxPart.Account
	rs.Sig = signer.Sign(ephID, rs.Response)

	return protocol.Encode(&rs)
}

// VerifyPrioResponse implements the network.NetPrioScheme interface
func (node *AlgorandFullNode) VerifyPrioResponse(challenge string, response []byte) (addr basics.Address, err error) {
	var rs netPrioResponseSigned
	err = protocol.Decode(response, &rs)
	if err != nil {
		return
	}

	if rs.Response.Nonce != challenge {
		err = fmt.Errorf("challenge/response mismatch")
		return
	}

	balanceRound := rs.Round.SubSaturate(2)
	proto, err := node.ledger.ConsensusParams(balanceRound)
	if err != nil {
		return
	}

	data, err := node.ledger.LookupAgreement(balanceRound, rs.Sender)
	if err != nil {
		return
	}

	ephID := basics.OneTimeIDForRound(rs.Round, data.KeyDilution(proto))
	if !data.VoteID.Verify(ephID, rs.Response, rs.Sig) {
		err = fmt.Errorf("signature verification failure")
		return
	}

	addr = rs.Sender
	return
}

// GetPrioWeight implements the network.NetPrioScheme interface
func (node *AlgorandFullNode) GetPrioWeight(addr basics.Address) uint64 {
	latest := node.ledger.LastRound()
	data, err := node.ledger.LookupAgreement(latest, addr)
	if err != nil {
		return 0
	}

	return data.MicroAlgosWithRewards.ToUint64()
}