diff --git a/go.mod b/go.mod index 1c351627..4c9a0578 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/routes/exchange.go b/routes/exchange.go index 30f54311..a5f0259d 100644 --- a/routes/exchange.go +++ b/routes/exchange.go @@ -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, @@ -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{} @@ -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)) @@ -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 @@ -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 diff --git a/routes/exchange_test.go b/routes/exchange_test.go index 2756e059..94518e99 100644 --- a/routes/exchange_test.go +++ b/routes/exchange_test.go @@ -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" @@ -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) } @@ -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() { @@ -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) @@ -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*/) { @@ -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) { diff --git a/routes/hot_feed.go b/routes/hot_feed.go index 0f058b2e..853cfc87 100644 --- a/routes/hot_feed.go +++ b/routes/hot_feed.go @@ -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 { diff --git a/routes/server.go b/routes/server.go index 2aba5c9b..7c46af86 100644 --- a/routes/server.go +++ b/routes/server.go @@ -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 @@ -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, diff --git a/routes/user.go b/routes/user.go index 5e6863aa..a38ec364 100644 --- a/routes/user.go +++ b/routes/user.go @@ -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 {