• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision7928c2ed391273f82803e8b72a6a7c653b10f3f3 (tree)
Time2019-07-23 10:33:14
Authormars <mars@byto...>
Commitermars

Log Message

add unit test

Change Summary

Incremental Difference

--- /dev/null
+++ b/toolbar/api_node/node_test.go
@@ -0,0 +1,70 @@
1+package api_node
2+
3+import (
4+ "encoding/json"
5+ "testing"
6+
7+ "github.com/vapor/consensus"
8+ "github.com/vapor/errors"
9+ "github.com/vapor/protocol/bc"
10+)
11+
12+func buildTxRequest(accountID string, outputs map[string]uint64) ([]byte, error) {
13+ totalBTM := uint64(10000000)
14+ actions := []interface{}{}
15+ for address, amount := range outputs {
16+ actions = append(actions, &ControlAddressAction{
17+ Address: address,
18+ AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: amount},
19+ })
20+ totalBTM += amount
21+ }
22+
23+ actions = append(actions, &SpendAccountAction{
24+ AccountID: accountID,
25+ AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: totalBTM},
26+ })
27+ payload, err := json.Marshal(&buildTxReq{Actions: actions})
28+ if err != nil {
29+ return nil, errors.Wrap(err, "Marshal spend request")
30+ }
31+
32+ return payload, nil
33+}
34+
35+type args struct {
36+ accountID string
37+ outputs map[string]uint64
38+}
39+
40+func TestBuildTxRequest(t *testing.T) {
41+ cases := []struct {
42+ args args
43+ want string
44+ }{
45+ {
46+ args: args{
47+ accountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
48+ outputs: map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100},
49+ },
50+ want: `{"actions":[{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":10000100}]}`,
51+ },
52+ {
53+ args: args{
54+ accountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
55+ outputs: map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100, "sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28": 200},
56+ },
57+ want: `{"actions":[{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"control_address","address":"sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":200},{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":10000300}]}`,
58+ },
59+ }
60+
61+ for i, c := range cases {
62+ tx, err := buildTxRequest(c.args.accountID, c.args.outputs)
63+ if err != nil {
64+ t.Fatal(err)
65+ }
66+ if string(tx) != string(c.want) {
67+ t.Fatal(i, string(tx))
68+ }
69+ }
70+}
Show on old repository browser