OSDN Git Service

update dashboard
[bytom/bytom.git] / account / utxo_keeper_test.go
index dde1ee7..5b4d8a7 100644 (file)
@@ -6,10 +6,9 @@ import (
        "testing"
        "time"
 
-       dbm "github.com/tendermint/tmlibs/db"
-
        "github.com/bytom/protocol/bc"
        "github.com/bytom/testutil"
+       dbm "github.com/bytom/database/leveldb"
 )
 
 func TestAddUnconfirmedUtxo(t *testing.T) {
@@ -498,14 +497,14 @@ func TestReserve(t *testing.T) {
                                                id: 2,
                                                utxos: []*UTXO{
                                                        &UTXO{
-                                                               OutputID:  bc.NewHash([32]byte{0x02}),
+                                                               OutputID:  bc.NewHash([32]byte{0x03}),
                                                                AccountID: "testAccount",
-                                                               Amount:    5,
+                                                               Amount:    7,
                                                        },
                                                        &UTXO{
-                                                               OutputID:  bc.NewHash([32]byte{0x03}),
+                                                               OutputID:  bc.NewHash([32]byte{0x02}),
                                                                AccountID: "testAccount",
-                                                               Amount:    7,
+                                                               Amount:    5,
                                                        },
                                                },
                                                change: 4,
@@ -1017,10 +1016,7 @@ func TestOptUTXOs(t *testing.T) {
                                },
                        },
                        input: []*UTXO{
-                               &UTXO{
-                                       OutputID: bc.NewHash([32]byte{0x01}),
-                                       Amount:   1,
-                               },
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
                        },
                        inputAmount:    13,
                        wantUtxos:      []*UTXO{},
@@ -1034,33 +1030,165 @@ func TestOptUTXOs(t *testing.T) {
                                },
                        },
                        input: []*UTXO{
-                               &UTXO{
-                                       OutputID: bc.NewHash([32]byte{0x01}),
-                                       Amount:   1,
-                               },
-                               &UTXO{
-                                       OutputID: bc.NewHash([32]byte{0x02}),
-                                       Amount:   3,
-                               },
-                               &UTXO{
-                                       OutputID: bc.NewHash([32]byte{0x03}),
-                                       Amount:   5,
-                               },
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 5},
                        },
                        inputAmount: 13,
                        wantUtxos: []*UTXO{
-                               &UTXO{
-                                       OutputID: bc.NewHash([32]byte{0x02}),
-                                       Amount:   3,
-                               },
-                               &UTXO{
-                                       OutputID: bc.NewHash([32]byte{0x03}),
-                                       Amount:   5,
-                               },
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 5},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 3},
                        },
                        optAmount:      8,
                        reservedAmount: 1,
                },
+               {
+                       uk: utxoKeeper{
+                               reserved: map[bc.Hash]uint64{
+                                       bc.NewHash([32]byte{0x01}): 1,
+                                       bc.NewHash([32]byte{0x02}): 2,
+                                       bc.NewHash([32]byte{0x03}): 3,
+                               },
+                       },
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 5},
+                       },
+                       inputAmount:    1,
+                       wantUtxos:      []*UTXO{},
+                       optAmount:      0,
+                       reservedAmount: 9,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 5},
+                       },
+                       inputAmount: 1,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                       },
+                       optAmount:      1,
+                       reservedAmount: 0,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 2},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 5},
+                       },
+                       inputAmount: 5,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 2},
+                       },
+                       optAmount:      5,
+                       reservedAmount: 0,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x04}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x06}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x08}), Amount: 6},
+                       },
+                       inputAmount: 6,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x08}), Amount: 6},
+                       },
+                       optAmount:      6,
+                       reservedAmount: 0,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x04}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x06}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x08}), Amount: 6},
+                       },
+                       inputAmount: 5,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x04}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x06}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                       },
+                       optAmount:      5,
+                       reservedAmount: 0,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 5},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x07}), Amount: 7},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x11}), Amount: 11},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x13}), Amount: 13},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x23}), Amount: 23},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x31}), Amount: 31},
+                       },
+                       inputAmount: 13,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x07}), Amount: 7},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 5},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 3},
+                       },
+                       optAmount:      15,
+                       reservedAmount: 0,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                       },
+                       inputAmount: 1,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                       },
+                       optAmount:      1,
+                       reservedAmount: 0,
+               },
+               {
+                       uk: utxoKeeper{},
+                       input: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 2},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x04}), Amount: 4},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 5},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x06}), Amount: 6},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x07}), Amount: 7},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x08}), Amount: 8},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x09}), Amount: 9},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x10}), Amount: 10},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x11}), Amount: 11},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x12}), Amount: 12},
+                       },
+                       inputAmount: 15,
+                       wantUtxos: []*UTXO{
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x05}), Amount: 5},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x04}), Amount: 4},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x03}), Amount: 3},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x02}), Amount: 2},
+                               &UTXO{OutputID: bc.NewHash([32]byte{0x01}), Amount: 1},
+                       },
+                       optAmount:      15,
+                       reservedAmount: 0,
+               },
        }
 
        for i, c := range cases {