Skip to content

83 new noderegistration fixbodybytes #126

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

Merged
merged 10 commits into from
Aug 6, 2019
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ issues:
- text: "lines are duplicate of"
linters:
- dupl
- text: "U1000: func `TransactionBodyInterface.isTxBody` is unused"
linters:
- unused
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
Expand Down
178 changes: 86 additions & 92 deletions api/service/transactionApiService_test.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion cmd/transaction/transactionGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func getTransaction(txType []byte) *model.Transaction {
NodeAddress: "127.0.0.1",
LockedBalance: 100000,
}
txBodyBytes := (&transaction.NodeRegistration{}).GetBodyBytes(txBody)
txBodyBytes := (&transaction.NodeRegistration{
Body: txBody,
}).GetBodyBytes()
return &model.Transaction{
Version: 1,
TransactionType: util.ConvertBytesToUint32(txTypeMap["registerNode"]),
Expand Down
9 changes: 8 additions & 1 deletion common/constant/fieldsSize.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ var (
BlockHash uint32 = 64
Height uint32 = 4
// NodeSignature node always signs using Ed25519 algorithm, that produces 64 bytes signatures
NodeSignature uint32 = 64
NodeSignature uint32 = 64
TransactionType uint32 = 4
TransactionVersion uint32 = 1
Timestamp uint32 = 8
Fee uint32 = 8
TransactionBodyLength uint32 = 4
// AccountSignature TODO: this is valid for signatures using Ed25519. in future we might have more implementations
AccountSignature uint32 = 64
)
10 changes: 10 additions & 0 deletions common/model/transactionBody.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package model

// type TransactionBodyInterface = isTransaction_TransactionBody
type TransactionBodyInterface interface {
isTxBody()
}

func (*NodeRegistrationTransactionBody) isTxBody() {}
func (*EmptyTransactionBody) isTxBody() {}
func (*SendMoneyTransactionBody) isTxBody() {}
10 changes: 10 additions & 0 deletions common/transaction/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ func (*TXEmpty) GetAmount() int64 {
func (tx *TXEmpty) GetSize() uint32 {
return 0
}

// ParseBodyBytes read and translate body bytes to body implementation fields
func (*TXEmpty) ParseBodyBytes(txBodyBytes []byte) model.TransactionBodyInterface {
return &model.EmptyTransactionBody{}
}

// GetBodyBytes translate tx body to bytes representation
func (*TXEmpty) GetBodyBytes() []byte {
return []byte{}
}
50 changes: 50 additions & 0 deletions common/transaction/fixtureGenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package transaction

import (
"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/util"
)

func GetFixtures() (poownMessage *model.ProofOfOwnershipMessage, poown *model.ProofOfOwnership,
txBody *model.NodeRegistrationTransactionBody, txBodyBytes []byte) {

senderAccountType := uint32(0)
senderAddress := "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN"
senderSeed := "prune filth cleaver removable earthworm tricky sulfur citation hesitate stout snort guy"
poownMessage = &model.ProofOfOwnershipMessage{
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
BlockHash: []byte{0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89,
107, 68, 101, 66, 86, 70, 87, 110, 98, 121, 86, 75},
BlockHeight: 0,
}
poownMessageBytes := util.GetProofOfOwnershipMessageBytes(poownMessage)
poownSignature := crypto.NewSignature().Sign(
poownMessageBytes,
senderAccountType,
senderAddress,
senderSeed,
)
poown = &model.ProofOfOwnership{
MessageBytes: poownMessageBytes,
Signature: poownSignature,
}
txBody = &model.NodeRegistrationTransactionBody{
NodePublicKey: []byte{0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43,
63, 224, 101, 127, 241, 62, 152, 187, 255},
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
RegistrationHeight: 0,
NodeAddressLength: 9,
NodeAddress: "10.10.0.1",
LockedBalance: 10000000000,
Poown: poown,
}
nr := NodeRegistration{
Body: txBody,
}
txBodyBytes = nr.GetBodyBytes()
return poownMessage, poown, txBody, txBodyBytes
}
53 changes: 29 additions & 24 deletions common/transaction/nodeRegistration.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,40 +169,45 @@ func (tx *NodeRegistration) GetAmount() int64 {
func (tx *NodeRegistration) GetSize() uint32 {
nodeAddress := tx.Body.NodeAddressLength
// ProofOfOwnership (message + signature)
poown := util.GetProofOfOwnershipMessageSize(true)
poown := util.GetProofOfOwnershipSize(true)
return constant.NodePublicKey + constant.AccountType + constant.NodeAddressLength + constant.AccountAddress +
constant.Balance + nodeAddress + poown
}

// ParseBodyBytes read and translate body bytes to body implementation fields
func (*NodeRegistration) ParseBodyBytes(txBodyBytes []byte) *model.NodeRegistrationTransactionBody {
func (*NodeRegistration) ParseBodyBytes(txBodyBytes []byte) model.TransactionBodyInterface {
buffer := bytes.NewBuffer(txBodyBytes)
nodePublicKey := buffer.Next(32)
accountTypeBytes := buffer.Next(2)
accountType := util.ConvertBytesToUint32([]byte{accountTypeBytes[0], accountTypeBytes[1], 0, 0})
accountAddressBytes := buffer.Next(44)
nodeAddressLength := util.ConvertBytesToUint32([]byte{buffer.Next(1)[0], 0, 0, 0}) // uint32 length of next bytes to read
nodeAddress := buffer.Next(int(nodeAddressLength)) // based on nodeAddressLength
lockedBalance := util.ConvertBytesToUint64(buffer.Next(8))
return &model.NodeRegistrationTransactionBody{
NodePublicKey: nodePublicKey,
AccountType: accountType,
AccountAddress: string(accountAddressBytes),
NodeAddressLength: nodeAddressLength,
NodeAddress: string(nodeAddress),
LockedBalance: int64(lockedBalance),
nodePublicKey := buffer.Next(int(constant.NodePublicKey))
accountType := util.ConvertBytesToUint32(buffer.Next(int(constant.AccountType)))
accountAddress := buffer.Next(int(constant.AccountAddress))
nodeRegistrationHeight := util.ConvertBytesToUint32(buffer.Next(int(constant.Height)))
nodeAddressLength := util.ConvertBytesToUint32(buffer.Next(int(constant.NodeAddressLength))) // uint32 length of next bytes to read
nodeAddress := buffer.Next(int(nodeAddressLength)) // based on nodeAddressLength
lockedBalance := util.ConvertBytesToUint64(buffer.Next(int(constant.Balance)))
poown := util.ParseProofOfOwnershipBytes(buffer.Next(int(util.GetProofOfOwnershipSize(true))))
txBody := &model.NodeRegistrationTransactionBody{
NodePublicKey: nodePublicKey,
AccountType: accountType,
AccountAddress: string(accountAddress),
RegistrationHeight: nodeRegistrationHeight,
NodeAddressLength: nodeAddressLength,
NodeAddress: string(nodeAddress),
LockedBalance: int64(lockedBalance),
Poown: poown,
}
return txBody
}

// GetBodyBytes translate tx body to bytes representation
func (*NodeRegistration) GetBodyBytes(txBody *model.NodeRegistrationTransactionBody) []byte {
func (tx *NodeRegistration) GetBodyBytes() []byte {
buffer := bytes.NewBuffer([]byte{})
buffer.Write(txBody.NodePublicKey)
buffer.Write(util.ConvertUint32ToBytes(txBody.AccountType)[:2])
buffer.Write([]byte(txBody.AccountAddress))
addressLengthBytes := util.ConvertUint32ToBytes(txBody.NodeAddressLength)
buffer.Write([]byte{addressLengthBytes[0]})
buffer.Write([]byte(txBody.NodeAddress))
buffer.Write(util.ConvertUint64ToBytes(uint64(txBody.LockedBalance)))
buffer.Write(tx.Body.NodePublicKey)
buffer.Write(util.ConvertUint32ToBytes(tx.Body.AccountType))
buffer.Write([]byte(tx.Body.AccountAddress))
buffer.Write(util.ConvertUint32ToBytes(tx.Body.RegistrationHeight))
buffer.Write(util.ConvertUint32ToBytes(tx.Body.NodeAddressLength))
buffer.Write([]byte(tx.Body.NodeAddress))
buffer.Write(util.ConvertUint64ToBytes(uint64(tx.Body.LockedBalance)))
buffer.Write(util.GetProofOfOwnershipBytes(tx.Body.Poown))
return buffer.Bytes()
}
47 changes: 12 additions & 35 deletions common/transaction/nodeRegistration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ func TestNodeRegistration_GetSize(t *testing.T) {
}

func TestNodeRegistration_ParseBodyBytes(t *testing.T) {
_, _, body, bodyBytes := GetFixtures()
// bodyBytes :=
type fields struct {
Body *model.NodeRegistrationTransactionBody
Fee int64
Expand All @@ -693,23 +695,9 @@ func TestNodeRegistration_ParseBodyBytes(t *testing.T) {
name: "ParseBodyBytes:success",
fields: fields{},
args: args{
txBodyBytes: []byte{0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89,
107, 68, 101, 66, 86, 70, 87, 110, 98, 121, 86, 75, 55, 118, 76, 114, 53, 79, 82, 70, 112, 84, 106, 103, 116, 78, 9,
49, 50, 55, 46, 48, 46, 48, 46, 49, 160, 134, 1, 0, 0, 0, 0, 0,
},
},
want: &model.NodeRegistrationTransactionBody{
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
NodePublicKey: []byte{
0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255,
},
NodeAddressLength: uint32(len([]byte("127.0.0.1"))),
NodeAddress: "127.0.0.1",
LockedBalance: 100000,
txBodyBytes: bodyBytes,
},
want: body,
},
}
for _, tt := range tests {
Expand All @@ -733,6 +721,7 @@ func TestNodeRegistration_ParseBodyBytes(t *testing.T) {
}

func TestNodeRegistration_GetBodyBytes(t *testing.T) {
_, _, body, bodyBytes := GetFixtures()
type fields struct {
Body *model.NodeRegistrationTransactionBody
Fee int64
Expand All @@ -754,26 +743,14 @@ func TestNodeRegistration_GetBodyBytes(t *testing.T) {
want []byte
}{
{
name: "GetBodyBytes:success",
fields: fields{},
args: args{
txBody: &model.NodeRegistrationTransactionBody{
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
NodePublicKey: []byte{
0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255,
},
NodeAddressLength: uint32(len([]byte("127.0.0.1"))),
NodeAddress: "127.0.0.1",
LockedBalance: 100000,
},
name: "GetBodyBytes:success",
fields: fields{
Body: body,
},
want: []byte{0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89,
107, 68, 101, 66, 86, 70, 87, 110, 98, 121, 86, 75, 55, 118, 76, 114, 53, 79, 82, 70, 112, 84, 106, 103, 116, 78, 9,
49, 50, 55, 46, 48, 46, 48, 46, 49, 160, 134, 1, 0, 0, 0, 0, 0,
args: args{
txBody: body,
},
want: bodyBytes,
},
}
for _, tt := range tests {
Expand All @@ -789,7 +766,7 @@ func TestNodeRegistration_GetBodyBytes(t *testing.T) {
NodeRegistrationQuery: tt.fields.NodeRegistrationQuery,
QueryExecutor: tt.fields.QueryExecutor,
}
if got := n.GetBodyBytes(tt.args.txBody); !reflect.DeepEqual(got, tt.want) {
if got := n.GetBodyBytes(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NodeRegistration.GetBodyBytes() = %v, want %v", got, tt.want)
}
})
Expand Down
6 changes: 3 additions & 3 deletions common/transaction/sendMoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,16 @@ func (*SendMoney) GetSize() uint32 {
}

// ParseBodyBytes read and translate body bytes to body implementation fields
func (*SendMoney) ParseBodyBytes(txBodyBytes []byte) *model.SendMoneyTransactionBody {
func (*SendMoney) ParseBodyBytes(txBodyBytes []byte) model.TransactionBodyInterface {
amount := util.ConvertBytesToUint64(txBodyBytes)
return &model.SendMoneyTransactionBody{
Amount: int64(amount),
}
}

// GetBodyBytes translate tx body to bytes representation
func (*SendMoney) GetBodyBytes(txBody *model.SendMoneyTransactionBody) []byte {
func (tx *SendMoney) GetBodyBytes() []byte {
buffer := bytes.NewBuffer([]byte{})
buffer.Write(util.ConvertUint64ToBytes(uint64(txBody.Amount)))
buffer.Write(util.ConvertUint64ToBytes(uint64(tx.Body.Amount)))
return buffer.Bytes()
}
8 changes: 6 additions & 2 deletions common/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type (
Validate() error
GetAmount() int64
GetSize() uint32
ParseBodyBytes(txBodyBytes []byte) model.TransactionBodyInterface
GetBodyBytes() []byte
}
TypeActionSwitcher interface {
GetTransactionType(tx *model.Transaction) TypeAction
Expand All @@ -36,8 +38,9 @@ func (ts *TypeSwitcher) GetTransactionType(tx *model.Transaction) TypeAction {
case 1:
switch buf[1] {
case 0:
sendMoneyBody := new(SendMoney).ParseBodyBytes(tx.TransactionBodyBytes)
return &SendMoney{
Body: (&SendMoney{}).ParseBodyBytes(tx.TransactionBodyBytes),
Body: sendMoneyBody.(*model.SendMoneyTransactionBody),
Fee: tx.Fee,
SenderAddress: tx.GetSenderAccountAddress(),
SenderAccountType: tx.GetSenderAccountType(),
Expand All @@ -54,8 +57,9 @@ func (ts *TypeSwitcher) GetTransactionType(tx *model.Transaction) TypeAction {
case 2:
switch buf[1] {
case 0:
nodeRegistrationBody := new(NodeRegistration).ParseBodyBytes(tx.TransactionBodyBytes)
return &NodeRegistration{
Body: (&NodeRegistration{}).ParseBodyBytes(tx.TransactionBodyBytes),
Body: nodeRegistrationBody.(*model.NodeRegistrationTransactionBody),
Fee: tx.Fee,
SenderAddress: tx.GetSenderAccountAddress(),
SenderAccountType: tx.GetSenderAccountType(),
Expand Down
46 changes: 8 additions & 38 deletions common/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestTypeSwitcher_GetTransactionType(t *testing.T) {
_, _, nodeRegistrationBody, nodeRegistrationBodyBytes := GetFixtures()
type fields struct {
Executor query.ExecutorInterface
}
Expand Down Expand Up @@ -138,48 +139,17 @@ func TestTypeSwitcher_GetTransactionType(t *testing.T) {
RecipientAccountType: 0,
RecipientAccountAddress: "",
TransactionBody: &model.Transaction_NodeRegistrationTransactionBody{
NodeRegistrationTransactionBody: &model.NodeRegistrationTransactionBody{
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
NodePublicKey: []byte{
0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255,
},
NodeAddressLength: uint32(len([]byte("127.0.0.1"))),
NodeAddress: "127.0.0.1",
LockedBalance: 100000,
},
},
TransactionType: binary.LittleEndian.Uint32([]byte{2, 0, 0, 0}),
TransactionBodyBytes: (&NodeRegistration{}).GetBodyBytes(&model.NodeRegistrationTransactionBody{
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
NodePublicKey: []byte{
0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255,
},
NodeAddressLength: uint32(len([]byte("127.0.0.1"))),
NodeAddress: "127.0.0.1",
LockedBalance: 100000,
NodeRegistrationTransactionBody: nodeRegistrationBody,
},
),
TransactionType: binary.LittleEndian.Uint32([]byte{2, 0, 0, 0}),
TransactionBodyBytes: nodeRegistrationBodyBytes,
},
},
want: &NodeRegistration{
Height: 0,
SenderAccountType: 0,
SenderAddress: "BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE",
Body: &model.NodeRegistrationTransactionBody{
AccountType: 0,
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
NodePublicKey: []byte{
0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226, 235, 126, 203, 5, 12, 152, 194, 170, 146, 43, 63, 224,
101, 127, 241, 62, 152, 187, 255,
},
NodeAddressLength: uint32(len([]byte("127.0.0.1"))),
NodeAddress: "127.0.0.1",
LockedBalance: 100000,
},
Height: 0,
SenderAccountType: 0,
SenderAddress: "BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE",
Body: nodeRegistrationBody,
QueryExecutor: &query.Executor{},
AccountBalanceQuery: query.NewAccountBalanceQuery(),
AccountQuery: query.NewAccountQuery(),
Expand Down
14 changes: 14 additions & 0 deletions common/util/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package util

import (
"bytes"
"errors"
)

func ReadTransactionBytes(buf *bytes.Buffer, nBytes int) ([]byte, error) {
nextBytes := buf.Next(nBytes)
if len(nextBytes) < nBytes {
return nil, errors.New("EndOfBufferReached")
}
return nextBytes, nil
}
Loading