github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/tlog/tlog.go (about)

     1  // Copyright (c) 2021-2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package tlog
     6  
     7  import (
     8  	"github.com/google/trillian"
     9  	"github.com/google/trillian/merkle/rfc6962"
    10  	"github.com/google/trillian/types"
    11  )
    12  
    13  // Client provides an interface for interacting with a trillian log (tlog). It
    14  // creates an abstraction over the trillian provided TrillianLogClient and
    15  // TrillianAdminClient, providing a simplified client API and allowing for the
    16  // creation of an implementation that can be used during testing.
    17  type Client interface {
    18  	// Close closes the client connection.
    19  	Close()
    20  
    21  	// TreeNew creates a new tree.
    22  	TreeNew() (*trillian.Tree, *trillian.SignedLogRoot, error)
    23  
    24  	// TreeFreeze sets the status of a tree to frozen and returns the
    25  	// updated tree.
    26  	TreeFreeze(treeID int64) (*trillian.Tree, error)
    27  
    28  	// Tree returns a tree.
    29  	Tree(treeID int64) (*trillian.Tree, error)
    30  
    31  	// TreesAll returns all trees in the trillian instance.
    32  	TreesAll() ([]*trillian.Tree, error)
    33  
    34  	// LeavesAppend appends leaves onto a tree.
    35  	LeavesAppend(treeID int64, leaves []*trillian.LogLeaf) ([]QueuedLeafProof,
    36  		*types.LogRootV1, error)
    37  
    38  	// LeavesAll returns all leaves of a tree.
    39  	LeavesAll(treeID int64) ([]*trillian.LogLeaf, error)
    40  
    41  	// SignedLogRoot returns the signed log root for a tree.
    42  	SignedLogRoot(tree *trillian.Tree) (*trillian.SignedLogRoot,
    43  		*types.LogRootV1, error)
    44  
    45  	// InclusionProof returns a proof for the inclusion of a merkle
    46  	// leaf hash in a log root.
    47  	InclusionProof(treeID int64, merkleLeafHashe []byte,
    48  		lrv1 *types.LogRootV1) (*trillian.Proof, error)
    49  }
    50  
    51  // QueuedLeafProof contains the results of a leaf append command, i.e. the
    52  // QueuedLeaf and the inclusion proof for that leaf. If the append leaf command
    53  // fails the QueuedLeaf will contain an error code from the failure and the
    54  // Proof will not be present.
    55  type QueuedLeafProof struct {
    56  	QueuedLeaf *trillian.QueuedLogLeaf
    57  	Proof      *trillian.Proof
    58  }
    59  
    60  // NewLogLeaf returns a new trillian LogLeaf.
    61  func NewLogLeaf(leafValue []byte, extraData []byte) *trillian.LogLeaf {
    62  	return &trillian.LogLeaf{
    63  		LeafValue: leafValue,
    64  		ExtraData: extraData,
    65  	}
    66  }
    67  
    68  var (
    69  	// hasher contains the log hasher that trillian uses to compute the merkle
    70  	// leaf hash for a log leaf.
    71  	hasher = rfc6962.DefaultHasher
    72  )
    73  
    74  // MerkleLeafHash returns the merkle leaf hash for the provided leaf value.
    75  // This is the same merkle leaf hash that is calculated by trillian.
    76  func MerkleLeafHash(leafValue []byte) []byte {
    77  	return hasher.HashLeaf(leafValue)
    78  }