common

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2020 License: GPL-3.0 Imports: 38 Imported by: 0

Documentation

Overview

common includes definition and interfaces common to several Archon Go packages

Index

Constants

View Source
const (
	Release = iota
	Beta
	Debug
)
View Source
const (
	Kilo        = 1000
	Mega        = Kilo * 1000
	Giga        = Mega * 1000
	Quintillion = Giga * Giga // 10^18
)
View Source
const (
	ArcProtocol     = "arc"
	ShardFileSuffix = "afs"
	HashFileSuffix  = "afh"
	WholeFileSuffix = "af"

	ContainsEndpoint   = "/contains"
	RetrieveEndpoint   = "/retrieve"
	SpProfilesEndpoint = "/spprofiles"
	StatsEndpoint      = "/stats"
	UploadEndpoint     = "/upload"
	DownloadEndpoint   = "/download"
)
View Source
const (
	//--------- for upload --------------
	UploadFileKey        = "uploadFile" // the key in form-data for the file path
	TransactionHashQuery = "txHash"
	OverwriteQuery       = "overwrite"
	HashUrlQuery         = "hashUrl" // request for hash URL (rather than named)
	ChainQuery           = "chain"   // Ethereum or Neo
	CloudDir             = "cloudDir"

	//--------- for download ----------------
	ShardIdxQuery  = "shardIdx"
	ArchonUrlQuery = "archonUrl"
)

Keys and queries

View Source
const ArchonHash = crypto.SHA3_256
View Source
const ArchonHashLen = 32 // Bytes
View Source
const ArchonSignatureLen = ecrypto.SignatureLength //65
View Source
const BootStrapNodeId = "QmNX6ASyukLch38D2Z1h4cMh39ATfqqDom1xJWv2YHc1eG"
View Source
const BuildConfig = Beta
View Source
const Localhost = "192.168.1.161" // Debugging only
View Source
const UrlsSep = "|"
View Source
const Version = "1.0"

Variables

View Source
var (
	// Defaults
	LogTrace   *log.Logger = log.New(os.Stderr, "Trace ", log.LstdFlags)
	LogDebug   *log.Logger = log.New(os.Stderr, "Debug ", log.LstdFlags)
	LogInfo    *log.Logger = log.New(os.Stderr, "Info ", log.LstdFlags)
	LogWarning *log.Logger = log.New(os.Stderr, "Warning ", log.LstdFlags)
	LogError   *log.Logger = log.New(os.Stderr, "Error ", log.LstdFlags)
)

Functions

func Abort

func Abort(err error)

Abort does nothing if err is nil, otherwise logs the error and exits

func AbortWithString

func AbortWithString(msg string)

func ArchonHashFromString

func ArchonHashFromString(hs string) ([]byte, error)

func ArchonHashString

func ArchonHashString(hash []byte) string

ArchonHashString is base58 encoded

func BasicAuthHandler

func BasicAuthHandler(h http.HandlerFunc) http.HandlerFunc

Leverages nemo's answer in http://stackoverflow.com/a/21937924/556573

func BigEndianUint32

func BigEndianUint32(val uint32) []byte

func BigEndianUint64

func BigEndianUint64(val int64) []byte

func BoolFromQuery

func BoolFromQuery(name string, r *http.Request) bool

BoolFromQuery return false if query is missing or set to false

func BytesToString

func BytesToString(data []byte) string

func CRC32

func CRC32(input []byte) []byte

func ChainHandlers

func ChainHandlers(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc

ChainHandlers provides a cleaner interface for chaining middleware for single routes. Middleware functions are simple HTTP handlers (w http.ResponseWriter, r *http.Request)

r.HandleFunc("/login", use(loginHandler, rateLimit, csrf))

See https://gist.github.com/elithrar/7600878#comment-955958 for how to extend it to suit simple http.Handler's

func CloneStringSlice

func CloneStringSlice(a []string) []string

func CompressString

func CompressString(s string) []byte

func Contains

func Contains(target string, list []string) bool

func CreateFile

func CreateFile(fullPath string) (*os.File, error)

func DefaultToExecutable

func DefaultToExecutable(path string) string

Default to executable returns full path. If input path is relative, it will considered relative to the the folder of the executable

func DirSize

func DirSize(path string) (int64, error)

DirSize returns the size the files in a folder and subfolders in bytes

func DivideRoundUp

func DivideRoundUp(numerator, divisor uint64) uint64

func DownloadFile

func DownloadFile(w io.Writer, url string) error

DownloadFile will download a file from a url. Assumes that url is a file download url

func EraseInt

func EraseInt(a []int, x int) []int

func EraseString

func EraseString(a []string, x string) []string

func ExtendedSliceToMultipleOf

func ExtendedSliceToMultipleOf(multiple int, input []byte) []byte

func FileExists

func FileExists(path string) bool

func FileSize

func FileSize(path string) int64

func FileSizeString

func FileSizeString(fullPath string) string

func FillRandom

func FillRandom(p []byte)

FillRandom fills a byte slice with random values

func FirstLiveUrl

func FirstLiveUrl(urls []string, port int) string

If port is non-zero it will be used for all the urls

func GetAllSeedUrls

func GetAllSeedUrls() []string

func GetAppConfiguration

func GetAppConfiguration(config interface{}) error

func GetArchonHash

func GetArchonHash(data []byte) []byte

func GetArchonHashOf

func GetArchonHashOf(r io.Reader) ([]byte, int64, error)

func GetArchonHashOfFile

func GetArchonHashOfFile(filePath string) ([]byte, int64, error)

func GetConfiguration

func GetConfiguration(config interface{}, path string) error

func GetFromSP

func GetFromSP(spUrl, endPoint, query string, timeout time.Duration) (contents string, err error)

func GetMultiAddressOf

func GetMultiAddressOf(ipOrDns string) (ma.Multiaddr, error)

func GetPassword

func GetPassword(prompt string, show bool) (password string)

func GetResponse

func GetResponse(resp *http.Response) (string, error)

GetResponse is read the http response body and returns text or error

func GetSeedUrls

func GetSeedUrls(wanted int) []string

func GetTempFile

func GetTempFile() (f *os.File, err error)

func I64ToA

func I64ToA(i int64) string

func IndexFromShardPath

func IndexFromShardPath(shp string) (index int, err error)

func InitLogging

func InitLogging(filePath string)

InitLogging initializes a rotating logger. This should be done once by the executable filePath is the full path of the log file

func InvalidArgs

func InvalidArgs(msg string)

func IsFullPath

func IsFullPath(path string) bool

func IsLegalFilePath

func IsLegalFilePath(path string) error

IsLegalFilePath checks for forbidden characters

func IsLegalUserName

func IsLegalUserName(userName string) error

IsLegalUserName checks for forbidden characters

func Join

func Join(dir, subpath string) string

func MakeFolders

func MakeFolders(folders []string) (err error)

func MakeStringOfLen

func MakeStringOfLen(length int) string

func Max

func Max(x, y int) int

Max returns the larger of x or y.

func MegaBytes

func MegaBytes(numBytes int64) int64

func Min

func Min(x, y int) int

Min returns the smaller of x or y.

func NeedArgs

func NeedArgs(arg string)

func NewArchonHash

func NewArchonHash() hash.Hash

NewArchonHash returns the hasher used by Archon code

func NumBytesDisplayString

func NumBytesDisplayString(s uint64) string

func NumLines

func NumLines(r io.Reader) (int, error)

func PickUrl

func PickUrl(urls []string, preferHttp bool) string

func PostFromReader

func PostFromReader(targetUrl string, r io.Reader, contentType string) (err error)

PostFromReader doest a POST to the targetUrl with data from r and reports an error, if any

func PostFromReaderWithProgress

func PostFromReaderWithProgress(targetUrl string, r io.Reader, contentType string, progress *ByteProgress) (err error)

func PromptForInput

func PromptForInput(prompt string) string

PromptForInput prompts on stdin and reads a line. Empty string is returned on error

func RandomInt

func RandomInt(lower, upperExclusive int) int

func RandomInt64

func RandomInt64(lower, upperExclusive int64) int64

func RandomIntFromSlice

func RandomIntFromSlice(a []int) int

func RandomIntRange

func RandomIntRange(n int) []int

RandomIntRange return n random ints in the range 0 to n-1

func RandomRange

func RandomRange(n, lower, upperExclusive int) []int

Returns range of n

func RandomStringFromSlice

func RandomStringFromSlice(a *[]string) string

RandomStringFromSlice returns a random string and removes it from the slice Empty string is returned once the slice is empty

func RawBytesToString

func RawBytesToString(data []byte) string

RawBytesToString returns a string without the 0x prefix

func ReadBigEndianInt32

func ReadBigEndianInt32(r io.Reader) (v int32, err error)

func ReadBigEndianInt64

func ReadBigEndianInt64(r io.Reader) (v int64, err error)

func ReadBigEndianUint32

func ReadBigEndianUint32(r io.Reader) (v uint32, err error)

func ReadBigEndianUint64

func ReadBigEndianUint64(r io.Reader) (v uint64, err error)

func ReadByte

func ReadByte(r io.Reader) (v byte, err error)

func ReadExactly

func ReadExactly(n int, r io.Reader) (data []byte, err error)

func Rewind

func Rewind(r io.ReadSeeker)

func RoundUp

func RoundUp(numToRound, multiple uint64) uint64

RoundUp rounds up. Amazing this does not exist in the standard packages

func SaveAppConfiguration

func SaveAppConfiguration(config interface{}) error

func SaveConfiguration

func SaveConfiguration(conf interface{}, path string) error

func SeedsPort

func SeedsPort() int

func SeparatedStringList

func SeparatedStringList(separator string, items ...interface{}) string

func SetLoggingLevel

func SetLoggingLevel(level LoggingLevel)

func SetLoggingLevelFromName

func SetLoggingLevelFromName(level string)

func StringKeysOf

func StringKeysOf(m map[string]bool) []string

func StringToBytes

func StringToBytes(s string) []byte

func StringToEthAddress

func StringToEthAddress(s string) (ea [20]byte)

func ToJsonString

func ToJsonString(data interface{}) string

func UnCompressString

func UnCompressString(b []byte) string

func WaitForCompletion

func WaitForCompletion(repeat, timeout time.Duration, task func() (interface{}, bool)) (interface{}, bool)

WaitForCompletion attempts task at repeat intervals until timeout or success (bool return is true)

func WriteLastLines

func WriteLastLines(w io.Writer, filepath string, numLines int)

WriteLastLines writes the last numLines from filepath to w

func WriteTempFile

func WriteTempFile(r io.Reader) string

WriteTempFile writes from the reader to a temp file and returns the path of the file or an empty string on error

func Yes

func Yes(prompt string) bool

Types

type ArchonHashBytes

type ArchonHashBytes [ArchonHashLen]byte

type ArchonSignature

type ArchonSignature [ArchonSignatureLen]byte

func NewArchonSignature

func NewArchonSignature(sig []byte) *ArchonSignature

func (*ArchonSignature) String

func (a *ArchonSignature) String() string

type ArchonUrl

type ArchonUrl struct {
	Username   string
	Permission UrlPermission
	Path       string // File path or hash
	Needed     int    // Min number of shard needed for reconstruction
	Total      int    // Total number of shards to upload
}

func NewArchonUrl

func NewArchonUrl(urlString string) (archonUrl *ArchonUrl, err error)

NewArchonUrl creates an Archon URL from the string representation

func (*ArchonUrl) DownloadUrl

func (a *ArchonUrl) DownloadUrl(shardIx int) string

func (*ArchonUrl) IsHash

func (a *ArchonUrl) IsHash() bool

func (*ArchonUrl) IsWholeFile

func (a *ArchonUrl) IsWholeFile() bool

func (*ArchonUrl) ShardPath

func (a *ArchonUrl) ShardPath(shardIx int) string

func (*ArchonUrl) ShardPaths

func (a *ArchonUrl) ShardPaths() (paths []string)

func (*ArchonUrl) String

func (a *ArchonUrl) String() string

func (*ArchonUrl) Tld

func (a *ArchonUrl) Tld() string

type ByteProgress

type ByteProgress struct {
	Total uint64
	// contains filtered or unexported fields
}

ByteProgress is needed when aggregating data from code running in parallel

func NewByteProgress

func NewByteProgress(prefix string, total uint64) *ByteProgress

func (*ByteProgress) End

func (bp *ByteProgress) End()

func (*ByteProgress) Progress

func (bp *ByteProgress) Progress(n uint64)

func (*ByteProgress) SetPrefix

func (bp *ByteProgress) SetPrefix(pref string)

type ContainsEpResponse

type ContainsEpResponse struct {
	ShardIdx []int `json:"shards"`
}

func NewContainsEpResponse

func NewContainsEpResponse(jsonData []byte) (*ContainsEpResponse, error)

func (*ContainsEpResponse) String

func (c *ContainsEpResponse) String() string

type EthAddress

type EthAddress ecommon.Address

func NewEthAddress

func NewEthAddress(a []byte) *EthAddress

type HashingWriter

type HashingWriter struct {
	ToData io.Writer
	ToHash hash.Hash
}

HashingWriter writes streams to both a ToData destination and the ToHash hasher

func NewHashingWriter

func NewHashingWriter(w io.Writer) *HashingWriter

func (*HashingWriter) GetHash

func (w *HashingWriter) GetHash() []byte

func (HashingWriter) Write

func (w HashingWriter) Write(p []byte) (n int, err error)

Implement io.Writer interface

type LoggingLevel

type LoggingLevel int
const (
	LogLevelUnknown LoggingLevel = iota
	LogLevelTrace
	LogLevelDebug
	LogLevelInfo
	LogLevelWarning
	LogLevelError
)

func GetLoggingLevel

func GetLoggingLevel() LoggingLevel

func (LoggingLevel) String

func (l LoggingLevel) String() string

type ProgressWriter

type ProgressWriter struct {
	Progress *ByteProgress
}

ProgressWriter counts the number of bytes written to it. It implements to the io.Reader interface and we can pass this into io.TeeReader() which will report progress on each Read cycle.

func (*ProgressWriter) Write

func (pw *ProgressWriter) Write(p []byte) (n int, err error)

Write only collects the number of bytes and calls Progress. It does not actually write

type RetrieveResponse

type RetrieveResponse struct {
	ArchonUrl string `json:"archon_url"`
	// Map from shard index to urls storing it
	Urls map[int][]string `json:"urls"`
}

func NewRetrieveResponse

func NewRetrieveResponse(jsonData []byte) (*RetrieveResponse, error)

func (*RetrieveResponse) String

func (r *RetrieveResponse) String() string

type SpProfile

type SpProfile struct {
	Urls Urls `json:"urls"`
	// The following for permissioned only
	Address string `json:"address"` // blockchain address
	// in the currency of the layer, per MByte per month
	// (for Eth: Wei/MByte, for Neo: Gas/MByte)
	MinAskPrice        int64   `json:"min_ask"`
	AvailableGigaBytes float64 `json:"available_giga_bytes"`
	PledgedGigaBytes   float64 `json:"pledged_giga_bytes"`
	NodeId             string  `json:"node_id"`
}

func (*SpProfile) AddressBytes

func (sp *SpProfile) AddressBytes() []byte

func (*SpProfile) Host

func (sp *SpProfile) Host() string

func (*SpProfile) IsValid

func (sp *SpProfile) IsValid() bool

func (*SpProfile) Url

func (sp *SpProfile) Url(preferHttp bool) string

type SpProfilesResponse

type SpProfilesResponse struct {
	Layer string      `json:"layer"`
	Sps   []SpProfile `json:"sps"`
}

func NewSpProfilesResponse

func NewSpProfilesResponse(jsonData string) (*SpProfilesResponse, error)

func (*SpProfilesResponse) String

func (r *SpProfilesResponse) String() string

type StorageProviders

type StorageProviders []SpProfile

func NewStorageProviders

func NewStorageProviders(num int) StorageProviders

func (*StorageProviders) Add

func (sps *StorageProviders) Add(sp *SpProfile)

func (*StorageProviders) Addresses

func (sps *StorageProviders) Addresses() (addr []string)

func (*StorageProviders) EthAddresses

func (sps *StorageProviders) EthAddresses() [][ecommon.AddressLength]byte

func (*StorageProviders) ForAllProfiles

func (sps *StorageProviders) ForAllProfiles(do func(*SpProfile))

func (*StorageProviders) Get

func (sps *StorageProviders) Get(i int) *SpProfile

func (*StorageProviders) GetOfAddress

func (sps *StorageProviders) GetOfAddress(addr string) *SpProfile

func (*StorageProviders) Hosts

func (sps *StorageProviders) Hosts() (hosts []string)

func (*StorageProviders) KeepOnly

func (sps *StorageProviders) KeepOnly(n int)

func (*StorageProviders) Num

func (sps *StorageProviders) Num() int

func (*StorageProviders) PickRandom

func (sps *StorageProviders) PickRandom(needed int) StorageProviders

pickRandom returns up to needed random SPs from input sps

func (*StorageProviders) PriceOfUpload

func (sps *StorageProviders) PriceOfUpload(numBytes int64) (totalPrice int64)

in currency of MinAskPrice, which depends on the blockchain for Eth it is Wei, for Neo it is Gas(int8)

func (*StorageProviders) Remove

func (sps *StorageProviders) Remove(removeThis func(profile *SpProfile) bool)

func (*StorageProviders) Set

func (sps *StorageProviders) Set(i int, sp *SpProfile)

func (*StorageProviders) SortByMinAsk

func (sps *StorageProviders) SortByMinAsk(granularity uint64)

Sort by min ask price

type TransactionHash

type TransactionHash [32]byte

Ethereum transaction

func NewTransactionHash

func NewTransactionHash(txHashString string) *TransactionHash

type UrlPermission

type UrlPermission string
const (
	Eth UrlPermission = "eth"
	Neo UrlPermission = "neo"
)

type Urls

type Urls struct {
	Host      string `json:"Host"`
	HttpPort  string `json:"http"`
	HttpsPort string `json:"https"`
}

Urls for SP. Can have http, https or both, but at least one

func NewUrls

func NewUrls(enc string) (*Urls, error)

func (*Urls) String

func (u *Urls) String() string

func (*Urls) Url

func (u *Urls) Url(preferHttp bool) string

Jump to

Keyboard shortcuts

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