-
Notifications
You must be signed in to change notification settings - Fork 95
/
api.go
169 lines (132 loc) · 5.83 KB
/
api.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
package iotago
import (
"encoding/binary"
"time"
"golang.org/x/crypto/blake2b"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2/serix"
)
type Version byte
const VersionLength = 1
func (v Version) Bytes() ([]byte, error) {
return []byte{byte(v)}, nil
}
func VersionFromBytes(b []byte) (Version, int, error) {
if len(b) < VersionLength {
return 0, 0, ierrors.New("invalid version bytes length")
}
return Version(b[0]), VersionLength, nil
}
// VersionSignalingParameters defines the parameters used by signaling protocol parameters upgrade.
type VersionSignalingParameters struct {
// WindowSize is the size of the window in epochs to find which version of protocol parameters was most signaled, from currentEpoch - windowSize to currentEpoch.
WindowSize uint8 `serix:""`
// WindowTargetRatio is the target number of supporters for a version to win in a windowSize.
WindowTargetRatio uint8 `serix:""`
// ActivationOffset is the offset in epochs to activate the new version of protocol parameters.
ActivationOffset uint8 `serix:""`
}
func (s VersionSignalingParameters) Equals(signaling VersionSignalingParameters) bool {
return s.WindowSize == signaling.WindowSize &&
s.WindowTargetRatio == signaling.WindowTargetRatio &&
s.ActivationOffset == signaling.ActivationOffset
}
// API handles en/decoding of IOTA protocol objects.
type API interface {
// Encode encodes the given object to bytes.
Encode(obj any, opts ...serix.Option) ([]byte, error)
// Decode decodes the given bytes into object.
Decode(b []byte, obj any, opts ...serix.Option) (int, error)
// JSONEncode encodes the given object to its json representation.
JSONEncode(obj any, opts ...serix.Option) ([]byte, error)
// JSONDecode decodes the json data into object.
JSONDecode(jsonData []byte, obj any, opts ...serix.Option) error
// Underlying returns the underlying serix.API instance.
Underlying() *serix.API
// Version returns the version of the protocol this API is used with.
Version() Version
// ProtocolParameters returns the protocol parameters this API is used with.
ProtocolParameters() ProtocolParameters
// StorageScoreStructure returns the storage score structure used by the protocol.
StorageScoreStructure() *StorageScoreStructure
// TimeProvider returns the underlying time provider used.
TimeProvider() *TimeProvider
// ManaDecayProvider returns the underlying mana decay provider used.
ManaDecayProvider() *ManaDecayProvider
// MaxBlockWork returns the maximum block work score.
MaxBlockWork() WorkScore
}
func LatestProtocolVersion() Version {
return apiV3Version
}
// LatestAPI creates a new API instance conforming to the latest IOTA protocol version.
func LatestAPI(protoParams ProtocolParameters) API {
//nolint:forcetypeassert // we can safely assume that these are V3ProtocolParameters
return V3API(protoParams.(*V3ProtocolParameters))
}
// NetworkID defines the ID of the network on which entities operate on.
type NetworkID = uint64
// NetworkIDFromString returns the network ID string's numerical representation.
func NetworkIDFromString(networkIDStr string) NetworkID {
networkIDBlakeHash := blake2b.Sum256([]byte(networkIDStr))
return binary.LittleEndian.Uint64(networkIDBlakeHash[:])
}
// ProtocolParametersType defines the type of protocol parameters.
type ProtocolParametersType byte
const (
// ProtocolParametersV3 denotes a V3ProtocolParameters.
ProtocolParametersV3 ProtocolParametersType = iota
)
// ProtocolParameters defines the parameters of the protocol.
type ProtocolParameters interface {
// Version defines the version of the protocol running.
Version() Version
// NetworkName defines the human friendly name of the network.
NetworkName() string
// NetworkID defines the ID of the network which is derived from the network name.
NetworkID() NetworkID
// Bech32HRP defines the HRP prefix used for Bech32 addresses in the network.
Bech32HRP() NetworkPrefix
// StorageScoreParameters defines the storage score structure used by the given network.
StorageScoreParameters() *StorageScoreParameters
// WorkScoreParameters defines the work score parameters used by the given network.
WorkScoreParameters() *WorkScoreParameters
// ManaParameters defines the mana parameters used by the given network.
ManaParameters() *ManaParameters
// TokenSupply defines the current token supply on the network.
TokenSupply() BaseToken
// GenesisBlockID defines the block ID of the genesis block.
GenesisBlockID() BlockID
// GenesisSlot defines the slot of the genesis.
GenesisSlot() SlotIndex
// GenesisUnixTimestamp defines the genesis timestamp at which the slots start to count.
GenesisUnixTimestamp() int64
// SlotDurationInSeconds defines the duration of each slot in seconds.
SlotDurationInSeconds() uint8
// SlotsPerEpochExponent is the number of slots in an epoch expressed as an exponent of 2.
SlotsPerEpochExponent() uint8
StakingUnbondingPeriod() EpochIndex
ValidationBlocksPerSlot() uint8
PunishmentEpochs() EpochIndex
LivenessThresholdLowerBound() time.Duration
LivenessThresholdUpperBound() time.Duration
MinCommittableAge() SlotIndex
MaxCommittableAge() SlotIndex
// EpochNearingThreshold is used by the epoch orchestrator to detect the slot that should trigger a new committee
// selection for the next and upcoming epoch.
EpochNearingThreshold() SlotIndex
// CongestionControlParameters returns the parameters used to calculate reference Mana cost.
CongestionControlParameters() *CongestionControlParameters
VersionSignalingParameters() *VersionSignalingParameters
RewardsParameters() *RewardsParameters
TargetCommitteeSize() uint8
ChainSwitchingThreshold() uint8
Bytes() ([]byte, error)
Hash() (Identifier, error)
Equals(other ProtocolParameters) bool
}
// Sizer is an object knowing its own byte size.
type Sizer interface {
// Size returns the size of the object in terms of bytes.
Size() int
}