summaryrefslogtreecommitdiff
path: root/data/basics/units.go
blob: af8743ee404ff2836ef6d5e01ebeda3bd63c6e11 (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 basics

import (
	"github.com/algorand/go-codec/codec"
	"github.com/algorand/msgp/msgp"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
)

// RoundInterval is a number of rounds
type RoundInterval uint64

// MicroAlgos is our unit of currency.  It is wrapped in a struct to nudge
// developers to use an overflow-checking library for any arithmetic.
type MicroAlgos struct {
	Raw uint64
}

// LessThan implements arithmetic comparison for MicroAlgos
func (a MicroAlgos) LessThan(b MicroAlgos) bool {
	return a.Raw < b.Raw
}

// GreaterThan implements arithmetic comparison for MicroAlgos
func (a MicroAlgos) GreaterThan(b MicroAlgos) bool {
	return a.Raw > b.Raw
}

// IsZero implements arithmetic comparison for MicroAlgos
func (a MicroAlgos) IsZero() bool {
	return a.Raw == 0
}

// ToUint64 converts the amount of algos to uint64
func (a MicroAlgos) ToUint64() uint64 {
	return a.Raw
}

// RewardUnits returns the number of reward units in some number of algos
func (a MicroAlgos) RewardUnits(proto config.ConsensusParams) uint64 {
	return a.Raw / proto.RewardUnit
}

// We generate our own encoders and decoders for MicroAlgos
// because we want it to appear as an integer, even though
// we represent it as a single-element struct.
//msgp:ignore MicroAlgos

// CodecEncodeSelf implements codec.Selfer to encode MicroAlgos as a simple int
func (a MicroAlgos) CodecEncodeSelf(enc *codec.Encoder) {
	enc.MustEncode(a.Raw)
}

// CodecDecodeSelf implements codec.Selfer to decode MicroAlgos as a simple int
func (a *MicroAlgos) CodecDecodeSelf(dec *codec.Decoder) {
	dec.MustDecode(&a.Raw)
}

// CanMarshalMsg implements msgp.Marshaler
func (MicroAlgos) CanMarshalMsg(z interface{}) bool {
	_, ok := (z).(MicroAlgos)
	return ok
}

// MarshalMsg implements msgp.Marshaler
func (a MicroAlgos) MarshalMsg(b []byte) (o []byte) {
	o = msgp.Require(b, msgp.Uint64Size)
	o = msgp.AppendUint64(o, a.Raw)
	return
}

// CanUnmarshalMsg implements msgp.Unmarshaler
func (*MicroAlgos) CanUnmarshalMsg(z interface{}) bool {
	_, ok := (z).(*MicroAlgos)
	return ok
}

// UnmarshalMsg implements msgp.Unmarshaler
func (a *MicroAlgos) UnmarshalMsg(bts []byte) (o []byte, err error) {
	return a.UnmarshalMsgWithState(bts, msgp.DefaultUnmarshalState)
}

// UnmarshalMsgWithState implements msgp.Unmarshaler
func (a *MicroAlgos) UnmarshalMsgWithState(bts []byte, st msgp.UnmarshalState) (o []byte, err error) {
	if st.AllowableDepth == 0 {
		return nil, msgp.ErrMaxDepthExceeded{}
	}
	a.Raw, o, err = msgp.ReadUint64Bytes(bts)
	return
}

// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (a MicroAlgos) Msgsize() (s int) {
	return msgp.Uint64Size
}

// MsgIsZero returns whether this is a zero value
func (a MicroAlgos) MsgIsZero() bool {
	return a.Raw == 0
}

// MicroAlgosMaxSize returns maximum possible msgp encoded size of MicroAlgos in bytes.
// It is expected by msgp generated MaxSize functions
func MicroAlgosMaxSize() (s int) {
	return msgp.Uint64Size
}

// Round represents a protocol round index
type Round uint64

// OneTimeIDForRound maps a round to the identifier for which ephemeral key
// should be used for that round.  keyDilution specifies the number of keys
// in the bottom-level of the two-level key structure.
func OneTimeIDForRound(round Round, keyDilution uint64) crypto.OneTimeSignatureIdentifier {
	return crypto.OneTimeSignatureIdentifier{
		Batch:  uint64(round) / keyDilution,
		Offset: uint64(round) % keyDilution,
	}
}

// SubSaturate subtracts x rounds with saturation arithmetic that does not
// wrap around past zero, and instead returns 0 on underflow.
func (round Round) SubSaturate(x Round) Round {
	if round < x {
		return 0
	}

	return round - x
}

// RoundUpToMultipleOf rounds up round to the next multiple of n.
func (round Round) RoundUpToMultipleOf(n Round) Round {
	return (round + n - 1) / n * n
}

// RoundDownToMultipleOf rounds down round to a multiple of n.
func (round Round) RoundDownToMultipleOf(n Round) Round {
	return (round / n) * n
}