/
fields.go
74 lines (59 loc) · 1.55 KB
/
fields.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"errors"
"time"
"github.com/lasthyphen/dijetsnodesgo/ids"
"github.com/lasthyphen/dijetsnodesgo/utils/constants"
p2ppb "github.com/lasthyphen/dijetsnodesgo/proto/pb/p2p"
)
var errMissingField = errors.New("message missing field")
type chainIDGetter interface {
GetChainId() []byte
}
func GetChainID(m any) (ids.ID, error) {
msg, ok := m.(chainIDGetter)
if !ok {
return ids.Empty, errMissingField
}
chainIDBytes := msg.GetChainId()
return ids.ToID(chainIDBytes)
}
type sourceChainIDGetter interface {
GetSourceChainID() ids.ID
}
func GetSourceChainID(m any) (ids.ID, error) {
msg, ok := m.(sourceChainIDGetter)
if !ok {
return GetChainID(m)
}
return msg.GetSourceChainID(), nil
}
type requestIDGetter interface {
GetRequestId() uint32
}
func GetRequestID(m any) (uint32, bool) {
if msg, ok := m.(requestIDGetter); ok {
requestID := msg.GetRequestId()
return requestID, true
}
// AppGossip is the only message currently not containing a requestID
// Here we assign the requestID already in use for gossiped containers
// to allow a uniform handling of all messages
if _, ok := m.(*p2ppb.AppGossip); ok {
return constants.GossipMsgRequestID, true
}
return 0, false
}
type deadlineGetter interface {
GetDeadline() uint64
}
func GetDeadline(m any) (time.Duration, bool) {
msg, ok := m.(deadlineGetter)
if !ok {
return 0, false
}
deadline := msg.GetDeadline()
return time.Duration(deadline), true
}