package account
import (
- "encoding/json"
- "os"
"testing"
"time"
- "github.com/vapor/database"
- dbm "github.com/vapor/database/leveldb"
"github.com/vapor/protocol/bc"
"github.com/vapor/testutil"
)
}
}
-func TestReserve(t *testing.T) {
- currentHeight := func() uint64 { return 9527 }
- testDB := dbm.NewDB("testdb", "leveldb", "temp")
- defer func() {
- testDB.Close()
- os.RemoveAll("temp")
- }()
-
- cases := []struct {
- before utxoKeeper
- after utxoKeeper
- err error
- reserveAmount uint64
- exp time.Time
- vote []byte
- }{
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- reserveAmount: 1,
- err: ErrInsufficient,
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- reserveAmount: 4,
- err: ErrInsufficient,
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- ValidHeight: 9528,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- ValidHeight: 9528,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- reserveAmount: 3,
- err: ErrImmature,
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 0,
- },
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 0,
- },
- reservations: map[uint64]*reservation{},
- },
- reserveAmount: 3,
- err: ErrReserved,
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 1,
- },
- reservations: map[uint64]*reservation{
- 1: &reservation{
- id: 1,
- utxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- change: 1,
- expiry: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- },
- },
- reserveAmount: 2,
- err: nil,
- exp: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- nextIndex: 1,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- bc.NewHash([32]byte{0x02}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 5,
- },
- bc.NewHash([32]byte{0x03}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x03}),
- AccountID: "testAccount",
- Amount: 7,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 1,
- },
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- bc.NewHash([32]byte{0x02}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 5,
- },
- bc.NewHash([32]byte{0x03}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x03}),
- AccountID: "testAccount",
- Amount: 7,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 1,
- bc.NewHash([32]byte{0x02}): 2,
- bc.NewHash([32]byte{0x03}): 2,
- },
- reservations: map[uint64]*reservation{
- 2: &reservation{
- id: 2,
- utxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x03}),
- AccountID: "testAccount",
- Amount: 7,
- },
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 5,
- },
- },
- change: 4,
- expiry: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- },
- },
- reserveAmount: 8,
- err: nil,
- exp: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- Vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- Vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 1,
- },
- reservations: map[uint64]*reservation{
- 1: &reservation{
- id: 1,
- utxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- Vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- },
- change: 1,
- expiry: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- },
- },
- reserveAmount: 2,
- err: nil,
- exp: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- }
-
- for i, c := range cases {
- if _, err := c.before.Reserve("testAccount", &bc.AssetID{}, c.reserveAmount, true, c.vote, c.exp); err != c.err {
- t.Errorf("case %d: got error %v want error %v", i, err, c.err)
- }
- checkUtxoKeeperEqual(t, i, &c.before, &c.after)
- }
-}
-
-func TestReserveParticular(t *testing.T) {
- currentHeight := func() uint64 { return 9527 }
- testDB := dbm.NewDB("testdb", "leveldb", "temp")
- defer os.RemoveAll("temp")
-
- cases := []struct {
- before utxoKeeper
- after utxoKeeper
- err error
- reserveHash bc.Hash
- exp time.Time
- }{
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 0,
- },
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 0,
- },
- reservations: map[uint64]*reservation{},
- },
- reserveHash: bc.NewHash([32]byte{0x01}),
- err: ErrReserved,
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- ValidHeight: 9528,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- ValidHeight: 9528,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- reserveHash: bc.NewHash([32]byte{0x01}),
- err: ErrImmature,
- },
- {
- before: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{},
- reservations: map[uint64]*reservation{},
- },
- after: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- reserved: map[bc.Hash]uint64{
- bc.NewHash([32]byte{0x01}): 1,
- },
- reservations: map[uint64]*reservation{
- 1: &reservation{
- id: 1,
- utxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- change: 0,
- expiry: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- },
- },
- reserveHash: bc.NewHash([32]byte{0x01}),
- err: nil,
- exp: time.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- }
-
- for i, c := range cases {
- if _, err := c.before.ReserveParticular(c.reserveHash, true, c.exp); err != c.err {
- t.Errorf("case %d: got error %v want error %v", i, err, c.err)
- }
- checkUtxoKeeperEqual(t, i, &c.before, &c.after)
- }
-}
-
func TestExpireReservation(t *testing.T) {
before := &utxoKeeper{
reservations: map[uint64]*reservation{
checkUtxoKeeperEqual(t, 0, before, after)
}
-func TestFindUtxos(t *testing.T) {
- currentHeight := func() uint64 { return 9527 }
- testDB := dbm.NewDB("testdb", "leveldb", "temp")
- defer func() {
- testDB.Close()
- os.RemoveAll("temp")
- }()
-
- cases := []struct {
- uk utxoKeeper
- dbUtxos []*UTXO
- useUnconfirmed bool
- wantUtxos []*UTXO
- immatureAmount uint64
- vote []byte
- }{
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: []*UTXO{},
- useUnconfirmed: true,
- wantUtxos: []*UTXO{},
- immatureAmount: 0,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- AssetID: bc.AssetID{V0: 6},
- Amount: 3,
- },
- },
- useUnconfirmed: false,
- wantUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- immatureAmount: 0,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 3,
- ValidHeight: 9528,
- },
- },
- useUnconfirmed: false,
- wantUtxos: []*UTXO{},
- immatureAmount: 3,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- },
- dbUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- useUnconfirmed: false,
- wantUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- immatureAmount: 0,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x11}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- },
- dbUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- useUnconfirmed: true,
- wantUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "testAccount",
- Amount: 3,
- },
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 3,
- },
- },
- immatureAmount: 0,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 1,
- },
- bc.NewHash([32]byte{0x02}): &UTXO{
- OutputID: bc.NewHash([32]byte{0x02}),
- AccountID: "notMe",
- Amount: 2,
- },
- },
- },
- dbUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x03}),
- AccountID: "testAccount",
- Amount: 3,
- },
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x04}),
- AccountID: "notMe",
- Amount: 4,
- },
- },
- useUnconfirmed: true,
- wantUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x03}),
- AccountID: "testAccount",
- Amount: 3,
- },
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 1,
- },
- },
- immatureAmount: 0,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 6,
- Vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- },
- useUnconfirmed: false,
- wantUtxos: []*UTXO{
- &UTXO{
- OutputID: bc.NewHash([32]byte{0x01}),
- AccountID: "testAccount",
- Amount: 6,
- Vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- },
- immatureAmount: 0,
- vote: []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"),
- },
- }
-
- for i, c := range cases {
- for _, u := range c.dbUtxos {
- data, err := json.Marshal(u)
- if err != nil {
- t.Error(err)
- }
- c.uk.store.SetStandardUTXO(u.OutputID, data)
- }
-
- gotUtxos, immatureAmount := c.uk.findUtxos("testAccount", &bc.AssetID{}, c.useUnconfirmed, c.vote)
- if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
- t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
- }
- if immatureAmount != c.immatureAmount {
- t.Errorf("case %d: got %v want %v", i, immatureAmount, c.immatureAmount)
- }
-
- for _, u := range c.dbUtxos {
- c.uk.store.DeleteStandardUTXO(u.OutputID)
- }
- }
-}
-
-func TestFindUtxo(t *testing.T) {
- currentHeight := func() uint64 { return 9527 }
- testDB := dbm.NewDB("testdb", "leveldb", "temp")
- defer os.RemoveAll("temp")
-
- cases := []struct {
- uk utxoKeeper
- dbUtxos map[string]*UTXO
- outHash bc.Hash
- useUnconfirmed bool
- wantUtxo *UTXO
- err error
- }{
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: map[string]*UTXO{},
- outHash: bc.NewHash([32]byte{0x01}),
- wantUtxo: nil,
- err: ErrMatchUTXO,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- },
- },
- dbUtxos: map[string]*UTXO{},
- outHash: bc.NewHash([32]byte{0x01}),
- wantUtxo: nil,
- useUnconfirmed: false,
- err: ErrMatchUTXO,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{
- bc.NewHash([32]byte{0x01}): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- },
- },
- dbUtxos: map[string]*UTXO{},
- outHash: bc.NewHash([32]byte{0x01}),
- wantUtxo: &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- useUnconfirmed: true,
- err: nil,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: map[string]*UTXO{
- string(database.StandardUTXOKey(bc.NewHash([32]byte{0x01}))): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- },
- outHash: bc.NewHash([32]byte{0x01}),
- wantUtxo: &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- useUnconfirmed: false,
- err: nil,
- },
- {
- uk: utxoKeeper{
- store: database.NewAccountStore(testDB),
- currentHeight: currentHeight,
- unconfirmed: map[bc.Hash]*UTXO{},
- },
- dbUtxos: map[string]*UTXO{
- string(database.ContractUTXOKey(bc.NewHash([32]byte{0x01}))): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- },
- outHash: bc.NewHash([32]byte{0x01}),
- wantUtxo: &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
- useUnconfirmed: false,
- err: nil,
- },
- }
-
- for i, c := range cases {
- for k, u := range c.dbUtxos {
- data, err := json.Marshal(u)
- if err != nil {
- t.Error(err)
- }
- testDB.Set([]byte(k), data)
- }
-
- gotUtxo, err := c.uk.findUtxo(c.outHash, c.useUnconfirmed)
- if !testutil.DeepEqual(gotUtxo, c.wantUtxo) {
- t.Errorf("case %d: got %v want %v", i, gotUtxo, c.wantUtxo)
- }
- if err != c.err {
- t.Errorf("case %d: got %v want %v", i, err, c.err)
- }
-
- for _, u := range c.dbUtxos {
- c.uk.store.DeleteStandardUTXO(u.OutputID)
- }
- }
-}
-
func TestOptUTXOs(t *testing.T) {
cases := []struct {
uk utxoKeeper
package test
import (
+ "encoding/json"
+ "io/ioutil"
+ "os"
"testing"
+ "time"
acc "github.com/vapor/account"
"github.com/vapor/blockchain/signers"
+ "github.com/vapor/blockchain/txbuilder"
"github.com/vapor/consensus"
"github.com/vapor/crypto/ed25519/chainkd"
+ "github.com/vapor/database"
+ dbm "github.com/vapor/database/leveldb"
+ "github.com/vapor/protocol/bc"
"github.com/vapor/testutil"
)
chainTxMergeGas = uint64(10000000)
)
-// func TestReserveBtmUtxoChain(t *testing.T) {
-// dirPath, err := ioutil.TempDir(".", "")
-// if err != nil {
-// t.Fatal(err)
-// }
-// defer os.RemoveAll(dirPath)
-// testDB := dbm.NewDB("testdb", "memdb", dirPath)
-// store := database.NewAccountStore(testDB)
-// chainTxUtxoNum = 3
-// utxos := []*acc.UTXO{}
-// m := mockAccountManager(t)
-// for i := uint64(1); i <= 20; i++ {
-// utxo := &acc.UTXO{
-// OutputID: bc.Hash{V0: i},
-// AccountID: "TestAccountID",
-// AssetID: *consensus.BTMAssetID,
-// Amount: i * chainTxMergeGas,
-// }
-// utxos = append(utxos, utxo)
-
-// data, err := json.Marshal(utxo)
-// if err != nil {
-// t.Fatal(err)
-// }
-
-// store.SetStandardUTXO(utxo.OutputID, data)
-// }
-
-// cases := []struct {
-// amount uint64
-// want []uint64
-// err bool
-// }{
-// {
-// amount: 1 * chainTxMergeGas,
-// want: []uint64{1},
-// },
-// {
-// amount: 888888 * chainTxMergeGas,
-// want: []uint64{},
-// err: true,
-// },
-// {
-// amount: 7 * chainTxMergeGas,
-// want: []uint64{4, 3, 1},
-// },
-// {
-// amount: 15 * chainTxMergeGas,
-// want: []uint64{5, 4, 3, 2, 1, 6},
-// },
-// {
-// amount: 163 * chainTxMergeGas,
-// want: []uint64{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 2, 1, 3},
-// },
-// }
-
-// for i, c := range cases {
-// m.utxoKeeper.expireReservation(time.Unix(999999999, 0))
-// utxos, err := m.Manager.ReserveBtmUtxoChain(&txbuilder.TemplateBuilder{}, "TestAccountID", c.amount, false)
-
-// if err != nil != c.err {
-// t.Fatalf("case %d got err %v want err = %v", i, err, c.err)
-// }
-
-// got := []uint64{}
-// for _, utxo := range utxos {
-// got = append(got, utxo.Amount/chainTxMergeGas)
-// }
-
-// if !testutil.DeepEqual(got, c.want) {
-// t.Fatalf("case %d got %d want %d", i, got, c.want)
-// }
-// }
-// }
+func TestReserveBtmUtxoChain(t *testing.T) {
+ dirPath, err := ioutil.TempDir(".", "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(dirPath)
+ testDB := dbm.NewDB("testdb", "memdb", dirPath)
+ store := database.NewAccountStore(testDB)
+ chainTxUtxoNum = 3
+ utxos := []*acc.UTXO{}
+ m := mockAccountManager(t)
+ for i := uint64(1); i <= 20; i++ {
+ utxo := &acc.UTXO{
+ OutputID: bc.Hash{V0: i},
+ AccountID: "TestAccountID",
+ AssetID: *consensus.BTMAssetID,
+ Amount: i * chainTxMergeGas,
+ }
+ utxos = append(utxos, utxo)
+
+ data, err := json.Marshal(utxo)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ store.SetStandardUTXO(utxo.OutputID, data)
+ }
+
+ cases := []struct {
+ amount uint64
+ want []uint64
+ err bool
+ }{
+ {
+ amount: 1 * chainTxMergeGas,
+ want: []uint64{1},
+ },
+ {
+ amount: 888888 * chainTxMergeGas,
+ want: []uint64{},
+ err: true,
+ },
+ {
+ amount: 7 * chainTxMergeGas,
+ want: []uint64{4, 3, 1},
+ },
+ {
+ amount: 15 * chainTxMergeGas,
+ want: []uint64{5, 4, 3, 2, 1, 6},
+ },
+ {
+ amount: 163 * chainTxMergeGas,
+ want: []uint64{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 2, 1, 3},
+ },
+ }
+
+ for i, c := range cases {
+ m.utxoKeeper.expireReservation(time.Unix(999999999, 0))
+ utxos, err := m.Manager.ReserveBtmUtxoChain(&txbuilder.TemplateBuilder{}, "TestAccountID", c.amount, false)
+
+ if err != nil != c.err {
+ t.Errorf("case %d got err %v want err = %v", i, err, c.err)
+ }
+
+ got := []uint64{}
+ for _, utxo := range utxos {
+ got = append(got, utxo.Amount/chainTxMergeGas)
+ }
+
+ if !testutil.DeepEqual(got, c.want) {
+ t.Errorf("case %d got %d want %d", i, got, c.want)
+ }
+ }
+}
func TestBuildBtmTxChain(t *testing.T) {
chainTxUtxoNum = 3