Skip to content

Commit 7fd2c0e

Browse files
committed
ir/container: Handle creation request calling new CreateV2 method
Will become available with nspcc-dev/neofs-contract#534 upgrade. Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
1 parent 0b04a9b commit 7fd2c0e

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Changelog for NeoFS Node
55

66
### Added
77
- SN now supports new `getInfo` and `createV2` methods of the Container contract (#3670)
8+
- IR now supports container creation requests submitted via new `createV2` contract method (#3670)
89

910
### Fixed
1011

pkg/innerring/processors/container/handlers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import (
1111
"go.uber.org/zap"
1212
)
1313

14+
func (cp *Processor) handleCreationRequest(ev event.Event) {
15+
err := cp.pool.Submit(func() { cp.processCreateContainerRequest(ev.(containerEvent.CreateContainerV2Request)) })
16+
if err != nil {
17+
// there system can be moved into controlled degradation stage
18+
cp.log.Warn("container processor worker pool drained",
19+
zap.Int("capacity", cp.pool.Cap()))
20+
}
21+
}
22+
1423
func (cp *Processor) handlePut(ev event.Event) {
1524
req, ok := ev.(containerEvent.CreateContainerRequest)
1625
if !ok {

pkg/innerring/processors/container/process_container.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
99
"github.com/nspcc-dev/neo-go/pkg/network/payload"
10+
fschaincontracts "github.com/nspcc-dev/neofs-node/pkg/morph/contracts"
1011
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
1112
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
1213
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
@@ -15,6 +16,31 @@ import (
1516
"go.uber.org/zap"
1617
)
1718

19+
func (cp *Processor) processCreateContainerRequest(req containerEvent.CreateContainerV2Request) {
20+
if !cp.alphabetState.IsAlphabet() {
21+
cp.log.Info("non alphabet mode, ignore container creation request")
22+
return
23+
}
24+
25+
cnr, err := fschaincontracts.ContainerFromStruct(req.Container)
26+
if err != nil {
27+
cp.log.Error("invalid container struct in creation request", zap.Error(err))
28+
return
29+
}
30+
31+
cnrBytes := cnr.Marshal()
32+
id := cid.NewFromMarshalledContainer(cnrBytes)
33+
34+
err = cp.checkPutContainer(cnr, cnrBytes, req.SessionToken, req.InvocationScript, req.VerificationScript, "", "")
35+
if err != nil {
36+
cp.log.Error("container creation request failed check",
37+
zap.Stringer("container", id), zap.Error(err))
38+
return
39+
}
40+
41+
cp.approvePutContainer(req.MainTransaction, cnr, id)
42+
}
43+
1844
// putEvent is a common interface of Put and PutNamed event.
1945
type putEvent interface {
2046
event.Event

pkg/innerring/processors/container/processor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ func (cp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
137137
p.SetParser(containerEvent.RestoreCreateContainerRequest)
138138
pp = append(pp, p)
139139

140+
p.SetRequestType(fschaincontracts.CreateContainerV2Method)
141+
p.SetParser(containerEvent.RestoreCreateContainerV2Request)
142+
pp = append(pp, p)
143+
140144
// container delete
141145
p.SetRequestType(containerEvent.DeleteNotaryEvent)
142146
p.SetParser(containerEvent.ParseDeleteNotary)
@@ -194,6 +198,10 @@ func (cp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
194198
h.SetHandler(cp.handlePut)
195199
hh = append(hh, h)
196200

201+
h.SetRequestType(fschaincontracts.CreateContainerV2Method)
202+
h.SetHandler(cp.handleCreationRequest)
203+
hh = append(hh, h)
204+
197205
// container delete
198206
h.SetRequestType(containerEvent.DeleteNotaryEvent)
199207
h.SetHandler(cp.handleDelete)

pkg/morph/contracts/models.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func ContainerFromStackItem(item stackitem.Item) (container.Container, error) {
2222
return container.Container{}, err
2323
}
2424

25+
return ContainerFromStruct(contractStruct)
26+
}
27+
28+
// ContainerFromStruct decodes container from contract structure.
29+
func ContainerFromStruct(contractStruct containerrpc.ContainerInfo) (container.Container, error) {
2530
mjr, mnr, err := containerVersionFromStruct(contractStruct.Version)
2631
if err != nil {
2732
return container.Container{}, err

pkg/morph/event/container/notary_requests.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,85 @@ import (
44
"fmt"
55

66
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
7+
"github.com/nspcc-dev/neo-go/pkg/vm"
78
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
9+
containerrpc "github.com/nspcc-dev/neofs-contract/rpc/container"
810
fschaincontracts "github.com/nspcc-dev/neofs-node/pkg/morph/contracts"
911
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
1012
)
1113

1214
func getArgsFromEvent(ne event.NotaryEvent, expectedNum int) ([]event.Op, error) {
1315
args := ne.Params()
1416
if len(args) != expectedNum {
15-
return nil, fmt.Errorf("wrong/unsupported arg num %d instead of %d", len(args), expectedNum)
17+
return nil, newWrongArgNumError(expectedNum, len(args))
1618
}
1719
return args, nil
1820
}
1921

2022
func getValueFromArg[T any](args []event.Op, i int, desc string, typ stackitem.Type, f func(event.Op) (T, error)) (v T, err error) {
2123
v, err = f(args[i])
2224
if err != nil {
23-
return v, fmt.Errorf("arg#%d (%s, %s): %w", i, typ, desc, err)
25+
return v, wrapInvalidArgError(i, typ, desc, err)
2426
}
2527
return v, nil
2628
}
2729

30+
func wrapInvalidArgError(i int, typ stackitem.Type, desc string, err error) error {
31+
return fmt.Errorf("arg#%d (%s, %s): %w", i, typ, desc, err)
32+
}
33+
34+
func newWrongArgNumError(expected, actual int) error {
35+
return fmt.Errorf("wrong/unsupported arg num %d instead of %d", actual, expected)
36+
}
37+
38+
// CreateContainerRequest wraps container creation request to provide
39+
// app-internal event.
40+
type CreateContainerV2Request struct {
41+
event.Event
42+
MainTransaction transaction.Transaction
43+
44+
Container containerrpc.ContainerInfo
45+
InvocationScript []byte
46+
VerificationScript []byte
47+
SessionToken []byte
48+
}
49+
50+
// RestoreCreateContainerV2Request restores [CreateContainerV2Request] from the
51+
// notary one.
52+
func RestoreCreateContainerV2Request(notaryReq event.NotaryEvent) (event.Event, error) {
53+
testVM := vm.New()
54+
testVM.LoadScript(notaryReq.ArgumentScript())
55+
56+
if err := testVM.Run(); err != nil {
57+
return nil, fmt.Errorf("exec script on test VM: %w", err)
58+
}
59+
60+
stack := testVM.Estack()
61+
const argNum = 4
62+
if got := stack.Len(); got != argNum {
63+
return nil, newWrongArgNumError(argNum, got)
64+
}
65+
66+
var res CreateContainerV2Request
67+
var err error
68+
69+
if err = res.Container.FromStackItem(stack.Pop().Item()); err != nil {
70+
return nil, wrapInvalidArgError(argNum-1, stackitem.StructT, "container", err)
71+
}
72+
if res.InvocationScript, err = stack.Pop().Item().TryBytes(); err != nil {
73+
return nil, wrapInvalidArgError(argNum-2, stackitem.ByteArrayT, "invocation script", err)
74+
}
75+
if res.VerificationScript, err = stack.Pop().Item().TryBytes(); err != nil {
76+
return nil, wrapInvalidArgError(argNum-3, stackitem.ByteArrayT, "verification script", err)
77+
}
78+
if res.SessionToken, err = stack.Pop().Item().TryBytes(); err != nil {
79+
return nil, wrapInvalidArgError(argNum-4, stackitem.ByteArrayT, "session token", err)
80+
}
81+
res.MainTransaction = *notaryReq.Raw().MainTransaction
82+
83+
return res, nil
84+
}
85+
2886
// CreateContainerRequest wraps container creation request to provide
2987
// app-internal event.
3088
type CreateContainerRequest struct {

0 commit comments

Comments
 (0)