Documentation
¶
Index ¶
- Constants
- func CurrentTimeslot() uint32
- func GenerateKeyPair() (PublicKey, PrivateKey)
- func GenerateSecureRandomInt(min, max int) int
- func GenerateTestDir(testName string) string
- func PubKeyToAddr(pk PublicKey) (string, error)
- func SendUDPReport(report []byte, location string) error
- func TimeslotToUnix(timeslot uint32) int64
- func UnixToTimeslot(time int64) (uint32, error)
- func Verify(publicKey PublicKey, data []byte, signature Signature) bool
- type EquipmentAuthorization
- type EquipmentReport
- type EventLogger
- type LogEntry
- type PrivateKey
- type PublicKey
- type RateLimiter
- type SafeMu
- type Signature
Constants ¶
const ( // GenesisTime determines what counts as 'week 0' for the Glow // protocol. It has been set to Sunday, November 19th, 2023 at 0:00:00 // UTC. GenesisTime = 1700352000 )
Variables ¶
This section is empty.
Functions ¶
func CurrentTimeslot ¶
func CurrentTimeslot() uint32
Returns the current time of the protocol, as measured in 5 minute increments from genesis. This function implies a genesis time.
func GenerateKeyPair ¶
func GenerateKeyPair() (PublicKey, PrivateKey)
GenerateKeyPair generates a new ECDSA private and public key pair. The function panics if there is an error during key generation. It also makes sure that the public key has the prefix 0x02.
func GenerateSecureRandomInt ¶
GenerateSecureRandomInt generates a secure random integer between min and max (inclusive). It uses the crypto/rand package for secure number generation.
Returns: - int: The secure random integer. - error: Any error that occurs during the random number generation.
func GenerateTestDir ¶
GenerateTestDir generates a temporary directory path for placing test files. The directory will be located in the temp folder of the operating system. The path will include the name of the test, the UNIX timestamp, and a 6-digit random number. The function also creates the directory and returns the path.
Returns: - string: The path of the temporary directory.
func PubKeyToAddr ¶
PubKeyToAddr will convert a PublicKey to its corresponding Ethereum Address.
func SendUDPReport ¶
SendUDPReport simulates sending a report to the server via UDP. The server should be listening on the given IP and port.
func TimeslotToUnix ¶
TimeslotToUnix converts a timeslot to the unix timestamp that it began.
func UnixToTimeslot ¶
UnixToTimeslot converts a unix time to the current timeslot.
Types ¶
type EquipmentAuthorization ¶
type EquipmentAuthorization struct { // The equipment will refer to itself using its own ShortID to save // bandwidth when submitting reports to GCA servers. ShortID uint32 PublicKey PublicKey // The geographic location of the solar farm. Three decimals of // precision should be used. Latitude float64 Longitude float64 // Capacity is the maximum power output of the solar farm, expressed in // milliwatthours per timeslot. A timeslot is 5 minutes. Debt is the // number of grams of CO2 debt that must be paid off every week by a // solar farm. Expiration is the timeslot where power reports are no // longer valid for this solar farm. Capacity uint64 Debt uint64 Expiration uint32 // Initialization defines the moment when the ProtocolFee was paid and // the solar farm was allowed to begin submitting power reports to the // protocol. Initialization uint32 ProtocolFee uint64 // The Signature is a signature from the GCA which confirms that a // device with the above properties is allowed to submit power reports // to the Glow protocol. Signature Signature }
EquipmentAuthorization struct defines an authorization for a piece of equipment. It contains all of the genesis information for the equipment that a GCA will need to know when submitting reports for the corresponding solar farm.
func DeserializeEquipmentAuthorization ¶
func DeserializeEquipmentAuthorization(data []byte) (ea EquipmentAuthorization, err error)
DeserializeEquipmentAuthorization deserializes a byte slice into an EquipmentAuthorization.
This function takes a byte slice and deserializes it directly into an EquipmentAuthorization struct. The byte slice should be serialized in the same order as the fields in the EquipmentAuthorization struct. It returns the deserialized EquipmentAuthorization and any error encountered.
func (*EquipmentAuthorization) Serialize ¶
func (ea *EquipmentAuthorization) Serialize() []byte
Serialize serializes the EquipmentAuthorization into a byte slice.
func (*EquipmentAuthorization) SigningBytes ¶
func (ea *EquipmentAuthorization) SigningBytes() []byte
SigningBytes generates the byte slice that needs to be signed to validate the object. It's almost the same as the serialization, except that a signing prefix has been added, and the signature has been stripped off.
type EquipmentReport ¶
type EquipmentReport struct { ShortID uint32 // A unique identifier for the equipment Timeslot uint32 // A field denoting the time of the report PowerOutput uint64 // The power output from the equipment Signature Signature // A digital signature for the report's authenticity }
EquipmentReport defines the structure for a report received from a piece of equipment.
func DeserializeReport ¶
func DeserializeReport(i []byte) (EquipmentReport, error)
DeserializeReport takes a byte slice and attempts to convert it back into an EquipmentReport structure.
Returns an error if the byte slice is not the correct length.
func (EquipmentReport) Serialize ¶
func (er EquipmentReport) Serialize() []byte
Serialize creates a compact binary representation of the data structure.
func (EquipmentReport) SigningBytes ¶
func (er EquipmentReport) SigningBytes() []byte
SigningBytes returns the bytes that should be signed when sending an equipment report.
type EventLogger ¶
type EventLogger struct {
// contains filtered or unexported fields
}
EventLogger keeps track logged events and manages their size in memory, allowing for duplicate entries.
func NewEventLogger ¶
func NewEventLogger(logExpiry time.Duration, logMaxBytes, logMaxLineBytes int) *EventLogger
NewEventLogger creates an in-memory event logger with size limitations to protect the client from run-time failures.
func (*EventLogger) DumpLogEntries ¶
func (l *EventLogger) DumpLogEntries() (map[string][]time.Time, []string)
DumpLogEntries expires the logs and returns a map of logs to timestamps, and a time ordered slice of logs.
func (*EventLogger) ExpireLogs ¶
func (l *EventLogger) ExpireLogs(now time.Time)
ExpireLogs expires all logs, removing them after they have no unexpired timestamps.
func (*EventLogger) Printf ¶
func (l *EventLogger) Printf(format string, a ...interface{})
Add a timestamped event to the log, truncating and removing logs according to the EventLogger rules.
type LogEntry ¶
type LogEntry struct {
// contains filtered or unexported fields
}
LogEntry contains a logged string, and a list of time ordered timestamps.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
Rate limiter, to allow a maximum number of requests for a time duration.
func NewRateLimiter ¶
func NewRateLimiter(limit int, rate time.Duration) *RateLimiter
Create a new RateLimiter.
func (*RateLimiter) Allow ¶
func (r *RateLimiter) Allow() bool
Ask the rate limiter if a request is allowed. Returns true if allowed, false otherwise.
type SafeMu ¶
type SafeMu struct {
// contains filtered or unexported fields
}
SafeMu is an object that wraps sync.Mutex to help locate deadlocks.