Skip to content

Commit 15b3596

Browse files
jannotticce
authored andcommitted
Chore: Move allocbounds (algorand#6324)
1 parent 9cacf3b commit 15b3596

40 files changed

+716
-615
lines changed

agreement/bundle.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type unauthenticatedBundle struct {
3636
Step step `codec:"step"`
3737
Proposal proposalValue `codec:"prop"`
3838

39-
Votes []voteAuthenticator `codec:"vote,allocbound=config.MaxVoteThreshold"`
40-
EquivocationVotes []equivocationVoteAuthenticator `codec:"eqv,allocbound=config.MaxVoteThreshold"`
39+
Votes []voteAuthenticator `codec:"vote,allocbound=bounds.MaxVoteThreshold"`
40+
EquivocationVotes []equivocationVoteAuthenticator `codec:"eqv,allocbound=bounds.MaxVoteThreshold"`
4141
}
4242

4343
// bundle is a set of votes, all from the same round, period, and step, and from distinct senders, that reaches quorum.
@@ -48,8 +48,8 @@ type bundle struct {
4848

4949
U unauthenticatedBundle `codec:"u"`
5050

51-
Votes []vote `codec:"vote,allocbound=config.MaxVoteThreshold"`
52-
EquivocationVotes []equivocationVote `codec:"eqv,allocbound=config.MaxVoteThreshold"`
51+
Votes []vote `codec:"vote,allocbound=bounds.MaxVoteThreshold"`
52+
EquivocationVotes []equivocationVote `codec:"eqv,allocbound=bounds.MaxVoteThreshold"`
5353
}
5454

5555
// voteAuthenticators omit the Round, Period, Step, and Proposal for compression

agreement/msgp_gen.go

Lines changed: 79 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/bounds/bounds.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright (C) 2019-2025 Algorand, Inc.
2+
// This file is part of go-algorand
3+
//
4+
// go-algorand is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// go-algorand is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package bounds
18+
19+
/* The bounds package is intended to hold conservative bounds on the sizes of
20+
various messages. Many cannot be static, because they depend on consensus
21+
parameters. They are set at runtime iterating over every consensus version
22+
and selecting the largest bound. This allows msgpack parsing to safely
23+
reject anything that NO consensus version would allow.
24+
*/
25+
26+
// MaxVoteThreshold is the largest threshold for a bundle over all supported
27+
// consensus protocols, used for decoding purposes.
28+
var MaxVoteThreshold int
29+
30+
// MaxEvalDeltaAccounts is the largest number of accounts that may appear in an
31+
// eval delta, used for decoding purposes.
32+
var MaxEvalDeltaAccounts int
33+
34+
// MaxStateDeltaKeys is the largest number of key/value pairs that may appear in
35+
// a StateDelta, used for decoding purposes.
36+
var MaxStateDeltaKeys int
37+
38+
// MaxLogCalls is the highest allowable log messages that may appear in any
39+
// version, used only for decoding purposes. Never decrease this value.
40+
var MaxLogCalls int
41+
42+
// MaxInnerTransactionsPerDelta is the maximum number of inner transactions in
43+
// one EvalDelta
44+
var MaxInnerTransactionsPerDelta int
45+
46+
// MaxLogicSigMaxSize is the largest logical signature appear in any of the
47+
// supported protocols, used for decoding purposes.
48+
var MaxLogicSigMaxSize int
49+
50+
// MaxTxnNoteBytes is the largest supported nodes field array size supported by
51+
// any of the consensus protocols. used for decoding purposes.
52+
var MaxTxnNoteBytes int
53+
54+
// MaxTxGroupSize is the largest supported number of transactions per
55+
// transaction group supported by any of the consensus protocols. used for
56+
// decoding purposes.
57+
var MaxTxGroupSize int
58+
59+
// MaxAppProgramLen is the largest supported app program size supported by any
60+
// of the consensus protocols. used for decoding purposes.
61+
var MaxAppProgramLen int
62+
63+
// MaxBytesKeyValueLen is a maximum length of key or value across all protocols.
64+
// used for decoding purposes.
65+
var MaxBytesKeyValueLen int
66+
67+
// MaxExtraAppProgramLen is the maximum extra app program length supported by
68+
// any of the consensus protocols. used for decoding purposes.
69+
var MaxExtraAppProgramLen int
70+
71+
// MaxAvailableAppProgramLen is the largest supported app program size including
72+
// the extra pages supported by any of the consensus protocols. used for
73+
// decoding purposes.
74+
var MaxAvailableAppProgramLen int
75+
76+
// MaxProposedExpiredOnlineAccounts is the maximum number of online accounts
77+
// that a proposer can take offline for having expired voting keys.
78+
var MaxProposedExpiredOnlineAccounts int
79+
80+
// MaxMarkAbsent is the maximum number of online accounts that a proposer can
81+
// suspend for not proposing "lately"
82+
var MaxMarkAbsent int
83+
84+
// MaxAppTotalArgLen is the maximum number of bytes across all arguments of an
85+
// application max sum([len(arg) for arg in txn.ApplicationArgs])
86+
var MaxAppTotalArgLen int
87+
88+
// MaxAssetNameBytes is the maximum asset name length in bytes
89+
var MaxAssetNameBytes int
90+
91+
// MaxAssetUnitNameBytes is the maximum asset unit name length in bytes
92+
var MaxAssetUnitNameBytes int
93+
94+
// MaxAssetURLBytes is the maximum asset URL length in bytes
95+
var MaxAssetURLBytes int
96+
97+
// MaxAppBytesValueLen is the maximum length of a bytes value used in an
98+
// application's global or local key/value store
99+
var MaxAppBytesValueLen int
100+
101+
// MaxAppBytesKeyLen is the maximum length of a key used in an application's
102+
// global or local key/value store
103+
var MaxAppBytesKeyLen int
104+
105+
// StateProofTopVoters is a bound on how many online accounts get to participate
106+
// in forming the state proof, by including the top StateProofTopVoters accounts
107+
// (by normalized balance) into the vector commitment.
108+
var StateProofTopVoters int
109+
110+
// MaxTxnBytesPerBlock determines the maximum number of bytes that transactions
111+
// can take up in a block. Specifically, the sum of the lengths of encodings of
112+
// each transaction in a block must not exceed MaxTxnBytesPerBlock.
113+
var MaxTxnBytesPerBlock int
114+
115+
// MaxAppTxnForeignApps is the max number of foreign apps per txn across all consensus versions
116+
var MaxAppTxnForeignApps int
117+
118+
// MaxEvalDeltaTotalLogSize is the maximum size of the sum of all log sizes in a single eval delta.
119+
const MaxEvalDeltaTotalLogSize = 1024
120+
121+
// MaxGenesisIDLen is the maximum length of the genesis ID set for purpose of
122+
// setting allocbounds on structs containing GenesisID and for purposes of
123+
// calculating MaxSize functions on those types. Current value is larger than
124+
// the existing network IDs and the ones used in testing
125+
const MaxGenesisIDLen = 128
126+
127+
// EncodedMaxAssetsPerAccount is the decoder limit of number of assets stored
128+
// per account. it's being verified by the unit test
129+
// TestEncodedAccountAllocationBounds to align with
130+
// config.Consensus[protocol.ConsensusCurrentVersion].MaxAssetsPerAccount; note
131+
// that the decoded parameter is used only for protecting the decoder against
132+
// malicious encoded account data stream. protocol-specific contents would be
133+
// tested once the decoding is complete.
134+
const EncodedMaxAssetsPerAccount = 1024
135+
136+
// EncodedMaxAppLocalStates is the decoder limit for number of opted-in apps in a single account.
137+
// It is verified in TestEncodedAccountAllocationBounds to align with
138+
// config.Consensus[protocol.ConsensusCurrentVersion].MaxAppsOptedIn
139+
const EncodedMaxAppLocalStates = 64
140+
141+
// EncodedMaxAppParams is the decoder limit for number of created apps in a single account.
142+
// It is verified in TestEncodedAccountAllocationBounds to align with
143+
// config.Consensus[protocol.ConsensusCurrentVersion].MaxAppsCreated
144+
const EncodedMaxAppParams = 64
145+
146+
// EncodedMaxKeyValueEntries is the decoder limit for the length of a key/value store.
147+
// It is verified in TestEncodedAccountAllocationBounds to align with
148+
// config.Consensus[protocol.ConsensusCurrentVersion].MaxLocalSchemaEntries and
149+
// config.Consensus[protocol.ConsensusCurrentVersion].MaxGlobalSchemaEntries
150+
const EncodedMaxKeyValueEntries = 1024
151+
152+
// MaxConsensusVersionLen must be larger than any URL length of any consensus
153+
// version (which is currently URL+hash=89)
154+
const MaxConsensusVersionLen = 128

config/config.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ const ConfigurableConsensusProtocolsFilename = "consensus.json"
8282
// do not expose in normal config so it is not in code generated local_defaults.go
8383
const defaultRelayGossipFanout = 8
8484

85-
// MaxGenesisIDLen is the maximum length of the genesis ID set for purpose of setting
86-
// allocbounds on structs containing GenesisID and for purposes of calculating MaxSize functions
87-
// on those types. Current value is larger than the existing network IDs and the ones used in testing
88-
const MaxGenesisIDLen = 128
89-
90-
// MaxEvalDeltaTotalLogSize is the maximum size of the sum of all log sizes in a single eval delta.
91-
const MaxEvalDeltaTotalLogSize = 1024
92-
9385
// CatchpointTrackingModeUntracked defines the CatchpointTracking mode that does _not_ track catchpoints
9486
const CatchpointTrackingModeUntracked = -1
9587

config/config_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/stretchr/testify/assert"
3131
"github.com/stretchr/testify/require"
3232

33+
"github.com/algorand/go-algorand/config/bounds"
3334
"github.com/algorand/go-algorand/protocol"
3435
"github.com/algorand/go-algorand/test/partitiontest"
3536
"github.com/algorand/go-algorand/util/codecs"
@@ -1170,3 +1171,28 @@ func TestTracksCatchpointsWithoutStoring(t *testing.T) {
11701171
require.Equal(t, true, cfg.TracksCatchpoints())
11711172
require.Equal(t, false, cfg.StoresCatchpoints())
11721173
}
1174+
1175+
func TestEncodedAccountAllocationBounds(t *testing.T) {
1176+
partitiontest.PartitionTest(t)
1177+
1178+
// ensure that all the supported protocols have value limits less or
1179+
// equal to their corresponding codec allocbounds
1180+
for protoVer, proto := range Consensus {
1181+
if proto.MaxAssetsPerAccount > 0 && proto.MaxAssetsPerAccount > bounds.EncodedMaxAssetsPerAccount {
1182+
require.Failf(t, "proto.MaxAssetsPerAccount > EncodedMaxAssetsPerAccount", "protocol version = %s", protoVer)
1183+
}
1184+
if proto.MaxAppsCreated > 0 && proto.MaxAppsCreated > bounds.EncodedMaxAppParams {
1185+
require.Failf(t, "proto.MaxAppsCreated > EncodedMaxAppParams", "protocol version = %s", protoVer)
1186+
}
1187+
if proto.MaxAppsOptedIn > 0 && proto.MaxAppsOptedIn > bounds.EncodedMaxAppLocalStates {
1188+
require.Failf(t, "proto.MaxAppsOptedIn > EncodedMaxAppLocalStates", "protocol version = %s", protoVer)
1189+
}
1190+
if proto.MaxLocalSchemaEntries > bounds.EncodedMaxKeyValueEntries {
1191+
require.Failf(t, "proto.MaxLocalSchemaEntries > EncodedMaxKeyValueEntries", "protocol version = %s", protoVer)
1192+
}
1193+
if proto.MaxGlobalSchemaEntries > bounds.EncodedMaxKeyValueEntries {
1194+
require.Failf(t, "proto.MaxGlobalSchemaEntries > EncodedMaxKeyValueEntries", "protocol version = %s", protoVer)
1195+
}
1196+
// There is no protocol limit to the number of Boxes per account, so that allocbound is not checked.
1197+
}
1198+
}

0 commit comments

Comments
 (0)