-
Notifications
You must be signed in to change notification settings - Fork 97
/
block.go
299 lines (241 loc) · 11.1 KB
/
block.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package types
type (
// BlockHash represents the hash of a block
BlockHash Digest
// A BlockHeader represents the metadata and commitments to the state of a Block.
// The Algorand Ledger may be defined minimally as a cryptographically authenticated series of BlockHeader objects.
BlockHeader struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Round Round `codec:"rnd"`
// The hash of the previous block
Branch BlockHash `codec:"prev"`
// Sortition seed
Seed [32]byte `codec:"seed"`
TxnCommitments
// TimeStamp in seconds since epoch
TimeStamp int64 `codec:"ts"`
// Genesis ID to which this block belongs.
GenesisID string `codec:"gen"`
// Genesis hash to which this block belongs.
GenesisHash Digest `codec:"gh"`
// Rewards.
//
// When a block is applied, some amount of rewards are accrued to
// every account with AccountData.Status=/=NotParticipating. The
// amount is (thisBlock.RewardsLevel-prevBlock.RewardsLevel) of
// MicroAlgos for every whole config.Protocol.RewardUnit of MicroAlgos in
// that account's AccountData.MicroAlgos.
//
// Rewards are not compounded (i.e., not added to AccountData.MicroAlgos)
// until some other transaction is executed on that account.
//
// Not compounding rewards allows us to precisely know how many algos
// of rewards will be distributed without having to examine every
// account to determine if it should get one more algo of rewards
// because compounding formed another whole config.Protocol.RewardUnit
// of algos.
RewardsState
// Consensus protocol versioning.
//
// Each block is associated with a version of the consensus protocol,
// stored under UpgradeState.CurrentProtocol. The protocol version
// for a block can be determined without having to first decode the
// block and its CurrentProtocol field, and this field is present for
// convenience and explicitness. Block.Valid() checks that this field
// correctly matches the expected protocol version.
//
// Each block is associated with at most one active upgrade proposal
// (a new version of the protocol). An upgrade proposal can be made
// by a block proposer, as long as no other upgrade proposal is active.
// The upgrade proposal lasts for many rounds (UpgradeVoteRounds), and
// in each round, that round's block proposer votes to support (or not)
// the proposed upgrade.
//
// If enough votes are collected, the proposal is approved, and will
// definitely take effect. The proposal lingers for some number of
// rounds to give clients a chance to notify users about an approved
// upgrade, if the client doesn't support it, so the user has a chance
// to download updated client software.
//
// Block proposers influence this upgrade machinery through two fields
// in UpgradeVote: UpgradePropose, which proposes an upgrade to a new
// protocol, and UpgradeApprove, which signals approval of the current
// proposal.
//
// Once a block proposer determines its UpgradeVote, then UpdateState
// is updated deterministically based on the previous UpdateState and
// the new block's UpgradeVote.
UpgradeState
UpgradeVote
// TxnCounter counts the number of transactions committed in the
// ledger, from the time at which support for this feature was
// introduced.
//
// Specifically, TxnCounter is the number of the next transaction
// that will be committed after this block. It is 0 when no
// transactions have ever been committed (since TxnCounter
// started being supported).
TxnCounter uint64 `codec:"tc"`
// StateProofTracking tracks the status of the state proofs, potentially
// for multiple types of ASPs (Algorand's State Proofs).
//msgp:sort protocol.StateProofType protocol.SortStateProofType
StateProofTracking map[StateProofType]StateProofTrackingData `codec:"spt,allocbound=NumStateProofTypes"`
// ParticipationUpdates contains the information needed to mark
// certain accounts offline because their participation keys expired
ParticipationUpdates
}
// TxnCommitments represents the commitments computed from the transactions in the block.
// It contains multiple commitments based on different algorithms and hash functions, to support different use cases.
TxnCommitments struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// Root of transaction merkle tree using SHA512_256 hash function.
// This commitment is computed based on the PaysetCommit type specified in the block's consensus protocol.
NativeSha512_256Commitment Digest `codec:"txn"`
// Root of transaction vector commitment merkle tree using SHA256 hash function
Sha256Commitment Digest `codec:"txn256"`
}
// ParticipationUpdates represents participation account data that
// needs to be checked/acted on by the network
ParticipationUpdates struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// ExpiredParticipationAccounts contains a list of online accounts
// that needs to be converted to offline since their
// participation key expired.
ExpiredParticipationAccounts []Address `codec:"partupdrmv"`
}
// RewardsState represents the global parameters controlling the rate
// at which accounts accrue rewards.
RewardsState struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// The FeeSink accepts transaction fees. It can only spend to
// the incentive pool.
FeeSink Address `codec:"fees"`
// The RewardsPool accepts periodic injections from the
// FeeSink and continually redistributes them to adresses as
// rewards.
RewardsPool Address `codec:"rwd"`
// RewardsLevel specifies how many rewards, in MicroAlgos,
// have been distributed to each config.Protocol.RewardUnit
// of MicroAlgos since genesis.
RewardsLevel uint64 `codec:"earn"`
// The number of new MicroAlgos added to the participation stake from rewards at the next round.
RewardsRate uint64 `codec:"rate"`
// The number of leftover MicroAlgos after the distribution of RewardsRate/rewardUnits
// MicroAlgos for every reward unit in the next round.
RewardsResidue uint64 `codec:"frac"`
// The round at which the RewardsRate will be recalculated.
RewardsRecalculationRound Round `codec:"rwcalr"`
}
// UpgradeVote represents the vote of the block proposer with
// respect to protocol upgrades.
UpgradeVote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// UpgradePropose indicates a proposed upgrade
UpgradePropose string `codec:"upgradeprop"`
// UpgradeDelay indicates the time between acceptance and execution
UpgradeDelay Round `codec:"upgradedelay"`
// UpgradeApprove indicates a yes vote for the current proposal
UpgradeApprove bool `codec:"upgradeyes"`
}
// UpgradeState tracks the protocol upgrade state machine. It is,
// strictly speaking, computable from the history of all UpgradeVotes
// but we keep it in the block for explicitness and convenience
// (instead of materializing it separately, like balances).
//msgp:ignore UpgradeState
UpgradeState struct {
CurrentProtocol string `codec:"proto"`
NextProtocol string `codec:"nextproto"`
NextProtocolApprovals uint64 `codec:"nextyes"`
NextProtocolVoteBefore Round `codec:"nextbefore"`
NextProtocolSwitchOn Round `codec:"nextswitch"`
}
// StateProofTrackingData tracks the status of state proofs.
StateProofTrackingData struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// StateProofVotersCommitment is the root of a vector commitment containing the
// online accounts that will help sign a state proof. The
// VC root, and the state proof, happen on blocks that
// are a multiple of ConsensusParams.StateProofRounds. For blocks
// that are not a multiple of ConsensusParams.StateProofRounds,
// this value is zero.
StateProofVotersCommitment GenericDigest `codec:"v"`
// StateProofOnlineTotalWeight is the total number of microalgos held by the online accounts
// during the StateProof round (or zero, if the merkle root is zero - no commitment for StateProof voters).
// This is intended for computing the threshold of votes to expect from StateProofVotersCommitment.
StateProofOnlineTotalWeight MicroAlgos `codec:"t"`
// StateProofNextRound is the next round for which we will accept
// a StateProof transaction.
StateProofNextRound Round `codec:"n"`
}
// A Block contains the Payset and metadata corresponding to a given Round.
Block struct {
BlockHeader
Payset Payset `codec:"txns"`
}
// A Payset represents a common, unforgeable, consistent, ordered set of SignedTxn objects.
//msgp:allocbound Payset 100000
Payset []SignedTxnInBlock
// SignedTxnInBlock is how a signed transaction is encoded in a block.
SignedTxnInBlock struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
SignedTxnWithAD
HasGenesisID bool `codec:"hgi"`
HasGenesisHash bool `codec:"hgh"`
}
// SignedTxnWithAD is a (decoded) SignedTxn with associated ApplyData
SignedTxnWithAD struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
SignedTxn
ApplyData
}
// ApplyData contains information about the transaction's execution.
ApplyData struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// Closing amount for transaction.
ClosingAmount MicroAlgos `codec:"ca"`
// Closing amount for asset transaction.
AssetClosingAmount uint64 `codec:"aca"`
// Rewards applied to the Sender, Receiver, and CloseRemainderTo accounts.
SenderRewards MicroAlgos `codec:"rs"`
ReceiverRewards MicroAlgos `codec:"rr"`
CloseRewards MicroAlgos `codec:"rc"`
EvalDelta EvalDelta `codec:"dt"`
ConfigAsset uint64 `codec:"caid"`
ApplicationID uint64 `codec:"apid"`
}
)
type EvalDelta struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
GlobalDelta StateDelta `codec:"gd"`
// When decoding EvalDeltas, the integer key represents an offset into
// [txn.Sender, txn.Accounts[0], txn.Accounts[1], ...]
LocalDeltas map[uint64]StateDelta `codec:"ld,allocbound=config.MaxEvalDeltaAccounts"`
// If a program modifies the local of an account that is not the Sender, or
// in txn.Accounts, it must be recorded here, so that the key in LocalDeltas
// can refer to it.
SharedAccts []Address `codec:"sa,allocbound=config.MaxEvalDeltaAccounts"`
Logs []string `codec:"lg"`
InnerTxns []SignedTxnWithAD `codec:"itx"`
}
// StateDelta is a map from key/value store keys to ValueDeltas, indicating
// what should happen for that key
//msgp:allocbound StateDelta config.MaxStateDeltaKeys
type StateDelta map[string]ValueDelta
// ValueDelta links a DeltaAction with a value to be set
type ValueDelta struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Action DeltaAction `codec:"at"`
Bytes string `codec:"bs"`
Uint uint64 `codec:"ui"`
}
// DeltaAction is an enum of actions that may be performed when applying a
// delta to a TEAL key/value store
type DeltaAction uint64
const (
// SetBytesAction indicates that a TEAL byte slice should be stored at a key
SetBytesAction DeltaAction = 1
// SetUintAction indicates that a Uint should be stored at a key
SetUintAction DeltaAction = 2
// DeleteAction indicates that the value for a particular key should be deleted
DeleteAction DeltaAction = 3
)