refactor: move nodeVotes to a dedicated file

This commit is contained in:
Derrick Hammer 2024-01-07 04:03:36 -05:00
parent 51d76a2d95
commit ea872fedc4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 55 additions and 45 deletions

View File

@ -39,50 +39,6 @@ type P2PImpl struct {
peers structs.Map peers structs.Map
} }
type NodeVotesImpl struct {
good int
bad int
}
func (n *NodeVotesImpl) Good() int {
return n.good
}
func (n *NodeVotesImpl) Bad() int {
return n.bad
}
func (n NodeVotesImpl) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeInt(int64(n.good))
if err != nil {
return err
}
err = enc.EncodeInt(int64(n.bad))
if err != nil {
return err
}
return nil
}
func (n *NodeVotesImpl) DecodeMsgpack(dec *msgpack.Decoder) error {
good, err := dec.DecodeInt()
if err != nil {
return err
}
bad, err := dec.DecodeInt()
if err != nil {
return err
}
n.good = good
n.bad = bad
return nil
}
func NewP2P(node interfaces.Node) *P2PImpl { func NewP2P(node interfaces.Node) *P2PImpl {
service := &P2PImpl{ service := &P2PImpl{
logger: node.Logger(), logger: node.Logger(),
@ -317,7 +273,7 @@ func (p *P2PImpl) OnNewPeerListen(peer *net.Peer, verifyId bool) {
func (p *P2PImpl) ReadNodeScore(nodeId *encoding.NodeId) (interfaces.NodeVotes, error) { func (p *P2PImpl) ReadNodeScore(nodeId *encoding.NodeId) (interfaces.NodeVotes, error) {
node := p.nodesBucket.Get(nodeId.Raw()) node := p.nodesBucket.Get(nodeId.Raw())
if node == nil { if node == nil {
return &NodeVotesImpl{}, nil return NewNodeVotes(), nil
} }
var score interfaces.NodeVotes var score interfaces.NodeVotes

54
service/vote.go Normal file
View File

@ -0,0 +1,54 @@
package service
import (
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"github.com/vmihailenco/msgpack/v5"
)
type NodeVotesImpl struct {
good int
bad int
}
func NewNodeVotes() interfaces.NodeVotes {
return &NodeVotesImpl{}
}
func (n *NodeVotesImpl) Good() int {
return n.good
}
func (n *NodeVotesImpl) Bad() int {
return n.bad
}
func (n NodeVotesImpl) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeInt(int64(n.good))
if err != nil {
return err
}
err = enc.EncodeInt(int64(n.bad))
if err != nil {
return err
}
return nil
}
func (n *NodeVotesImpl) DecodeMsgpack(dec *msgpack.Decoder) error {
good, err := dec.DecodeInt()
if err != nil {
return err
}
bad, err := dec.DecodeInt()
if err != nil {
return err
}
n.good = good
n.bad = bad
return nil
}