Skip to content

Commit dffba4a

Browse files
andy-shi88capt4ce
authored andcommitted
83 new node registration (#100)
* #83 add node registration implementation * #83 update node registration height type * #83 add node address length * #83 fix some field missing
1 parent ef7701b commit dffba4a

File tree

13 files changed

+1424
-328
lines changed

13 files changed

+1424
-328
lines changed

api/client/PostTransaction/client.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main() {
1919
c := rpc_service.NewTransactionServiceClient(conn)
2020

2121
response, err := c.PostTransaction(context.Background(), &rpc_model.PostTransactionRequest{
22-
// TransactionBytes: []byte{ // keep this to test multiple transaction in single block.
22+
// TransactionBytes: []byte{ // keep this to test multiple transaction in single block.
2323
// 1, 0, 1, 82, 108, 58, 93, 0, 0, 0, 0, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89,
2424
// 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, 0, 0, 66,
2525
// 67, 90, 75, 76, 118, 103, 85, 89, 90, 49, 75, 75, 120, 45, 106, 116, 70, 57, 75, 111, 74, 115, 107, 106, 86, 80, 118, 66, 57,
@@ -28,13 +28,25 @@ func main() {
2828
// 66, 6, 146, 16, 61, 104, 106, 112, 122, 96, 233, 224, 208, 119, 245, 148, 60, 9, 131, 211, 110, 68, 167, 115, 243, 251, 90, 64, 234,
2929
// 66, 108, 30, 116, 9,
3030
// },
31+
// TransactionBytes: []byte{
32+
// 1, 0, 1, 53, 119, 58, 93, 0, 0, 0, 0, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89, 107, 68,
33+
// 101, 66, 86, 70, 87, 110, 98, 121, 86, 75, 55, 118, 76, 114, 53, 79, 82, 70, 112, 84, 106, 103, 116, 78, 0, 0, 66, 67, 90, 75, 76,
34+
// 118, 103, 85, 89, 90, 49, 75, 75, 120, 45, 106, 116, 70, 57, 75, 111, 74, 115, 107, 106, 86, 80, 118, 66, 57, 106, 112, 73, 106,
35+
// 102, 122, 122, 73, 54, 122, 68, 87, 48, 74, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 16, 39, 0, 0, 0, 0, 0, 0, 32, 85, 34, 198, 89, 78,
36+
// 166, 142, 59, 148, 243, 133, 69, 66, 123, 219, 2, 3, 229, 172, 221, 35, 185, 208, 43, 44, 172, 96, 166, 116, 205, 93, 78, 194, 153,
37+
// 95, 243, 145, 108, 96, 42, 6, 186, 128, 59, 117, 83, 196, 26, 9, 15, 157, 215, 108, 180, 35, 195, 100, 7, 142, 47, 96, 108, 10,
38+
// },
39+
// add node
3140
TransactionBytes: []byte{
32-
1, 0, 1, 53, 119, 58, 93, 0, 0, 0, 0, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89, 107, 68,
33-
101, 66, 86, 70, 87, 110, 98, 121, 86, 75, 55, 118, 76, 114, 53, 79, 82, 70, 112, 84, 106, 103, 116, 78, 0, 0, 66, 67, 90, 75, 76,
34-
118, 103, 85, 89, 90, 49, 75, 75, 120, 45, 106, 116, 70, 57, 75, 111, 74, 115, 107, 106, 86, 80, 118, 66, 57, 106, 112, 73, 106,
35-
102, 122, 122, 73, 54, 122, 68, 87, 48, 74, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 16, 39, 0, 0, 0, 0, 0, 0, 32, 85, 34, 198, 89, 78,
36-
166, 142, 59, 148, 243, 133, 69, 66, 123, 219, 2, 3, 229, 172, 221, 35, 185, 208, 43, 44, 172, 96, 166, 116, 205, 93, 78, 194, 153,
37-
95, 243, 145, 108, 96, 42, 6, 186, 128, 59, 117, 83, 196, 26, 9, 15, 157, 215, 108, 180, 35, 195, 100, 7, 142, 47, 96, 108, 10,
41+
2, 0, 1, 218, 138, 66, 93, 0, 0, 0, 0, 0, 0, 66, 67, 90, 110, 83, 102, 113, 112, 80, 53, 116, 113, 70, 81, 108, 77, 84, 89, 107,
42+
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, 0, 0, 66, 67, 90, 75,
43+
76, 118, 103, 85, 89, 90, 49, 75, 75, 120, 45, 106, 116, 70, 57, 75, 111, 74, 115, 107, 106, 86, 80, 118, 66, 57, 106, 112, 73, 106,
44+
102, 122, 122, 73, 54, 122, 68, 87, 48, 74, 1, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 14, 6, 218, 170, 54, 60, 50, 2, 66, 130, 119, 226,
45+
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,
46+
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,
47+
84, 106, 103, 116, 78, 9, 49, 50, 55, 46, 48, 46, 48, 46, 49, 160, 134, 1, 0, 0, 0, 0, 0, 118, 96, 0, 82, 83, 206, 138, 84, 224, 106,
48+
207, 135, 30, 2, 186, 237, 239, 131, 229, 86, 45, 235, 250, 248, 8, 166, 83, 102, 108, 132, 208, 227, 121, 235, 59, 31, 146, 98, 125,
49+
173, 86, 83, 138, 34, 164, 165, 200, 3, 149, 209, 190, 117, 102, 152, 173, 38, 151, 0, 212, 64, 253, 97, 123, 12,
3850
},
3951
})
4052

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package transaction
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"time"
7+
8+
"github.com/zoobc/zoobc-core/common/transaction"
9+
10+
"github.com/zoobc/zoobc-core/common/crypto"
11+
12+
"github.com/sirupsen/logrus"
13+
"github.com/spf13/cobra"
14+
"github.com/zoobc/zoobc-core/common/model"
15+
"github.com/zoobc/zoobc-core/common/util"
16+
)
17+
18+
var txTypeMap = map[string][]byte{
19+
"sendMoney": {1, 0, 0, 0},
20+
"registerNode": {2, 0, 0, 0},
21+
}
22+
23+
func GenerateTransactionBytes(logger *logrus.Logger,
24+
signature crypto.SignatureInterface) *cobra.Command {
25+
var (
26+
txType string
27+
)
28+
var txCmd = &cobra.Command{
29+
Use: "tx",
30+
Short: "tx command used to generate transaction.",
31+
Long: `tx command generate signed transaction bytes in form of hex or []bytes
32+
`,
33+
Args: cobra.MinimumNArgs(1),
34+
Run: func(cmd *cobra.Command, args []string) {
35+
if args[0] == "generate" {
36+
seed := "prune filth cleaver removable earthworm tricky sulfur citation hesitate stout snort guy"
37+
38+
tx := getTransaction(txTypeMap[txType])
39+
unsignedTxBytes, _ := util.GetTransactionBytes(tx, false)
40+
tx.Signature = signature.Sign(
41+
unsignedTxBytes,
42+
tx.SenderAccountType,
43+
tx.SenderAccountAddress,
44+
seed,
45+
)
46+
signedTxBytes, _ := util.GetTransactionBytes(tx, true)
47+
var signedTxByteString string
48+
for _, b := range signedTxBytes {
49+
signedTxByteString += fmt.Sprintf("%v, ", b)
50+
}
51+
logger.Printf("tx-bytes:byte = %v", signedTxByteString)
52+
logger.Printf("tx-bytes:hex = %s", hex.EncodeToString(signedTxBytes))
53+
} else {
54+
logger.Error("unknown command")
55+
}
56+
},
57+
}
58+
txCmd.Flags().StringVarP(&txType, "type", "t", "sendMoney", "number of account to generate")
59+
return txCmd
60+
}
61+
62+
func getTransaction(txType []byte) *model.Transaction {
63+
switch util.ConvertBytesToUint32(txType) {
64+
case util.ConvertBytesToUint32(txTypeMap["sendMoney"]):
65+
amount := int64(10000)
66+
return &model.Transaction{
67+
Version: 1,
68+
TransactionType: util.ConvertBytesToUint32(txTypeMap["sendMoney"]),
69+
Timestamp: time.Now().Unix(),
70+
SenderAccountType: 0,
71+
SenderAccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
72+
RecipientAccountType: 0,
73+
RecipientAccountAddress: "BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J",
74+
Fee: 1,
75+
TransactionBodyLength: 8,
76+
TransactionBody: &model.Transaction_SendMoneyTransactionBody{
77+
SendMoneyTransactionBody: &model.SendMoneyTransactionBody{
78+
Amount: amount,
79+
},
80+
},
81+
TransactionBodyBytes: util.ConvertUint64ToBytes(uint64(amount)),
82+
}
83+
case util.ConvertBytesToUint32(txTypeMap["registerNode"]):
84+
txBody := &model.NodeRegistrationTransactionBody{
85+
AccountType: 0,
86+
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
87+
NodePublicKey: []byte{
88+
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,
89+
101, 127, 241, 62, 152, 187, 255,
90+
},
91+
NodeAddressLength: uint32(len([]byte("127.0.0.1"))),
92+
NodeAddress: "127.0.0.1",
93+
LockedBalance: 100000,
94+
}
95+
txBodyBytes := (&transaction.NodeRegistration{}).GetBodyBytes(txBody)
96+
return &model.Transaction{
97+
Version: 1,
98+
TransactionType: util.ConvertBytesToUint32(txTypeMap["registerNode"]),
99+
Timestamp: time.Now().Unix(),
100+
SenderAccountType: 0,
101+
SenderAccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
102+
RecipientAccountType: 0,
103+
RecipientAccountAddress: "BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J",
104+
Fee: 1,
105+
TransactionBodyLength: uint32(len(txBodyBytes)),
106+
TransactionBody: &model.Transaction_NodeRegistrationTransactionBody{
107+
NodeRegistrationTransactionBody: txBody,
108+
},
109+
TransactionBodyBytes: txBodyBytes,
110+
}
111+
}
112+
return nil
113+
}

cmd/transaction/transactionSendMoney.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)