Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add LastSent and LastReceived properties to peer #569

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sync/firewall/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewFirewall(conf *Config, net network.Network, peerSet *peerset.PeerSet, st

func (f *Firewall) OpenGossipBundle(data []byte, source peer.ID, from peer.ID) *bundle.Bundle {
if from != source {
f.peerSet.UpdateLastSeen(from)
f.peerSet.UpdateLastReceived(from)
if f.isPeerBanned(from) {
f.logger.Warn("firewall: from peer banned", "from", from)
f.closeConnection(from)
Expand Down Expand Up @@ -72,7 +72,7 @@ func (f *Firewall) OpenStreamBundle(r io.Reader, from peer.ID) *bundle.Bundle {
}

func (f *Firewall) openBundle(r io.Reader, source peer.ID) (*bundle.Bundle, error) {
f.peerSet.UpdateLastSeen(source)
f.peerSet.UpdateLastReceived(source)
f.peerSet.IncreaseReceivedBundlesCounter(source)

if f.isPeerBanned(source) {
Expand Down
6 changes: 3 additions & 3 deletions sync/firewall/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestDisabledFirewall(t *testing.T) {
assert.False(t, td.network.IsClosed(td.badPeerID))
}

func TestUpdateLastSeen(t *testing.T) {
func TestUpdateLastReceived(t *testing.T) {
td := setup(t)

bdl := bundle.NewBundle(td.goodPeerID, message.NewQueryProposalMessage(100, 1))
Expand All @@ -197,8 +197,8 @@ func TestUpdateLastSeen(t *testing.T) {

peerUnknown := td.firewall.peerSet.GetPeer(td.unknownPeerID)
peerGood := td.firewall.peerSet.GetPeer(td.goodPeerID)
assert.GreaterOrEqual(t, peerUnknown.LastSeen.UnixNano(), now)
assert.GreaterOrEqual(t, peerGood.LastSeen.UnixNano(), now)
assert.GreaterOrEqual(t, peerUnknown.LastReceived.UnixNano(), now)
assert.GreaterOrEqual(t, peerGood.LastReceived.UnixNano(), now)
}

func TestNetworkFlags(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion sync/peerset/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Peer struct {
PeerID peer.ID
ConsensusKeys map[bls.PublicKey]bool
Flags int
LastSeen time.Time
LastSent time.Time
LastReceived time.Time
Height uint32
ReceivedBundles int
InvalidBundles int
Expand Down
12 changes: 10 additions & 2 deletions sync/peerset/peer_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,20 @@ func (ps *PeerSet) UpdateStatus(pid peer.ID, status StatusCode) {
p.Status = status
}

func (ps *PeerSet) UpdateLastSeen(pid peer.ID) {
func (ps *PeerSet) UpdateLastSent(pid peer.ID) {
ps.lk.Lock()
defer ps.lk.Unlock()

p := ps.mustGetPeer(pid)
p.LastSeen = time.Now()
p.LastSent = time.Now()
}

func (ps *PeerSet) UpdateLastReceived(pid peer.ID) {
ps.lk.Lock()
defer ps.lk.Unlock()

p := ps.mustGetPeer(pid)
p.LastReceived = time.Now()
}

func (ps *PeerSet) IncreaseReceivedBundlesCounter(pid peer.ID) {
Expand Down
14 changes: 11 additions & 3 deletions sync/peerset/peer_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,20 @@ func TestPeerSet(t *testing.T) {
assert.Equal(t, peer1.Status, StatusCodeBanned)
})

t.Run("Testing UpdateLastSeen", func(t *testing.T) {
t.Run("Testing UpdateLastSent", func(t *testing.T) {
now := time.Now()
peerSet.UpdateLastSeen(pid1)
peerSet.UpdateLastSent(pid1)

peer1 := peerSet.getPeer(pid1)
assert.GreaterOrEqual(t, peer1.LastSeen, now)
assert.GreaterOrEqual(t, peer1.LastSent, now)
})

t.Run("Testing UpdateLastReceived", func(t *testing.T) {
now := time.Now()
peerSet.UpdateLastReceived(pid1)

peer1 := peerSet.getPeer(pid1)
assert.GreaterOrEqual(t, peer1.LastReceived, now)
})

t.Run("Testing StartedAt", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func (sync *synchronizer) sendTo(msg message.Message, to peer.ID, sessionID int)
bdl := sync.prepareBundle(msg)
if bdl != nil {
data, _ := bdl.Encode()
sync.peerSet.UpdateLastSent(to)
err := sync.network.SendTo(data, to)
if err != nil {
sync.logger.Warn("error on sending bundle", "bundle", bdl, "err", err, "to", to)
Expand Down
50 changes: 32 additions & 18 deletions www/grpc/gen/dart/network.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ class PeerInfo extends $pb.GeneratedMessage {
..a<$core.int>(8, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'invalidMessages', $pb.PbFieldType.O3)
..a<$core.int>(9, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'receivedBytes', $pb.PbFieldType.O3)
..a<$core.int>(10, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'status', $pb.PbFieldType.O3)
..aInt64(11, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'lastSeen')
..a<$core.int>(12, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'sendSuccess', $pb.PbFieldType.O3)
..a<$core.int>(13, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'sendFailed', $pb.PbFieldType.O3)
..aInt64(11, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'lastSent')
..aInt64(12, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'lastReceived')
..a<$core.int>(13, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'sendSuccess', $pb.PbFieldType.O3)
..a<$core.int>(14, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'sendFailed', $pb.PbFieldType.O3)
..hasRequiredFields = false
;

Expand All @@ -257,7 +258,8 @@ class PeerInfo extends $pb.GeneratedMessage {
$core.int? invalidMessages,
$core.int? receivedBytes,
$core.int? status,
$fixnum.Int64? lastSeen,
$fixnum.Int64? lastSent,
$fixnum.Int64? lastReceived,
$core.int? sendSuccess,
$core.int? sendFailed,
}) {
Expand Down Expand Up @@ -292,8 +294,11 @@ class PeerInfo extends $pb.GeneratedMessage {
if (status != null) {
_result.status = status;
}
if (lastSeen != null) {
_result.lastSeen = lastSeen;
if (lastSent != null) {
_result.lastSent = lastSent;
}
if (lastReceived != null) {
_result.lastReceived = lastReceived;
}
if (sendSuccess != null) {
_result.sendSuccess = sendSuccess;
Expand Down Expand Up @@ -409,31 +414,40 @@ class PeerInfo extends $pb.GeneratedMessage {
void clearStatus() => clearField(10);

@$pb.TagNumber(11)
$fixnum.Int64 get lastSeen => $_getI64(10);
$fixnum.Int64 get lastSent => $_getI64(10);
@$pb.TagNumber(11)
set lastSeen($fixnum.Int64 v) { $_setInt64(10, v); }
set lastSent($fixnum.Int64 v) { $_setInt64(10, v); }
@$pb.TagNumber(11)
$core.bool hasLastSeen() => $_has(10);
$core.bool hasLastSent() => $_has(10);
@$pb.TagNumber(11)
void clearLastSeen() => clearField(11);
void clearLastSent() => clearField(11);

@$pb.TagNumber(12)
$core.int get sendSuccess => $_getIZ(11);
$fixnum.Int64 get lastReceived => $_getI64(11);
@$pb.TagNumber(12)
set sendSuccess($core.int v) { $_setSignedInt32(11, v); }
set lastReceived($fixnum.Int64 v) { $_setInt64(11, v); }
@$pb.TagNumber(12)
$core.bool hasSendSuccess() => $_has(11);
$core.bool hasLastReceived() => $_has(11);
@$pb.TagNumber(12)
void clearSendSuccess() => clearField(12);
void clearLastReceived() => clearField(12);

@$pb.TagNumber(13)
$core.int get sendFailed => $_getIZ(12);
$core.int get sendSuccess => $_getIZ(12);
@$pb.TagNumber(13)
set sendFailed($core.int v) { $_setSignedInt32(12, v); }
set sendSuccess($core.int v) { $_setSignedInt32(12, v); }
@$pb.TagNumber(13)
$core.bool hasSendFailed() => $_has(12);
$core.bool hasSendSuccess() => $_has(12);
@$pb.TagNumber(13)
void clearSendFailed() => clearField(13);
void clearSendSuccess() => clearField(13);

@$pb.TagNumber(14)
$core.int get sendFailed => $_getIZ(13);
@$pb.TagNumber(14)
set sendFailed($core.int v) { $_setSignedInt32(13, v); }
@$pb.TagNumber(14)
$core.bool hasSendFailed() => $_has(13);
@$pb.TagNumber(14)
void clearSendFailed() => clearField(14);
}

class NetworkApi {
Expand Down
9 changes: 5 additions & 4 deletions www/grpc/gen/dart/network.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ const PeerInfo$json = const {
const {'1': 'invalid_messages', '3': 8, '4': 1, '5': 5, '10': 'invalidMessages'},
const {'1': 'received_bytes', '3': 9, '4': 1, '5': 5, '10': 'receivedBytes'},
const {'1': 'status', '3': 10, '4': 1, '5': 5, '10': 'status'},
const {'1': 'last_seen', '3': 11, '4': 1, '5': 3, '10': 'lastSeen'},
const {'1': 'send_success', '3': 12, '4': 1, '5': 5, '10': 'sendSuccess'},
const {'1': 'send_failed', '3': 13, '4': 1, '5': 5, '10': 'sendFailed'},
const {'1': 'last_sent', '3': 11, '4': 1, '5': 3, '10': 'lastSent'},
const {'1': 'last_received', '3': 12, '4': 1, '5': 3, '10': 'lastReceived'},
const {'1': 'send_success', '3': 13, '4': 1, '5': 5, '10': 'sendSuccess'},
const {'1': 'send_failed', '3': 14, '4': 1, '5': 5, '10': 'sendFailed'},
],
};

/// Descriptor for `PeerInfo`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List peerInfoDescriptor = $convert.base64Decode('CghQZWVySW5mbxIYCgdtb25pa2VyGAEgASgJUgdtb25pa2VyEhQKBWFnZW50GAIgASgJUgVhZ2VudBIXCgdwZWVyX2lkGAMgASgMUgZwZWVySWQSJQoOY29uc2Vuc3VzX2t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Vk');
const $core.Map<$core.String, $core.dynamic> NetworkServiceBase$json = const {
'1': 'Network',
'2': const [
Expand Down
103 changes: 103 additions & 0 deletions www/grpc/gen/go/blockchain.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading