Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update backend to use BackendMempool interface instead of DeSoMempool #513

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/fatih/structs v1.1.0
github.com/golang-jwt/jwt/v4 v4.1.0
github.com/golang/glog v1.0.0
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.8.0
github.com/h2non/bimg v1.1.5
github.com/holiman/uint256 v1.1.1
Expand Down Expand Up @@ -68,6 +67,7 @@ require (
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v2.0.0+incompatible // indirect
github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.8 // indirect
Expand Down
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
Loading