diff --git a/go.mod b/go.mod index c53f99f3..d80c245e 100644 --- a/go.mod +++ b/go.mod @@ -33,11 +33,11 @@ require ( github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.18.2 - github.com/stretchr/testify v1.9.0 + github.com/stretchr/testify v1.10.0 github.com/tyler-smith/go-bip39 v1.1.0 - golang.org/x/crypto v0.28.0 + golang.org/x/crypto v0.29.0 golang.org/x/image v0.21.0 - golang.org/x/sync v0.8.0 + golang.org/x/sync v0.9.0 google.golang.org/api v0.201.0 gopkg.in/DataDog/dd-trace-go.v1 v1.69.0 ) @@ -78,7 +78,6 @@ require ( github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect - github.com/decred/dcrd/container/lru v1.0.0 // indirect github.com/decred/dcrd/crypto/blake256 v1.1.0 // indirect github.com/deso-protocol/go-merkle-tree v1.0.0 // indirect github.com/dgraph-io/ristretto v0.2.0 // indirect @@ -123,7 +122,7 @@ require ( github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/kevinburke/go-types v0.0.0-20240719050749-165e75e768f7 // indirect github.com/kevinburke/rest v0.0.0-20240617045629-3ed0ad3487f0 // indirect - github.com/klauspost/compress v1.17.1 // indirect + github.com/klauspost/compress v1.17.11 // indirect github.com/kyokomi/emoji/v2 v2.2.13 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -181,11 +180,11 @@ require ( go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect golang.org/x/mod v0.21.0 // indirect - golang.org/x/net v0.30.0 // indirect + golang.org/x/net v0.31.0 // indirect golang.org/x/oauth2 v0.23.0 // indirect - golang.org/x/sys v0.26.0 // indirect - golang.org/x/term v0.25.0 // indirect - golang.org/x/text v0.19.0 // indirect + golang.org/x/sys v0.27.0 // indirect + golang.org/x/term v0.26.0 // indirect + golang.org/x/text v0.20.0 // indirect golang.org/x/time v0.7.0 // indirect golang.org/x/tools v0.26.0 // indirect golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect diff --git a/go.sum b/go.sum index bcb10daa..54bcb038 100644 --- a/go.sum +++ b/go.sum @@ -134,8 +134,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/decred/dcrd/container/lru v1.0.0 h1:7foQymtbu18aQWYiY9RnNIeE+kvpiN+fiBQ3+viyJjI= -github.com/decred/dcrd/container/lru v1.0.0/go.mod h1:vlPwj0l+IzAHhQSsbgQnJgO5Cte78+yI065V+Mc5PRQ= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/crypto/blake256 v1.1.0 h1:zPMNGQCm0g4QTY27fOCorQW7EryeQ/U0x++OzVrdms8= github.com/decred/dcrd/crypto/blake256 v1.1.0/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= @@ -317,8 +315,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= -github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= -github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= +github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -461,8 +459,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= @@ -536,8 +534,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= -golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= +golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= @@ -572,8 +570,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= -golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= -golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= +golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= +golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= @@ -586,8 +584,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= +golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -619,16 +617,16 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= -golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= -golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24= -golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M= +golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU= +golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -636,8 +634,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= -golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= +golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/routes/exchange.go b/routes/exchange.go index 8773cae6..3b7dda31 100644 --- a/routes/exchange.go +++ b/routes/exchange.go @@ -1362,7 +1362,7 @@ func (fes *APIServer) getTxindexMetadataForUncommittedBlock( uncommittedTxnMetaMap := make(map[lib.BlockHash]*lib.TransactionMetadata) txindexUtxoView := lib.NewUtxoView(fes.blockchain.DB(), fes.Params, nil, nil, nil) if blockNode.Header.PrevBlockHash != nil && !txindexUtxoView.TipHash.IsEqual(blockNode.Header.PrevBlockHash) { - utxoViewAndUtxoOps, err := fes.blockchain.GetUtxoViewAndUtxoOpsAtBlockHash(*blockNode.Header.PrevBlockHash) + utxoViewAndUtxoOps, err := fes.blockchain.GetUtxoViewAndUtxoOpsAtBlockHash(*blockNode.Header.PrevBlockHash, blockNode.Header.Height-1) if err != nil { return nil, errors.Wrap(err, "getTxindexMetadataForUncommittedBlock: Problem fetching utxoView for uncommitted block") @@ -1398,7 +1398,7 @@ func (fes *APIServer) APIBlock(ww http.ResponseWriter, rr *http.Request) { // For this endpoint we need to lock the blockchain for reading. // If the HashHex is set, look the block up using that. - numBlocks := len(fes.blockchain.BestChain()) + numBlocks := fes.blockchain.BlockTip().Height + 1 var blockNode *lib.BlockNode var blockHash *lib.BlockHash @@ -1420,7 +1420,7 @@ func (fes *APIServer) APIBlock(ww http.ResponseWriter, rr *http.Request) { } else { // Find the block node with the corresponding height on the best chain. if blockRequest.Height >= int64(numBlocks) || blockRequest.Height < 0 { - maxHeight := len(fes.blockchain.BestChain()) - 1 + maxHeight := fes.blockchain.BlockTip().Height APIAddError(ww, fmt.Sprintf("APIBlockRequest: Height requested "+ "%d must be >= 0 and <= "+ @@ -1428,7 +1428,15 @@ func (fes *APIServer) APIBlock(ww http.ResponseWriter, rr *http.Request) { maxHeight)) return } - blockNode = fes.blockchain.BestChain()[blockRequest.Height] + blockNode, exists, err := fes.blockchain.GetBlockFromBestChainByHeight(uint64(blockRequest.Height), false) + if err != nil { + APIAddError(ww, fmt.Sprintf("APIBlockRequest: Problem fetching block: %v", err)) + return + } + if !exists { + APIAddError(ww, fmt.Sprintf("APIBlockRequest: Block with height %d not found", blockRequest.Height)) + return + } blockHash = blockNode.Hash } diff --git a/routes/exchange_test.go b/routes/exchange_test.go index 248c2f8e..4444f44e 100644 --- a/routes/exchange_test.go +++ b/routes/exchange_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "encoding/json" + "github.com/deso-protocol/core/collections" "io" "log" "net/http" @@ -146,6 +147,13 @@ func NewLowDifficultyBlockchainWithParams(t *testing.T, params *lib.DeSoParams) return chain, ¶msCopy, db, dir } +func GetBlockFromBestChainByHeight(t *testing.T, blockchain *lib.Blockchain, height uint32) *lib.BlockNode { + blockNode, exists, err := blockchain.GetBlockFromBestChainByHeight(uint64(height), false) + require.NoError(t, err) + require.True(t, exists) + return blockNode +} + 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) @@ -595,16 +603,18 @@ func TestAPI(t *testing.T) { } assert.Equal("", transactionInfoRes.Error) assert.Equal(2, len(transactionInfoRes.Transactions)) + block1 := GetBlockFromBestChainByHeight(t, apiServer.blockchain, 1) + block2 := GetBlockFromBestChainByHeight(t, apiServer.blockchain, 2) assert.Contains( []string{ - hex.EncodeToString(apiServer.blockchain.BestChain()[1].Hash[:]), - hex.EncodeToString(apiServer.blockchain.BestChain()[2].Hash[:]), + hex.EncodeToString(block1.Hash[:]), + hex.EncodeToString(block2.Hash[:]), }, transactionInfoRes.Transactions[0].BlockHashHex) assert.Contains( []string{ - hex.EncodeToString(apiServer.blockchain.BestChain()[1].Hash[:]), - hex.EncodeToString(apiServer.blockchain.BestChain()[2].Hash[:]), + hex.EncodeToString(block1.Hash[:]), + hex.EncodeToString(block2.Hash[:]), }, transactionInfoRes.Transactions[1].BlockHashHex) assert.Equal(0, len(transactionInfoRes.Transactions[0].Inputs)) @@ -614,10 +624,10 @@ func TestAPI(t *testing.T) { var firstBlockTxn *lib.MsgDeSoTxn var secondBlockTxn *lib.MsgDeSoTxn { - blockHash := apiServer.blockchain.BestChain()[1].Hash + blockHash := GetBlockFromBestChainByHeight(t, apiServer.blockchain, 1).Hash blockLookup, err := lib.GetBlock(blockHash, apiServer.blockchain.DB(), apiServer.blockchain.Snapshot()) require.NoError(err) - block2Lookup, err := lib.GetBlock(apiServer.blockchain.BestChain()[2].Hash, apiServer.blockchain.DB(), apiServer.blockchain.Snapshot()) + block2Lookup, err := lib.GetBlock(GetBlockFromBestChainByHeight(t, apiServer.blockchain, 2).Hash, apiServer.blockchain.DB(), apiServer.blockchain.Snapshot()) firstBlockTxn = blockLookup.Txns[0] secondBlockTxn = block2Lookup.Txns[0] @@ -643,7 +653,7 @@ func TestAPI(t *testing.T) { assert.Equal(1, len(transactionInfoRes.Transactions)) assert.Contains( []string{ - hex.EncodeToString(apiServer.blockchain.BestChain()[1].Hash[:]), + hex.EncodeToString(GetBlockFromBestChainByHeight(t, apiServer.blockchain, 1).Hash[:]), }, transactionInfoRes.Transactions[0].BlockHashHex) assert.Equal(0, len(transactionInfoRes.Transactions[0].Inputs)) @@ -1335,8 +1345,16 @@ func TestAPI(t *testing.T) { // Set the tip to the first block and make sure all txns get deleted. { - chainWithFirstBlockOnly := apiServer.blockchain.BestChain()[:2] - oldBestChain := apiServer.blockchain.BestChain() + block0 := GetBlockFromBestChainByHeight(t, apiServer.blockchain, 0) + block1 := GetBlockFromBestChainByHeight(t, apiServer.blockchain, 1) + chainWithFirstBlockOnly := []*lib.BlockNode{block0, block1} + oldBestChainTip := apiServer.blockchain.BlockTip() + oldBestChain := []*lib.BlockNode{} + for oldBestChainTip != nil { + oldBestChain = append(oldBestChain, oldBestChainTip) + oldBestChainTip = oldBestChainTip.GetParent(apiServer.blockchain.GetBlockIndex()) + } + oldBestChain = collections.Reverse(oldBestChain) apiServer.blockchain.SetBestChain(chainWithFirstBlockOnly) require.NoError(apiServer.TXIndex.Update()) @@ -1391,7 +1409,7 @@ func TestAPI(t *testing.T) { assert.Equal(1, len(transactionInfoRes.Transactions)) assert.Contains( []string{ - hex.EncodeToString(apiServer.blockchain.BestChain()[1].Hash[:]), + hex.EncodeToString(GetBlockFromBestChainByHeight(t, apiServer.blockchain, 1).Hash[:]), }, transactionInfoRes.Transactions[0].BlockHashHex) assert.Equal(0, len(transactionInfoRes.Transactions[0].Inputs)) diff --git a/routes/hot_feed.go b/routes/hot_feed.go index dd4619a3..8a8529dc 100644 --- a/routes/hot_feed.go +++ b/routes/hot_feed.go @@ -403,15 +403,11 @@ func (fes *APIServer) UpdateHotFeedOrderedList( // This offset allows us to see what the hot feed would look like in the past, // which is useful for testing purposes. - blockOffsetForTesting := 0 + // TODO: this is a little more annoying to support without the full history of best chain. + // we can revisit implementing later if needed. Just takes a few more minutes. + // blockOffsetForTesting := 0 lookbackWindowBlocks := LookbackWindowBlocks - // Check if the most recent blocks that we'll be considering in hot feed computation have been processed. - for _, blockNode := range fes.blockchain.BestChain() { - if blockNode.Height < blockTip.Height-uint32(lookbackWindowBlocks+blockOffsetForTesting) { - continue - } - } // Log how long this routine takes, since it could be heavy. glog.V(2).Info("UpdateHotFeedOrderedList: Starting new update cycle.") @@ -425,10 +421,11 @@ func (fes *APIServer) UpdateHotFeedOrderedList( } // Grab the last 24 hours worth of blocks (288 blocks @ 5min/block). - blockTipIndex := len(fes.blockchain.BestChain()) - 1 - blockOffsetForTesting - relevantNodes := fes.blockchain.BestChain() - if len(fes.blockchain.BestChain()) > (lookbackWindowBlocks + blockOffsetForTesting) { - relevantNodes = fes.blockchain.BestChain()[blockTipIndex-lookbackWindowBlocks-blockOffsetForTesting : blockTipIndex] + startNode := fes.blockchain.BlockTip() + relevantNodes := []*lib.BlockNode{} + for len(relevantNodes) < lookbackWindowBlocks && startNode != nil { + relevantNodes = append(relevantNodes, startNode) + startNode = startNode.GetParent(fes.blockchain.GetBlockIndex()) } var hotnessInfoBlocks []*HotnessInfoBlock diff --git a/routes/miner.go b/routes/miner.go index 6bcd3af2..8a911cf6 100644 --- a/routes/miner.go +++ b/routes/miner.go @@ -198,7 +198,7 @@ func (fes *APIServer) SubmitBlock(ww http.ResponseWriter, req *http.Request) { // The optimal solution is to check signatures in a way that doesn't acquire the // ChainLock, which is what Bitcoin Core does. isMainChain, isOrphan, _, err := fes.blockchain.ProcessBlock( - blockFound, true /*verifySignatures*/) + blockFound, nil, true /*verifySignatures*/) glog.V(1).Infof("Called ProcessBlock/ConnectBlock: isMainChain=(%v), isOrphan=(%v), err=(%v)", isMainChain, isOrphan, err) if err != nil { diff --git a/routes/transaction.go b/routes/transaction.go index 04c83adf..e94175ca 100644 --- a/routes/transaction.go +++ b/routes/transaction.go @@ -4553,9 +4553,9 @@ func (fes *APIServer) GetTxnConstructionParams(ww http.ResponseWriter, req *http func (fes *APIServer) GetCommittedTipBlockInfo(ww http.ResponseWriter, req *http.Request) { // Get the block tip from the blockchain. fes.backendServer.GetBlockchain().ChainLock.RLock() - blockTip, idx := fes.backendServer.GetBlockchain().GetCommittedTip() + blockTip, exists := fes.backendServer.GetBlockchain().GetCommittedTip() fes.backendServer.GetBlockchain().ChainLock.RUnlock() - if idx == -1 { + if !exists { _AddBadRequestError(ww, "GetCommittedTipBlockInfo: Problem getting block tip") return } diff --git a/scripts/tools/toolslib/chain.go b/scripts/tools/toolslib/chain.go index 50c7bae0..0165072e 100644 --- a/scripts/tools/toolslib/chain.go +++ b/scripts/tools/toolslib/chain.go @@ -18,6 +18,8 @@ func OpenDataDir(dataDir string) (*badger.DB, error) { return db, nil } +// TODO: This utility function needs to be updated to account for not having the entire chain in +// memory (no more BEST CHAIN representing the entire history of the blockchain). // Returns the best chain associated with a badgerDB handle. func GetBestChainFromBadger(syncedDBHandle *badger.DB, params *lib.DeSoParams) ([]*lib.BlockNode, error) { bestBlockHash := lib.DbGetBestHash(syncedDBHandle, nil, lib.ChainTypeDeSoBlock)