Back to godoc.org

Package api

v0.2.16
Latest Go to latest

The highest tagged major version is .

Published: Oct 15, 2020 | License: AGPL-3.0 | Module: github.com/SouthbankSoftware/proofable

Index

Examples

Constants

const (

	// MetadataPrefix is the prefix for a metadata key
	MetadataPrefix = metadataPrefixHeader + metadataPrefixBody
	// MetadataSep is the separator for a metadata key
	MetadataSep = "/"
)

Variables

var (
	// DefaultGetFilePathKeyValueStreamConcurrency is the default concurrency for
	// GetFilePathKeyValueStream
	DefaultGetFilePathKeyValueStreamConcurrency uint32 = 4

	// FileExtensionTrie is the file extension for a trie
	FileExtensionTrie = ".proofable"
	// FileExtensionKeyValuesProof is the file extension for a key-values proof
	FileExtensionKeyValuesProof = ".subproofable"
	// FileExtensionDotGraph is the file extension for a Graphviz Dot Graph
	FileExtensionDotGraph = ".dot"

	// ErrFileSkipped is the error returned when a file is skipped
	ErrFileSkipped = errors.New("file skipped")
)

func CreateKeyValuesProof

func CreateKeyValuesProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	trieID,
	proofID string,
	filter *apiPB.KeyValuesFilter,
	outputPath string,
) (er error)

CreateKeyValuesProof creates a key-values proof for the provided key-values out of the given trie proof. When ProofID is zero, a new trie proof will be created on-the-fly

func CreateTrie

func CreateTrie(ctx context.Context, cli apiPB.APIServiceClient) (
	id, root string, er error)

CreateTrie creates a new empty trie

func CreateTrieProof

func CreateTrieProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	root string,
	anchorType anchorPB.Anchor_Type,
) (tp *apiPB.TrieProof, er error)

CreateTrieProof creates a trie proof for the given trie root. When root is zero (""), the current root hash of the trie will be used, and the request will be blocked until all ongoing updates are finished

func DeleteTrie

func DeleteTrie(ctx context.Context, cli apiPB.APIServiceClient, id string) error

DeleteTrie deletes the given trie

func DeleteTrieProof

func DeleteTrieProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	proofID string,
) (tp *apiPB.TrieProof, er error)

DeleteTrieProof deletes a proof for a trie root

func ExportTrie

func ExportTrie(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	outputPath string,
) error

ExportTrie exports the given trie

func GetEthTrieFromKeyValuesProof

func GetEthTrieFromKeyValuesProof(path string) (et *proof.EthTrie, er error)

GetEthTrieFromKeyValuesProof returns the EthTrie from the given key-values proof

func GetFilePathKeyMetadataKeyValues

func GetFilePathKeyMetadataKeyValues(key, fp string, de *godirwalk.Dirent) (
	kvs []*apiPB.KeyValue, er error)

GetFilePathKeyMetadataKeyValues gets the metadata key-values for a file path key

func GetFilePathKeyValueStream

func GetFilePathKeyValueStream(
	ctx context.Context,
	path string,
	concurrency uint32,
	ordered bool,
	onFilePathKey OnFilePathKeyFunc,
) (kvCH <-chan *apiPB.KeyValue, errCH <-chan error)

GetFilePathKeyValueStream returns a key value stream of the file path. When concurrency is 1 or ordered is true, the key-value stream is guaranteed to be sorted lexically by key; when concurrency is 0, DefaultGetFilePathKeyValueStreamConcurrency is used. In summary, the three working modes of the stream generator are:

1. parallel unordered processing: concurrency > 1, fastest speed
2. parallel ordered processing: concurrency > 1, normal speed
3. serial processing: ordered, concurrency == 1, slowest speed
Example

Code:

kvCH, errCH := GetFilePathKeyValueStream(
	context.Background(),
	"../../cmd/playground/",
	1,
	true,
	func(key, fp string, de *godirwalk.Dirent) (kvs []*apiPB.KeyValue, er error) {
		// use this callback to define the metadata embedding logic and ignored files

		kvs = []*apiPB.KeyValue{
			{Key: []byte(key + "/!metadata/2"), Value: []byte("value2")},
			{Key: []byte(key + "/!metadata/1"), Value: []byte("value1")},
		}
		return
	})

for kv := range kvCH {
	fmt.Printf("%s -> %x\n", kv.GetKey(), kv.GetValue())
}

err := <-errCH
if err != nil {
	log.Fatal(err)
}
./!metadata/1 -> 76616c756531
./!metadata/2 -> 76616c756532
playground.go -> 87768d49ee2a77adc0cc99f751cf7c9a2bcd15e547bbd78c3791ce1f46345b5f
playground.go/!metadata/1 -> 76616c756531
playground.go/!metadata/2 -> 76616c756532

func GetTrie

func GetTrie(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id string,
) (root string, er error)

GetTrie gets a trie

func GetTrieKeyValue

func GetTrieKeyValue(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	root string,
	key *apiPB.Key,
) (kv *apiPB.KeyValue, er error)

GetTrieKeyValue get a key-value of the trie at the given root. When root is zero (""), the current root hash of the trie will be used, and the request will be blocked until all ongoing updates are finished

func GetTrieKeyValues

func GetTrieKeyValues(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	root string,
) (kvCH <-chan *apiPB.KeyValue, errCH <-chan error)

GetTrieKeyValues gets the key-values of the trie at the given root. When root is zero (""), the current root hash of the trie will be used, and the request will be blocked until all ongoing updates are finished

func GetTrieProof

func GetTrieProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	trieID,
	proofID,
	trieRoot string,
) (tp *apiPB.TrieProof, er error)

GetTrieProof gets a trie proof by either proof ID or root. If by root, the latest proof of that root will be returned

func GetTrieProofs

func GetTrieProofs(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id string,
	filter *apiPB.RootFilter,
) (tpCH <-chan *apiPB.TrieProof, errCH <-chan error)

GetTrieProofs gets proofs of a trie

func GetTrieRoots

func GetTrieRoots(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id string,
	filter *apiPB.RootFilter,
) (trCH <-chan *apiPB.TrieRoot, errCH <-chan error)

GetTrieRoots gets roots of a trie. This is a series of roots showing the modification history of a trie

func GetTries

func GetTries(
	ctx context.Context,
	cli apiPB.APIServiceClient,
) (trCH <-chan *apiPB.Trie, errCH <-chan error)

GetTries gets all tries. Admin privilege is required

func ImportTrie

func ImportTrie(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	path string,
) (newID, root string, er error)

ImportTrie imports the trie data and creates a new trie. If ID is zero, a new trie ID will be generated, which is recommended when importing

func InterceptKeyValueStream

func InterceptKeyValueStream(
	ctx context.Context,
	input <-chan *apiPB.KeyValue,
	fn func(kv *apiPB.KeyValue) *apiPB.KeyValue,
) (output <-chan *apiPB.KeyValue)

InterceptKeyValueStream intercepts the key-value stream with the given callback function

func MarshalToKeyValues

func MarshalToKeyValues(keyPrefix string, v interface{}) (kvs []*apiPB.KeyValue, er error)

MarshalToKeyValues marshals the given value to key-values while respecting the JSON tags

func NewAPIClient

func NewAPIClient(hostPort string, creds credentials.PerRPCCredentials) (
	con *grpc.ClientConn, cli apiPB.APIServiceClient, er error)

NewAPIClient creates a new API client

func NormalizeKey

func NormalizeKey(key []byte) []byte

NormalizeKey normalizes the given key to make sure that the metadata prefix if presented has the `metadataPrefixHeader`

func SetTrieKeyValues

func SetTrieKeyValues(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	root string,
	rangeable interface{},
) (nextRoot string, er error)

SetTrieKeyValues sets the key-values to the trie. When root is zero (""), the current root hash of the trie will be used, and the request will be blocked until all ongoing updates are finished

func SetTrieRoot

func SetTrieRoot(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	root string,
) (er error)

SetTrieRoot sets the root of a trie to the given one. This will add an entry in the root history

func StripCompoundKeyAnchorTriePart

func StripCompoundKeyAnchorTriePart(
	kv *apiPB.KeyValue,
) *apiPB.KeyValue

StripCompoundKeyAnchorTriePart strips away the anchor trie part from the compound key. The anchor trie part of a key is added by Anchor Service after a successful anchoring

func SubscribeTrieProof

func SubscribeTrieProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	trieID,
	proofID string,
) (tpCH <-chan *apiPB.TrieProof, errCH <-chan error)

SubscribeTrieProof subscribes to the given trie proof

func UnmarshalFromKeyValues

func UnmarshalFromKeyValues(
	keyPrefix string,
	getKeyValue func() (*apiPB.KeyValue, error),
	v interface{},
) (er error)

UnmarshalFromKeyValues unmarshals the given value from the key-values while respecting the JSON tags

func VerifyKeyValuesProof

func VerifyKeyValuesProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	path string,
	outputKeyValues bool,
	dotGraphOutputPath string,
) (kvCH <-chan *apiPB.KeyValue, rpCH <-chan *apiPB.VerifyProofReply, errCH <-chan error)

VerifyKeyValuesProof verifies the given key-values proof. When dotGraphOutputPath is non-zero, a Graphviz Dot Graph will be output

func VerifyTrieProof

func VerifyTrieProof(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	proofID string,
	outputKeyValues bool,
	dotGraphOutputPath string,
) (kvCH <-chan *apiPB.KeyValue, rpCH <-chan *apiPB.VerifyProofReply, errCH <-chan error)

VerifyTrieProof verifies the given trie proof. When dotGraphOutputPath is non-zero, a Graphviz Dot Graph will be output

func WithAPIClient

func WithAPIClient(hostPort string, creds credentials.PerRPCCredentials,
	fn func(cli apiPB.APIServiceClient) error) error

WithAPIClient provides an API client to a closure that is automatically destroyed when done

func WithImportedTrie

func WithImportedTrie(
	ctx context.Context,
	cli apiPB.APIServiceClient,
	id,
	path string,
	fn func(id, root string) error) (er error)

WithImportedTrie provides a new imported trie to the closure that is automatically destroyed when done. If ID is zero, a new trie ID will be generated, which is recommended when importing

func WithTrie

func WithTrie(ctx context.Context, cli apiPB.APIServiceClient,
	fn func(id, root string) error) (er error)

WithTrie provides a new trie to the closure that is automatically destroyed when done

type FilePathKeyMetadata

type FilePathKeyMetadata struct {
	Size       int64       `json:"size"`
	Mode       os.FileMode `json:"mode"`
	ModTime    int64       `json:"modTime"`
	ChangeTime int64       `json:"changeTime,omitempty"`
	BirthTime  int64       `json:"birthTime,omitempty"`
}

FilePathKeyMetadata represents a file path key metadata

func GetFilePathMetadata

func GetFilePathMetadata(fp string) (
	md *FilePathKeyMetadata, er error)

GetFilePathMetadata gets the metadata for a file path key

type OnFilePathKeyFunc

type OnFilePathKeyFunc func(key, fp string, de *godirwalk.Dirent) (kvs []*apiPB.KeyValue, er error)

OnFilePathKeyFunc represents the function called when a file path key is about to be generated. The returned key-values will be added to the result stream, which can be used to generate metadata key-values. Those key-values should be prefixed by the key to indicate their parent hierarchy. Return an ErrFileSkipped to skip current file and the walk will continue on the rest

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier