Skip to content

Commit d0ffa2d

Browse files
authored
Merge pull request #583 from nspcc-dev/fix/gettx
consensus: return nil interface from getTx
2 parents 68c78d8 + 8d1f564 commit d0ffa2d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pkg/consensus/consensus.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ func (s *service) getTx(h util.Uint256) block.Transaction {
253253

254254
tx, _, _ := s.Config.Chain.GetTransaction(h)
255255

256-
return tx
256+
// this is needed because in case of absent tx dBFT expects to
257+
// get nil interface, not a nil pointer to any concrete type
258+
if tx != nil {
259+
return tx
260+
}
261+
262+
return nil
257263
}
258264

259265
func (s *service) verifyBlock(b block.Block) bool {

pkg/consensus/consensus_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ func TestService_ValidatePayload(t *testing.T) {
5656
})
5757
}
5858

59+
func TestService_getTx(t *testing.T) {
60+
srv := newTestService(t)
61+
62+
t.Run("transaction in mempool", func(t *testing.T) {
63+
tx := newMinerTx(1234)
64+
h := tx.Hash()
65+
66+
require.Equal(t, nil, srv.getTx(h))
67+
68+
item := core.NewPoolItem(tx, new(feer))
69+
srv.Chain.GetMemPool().TryAdd(h, item)
70+
71+
got := srv.getTx(h)
72+
require.NotNil(t, got)
73+
require.Equal(t, h, got.Hash())
74+
})
75+
76+
t.Run("transaction in local cache", func(t *testing.T) {
77+
tx := newMinerTx(4321)
78+
h := tx.Hash()
79+
80+
require.Equal(t, nil, srv.getTx(h))
81+
82+
srv.txx.Add(tx)
83+
84+
got := srv.getTx(h)
85+
require.NotNil(t, got)
86+
require.Equal(t, h, got.Hash())
87+
})
88+
}
89+
5990
func TestService_OnPayload(t *testing.T) {
6091
srv := newTestService(t)
6192

0 commit comments

Comments
 (0)