Skip to content

Commit

Permalink
handlde addnode
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov committed Feb 4, 2025
1 parent 04dd03d commit 6e31adb
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 3 deletions.
19 changes: 19 additions & 0 deletions pkg/innerring/processors/netmap/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ func (np *Processor) handleAddPeer(ev event.Event) {
}
}

func (np *Processor) handleAddNode(ev event.Event) {
newNode := ev.(netmapEvent.AddNode)

np.log.Info("notification",
zap.String("type", "add node"),
)

// send an event to the worker pool

err := np.pool.Submit(func() {
np.processAddNode(newNode)
})
if err != nil {
// there system can be moved into controlled degradation stage
np.log.Warn("netmap worker pool drained",
zap.Int("capacity", np.pool.Cap()))
}

Check warning on line 77 in pkg/innerring/processors/netmap/handlers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/handlers.go#L61-L77

Added lines #L61 - L77 were not covered by tests
}

func (np *Processor) handleUpdateState(ev event.Event) {
updPeer := ev.(netmapEvent.UpdatePeer)
np.log.Info("notification",
Expand Down
58 changes: 57 additions & 1 deletion pkg/innerring/processors/netmap/process_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@ package netmap

import (
"encoding/hex"
"fmt"

"github.com/nspcc-dev/neo-go/pkg/core/transaction"
netmaprpc "github.com/nspcc-dev/neofs-contract/rpc/netmap"
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"go.uber.org/zap"
)

// node2Info converts [netmaprpc.NetmapNode2] into [netmap.NodeInfo].
func node2Info(n2 netmaprpc.NetmapNode2) (netmap.NodeInfo, error) {
var ni netmap.NodeInfo

ni.SetNetworkEndpoints(n2.Addresses...)
for k, v := range n2.Attributes {
ni.SetAttribute(k, v)
}
ni.SetPublicKey(n2.Key.Bytes())
switch {
case n2.State.Cmp(netmaprpc.NodeStateOnline) == 0:
ni.SetOnline()
case n2.State.Cmp(netmaprpc.NodeStateMaintenance) == 0:
ni.SetMaintenance()
default:
return netmap.NodeInfo{}, fmt.Errorf("unsupported node state %v", n2.State)

Check warning on line 29 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L15-L29

Added lines #L15 - L29 were not covered by tests
}
return ni, nil

Check warning on line 31 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L31

Added line #L31 was not covered by tests
}

// Process add peer notification by sanity check of new node
// local epoch timer.
func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
Expand Down Expand Up @@ -36,8 +59,12 @@ func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
return
}

np.validateCandidate(tx, nodeInfo)

Check warning on line 62 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L62

Added line #L62 was not covered by tests
}

func (np *Processor) validateCandidate(tx *transaction.Transaction, nodeInfo netmap.NodeInfo) {

Check warning on line 65 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L65

Added line #L65 was not covered by tests
// validate node info
err = np.nodeValidator.Verify(nodeInfo)
var err = np.nodeValidator.Verify(nodeInfo)

Check warning on line 67 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L67

Added line #L67 was not covered by tests
if err != nil {
np.log.Warn("could not verify and update information about network map candidate",
zap.String("public_key", hex.EncodeToString(nodeInfo.PublicKey())),
Expand Down Expand Up @@ -68,6 +95,35 @@ func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
}
}

// Check the new node and allow/reject adding it to netmap.
func (np *Processor) processAddNode(ev netmapEvent.AddNode) {
if !np.alphabetState.IsAlphabet() {
np.log.Info("non alphabet mode, ignore new node notification")
return
}

Check warning on line 103 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L99-L103

Added lines #L99 - L103 were not covered by tests

// check if notary transaction is valid, see #976
originalRequest := ev.NotaryRequest()
tx := originalRequest.MainTransaction
ok, err := np.netmapClient.Morph().IsValidScript(tx.Script, tx.Signers)
if err != nil || !ok {
np.log.Warn("non-halt notary transaction",
zap.String("method", "netmap.AddNode"),
zap.String("hash", tx.Hash().StringLE()),
zap.Error(err))
return
}

Check warning on line 115 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L106-L115

Added lines #L106 - L115 were not covered by tests

// unmarshal node info
nodeInfo, err := node2Info(ev.Node)
if err != nil {
// it will be nice to have tx id at event structure to log it
np.log.Warn("can't parse network map candidate")
return
}
np.validateCandidate(tx, nodeInfo)

Check warning on line 124 in pkg/innerring/processors/netmap/process_peers.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/process_peers.go#L118-L124

Added lines #L118 - L124 were not covered by tests
}

// Process update peer notification by sending approval tx to the smart contract.
func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) {
if !np.alphabetState.IsAlphabet() {
Expand Down
13 changes: 11 additions & 2 deletions pkg/innerring/processors/netmap/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
var (
p event.NotaryParserInfo

pp = make([]event.NotaryParserInfo, 0, 2)
pp = make([]event.NotaryParserInfo, 0, 3)

Check warning on line 187 in pkg/innerring/processors/netmap/processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/processor.go#L187

Added line #L187 was not covered by tests
)

p.SetScriptHash(np.netmapClient.ContractAddress())
Expand All @@ -194,6 +194,11 @@ func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
p.SetParser(netmapEvent.ParseAddPeerNotary)
pp = append(pp, p)

// new node
p.SetRequestType(netmapEvent.AddNodeNotaryEvent)
p.SetParser(netmapEvent.ParseAddNodeNotary)
pp = append(pp, p)

Check warning on line 201 in pkg/innerring/processors/netmap/processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/processor.go#L197-L201

Added lines #L197 - L201 were not covered by tests
// update state
p.SetRequestType(netmapEvent.UpdateStateNotaryEvent)
p.SetParser(netmapEvent.ParseUpdatePeerNotary)
Expand All @@ -207,7 +212,7 @@ func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
var (
h event.NotaryHandlerInfo

hh = make([]event.NotaryHandlerInfo, 0, 2)
hh = make([]event.NotaryHandlerInfo, 0, 3)

Check warning on line 215 in pkg/innerring/processors/netmap/processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/processor.go#L215

Added line #L215 was not covered by tests
)

h.SetScriptHash(np.netmapClient.ContractAddress())
Expand All @@ -217,6 +222,10 @@ func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
h.SetHandler(np.handleAddPeer)
hh = append(hh, h)

h.SetRequestType(netmapEvent.AddNodeNotaryEvent)
h.SetHandler(np.handleAddNode)
hh = append(hh, h)

Check warning on line 228 in pkg/innerring/processors/netmap/processor.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/processor.go#L225-L228

Added lines #L225 - L228 were not covered by tests
// update state
h.SetRequestType(netmapEvent.UpdateStateNotaryEvent)
h.SetHandler(np.handleUpdateState)
Expand Down
19 changes: 19 additions & 0 deletions pkg/morph/event/netmap/add_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netmap

import (
"github.com/nspcc-dev/neo-go/pkg/network/payload"
netmaprpc "github.com/nspcc-dev/neofs-contract/rpc/netmap"
)

type AddPeer struct {
Expand All @@ -24,3 +25,21 @@ func (s AddPeer) Node() []byte {
func (s AddPeer) NotaryRequest() *payload.P2PNotaryRequest {
return s.notaryRequest
}

// AddNode contains addNode method parameters.
type AddNode struct {
Node netmaprpc.NetmapNode2

// For notary notifications only.
// Contains raw transactions of notary request.
notaryRequest *payload.P2PNotaryRequest
}

// MorphEvent implements Neo:Morph Event interface.
func (AddNode) MorphEvent() {}

Check warning on line 39 in pkg/morph/event/netmap/add_peer.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/event/netmap/add_peer.go#L39

Added line #L39 was not covered by tests

// NotaryRequest returns raw notary request if notification
// was received via notary service. Otherwise, returns nil.
func (s AddNode) NotaryRequest() *payload.P2PNotaryRequest {
return s.notaryRequest

Check warning on line 44 in pkg/morph/event/netmap/add_peer.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/event/netmap/add_peer.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}
36 changes: 36 additions & 0 deletions pkg/morph/event/netmap/add_peer_notary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package netmap

import (
"errors"

"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
)
Expand All @@ -16,6 +19,12 @@ const (
// in `Netmap` contract. Is used as identificator for notary
// peer addition requests.
AddPeerNotaryEvent = "addPeer"

// AddNodeNotaryEvent is method name for netmap `addNode` operation
// in `Netmap` contract. Is used as identificator for notary
// node addition requests. It's the new method used instead of
// AddPeerNotaryEvent on appropriate networks.
AddNodeNotaryEvent = "addNode"
)

// ParseAddPeerNotary from NotaryEvent into netmap event structure.
Expand Down Expand Up @@ -48,3 +57,30 @@ func ParseAddPeerNotary(ne event.NotaryEvent) (event.Event, error) {

return ev, nil
}

// ParseAddNodeNotary from NotaryEvent into netmap event structure.
func ParseAddNodeNotary(ne event.NotaryEvent) (event.Event, error) {
var (
ev AddNode
script []byte
v = vm.New()
)

for _, op := range ne.Params() {
script = append(script, byte(op.Code()))
script = append(script, op.Param()...)
}
v.LoadScript(script)
v.Run()
var es = v.Estack()

if es.Len() != 1 {
return nil, errors.New("incorrect argument evaluation result for addNode")
}
var err = ev.Node.FromStackItem(es.Pop().Item())
if err != nil {
return nil, err
}

Check warning on line 83 in pkg/morph/event/netmap/add_peer_notary.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/event/netmap/add_peer_notary.go#L62-L83

Added lines #L62 - L83 were not covered by tests

return ev, nil

Check warning on line 85 in pkg/morph/event/netmap/add_peer_notary.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/event/netmap/add_peer_notary.go#L85

Added line #L85 was not covered by tests
}

0 comments on commit 6e31adb

Please sign in to comment.