OSDN Git Service

modify MergeSpendAction order by origin action list (#1048)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Thu, 21 Jun 2018 02:38:28 +0000 (10:38 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 21 Jun 2018 02:38:27 +0000 (10:38 +0800)
* modify MergeSpendAction order by origin action list

* optimise

account/builder.go
account/builder_test.go
api/transact.go

index ca9fac6..5672085 100644 (file)
@@ -32,30 +32,25 @@ type spendAction struct {
 }
 
 // MergeSpendAction merge common assetID and accountID spend action
-func MergeSpendAction(spendActions []txbuilder.Action) []txbuilder.Action {
-       actions := []txbuilder.Action{}
-       actionMap := make(map[string]*spendAction)
+func MergeSpendAction(actions []txbuilder.Action) []txbuilder.Action {
+       resultActions := []txbuilder.Action{}
+       spendActionMap := make(map[string]*spendAction)
 
-       for _, act := range spendActions {
+       for _, act := range actions {
                switch act := act.(type) {
                case *spendAction:
                        actionKey := act.AssetId.String() + act.AccountID
-                       if tmpAct, ok := actionMap[actionKey]; ok {
+                       if tmpAct, ok := spendActionMap[actionKey]; ok {
                                tmpAct.Amount += act.Amount
                        } else {
-                               actionMap[actionKey] = act
+                               spendActionMap[actionKey] = act
+                               resultActions = append(resultActions, act)
                        }
                default:
-                       actions = append(actions, act)
+                       resultActions = append(resultActions, act)
                }
        }
-
-       for actKey := range actionMap {
-               spend := actionMap[actKey]
-               actions = append(actions, txbuilder.Action(spend))
-       }
-
-       return actions
+       return resultActions
 }
 
 func (a *spendAction) Build(ctx context.Context, b *txbuilder.TemplateBuilder) error {
index b522f3e..9ebe5ac 100644 (file)
@@ -252,6 +252,9 @@ func TestMergeSpendAction(t *testing.T) {
                },
                {
                        testActions: []txbuilder.Action{
+                               txbuilder.Action(&spendUTXOAction{
+                                       OutputID: &bc.Hash{V0: 128},
+                               }),
                                txbuilder.Action(&spendAction{
                                        AssetAmount: bc.AssetAmount{
                                                AssetId: testBTM,
@@ -266,6 +269,9 @@ func TestMergeSpendAction(t *testing.T) {
                                        },
                                        AccountID: "test_account1",
                                }),
+                               txbuilder.Action(&spendUTXOAction{
+                                       OutputID: &bc.Hash{V0: 256},
+                               }),
                                txbuilder.Action(&spendAction{
                                        AssetAmount: bc.AssetAmount{
                                                AssetId: testAssetID2,
@@ -275,6 +281,9 @@ func TestMergeSpendAction(t *testing.T) {
                                }),
                        },
                        wantActions: []txbuilder.Action{
+                               txbuilder.Action(&spendUTXOAction{
+                                       OutputID: &bc.Hash{V0: 128},
+                               }),
                                txbuilder.Action(&spendAction{
                                        AssetAmount: bc.AssetAmount{
                                                AssetId: testBTM,
@@ -289,6 +298,9 @@ func TestMergeSpendAction(t *testing.T) {
                                        },
                                        AccountID: "test_account1",
                                }),
+                               txbuilder.Action(&spendUTXOAction{
+                                       OutputID: &bc.Hash{V0: 256},
+                               }),
                                txbuilder.Action(&spendAction{
                                        AssetAmount: bc.AssetAmount{
                                                AssetId: testAssetID2,
@@ -297,8 +309,8 @@ func TestMergeSpendAction(t *testing.T) {
                                        AccountID: "test_account2",
                                }),
                        },
-                       testActionCount: 3,
-                       wantActionCount: 3,
+                       testActionCount: 5,
+                       wantActionCount: 5,
                },
        }
 
index 63c61e5..befa286 100644 (file)
@@ -59,7 +59,6 @@ func (a *API) buildSingle(ctx context.Context, req *BuildRequest) (*txbuilder.Te
                return nil, errors.New("transaction only contain spend actions, didn't have output actions")
        }
 
-       spendActions := []txbuilder.Action{}
        actions := make([]txbuilder.Action, 0, len(req.Actions))
        for i, act := range req.Actions {
                typ, ok := act["type"].(string)
@@ -81,14 +80,9 @@ func (a *API) buildSingle(ctx context.Context, req *BuildRequest) (*txbuilder.Te
                if err != nil {
                        return nil, errors.WithDetailf(errBadAction, "%s on action %d", err.Error(), i)
                }
-
-               if typ == "spend_account" {
-                       spendActions = append(spendActions, action)
-               } else {
-                       actions = append(actions, action)
-               }
+               actions = append(actions, action)
        }
-       actions = append(account.MergeSpendAction(spendActions), actions...)
+       actions = account.MergeSpendAction(actions)
 
        ttl := req.TTL.Duration
        if ttl == 0 {