Skip to content

Commit

Permalink
Check if any unmined transactions have been mined which are not older…
Browse files Browse the repository at this point in the history
… than 20min
  • Loading branch information
boecklim committed Dec 14, 2023
1 parent c5d34da commit ce9f960
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
11 changes: 6 additions & 5 deletions metamorph/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

const (
// number of times we will retry announcing transaction if we haven't seen it on the network
// MaxRetries number of times we will retry announcing transaction if we haven't seen it on the network
MaxRetries = 15
// length of interval for checking transactions if they are seen on the network
// if not we resend them again for a few times
Expand All @@ -37,6 +37,7 @@ const (

failedToUpdateStatus = "Failed to update status"
dataRetentionPeriodDefault = 14 * 24 * time.Hour // 14 days
checkIfMinedTimeRange = time.Minute * 20
)

type Processor struct {
Expand Down Expand Up @@ -120,7 +121,7 @@ func NewProcessor(s store.MetamorphStore, pm p2p.PeerManagerI,

// Start a goroutine to resend transactions that have not been seen on the network
go p.processExpiredTransactions()
go p.processExpiredSeenTransactions()
go p.processCheckIfMined()

gocore.AddAppPayloadFn("mtm", func() interface{} {
return p.GetStats(false)
Expand Down Expand Up @@ -173,10 +174,10 @@ func (p *Processor) unlockItems() error {
return p.store.SetUnlocked(context.Background(), hashes)
}

func (p *Processor) processExpiredSeenTransactions() {
// filterFunc returns true if the transaction has not been seen on the network
func (p *Processor) processCheckIfMined() {
// filter for transactions which have been at least announced but not mined and which haven't started to be processed longer than a specified amount of time ago
filterFunc := func(processorResp *processor_response.ProcessorResponse) bool {
return processorResp.GetStatus() == metamorph_api.Status_SEEN_ON_NETWORK && p.now().Sub(processorResp.Start) > p.processExpiredSeenTxsInterval
return (processorResp.GetStatus() != metamorph_api.Status_MINED && processorResp.GetStatus() != metamorph_api.Status_CONFIRMED) && p.now().Sub(processorResp.Start) < checkIfMinedTimeRange
}

// Check transactions that have been seen on the network, but haven't been marked as mined
Expand Down
31 changes: 13 additions & 18 deletions metamorph/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func BenchmarkProcessTransaction(b *testing.B) {
time.Sleep(1 * time.Second)
}

func TestProcessExpiredSeenTransactions(t *testing.T) {
func TestProcessCheckIfMined(t *testing.T) {
txsBlocks := []*blocktx_api.TransactionBlock{
{
BlockHash: testdata.Block1Hash[:],
Expand All @@ -730,52 +730,46 @@ func TestProcessExpiredSeenTransactions(t *testing.T) {
getTransactionBlocksErr error
updateMinedErr error

expectedNrOfUpdates int
expectedNrOfBlockTxRequests int
expectedNrOfUpdates int
}{
{
name: "expired seen txs",
name: "expired txs",
blocks: txsBlocks,

expectedNrOfUpdates: 3,
expectedNrOfBlockTxRequests: 1,
expectedNrOfUpdates: 3,
},
{
name: "failed to get transaction blocks",
getTransactionBlocksErr: errors.New("failed to get transaction blocks"),

expectedNrOfUpdates: 0,
expectedNrOfBlockTxRequests: 1,
expectedNrOfUpdates: 0,
},
{
name: "failed to parse block hash",
blocks: []*blocktx_api.TransactionBlock{{
BlockHash: []byte("not a valid block hash"),
}},

expectedNrOfUpdates: 0,
expectedNrOfBlockTxRequests: 1,
expectedNrOfUpdates: 0,
},
{
name: "failed to update mined",
blocks: txsBlocks,
updateMinedErr: errors.New("failed to update mined"),

expectedNrOfUpdates: 3,
expectedNrOfBlockTxRequests: 1,
expectedNrOfUpdates: 3,
},
{
name: "failed to get tx from response map",
blocks: []*blocktx_api.TransactionBlock{
{
BlockHash: testdata.Block1Hash[:],
BlockHeight: 1234,
TransactionHash: testdata.TX4Hash[:],
TransactionHash: testdata.TX5Hash[:],
},
},

expectedNrOfUpdates: 0,
expectedNrOfBlockTxRequests: 1,
expectedNrOfUpdates: 0,
},
}

Expand Down Expand Up @@ -813,14 +807,15 @@ func TestProcessExpiredSeenTransactions(t *testing.T) {

require.Equal(t, 0, processor.ProcessorResponseMap.Len())

processor.ProcessorResponseMap.Set(testdata.TX1Hash, processor_response.NewProcessorResponseWithStatus(testdata.TX1Hash, metamorph_api.Status_SEEN_ON_NETWORK))
processor.ProcessorResponseMap.Set(testdata.TX1Hash, processor_response.NewProcessorResponseWithStatus(testdata.TX1Hash, metamorph_api.Status_STORED))
processor.ProcessorResponseMap.Set(testdata.TX2Hash, processor_response.NewProcessorResponseWithStatus(testdata.TX2Hash, metamorph_api.Status_SEEN_ON_NETWORK))
processor.ProcessorResponseMap.Set(testdata.TX3Hash, processor_response.NewProcessorResponseWithStatus(testdata.TX3Hash, metamorph_api.Status_SEEN_ON_NETWORK))
processor.ProcessorResponseMap.Set(testdata.TX3Hash, processor_response.NewProcessorResponseWithStatus(testdata.TX3Hash, metamorph_api.Status_REJECTED))
processor.ProcessorResponseMap.Set(testdata.TX4Hash, processor_response.NewProcessorResponseWithStatus(testdata.TX4Hash, metamorph_api.Status_MINED))

time.Sleep(25 * time.Millisecond)

require.Equal(t, tc.expectedNrOfUpdates, len(metamorphStore.UpdateMinedCalls()))
require.Equal(t, tc.expectedNrOfBlockTxRequests, len(btxMock.GetTransactionBlocksCalls()))
require.Equal(t, 1, len(btxMock.GetTransactionBlocksCalls()))
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions testdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var (
TX4 = "88eab41a8d0b7b4bc395f8f988ea3d6e63c8bc339526fd2f00cb7ce6fd7df0f7"
TX4Hash, _ = chainhash.NewHashFromStr(TX4)

TX5 = "df931ab7d4ff0bbf96ff186f221c466f09c052c5331733641040defabf9dcd93"
TX5Hash, _ = chainhash.NewHashFromStr(TX5)

Time = time.Date(2009, 1, 03, 18, 15, 05, 0, time.UTC)
DefaultPolicy = `{"excessiveblocksize":2000000000,"blockmaxsize":512000000,"maxtxsizepolicy":10000000,"maxorphantxsize":1000000000,"datacarriersize":4294967295,"maxscriptsizepolicy":500000,"maxopsperscriptpolicy":4294967295,"maxscriptnumlengthpolicy":10000,"maxpubkeyspermultisigpolicy":4294967295,"maxtxsigopscountspolicy":4294967295,"maxstackmemoryusagepolicy":100000000,"maxstackmemoryusageconsensus":200000000,"limitancestorcount":10000,"limitcpfpgroupmemberscount":25,"maxmempool":2000000000,"maxmempoolsizedisk":0,"mempoolmaxpercentcpfp":10,"acceptnonstdoutputs":true,"datacarrier":true,"minminingtxfee":5e-7,"maxstdtxvalidationduration":3,"maxnonstdtxvalidationduration":1000,"maxtxchainvalidationbudget":50,"validationclockcpu":true,"minconsolidationfactor":20,"maxconsolidationinputscriptsize":150,"minconfconsolidationinput":6,"minconsolidationinputmaturity":6,"acceptnonstdconsolidationinput":false}`
)

0 comments on commit ce9f960

Please sign in to comment.