parse p2p pkg

This commit is contained in:
alex.sharov 2021-08-07 17:38:27 +07:00
parent 3f660970ce
commit a892707c19
3 changed files with 7 additions and 10 deletions

View File

@ -181,11 +181,6 @@ func ParsePooledTransactions65(payload []byte, pos int, ctx *TxParseContext, txS
return 0, err return 0, err
} }
for i := range txSlots.txs {
if txSlots.txs[i] == nil {
txSlots.txs[i] = &TxSlot{}
}
}
for i := 0; pos != len(payload); i++ { for i := 0; pos != len(payload); i++ {
txSlots.Growth(i) txSlots.Growth(i)
pos, err = ctx.ParseTransaction(payload, pos, txSlots.txs[i], txSlots.senders.At(i)) pos, err = ctx.ParseTransaction(payload, pos, txSlots.txs[i], txSlots.senders.At(i))
@ -210,11 +205,6 @@ func ParsePooledTransactions66(payload []byte, pos int, ctx *TxParseContext, txS
return requestID, 0, err return requestID, 0, err
} }
for i := range txSlots.txs {
if txSlots.txs[i] == nil {
txSlots.txs[i] = &TxSlot{}
}
}
for i := 0; pos != len(payload); i++ { for i := 0; pos != len(payload); i++ {
txSlots.Growth(i) txSlots.Growth(i)
pos, err = ctx.ParseTransaction(payload, pos, txSlots.txs[i], txSlots.senders.At(i)) pos, err = ctx.ParseTransaction(payload, pos, txSlots.txs[i], txSlots.senders.At(i))

View File

@ -115,6 +115,9 @@ func (s *TxSlots) Growth(targetSize int) {
for s.senders.Len() < targetSize { for s.senders.Len() < targetSize {
s.senders = append(s.senders, addressesGrowth...) s.senders = append(s.senders, addressesGrowth...)
} }
for i := len(s.txs) - 1; i >= 0 && s.txs[i] == nil; i-- {
s.txs[i] = &TxSlot{}
}
} }
const ( const (

View File

@ -97,15 +97,19 @@ func TestTxSlotsGrowth(t *testing.T) {
s.Growth(11) s.Growth(11)
assert.Equal(11, len(s.txs)) assert.Equal(11, len(s.txs))
assert.Equal(11, s.senders.Len()) assert.Equal(11, s.senders.Len())
assert.NotNil(s.txs[0])
s.Growth(23) s.Growth(23)
assert.Equal(23, len(s.txs)) assert.Equal(23, len(s.txs))
assert.Equal(23, s.senders.Len()) assert.Equal(23, s.senders.Len())
assert.NotNil(s.txs[12])
s = &TxSlots{txs: make([]*TxSlot, 20), senders: make(Addresses, 20*20)} s = &TxSlots{txs: make([]*TxSlot, 20), senders: make(Addresses, 20*20)}
s.Growth(20) s.Growth(20)
assert.Equal(20, len(s.txs)) assert.Equal(20, len(s.txs))
assert.Equal(20, s.senders.Len()) assert.Equal(20, s.senders.Len())
assert.NotNil(s.txs[0])
s.Growth(23) s.Growth(23)
assert.Equal(23, len(s.txs)) assert.Equal(23, len(s.txs))
assert.Equal(23, s.senders.Len()) assert.Equal(23, s.senders.Len())
assert.NotNil(s.txs[21])
} }