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 dff0eb7 commit 79b8c9d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 293 deletions.
156 changes: 0 additions & 156 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion routes/access_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func ExecuteRequest(t *testing.T, apiServer *APIServer, routePath string, reques
func TestAPIAccessGroupBaseGroupMembership(t *testing.T) {
assert := assert.New(t)

apiServer, _, _ := newTestAPIServer(t, "" /*globalStateRemoteNode*/)
apiServer, _, _ := newTestAPIServer(t, "" /*globalStateRemoteNode*/, false)

// form the request for RoutePathGetAllUserAccessGroups
values := GetAccessGroupsRequest{PublicKeyBase58Check: senderPkString}
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
126 changes: 11 additions & 115 deletions routes/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
chainlib "github.com/btcsuite/btcd/blockchain"
"github.com/deso-protocol/backend/config"
coreCmd "github.com/deso-protocol/core/cmd"
"github.com/google/uuid"
"github.com/deso-protocol/core/lib"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/deso-protocol/backend/config"
"github.com/deso-protocol/core/lib"

"github.com/dgraph-io/badger/v3"

Expand Down Expand Up @@ -57,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 All @@ -78,106 +73,7 @@ func GetTestBadgerDb(t *testing.T) (_db *badger.DB, _dir string) {
return db, dir
}

func NewLowDifficultyBlockchain(t *testing.T) (*lib.Blockchain, *lib.DeSoParams, *badger.DB, string) {

// Set the number of txns per view regeneration to one while creating the txns
lib.ReadOnlyUtxoViewRegenerationIntervalTxns = 1

return NewLowDifficultyBlockchainWithParams(t, &lib.DeSoTestnetParams)
}

func NewLowDifficultyBlockchainWithParams(t *testing.T, params *lib.DeSoParams) (
*lib.Blockchain, *lib.DeSoParams, *badger.DB, string) {

// Set the number of txns per view regeneration to one while creating the txns
lib.ReadOnlyUtxoViewRegenerationIntervalTxns = 1

db, dir := GetTestBadgerDb(t)
timesource := chainlib.NewMedianTime()

// Set some special parameters for testing. If the blocks above are changed
// these values should be updated to reflect the latest testnet values.
paramsCopy := *params
paramsCopy.GenesisBlock = &lib.MsgDeSoBlock{
Header: &lib.MsgDeSoHeader{
Version: 0,
PrevBlockHash: lib.MustDecodeHexBlockHash("0000000000000000000000000000000000000000000000000000000000000000"),
TransactionMerkleRoot: lib.MustDecodeHexBlockHash("097158f0d27e6d10565c4dc696c784652c3380e0ff8382d3599a4d18b782e965"),
Height: uint64(0),
Nonce: uint64(0),
// No ExtraNonce is set in the genesis block
},
Txns: []*lib.MsgDeSoTxn{
{
TxInputs: []*lib.DeSoInput{},
TxOutputs: []*lib.DeSoOutput{},
TxnMeta: &lib.BlockRewardMetadataa{
ExtraData: []byte("They came here, to the new world. World 2.0, version 1776."),
},
// A signature is not required for BLOCK_REWARD transactions since they
// don't spend anything.
},
},
}
paramsCopy.GenesisBlock.Header.SetTstampSecs(uint64(1560735050))
paramsCopy.MinDifficultyTargetHex = "999999948931e5874cf66a74c0fda790dd8c7458243d400324511a4c71f54faa"
paramsCopy.MinChainWorkHex = "0000000000000000000000000000000000000000000000000000000000000000"
paramsCopy.MiningIterationsPerCycle = 500
// Set maturity to 2 blocks so we can test spending on short chains. The
// tests rely on the maturity equaling exactly two blocks (i.e. being
// two times the time between blocks).
paramsCopy.TimeBetweenBlocks = 2 * time.Second
paramsCopy.BlockRewardMaturity = time.Second * 4
paramsCopy.TimeBetweenDifficultyRetargets = 100 * time.Second
paramsCopy.MaxDifficultyRetargetFactor = 2
paramsCopy.SeedBalances = []*lib.DeSoOutput{
{
PublicKey: lib.MustBase58CheckDecode(moneyPkString),
AmountNanos: uint64(2000000 * lib.NanosPerUnit),
},
}

// Temporarily modify the seed balances to make a specific public
// key have some DeSo
chain, err := lib.NewBlockchain([]string{blockSignerPk}, 0, 0,
&paramsCopy, timesource, db, nil, nil, nil, false)
if err != nil {
log.Fatal(err)
}

return chain, &paramsCopy, db, dir
}

func NewTestMiner(t *testing.T, chain *lib.Blockchain, params *lib.DeSoParams, isSender bool) (*lib.DeSoMempool, *lib.DeSoMiner) {
assert := assert.New(t)
require := require.New(t)
_ = assert
_ = require

mempool := lib.NewDeSoMempool(
chain, 0, /* rateLimitFeeRateNanosPerKB */
0 /* minFeeRateNanosPerKB */, "", true,
"" /*dataDir*/, "", true)
minerPubKeys := []string{}
if isSender {
minerPubKeys = append(minerPubKeys, senderPkString)
} else {
minerPubKeys = append(minerPubKeys, recipientPkString)
}

blockProducer, err := lib.NewDeSoBlockProducer(
0, 1,
blockSignerSeed,
mempool, chain,
params, nil)
require.NoError(err)

newMiner, err := lib.NewDeSoMiner(minerPubKeys, 1 /*numThreads*/, blockProducer, params)
require.NoError(err)
return mempool, newMiner
}

func newTestAPIServer(t *testing.T, globalStateRemoteNode string) (*APIServer, *APIServer, *lib.DeSoMiner) {
func newTestAPIServer(t *testing.T, globalStateRemoteNode string, txindex bool) (*APIServer, *APIServer, *lib.DeSoMiner) {
assert := assert.New(t)
require := require.New(t)
_, _ = assert, require
Expand All @@ -202,7 +98,7 @@ func newTestAPIServer(t *testing.T, globalStateRemoteNode string) (*APIServer, *
coreConfig := coreCmd.LoadConfig()
coreConfig.Params = &lib.DeSoTestnetParams
coreConfig.DataDirectory = badgerDir
coreConfig.TXIndex = true
coreConfig.TXIndex = txindex
coreConfig.MinerPublicKeys = []string{senderPkString}
coreConfig.NumMiningThreads = 1
coreConfig.HyperSync = false
Expand Down Expand Up @@ -237,9 +133,9 @@ func newTestAPIServer(t *testing.T, globalStateRemoteNode string) (*APIServer, *
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 All @@ -257,7 +153,7 @@ func TestAPI(t *testing.T) {
require := require.New(t)
_, _ = assert, require

apiServer, _, miner := newTestAPIServer(t, "" /*globalStateRemoteNode*/)
apiServer, _, miner := newTestAPIServer(t, "" /*globalStateRemoteNode*/, true)

{
request, _ := http.NewRequest("GET", RoutePathAPIBase, nil)
Expand Down Expand Up @@ -766,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 @@ -881,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 @@ -1063,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
6 changes: 3 additions & 3 deletions routes/global_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestGlobalStateServicePutGetDeleteWithDB(t *testing.T) {
_, _ = assert, require

apiServer, _, _ := newTestAPIServer(
t, "" /*globalStateRemoteNode*/)
t, "" /*globalStateRemoteNode*/, false)

// Getting when no value is present should return nil without an
// error.
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestGlobalStateServicePutGetDeleteWithRemoteNode(t *testing.T) {
_, _ = assert, require

apiServer, _, _ := newTestAPIServer(
t, "" /*globalStateRemoteNode*/)
t, "" /*globalStateRemoteNode*/, false)

// Getting when no value is present should return nil without an
// error.
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestGlobalStateServiceURLCreation(t *testing.T) {
_, _ = assert, require

apiServer, _, _ := newTestAPIServer(
t, "https://deso.com:17001" /*globalStateRemoteNode*/)
t, "https://deso.com:17001" /*globalStateRemoteNode*/, false)

{
url, _, err := apiServer.GlobalState.CreateGetRequest([]byte("woo"))
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 79b8c9d

Please sign in to comment.