Skip to content

Instantly share code, notes, and snippets.

@jsteenb2
Last active July 16, 2022 19:43
Show Gist options
  • Save jsteenb2/8ad075b64953673dd3002371633734ee to your computer and use it in GitHub Desktop.
Save jsteenb2/8ad075b64953673dd3002371633734ee to your computer and use it in GitHub Desktop.
Tendermint Mempool Example
package mempool
import (
"context"
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/types"
)
// TXReaper is the output of a Reap query for a mempool implemenation. Examples of
// this are for hte existing mempools, where we return Txs, and the DAG mempools which
// use DAG
type TXReaper interface {
// TXs retrieves TXs for the given DAGCerts. It will take TXs from the
// RootCert and ExtraCerts in addition to all the TXs from the descandant
// certificates of the RootCert.
TXs(ctx context.Context) (types.Txs, error)
}
// MempoolV2 represents the bare minimum functionality for a mempool. It does
// not line up exactly with v1 of the mempool. Note that V1 can be updated to
// identify with this new interface and then add its additional concerns.
// TODO(berg): need a better name
type MempoolV2[T TXReaper] interface {
// CheckTx executes a new transaction against the application to determine
// its validity and whether it should be added to the mempool.
CheckTx(ctx context.Context, tx types.Tx, callback func(*abci.Response), txInfo TxInfo) error
// Reap allows the caller to reap certificate digests used for proposal. Callers
// can choose whatever predicate suits their needs.
Reap(ctx context.Context, opts types.ReapOpts) (T, error)
// AfterBlockFinality informs the mempool that the given input were committed and
// the mempool may discard/tombstone/etc the targets as needed.
AfterBlockFinality(blockHeight int64, input T, deliverTxResponses []*abci.ResponseDeliverTx) error
}
// ReapOpts is the options to reaping a collection useful to proposal from the mempool.
// A collection for proposal in teh Clist or Priority mempools, would be a list of TXs.
// For a DAG (narwhal) mempool, we'd want the DAGCerts for the proposal. However,
// we use the same predicates to reap from all mempools. When a predicate has multiple opts
// specified, will take the collections that satisfy all limits specified are adhered too.
//
// Example predicate: BlockSizeLimit AND NumTXs are both set enforcing that BlockSizeLimit
// and NumTXs limits are satisfied in the Reaping.
type ReapOpts struct {
BlockSizeLimit int64
GasLimit int64
NumTXs int64
}
// OK validates the options provided.
func (r ReapOpts) OK() error {
if r.BlockSizeLimit <= 0 && r.NumTXs <= 0 && r.GasLimit <= 0 {
return fmt.Errorf("at least one of [BlockSizeLimit Gaslimit NumTXs] must be greater than 0")
}
return nil
}
package narwhalc
import (
context
github.com/PeggyJV/tendermint/types
)
// CertificateDigest represents a certificate digest of a collection/batch/list of TXs that compose
// list of TXs. Often referred to as collection ID in the narwhal world.
type CertificateDigest []byte
// DAGCerts represents the certificate digests from a mempool DAG. We traverse
// DAG starting from the RootCert, obtaining all the descendants from it. In
// addition, we make room for ExtraCerts, which will fit into the block, but are not reachable
// from the RootCert provided.
type DAGCerts struct {
// RootCert represents the root cert to traverse the DAG.
RootCert CertificateDigest
// ExtraCerts represent additional certs that are not reachable from the RootCert
// to be included in the block proposal. This is useful when you have the capacity
// to add more certificates, are unable to add all certificates for the RootCert's
// parent node.
ExtraCerts []CertificateDigest
client interface {
DAGCertTXs(ctx context.Context, certs DAGCerts) (types.Txs, error)
}
}
// TXs returns all TXs via walking the DAG from teh RootCert and combines
// TXs from the ExtraCerts.
func (d DAGCerts) TXs(ctx context.Context) (types.Txs, error) {
return d.client.DAGCertTXs(ctx, d)
}
package types
import (
context
)
// Txs is a slice of Tx.
type Txs []Tx
// TXs returns the transactions.
func (txs Txs) TXs(_ context.Context) (Txs, error) {
return txs, nil
}
// ... trim the rest of the Txs type's methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment