glow

package
v0.0.0-...-769be50 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
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

func GenerateSecureRandomInt(min, max int) int

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

func GenerateTestDir(testName string) string

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

func PubKeyToAddr(pk PublicKey) (string, error)

PubKeyToAddr will convert a PublicKey to its corresponding Ethereum Address.

func SendUDPReport

func SendUDPReport(report []byte, location string) error

SendUDPReport simulates sending a report to the server via UDP. The server should be listening on the given IP and port.

func TimeslotToUnix

func TimeslotToUnix(timeslot uint32) int64

TimeslotToUnix converts a timeslot to the unix timestamp that it began.

func UnixToTimeslot

func UnixToTimeslot(time int64) (uint32, error)

UnixToTimeslot converts a unix time to the current timeslot.

func Verify

func Verify(publicKey PublicKey, data []byte, signature Signature) bool

Verify checks the Ethereum signature for given data and a public key.

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 PrivateKey

type PrivateKey [32]byte

PrivateKey represents a 32-byte private key.

type PublicKey

type PublicKey [32]byte

PublicKey represents a 32-byte public key.

type SafeMu

type SafeMu struct {
	// contains filtered or unexported fields
}

SafeMu is an object that wraps sync.Mutex to help locate deadlocks.

func (*SafeMu) Lock

func (sm *SafeMu) Lock()

Wraps the sync.Mutex Lock() call with some logic that records the goroutine in a map, and then checks 20 seconds later if the goroutine is still holding the mutex.

func (*SafeMu) Unlock

func (sm *SafeMu) Unlock()

Unlock will delete this goroutine from the lock map so the sleeping goroutine will see that the mutex was released correctly.

type Signature

type Signature [64]byte

Signature represents a 64-byte signature.

func Sign

func Sign(data []byte, privateKey PrivateKey) Signature

Sign generates an Ethereum signature for given data using a private key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL