util

package
v1.7.5 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: MIT Imports: 18 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLeafExists leaf has been computed, it can be skipped now
	ErrLeafExists = goErrors.New("merkle: leaf exists, it can be skipped")
	// ErrLeafNoSequenced leaf MUST be pushed one by one
	ErrLeafNoSequenced = goErrors.New("merkle: leaf must be pushed with sequence")
)
View Source
var (

	// ErrNoItem there is no item anymore
	ErrNoItem = errors.New("rand: there is no item anymore")
)

Functions

func GetRandom

func GetRandom(in []string, n int) []string

GetRandom returns n random slice from in

func Hash

func Hash(text string) string

Hash - the hashing used for the merkle tree construction

func HashStringToBytes

func HashStringToBytes(hash string) []byte

HashStringToBytes - convert a hex hash string to bytes

func MHash

func MHash(h1 string, h2 string) string

MHash - merkle hashing of a pair of child hashes

func MaxInt

func MaxInt(x, y int) int

func MaxInt64

func MaxInt64(x, y int64) int64

func MinInt

func MinInt(x, y int) int

func MinInt64

func MinInt64(x, y int64) int64

func Shuffle

func Shuffle(in []string) (shuffle []string)

func ToHex

func ToHex(buf []byte) string

ToHex - converts a byte array to hex encoding with upper case

func VerifyMerklePath

func VerifyMerklePath(hash string, path *MTPath, root string) bool

Types

type CompactMerkleTree added in v1.2.88

type CompactMerkleTree struct {
	Tree        []string                        `json:"tree"` //node tree with computed as many parent hashes as it can
	Hash        func(left, right string) string `json:"-"`    //it should be set once CompactMerkleTree is created
	Initialized bool                            `json:"initialized"`
	LastIndex   int                             `json:"last_index"` //how many leaves has been pushed
}

CompactMerkleTree it is a stateful algorithm. It takes data in (leaf nodes), hashes it, and computes as many parent hashes as it can. see detail on https://github.com/0chain/blobber/wiki/Protocols#what-is-compactmerkletree

func NewCompactMerkleTree added in v1.2.88

func NewCompactMerkleTree(hash func(left, right string) string) *CompactMerkleTree

NewCompactMerkleTree create a CompactMerkleTree with specify hash method

func (*CompactMerkleTree) AddDataBlocks added in v1.2.88

func (cmt *CompactMerkleTree) AddDataBlocks(buf []byte, index int) error

AddLeaf add leaf hash and update the the Merkle tree.

func (*CompactMerkleTree) AddLeaf added in v1.2.88

func (cmt *CompactMerkleTree) AddLeaf(leaf string, index int) error

AddLeaf add leaf hash and update the the Merkle tree.

func (*CompactMerkleTree) GetMerkleRoot added in v1.2.88

func (cmt *CompactMerkleTree) GetMerkleRoot() string

GetMerkleRoot calculate the Merkle root when all leave has been added, For the last, lowest-level hash, we hash it with itself. From there, the nodes are hashed to the top level to calculate the Merkle root.

func (*CompactMerkleTree) Reload added in v1.2.88

func (cmt *CompactMerkleTree) Reload(chunkSize int64, reader io.Reader) error

Reload reset and reload leaves from io.Reader

type FixedMerkleTree added in v1.2.88

type FixedMerkleTree struct {
	// ChunkSize size of chunk
	ChunkSize int `json:"chunk_size,omitempty"`
	// Leaves a leaf is a CompactMerkleTree for 1/1024 shard data
	Leaves []*CompactMerkleTree `json:"leaves,omitempty"`
}

FixedMerkleTree A trusted mekerl tree for outsourcing attack protection. see section 1.8 on whitepager see detail on https://github.com/0chain/blobber/wiki/Protocols#what-is-fixedmerkletree

func NewFixedMerkleTree added in v1.2.88

func NewFixedMerkleTree(chunkSize int) *FixedMerkleTree

NewFixedMerkleTree create a FixedMerkleTree with specify hash method

func (*FixedMerkleTree) GetMerkleRoot added in v1.2.88

func (fmt *FixedMerkleTree) GetMerkleRoot() string

GetMerkleRoot get merkle root

func (*FixedMerkleTree) GetMerkleTree added in v1.2.88

func (fmt *FixedMerkleTree) GetMerkleTree() MerkleTreeI

GetMerkleRoot get merkle tree

func (*FixedMerkleTree) Reload added in v1.2.88

func (fmt *FixedMerkleTree) Reload(reader io.Reader) error

Reload reset and reload leaves from io.Reader

func (*FixedMerkleTree) Write added in v1.2.88

func (fmt *FixedMerkleTree) Write(buf []byte, chunkIndex int) error

type GetRequest

type GetRequest struct {
	*PostRequest
}

func NewHTTPGetRequest

func NewHTTPGetRequest(url string) (*GetRequest, error)

NewHTTPGetRequest create a GetRequest instance with 60s timeout

func NewHTTPGetRequestContext

func NewHTTPGetRequestContext(ctx context.Context, url string) (*GetRequest, error)

NewHTTPGetRequestContext create a GetRequest with context and url

func (*GetRequest) Get

func (r *GetRequest) Get() (*GetResponse, error)

type GetResponse

type GetResponse struct {
	*PostResponse
}

type Hashable

type Hashable interface {
	GetHash() string
	GetHashBytes() []byte
}

Hashable - anything that can provide it's hash

type HttpClient

type HttpClient interface {
	Do(req *http.Request) (*http.Response, error)
}
var Client HttpClient

type MTPath

type MTPath struct {
	Nodes     []string `json:"nodes"`
	LeafIndex int      `json:"leaf_index"`
}

MTPath - The merkle tree path

type MerkleTree

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

MerkleTree - A data structure that implements MerkleTreeI interface

func (*MerkleTree) ComputeTree

func (mt *MerkleTree) ComputeTree(hashes []Hashable)

ComputeTree - given the leaf nodes, compute the merkle tree

func (*MerkleTree) GetLeafIndex

func (mt *MerkleTree) GetLeafIndex(hash Hashable) int

GetLeafIndex - Get the index of the leaf node in the tree

func (*MerkleTree) GetPath

func (mt *MerkleTree) GetPath(hash Hashable) *MTPath

GetPath - get the path that can be used to verify the merkle tree

func (*MerkleTree) GetPathByIndex

func (mt *MerkleTree) GetPathByIndex(idx int) *MTPath

GetPathByIndex - get the path of a leaf node at index i

func (*MerkleTree) GetRoot

func (mt *MerkleTree) GetRoot() string

GetRoot - get the root of the merkle tree

func (*MerkleTree) GetTree

func (mt *MerkleTree) GetTree() []string

GetTree - get the entire merkle tree

func (*MerkleTree) SetTree

func (mt *MerkleTree) SetTree(leavesCount int, tree []string) error

SetTree - set the entire merkle tree

func (*MerkleTree) VerifyPath

func (mt *MerkleTree) VerifyPath(hash Hashable, path *MTPath) bool

VerifyPath - given a leaf node and the path, verify that the node is part of the tree

type MerkleTreeI

type MerkleTreeI interface {
	//API to create a tree from leaf nodes
	ComputeTree(hashes []Hashable)
	GetRoot() string
	GetTree() []string

	//API to load an existing tree
	SetTree(leavesCount int, tree []string) error

	// API for verification when the leaf node is known
	GetPath(hash Hashable) *MTPath               // Server needs to provide this
	VerifyPath(hash Hashable, path *MTPath) bool //This is only required by a client but useful for testing

	/* API for random verification when the leaf node is uknown
	(verification of the data to hash used as leaf node is outside this API) */
	GetPathByIndex(idx int) *MTPath
}

MerkleTreeI - a merkle tree interface required for constructing and providing verification

type PostRequest

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

func NewHTTPPostRequest

func NewHTTPPostRequest(url string, data interface{}) (*PostRequest, error)

func (*PostRequest) Post

func (r *PostRequest) Post() (*PostResponse, error)

type PostResponse

type PostResponse struct {
	Url        string
	StatusCode int
	Status     string
	Body       string
}

type Rand added in v1.2.82

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

Rand a progressive rand

func NewRand added in v1.2.82

func NewRand(max int) Rand

NewRand create a ProgressiveRand instance

func (*Rand) Next added in v1.2.82

func (r *Rand) Next() (int, error)

Next get next random item

type SecureSerializableValue

type SecureSerializableValue struct {
	Buffer []byte
}

SecureSerializableValue - a proxy persisted value that just tracks the encoded bytes of a persisted value

func (*SecureSerializableValue) Decode

func (spv *SecureSerializableValue) Decode(buf []byte) error

Decode - implement interface

func (*SecureSerializableValue) Encode

func (spv *SecureSerializableValue) Encode() []byte

Encode - implement interface

func (*SecureSerializableValue) GetHash

func (spv *SecureSerializableValue) GetHash() string

GetHash - implement interface

func (*SecureSerializableValue) GetHashBytes

func (spv *SecureSerializableValue) GetHashBytes() []byte

GetHashBytes - implement interface

type SecureSerializableValueI

type SecureSerializableValueI interface {
	Serializable
	Hashable
}

SecureSerializableValueI an interface that makes a serializable value secure with hashing

type Serializable

type Serializable interface {
	Encode() []byte
	Decode([]byte) error
}

Serializable interface

type StringHashable

type StringHashable struct {
	Hash string
}

func NewStringHashable

func NewStringHashable(hash string) *StringHashable

func (*StringHashable) GetHash

func (sh *StringHashable) GetHash() string

func (*StringHashable) GetHashBytes

func (sh *StringHashable) GetHashBytes() []byte

Jump to

Keyboard shortcuts

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