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

Feature/stream start, finish #111

Open
wants to merge 7 commits into
base: feature/ReplicatorOffboarding
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ type Client struct {
Contract *ContractService
Metadata *MetadataService
MetadataNem *MetadataNemService
StreamingV2 *StreamingV2Service
}

type service struct {
Expand Down Expand Up @@ -213,6 +214,7 @@ func NewClient(httpClient *http.Client, conf *Config) *Client {
c.Contract = (*ContractService)(&c.common)
c.Metadata = (*MetadataService)(&c.common)
c.MetadataNem = (*MetadataNemService)(&c.common)
c.StreamingV2 = (*StreamingV2Service)(&c.common)

return c
}
Expand Down Expand Up @@ -925,3 +927,12 @@ func (c *Client) NewMosaicRemoveLevyTransaction(deadline *Deadline, mosaicId *Mo

return tx, err
}

func (c *Client) NewStreamStartTransaction(deadline *Deadline, driveKey string, expectedUploadSize StorageSize, folder string, feedbackFeeAmount Amount) (*StreamStartTransaction, error) {
tx, err := NewStreamStartTransaction(deadline, driveKey, expectedUploadSize, folder, feedbackFeeAmount, c.config.NetworkType)
if tx != nil {
c.modifyTransaction(tx)
}

return tx, err
}
1 change: 1 addition & 0 deletions sdk/sdk_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,5 @@ const (
ByteSize VarSize = 1
ShortSize VarSize = 2
IntSize VarSize = 4
LongSize VarSize = 8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually, we use an array of IntSize for uint64/int64

)
20 changes: 12 additions & 8 deletions sdk/storagev2_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
)

type activeDataModificationDTO struct {
Id hashDto `json:"id"`
Owner string `json:"owner"`
DownloadDataCdi hashDto `json:"downloadDataCdi"`
UploadSize uint64DTO `json:"uploadSize"`
Id hashDto `json:"id"`
Owner string `json:"owner"`
DownloadDataCdi hashDto `json:"downloadDataCdi"`
ExpectedUploadSize uint64DTO `json:"ExpectedUploadSize"`
ActualUploadSize uint64DTO `json:"ActualUploadSize"`
Folder string `json:"Folder"`
}

func (ref *activeDataModificationDTO) toStruct(networkType NetworkType) (*ActiveDataModification, error) {
Expand All @@ -32,10 +34,12 @@ func (ref *activeDataModificationDTO) toStruct(networkType NetworkType) (*Active
}

return &ActiveDataModification{
Id: id,
Owner: owner,
DownloadDataCdi: downloadDataCdi,
UploadSize: ref.UploadSize.toStruct(),
Id: id,
Owner: owner,
DownloadDataCdi: downloadDataCdi,
ExpectedUploadSize: ref.ExpectedUploadSize.toStruct(),
ActualUploadSize: ref.ActualUploadSize.toStruct(),
Folder: ref.Folder,
}, nil
}

Expand Down
20 changes: 13 additions & 7 deletions sdk/storagev2_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ const (
)

type ActiveDataModification struct {
Id *Hash
Owner *PublicAccount
DownloadDataCdi *Hash
UploadSize StorageSize
Id *Hash
Owner *PublicAccount
DownloadDataCdi *Hash
ExpectedUploadSize StorageSize
ActualUploadSize StorageSize
Folder string
}

func (active *ActiveDataModification) String() string {
Expand All @@ -28,12 +30,16 @@ func (active *ActiveDataModification) String() string {
"Id": %s,
"Owner": %s,
"DownloadDataCdi": %s,
"UploadSize": %d,
"ExpectedUploadSize": %d,
"ActualUploadSize": %d,
"Folder": %s
`,
active.Id.String(),
active.Owner.String(),
active.DownloadDataCdi.String(),
active.UploadSize,
active.ExpectedUploadSize,
active.ActualUploadSize,
active.Folder,
)
}

Expand Down Expand Up @@ -171,4 +177,4 @@ type DriveClosureTransaction struct {
// Replicator Offboarding Transaction
type ReplicatorOffboardingTransaction struct {
AbstractTransaction
}
}
31 changes: 19 additions & 12 deletions sdk/storagev2_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"github.com/stretchr/testify/assert"
)


var testActiveDataModification = &ActiveDataModification{&Hash{1}, testDriveAccount, &Hash{2}, 12}
var testActiveDataModification = &ActiveDataModification{&Hash{1}, testDriveAccount, &Hash{2}, 12, 11, "foo/bar"}
var testCompletedDataModification = CompletedDataModification{testActiveDataModification, Succeeded}
var testBcDrive = BcDrive{testDriveAccount, testDriveOwnerAccount, &Hash{3}, 12, 13, 14, 3, []*ActiveDataModification{testActiveDataModification}, []*CompletedDataModification{&testCompletedDataModification}}
var testDriveInfov2 = &DriveInfo{&Hash{4}, false, 1, 1}
var driveInfoMap = map[string]*DriveInfo{"CFC31B3080B36BC3D59DF4AB936AC72F4DC15CE3C3E1B1EC5EA41415A4C33FEE" : testDriveInfov2}
var driveInfoMap = map[string]*DriveInfo{"CFC31B3080B36BC3D59DF4AB936AC72F4DC15CE3C3E1B1EC5EA41415A4C33FEE": testDriveInfov2}
var testReplicator = Replicator{testReplicatorAccount, 2, Amount(10), "blskeys", driveInfoMap}

func TestActiveDataModificationString(t *testing.T) {
Expand All @@ -21,26 +20,30 @@ func TestActiveDataModificationString(t *testing.T) {
"Id": 0100000000000000000000000000000000000000000000000000000000000000,
"Owner": Address: [Type=168, Address=VBEHMADGUUHQ6ZMCBUYARJ44647BANFFMRKLLUPF], PublicKey: "415C7C61822B063F62A4876A6F6BA2DAAE114AB298D7AC7FC56FDBA95872C309",
"DownloadDataCdi": 0200000000000000000000000000000000000000000000000000000000000000,
"UploadSize": 12,
"ExpectedUploadSize": 12,
"ActualUploadSize": 11,
"Folder": foo/bar
`)
assert.Equal(t, expectedResult, testActiveDataModification.String())
}

func TestCompletedDataModificationString(t *testing.T){
func TestCompletedDataModificationString(t *testing.T) {
expectedResult := fmt.Sprintf(
`
"ActiveDataModification":
"Id": 0100000000000000000000000000000000000000000000000000000000000000,
"Owner": Address: [Type=168, Address=VBEHMADGUUHQ6ZMCBUYARJ44647BANFFMRKLLUPF], PublicKey: "415C7C61822B063F62A4876A6F6BA2DAAE114AB298D7AC7FC56FDBA95872C309",
"DownloadDataCdi": 0200000000000000000000000000000000000000000000000000000000000000,
"UploadSize": 12,
"ExpectedUploadSize": 12,
"ActualUploadSize": 11,
"Folder": foo/bar
,
"State:" 0,
`)
assert.Equal(t, expectedResult, testCompletedDataModification.String())
}

func TestBcDriveString(t *testing.T){
func TestBcDriveString(t *testing.T) {
expectedResult := fmt.Sprintf(
`
"BcDriveAccount": Address: [Type=168, Address=VBEHMADGUUHQ6ZMCBUYARJ44647BANFFMRKLLUPF], PublicKey: "415C7C61822B063F62A4876A6F6BA2DAAE114AB298D7AC7FC56FDBA95872C309",
Expand All @@ -54,22 +57,26 @@ func TestBcDriveString(t *testing.T){
"Id": 0100000000000000000000000000000000000000000000000000000000000000,
"Owner": Address: [Type=168, Address=VBEHMADGUUHQ6ZMCBUYARJ44647BANFFMRKLLUPF], PublicKey: "415C7C61822B063F62A4876A6F6BA2DAAE114AB298D7AC7FC56FDBA95872C309",
"DownloadDataCdi": 0200000000000000000000000000000000000000000000000000000000000000,
"UploadSize": 12,
"ExpectedUploadSize": 12,
"ActualUploadSize": 11,
"Folder": foo/bar
],
"CompletedDataModifications": [
"ActiveDataModification":
"Id": 0100000000000000000000000000000000000000000000000000000000000000,
"Owner": Address: [Type=168, Address=VBEHMADGUUHQ6ZMCBUYARJ44647BANFFMRKLLUPF], PublicKey: "415C7C61822B063F62A4876A6F6BA2DAAE114AB298D7AC7FC56FDBA95872C309",
"DownloadDataCdi": 0200000000000000000000000000000000000000000000000000000000000000,
"UploadSize": 12,
"ExpectedUploadSize": 12,
"ActualUploadSize": 11,
"Folder": foo/bar
,
"State:" 0,
],
`)
assert.Equal(t, expectedResult, testBcDrive.String())
}

func TestDriveInfoString(t *testing.T){
func TestDriveInfoString(t *testing.T) {
expectedResult := fmt.Sprintf(
`
"LastApprovedDataModificationId": 0400000000000000000000000000000000000000000000000000000000000000,
Expand All @@ -80,7 +87,7 @@ func TestDriveInfoString(t *testing.T){
assert.Equal(t, expectedResult, testDriveInfov2.String())
}

func TestReplicatorString(t *testing.T){
func TestReplicatorString(t *testing.T) {
expectedResult := fmt.Sprintf(
`
ReplicatorAccount: Address: [Type=168, Address=VDQPWCXBJRL5JW4VAWVWGWXDCLREERSI24PBUSUL], PublicKey: "36E7F50C8B8BC9A4FC6325B2359E0E5DB50C75A914B5292AD726FD5AE3992691",
Expand All @@ -95,4 +102,4 @@ func TestReplicatorString(t *testing.T){
],
`)
assert.Equal(t, expectedResult, testReplicator.String())
}
}
17 changes: 16 additions & 1 deletion sdk/storagev2_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ func replicatorOffboardingTransactionSchema() *schema {
newScalarAttribute("type", ShortSize),
newArrayAttribute("maxFee", IntSize),
newArrayAttribute("deadline", IntSize),
newArrayAttribute("recipient", ByteSize),
newScalarAttribute("messageSize", ShortSize),
newScalarAttribute("numMosaics", ByteSize),
newTableAttribute("message", schema{
[]schemaAttribute{
newScalarAttribute("type", ByteSize),
newArrayAttribute("payload", ByteSize),
},
}.schemaDefinition),
newTableArrayAttribute("mosaics", schema{
[]schemaAttribute{
newArrayAttribute("id", IntSize),
newArrayAttribute("amount", IntSize),
},
}.schemaDefinition),
},
}
}
}
7 changes: 7 additions & 0 deletions sdk/streamingv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2021 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package sdk

type StreamingV2Service service
13 changes: 13 additions & 0 deletions sdk/streamingv2_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2021 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package sdk

type StreamStartTransaction struct {
AbstractTransaction
DriveKey string
ExpectedUploadSize StorageSize
Folder string
FeedbackFeeAmount Amount
}
24 changes: 24 additions & 0 deletions sdk/streamingv2_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package sdk

func streamStartTransactionSchema() *schema {
return &schema{
[]schemaAttribute{
newScalarAttribute("size", IntSize),
newArrayAttribute("signature", ByteSize),
newArrayAttribute("signer", ByteSize),
newScalarAttribute("version", IntSize),
newScalarAttribute("type", ShortSize),
newArrayAttribute("maxFee", IntSize),
newArrayAttribute("deadline", IntSize),
newArrayAttribute("driveKey", ByteSize),
newScalarAttribute("expectedUploadSize", LongSize),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look at other uint64 fields (maxFee, deadline, etc). They use IntSize array for it. So:

newArrayAttribute("expectedUploadSize", IntSize)

newScalarAttribute("folderSize", ShortSize),
newScalarAttribute("feedbackFeeAmount", LongSize),
newArrayAttribute("folder", ByteSize),
},
}
}
119 changes: 119 additions & 0 deletions sdk/streamingv2_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2021 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package sdk

import (
"encoding/hex"
flatbuffers "github.com/google/flatbuffers/go"
"github.com/proximax-storage/go-xpx-chain-sdk/transactions"
)

func NewStreamStartTransaction(
deadline *Deadline,
driveKey string,
expectedUploadSize StorageSize,
folder string,
feedbackFeeAmount Amount,
networkType NetworkType,
) (*StreamStartTransaction, error) {

tx := StreamStartTransaction{
AbstractTransaction: AbstractTransaction{
Deadline: deadline,
Version: StreamStartVersion,
Type: StreamStart,
NetworkType: networkType,
},
DriveKey: driveKey,
ExpectedUploadSize: expectedUploadSize,
Folder: folder,
FeedbackFeeAmount: feedbackFeeAmount,
}

return &tx, nil
}

func (tx *StreamStartTransaction) GetAbstractTransaction() *AbstractTransaction {
return &tx.AbstractTransaction
}

func (tx *StreamStartTransaction) String() string {
return ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgotten implementation?

}

func (tx *StreamStartTransaction) Bytes() ([]byte, error) {
builder := flatbuffers.NewBuilder(0)

bytes := []byte(tx.Folder)
fp := transactions.TransactionBufferCreateByteVector(builder, bytes)

v, signatureV, signerV, deadlineV, fV, err := tx.AbstractTransaction.generateVectors(builder)
if err != nil {
return nil, err
}

driveKeyB, err := hex.DecodeString(tx.DriveKey)
if err != nil {
return nil, err
}

driveKeyV := transactions.TransactionBufferCreateByteVector(builder, driveKeyB)

transactions.StreamStartTransactionBufferStart(builder)
transactions.TransactionBufferAddSize(builder, tx.Size())
tx.AbstractTransaction.buildVectors(builder, v, signatureV, signerV, deadlineV, fV)

transactions.StreamStartTransactionBufferAddDriveKey(builder, driveKeyV)
transactions.StreamStartTransactionBufferAddExpectedUploadSize(builder, uint64(tx.ExpectedUploadSize))
transactions.StreamStartTransactionBufferAddFolderSize(builder, uint16(tx.FolderSize()))

transactions.StreamStartTransactionBufferAddFolder(builder, fp)

transactions.StreamStartTransactionBufferAddFeedbackFeeAmount(builder, uint64(tx.FeedbackFeeAmount))

t := transactions.StreamStartTransactionBufferEnd(builder)
builder.Finish(t)

return streamStartTransactionSchema().serialize(builder.FinishedBytes()), nil
}

func (tx *StreamStartTransaction) Size() int {
return StreamStartHeaderSize + tx.FolderSize()
}

func (tx *StreamStartTransaction) FolderSize() int {
return len(tx.Folder)
}

type streamStartTransactionDTO struct {
Tx struct {
abstractTransactionDTO
DriveKey string `json:"driveKey"`
ExpectedUploadSize uint64DTO `json:"expectedUploadSize"`
Folder string `json:"folder"`
FeedbackFeeAmount uint64DTO `json:"feedbackFeeAmount"`
} `json:"transaction"`
TDto transactionInfoDTO `json:"meta"`
}

func (dto *streamStartTransactionDTO) toStruct(*Hash) (Transaction, error) {
info, err := dto.TDto.toStruct()
if err != nil {
return nil, err
}

atx, err := dto.Tx.abstractTransactionDTO.toStruct(info)
if err != nil {
return nil, err
}

return &StreamStartTransaction{
*atx,
dto.Tx.DriveKey,
dto.Tx.ExpectedUploadSize.toStruct(),
dto.Tx.Folder,
dto.Tx.FeedbackFeeAmount.toStruct(),
}, nil
}
Loading