Skip to content

Commit

Permalink
Update backend to use BackendMempool interface instead of DeSoMempool
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazy Nina committed Dec 5, 2023
1 parent c40453c commit 0d4898a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 182 deletions.
156 changes: 0 additions & 156 deletions go.sum

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions routes/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func _headerToResponse(header *lib.MsgDeSoHeader, hash string) *HeaderResponse {
PrevBlockHashHex: header.PrevBlockHash.String(),
TransactionMerkleRootHex: header.TransactionMerkleRoot.String(),
TstampSecs: header.GetTstampSecs(),
TstampNanoSecs: header.TstampNanoSecs,
Height: header.Height,
Nonce: header.Nonce,
ExtraNonce: header.ExtraNonce,
Expand Down Expand Up @@ -968,11 +969,7 @@ func (fes *APIServer) APITransactionInfo(ww http.ResponseWriter, rr *http.Reques
// IsMempool means we should just return all of the transactions that are currently in the mempool.
if transactionInfoRequest.IsMempool {
// Get all the txns from the mempool.
poolTxns, _, err := fes.mempool.GetTransactionsOrderedByTimeAdded()
if err != nil {
APIAddError(ww, fmt.Sprintf("APITransactionInfo: Error getting txns from mempool: %v", err))
return
}
poolTxns := fes.mempool.GetOrderedTransactions()

res := &APITransactionInfoResponse{}
res.Transactions = []*TransactionResponse{}
Expand Down Expand Up @@ -1039,7 +1036,7 @@ func (fes *APIServer) APITransactionInfo(ww http.ResponseWriter, rr *http.Reques

if txn == nil {
// Try to look the transaction up in the mempool before giving up.
txnInPool := fes.mempool.GetTransaction(txID)
txnInPool := fes.mempool.GetMempoolTx(txID)
if txnInPool == nil {
APIAddError(ww, fmt.Sprintf("APITransactionInfo: Could not find transaction with TransactionIDBase58Check = %s",
transactionInfoRequest.TransactionIDBase58Check))
Expand Down Expand Up @@ -1154,11 +1151,7 @@ func (fes *APIServer) APITransactionInfo(ww http.ResponseWriter, rr *http.Reques
if transactionInfoRequest.LastPublicKeyTransactionIndex <= 0 {

// Start with the mempool
poolTxns, _, err := fes.mempool.GetTransactionsOrderedByTimeAdded()
if err != nil {
APIAddError(ww, fmt.Sprintf("APITransactionInfo: Error getting txns from mempool: %v", err))
return
}
poolTxns := fes.mempool.GetOrderedTransactions()

// Go from most recent to least recent
// TODO: Support pagination for mempool transactions
Expand Down Expand Up @@ -1270,6 +1263,9 @@ type HeaderResponse struct {
// The unix timestamp (in seconds) specifying when this block was
// mined.
TstampSecs uint64
// The unix timestamp (in nanoseconds) specifying when this block was
// mined.
TstampNanoSecs uint64
// The height of the block this header corresponds to.
Height uint64

Expand Down
14 changes: 6 additions & 8 deletions routes/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/deso-protocol/backend/config"
coreCmd "github.com/deso-protocol/core/cmd"
"github.com/deso-protocol/core/lib"
"github.com/google/uuid"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -54,7 +52,7 @@ func CleanUpBadger(db *badger.DB) {
}

func GetTestBadgerDb(t *testing.T) (_db *badger.DB, _dir string) {
dir, err := ioutil.TempDir("", "badgerdb-"+uuid.New().String())
dir, err := os.MkdirTemp("", "badgerdb")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -135,9 +133,9 @@ func newTestAPIServer(t *testing.T, globalStateRemoteNode string, txindex bool)
privateApiServer.initState()

miner := node.Server.GetMiner()
_, err = miner.MineAndProcessSingleBlock(0, node.Server.GetMempool())
_, err = miner.MineAndProcessSingleBlock(0, node.Server.GetMempool().(*lib.DeSoMempool))
require.NoError(err)
_, err = miner.MineAndProcessSingleBlock(0, node.Server.GetMempool())
_, err = miner.MineAndProcessSingleBlock(0, node.Server.GetMempool().(*lib.DeSoMempool))
require.NoError(err)

t.Cleanup(func() {
Expand Down Expand Up @@ -664,7 +662,7 @@ func TestAPI(t *testing.T) {
txn1 := &lib.MsgDeSoTxn{}
txn1Bytes, _ := hex.DecodeString(txn1Hex)
_ = txn1.FromBytes(txn1Bytes)
_, err := apiServer.mempool.ProcessTransaction(
_, err := apiServer.mempool.(*lib.DeSoMempool).ProcessTransaction(
txn1, false /*allowOrphan*/, true /*rateLimit*/, 0, /*peerID*/
true /*verifySignatures*/)
require.NoError(err)
Expand Down Expand Up @@ -779,7 +777,7 @@ func TestAPI(t *testing.T) {
txn2 := &lib.MsgDeSoTxn{}
txn2Bytes, _ := hex.DecodeString(txn2Hex)
_ = txn2.FromBytes(txn2Bytes)
apiServer.mempool.ProcessTransaction(
apiServer.mempool.(*lib.DeSoMempool).ProcessTransaction(
txn2, false /*allowOrphan*/, true /*rateLimit*/, 0, /*peerID*/
true /*verifySignatures*/)
{
Expand Down Expand Up @@ -961,7 +959,7 @@ func TestAPI(t *testing.T) {
}

// Mine a block and check the balances.
block3, err := miner.MineAndProcessSingleBlock(0 /*threadIndex*/, apiServer.mempool)
block3, err := miner.MineAndProcessSingleBlock(0 /*threadIndex*/, apiServer.mempool.(*lib.DeSoMempool))
require.NoError(err)
balSum := int64(0)
{
Expand Down
2 changes: 1 addition & 1 deletion routes/hot_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (fes *APIServer) UpdateHotFeedOrderedList(
// Create new "block" for mempool txns, give it a block age of 1 greater than the current tip

// First get all MempoolTxns from mempool.
mempoolTxnsOrderedByTime, _, err := fes.backendServer.GetMempool().GetTransactionsOrderedByTimeAdded()
mempoolTxnsOrderedByTime := fes.backendServer.GetMempool().GetOrderedTransactions()
// Extract MsgDesoTxn from each MempoolTxn
var txnsFromMempoolOrderedByTime []*lib.MsgDeSoTxn
for _, mempoolTxn := range mempoolTxnsOrderedByTime {
Expand Down
4 changes: 2 additions & 2 deletions routes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ const (
// frontend cares about, from posts to profiles to purchasing DeSo with Bitcoin.
type APIServer struct {
backendServer *lib.Server
mempool *lib.DeSoMempool
mempool lib.Mempool
blockchain *lib.Blockchain
blockProducer *lib.DeSoBlockProducer
Params *lib.DeSoParams
Expand Down Expand Up @@ -475,7 +475,7 @@ type LastTradePriceHistoryItem struct {
// NewAPIServer ...
func NewAPIServer(
_backendServer *lib.Server,
_mempool *lib.DeSoMempool,
_mempool lib.Mempool,
_blockchain *lib.Blockchain,
_blockProducer *lib.DeSoBlockProducer,
txIndex *lib.TXIndex,
Expand Down
5 changes: 1 addition & 4 deletions routes/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2535,10 +2535,7 @@ func (fes *APIServer) _getMempoolNotifications(request *GetNotificationsRequest,
//
// TODO(performance): This could get slow if the mempool gets big. Fix is to organize everything
// in the mempool by public key and only look up transactions that are relevant to this public key.
poolTxns, _, err := fes.mempool.GetTransactionsOrderedByTimeAdded()
if err != nil {
return nil, errors.Errorf("APITransactionInfo: Error getting txns from mempool: %v", err)
}
poolTxns := fes.mempool.GetOrderedTransactions()

mempoolTxnMetadata := []*TransactionMetadataResponse{}
for _, poolTx := range poolTxns {
Expand Down

0 comments on commit 0d4898a

Please sign in to comment.