decred.org/dcrdex@v1.0.5/dex/order/order_test.go (about)

     1  // Package order defines the Order and Match types used throughout the DEX.
     2  package order
     3  
     4  import (
     5  	"bytes"
     6  	"encoding/binary"
     7  	"encoding/hex"
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  
    12  	"decred.org/dcrdex/server/account"
    13  )
    14  
    15  var (
    16  	acct0 = account.AccountID{
    17  		0x22, 0x4c, 0xba, 0xaa, 0xfa, 0x80, 0xbf, 0x3b,
    18  		0xd1, 0xff, 0x73, 0x15, 0x90, 0xbc, 0xbd, 0xda,
    19  		0x5a, 0x76, 0xf9, 0x1e, 0x60, 0xa1, 0x56, 0x99,
    20  		0x46, 0x34, 0xe9, 0x1c, 0xec, 0x25, 0xd5, 0x40,
    21  	}
    22  	preimage0 = Preimage{
    23  		0x90, 0xbc, 0xbd, 0xda, 0x5a, 0x76, 0xf9, 0x1e,
    24  		0x60, 0xa1, 0x56, 0x99, 0x46, 0x34, 0xe9, 0x1c,
    25  		0xec, 0x25, 0xd5, 0x40, 0x22, 0x4c, 0xba, 0xaa,
    26  		0xfa, 0x80, 0xbf, 0x3b, 0xd1, 0xff, 0x73, 0x15,
    27  	}
    28  	commit0 = Commitment{
    29  		0xd9, 0x83, 0xec, 0xdf, 0x34, 0x0f, 0xd9, 0xaf,
    30  		0xda, 0xb8, 0x81, 0x8d, 0x5a, 0x29, 0x36, 0xe0,
    31  		0x71, 0xaf, 0x3c, 0xbb, 0x3d, 0xa8, 0xac, 0xf4,
    32  		0x38, 0xb6, 0xc2, 0x91, 0x65, 0xf2, 0x0d, 0x8d,
    33  	}
    34  )
    35  
    36  const (
    37  	AssetDCR uint32 = iota
    38  	AssetBTC
    39  )
    40  
    41  func utxoCoinID(txid string, vout uint32) CoinID {
    42  	hash, err := hex.DecodeString(txid)
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	hashLen := len(hash)
    47  	b := make([]byte, hashLen+4)
    48  	copy(b[:hashLen], hash)
    49  	binary.BigEndian.PutUint32(b[hashLen:], vout)
    50  	return b
    51  }
    52  
    53  func Test_calcOrderID(t *testing.T) {
    54  	mo := &MarketOrder{}
    55  	defer func() {
    56  		if recover() == nil {
    57  			t.Error("MarketOrder.ID should have paniced with ServerTime unset.")
    58  		}
    59  	}()
    60  	_ = calcOrderID(mo)
    61  }
    62  
    63  func TestPrefix_Serialize(t *testing.T) {
    64  	type fields struct {
    65  		AccountID  account.AccountID
    66  		BaseAsset  uint32
    67  		QuoteAsset uint32
    68  		OrderType  OrderType
    69  		ClientTime time.Time
    70  		ServerTime time.Time
    71  		Commit     Commitment
    72  	}
    73  	tests := []struct {
    74  		name   string
    75  		fields fields
    76  		want   []byte
    77  	}{
    78  		{
    79  			"ok acct0",
    80  			fields{
    81  				AccountID:  acct0,
    82  				BaseAsset:  AssetDCR,
    83  				QuoteAsset: AssetBTC,
    84  				OrderType:  LimitOrderType,
    85  				ClientTime: time.Unix(1566497653, 0),
    86  				ServerTime: time.Unix(1566497656, 0),
    87  				Commit:     commit0,
    88  			},
    89  			[]byte{
    90  				// AccountID 32 bytes
    91  				0x22, 0x4c, 0xba, 0xaa, 0xfa, 0x80, 0xbf, 0x3b,
    92  				0xd1, 0xff, 0x73, 0x15, 0x90, 0xbc, 0xbd, 0xda,
    93  				0x5a, 0x76, 0xf9, 0x1e, 0x60, 0xa1, 0x56, 0x99,
    94  				0x46, 0x34, 0xe9, 0x1c, 0xec, 0x25, 0xd5, 0x40,
    95  				// BaseAsset 4 bytes
    96  				0x0, 0x0, 0x0, 0x0,
    97  				// QuoteAsset 4 bytes
    98  				0x0, 0x0, 0x0, 0x1,
    99  				// OrderType 1 byte
   100  				0x1,
   101  				// ClientTime 8 bytes
   102  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0x41, 0x8,
   103  				// ServerTime 8 bytes
   104  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0x4c, 0xc0,
   105  				// Commitment 32 bytes
   106  				0xd9, 0x83, 0xec, 0xdf, 0x34, 0x0f, 0xd9, 0xaf,
   107  				0xda, 0xb8, 0x81, 0x8d, 0x5a, 0x29, 0x36, 0xe0,
   108  				0x71, 0xaf, 0x3c, 0xbb, 0x3d, 0xa8, 0xac, 0xf4,
   109  				0x38, 0xb6, 0xc2, 0x91, 0x65, 0xf2, 0x0d, 0x8d,
   110  			},
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			p := &Prefix{
   116  				AccountID:  tt.fields.AccountID,
   117  				BaseAsset:  tt.fields.BaseAsset,
   118  				QuoteAsset: tt.fields.QuoteAsset,
   119  				OrderType:  tt.fields.OrderType,
   120  				ClientTime: tt.fields.ClientTime,
   121  				ServerTime: tt.fields.ServerTime,
   122  				Commit:     tt.fields.Commit,
   123  			}
   124  			got := p.Serialize()
   125  			if !reflect.DeepEqual(got, tt.want) {
   126  				t.Errorf("Prefix.Serialize() = %#v, want %#v", got, tt.want)
   127  			}
   128  			sz := p.serializeSize()
   129  			wantSz := len(got)
   130  			if sz != wantSz {
   131  				t.Errorf("Prefix.serializeSize() = %d,\n want %d", sz, wantSz)
   132  			}
   133  		})
   134  	}
   135  }
   136  
   137  func TestMarketOrder_Serialize_serializeSize(t *testing.T) {
   138  	type fields struct {
   139  		Prefix   Prefix
   140  		Coins    []CoinID
   141  		Sell     bool
   142  		Quantity uint64
   143  		Address  string
   144  	}
   145  	tests := []struct {
   146  		name   string
   147  		fields fields
   148  		want   []byte
   149  	}{
   150  		{
   151  			"ok acct0",
   152  			fields{
   153  				Prefix: Prefix{
   154  					AccountID:  acct0,
   155  					BaseAsset:  AssetDCR,
   156  					QuoteAsset: AssetBTC,
   157  					OrderType:  MarketOrderType,
   158  					ClientTime: time.Unix(1566497653, 0),
   159  					ServerTime: time.Unix(1566497656, 0),
   160  					Commit:     commit0,
   161  				},
   162  				Coins: []CoinID{
   163  					utxoCoinID("aed8e9b2b889bf0a78e559684796800144cd76dc8faac2aeac44fbd1c310124b", 1),
   164  					utxoCoinID("45b82138ca90e665a1c8793aa901aa232dd82be41b8e630dd621f24e717fc13a", 2),
   165  				},
   166  				Sell:     false,
   167  				Quantity: 132413241324,
   168  				Address:  "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui",
   169  			},
   170  			[]byte{
   171  				// Prefix - AccountID 32 bytes
   172  				0x22, 0x4c, 0xba, 0xaa, 0xfa, 0x80, 0xbf, 0x3b,
   173  				0xd1, 0xff, 0x73, 0x15, 0x90, 0xbc, 0xbd, 0xda,
   174  				0x5a, 0x76, 0xf9, 0x1e, 0x60, 0xa1, 0x56, 0x99,
   175  				0x46, 0x34, 0xe9, 0x1c, 0xec, 0x25, 0xd5, 0x40,
   176  				// Prefix - BaseAsset 4 bytes
   177  				0x0, 0x0, 0x0, 0x0,
   178  				// Prefix - QuoteAsset 4 bytes
   179  				0x0, 0x0, 0x0, 0x1,
   180  				// Prefix - OrderType 1 byte
   181  				0x2,
   182  				// Prefix - ClientTime 8 bytes
   183  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0x41, 0x8,
   184  				// Prefix - ServerTime 8 bytes
   185  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0x4c, 0xc0,
   186  				// Prefix - Commit, 32 bytes
   187  				0xd9, 0x83, 0xec, 0xdf, 0x34, 0x0f, 0xd9, 0xaf,
   188  				0xda, 0xb8, 0x81, 0x8d, 0x5a, 0x29, 0x36, 0xe0,
   189  				0x71, 0xaf, 0x3c, 0xbb, 0x3d, 0xa8, 0xac, 0xf4,
   190  				0x38, 0xb6, 0xc2, 0x91, 0x65, 0xf2, 0x0d, 0x8d,
   191  				// UTXO count 1 byte
   192  				0x2,
   193  				// UTXO 1 hash 32 bytes
   194  				0xae, 0xd8, 0xe9, 0xb2, 0xb8, 0x89, 0xbf, 0x0a,
   195  				0x78, 0xe5, 0x59, 0x68, 0x47, 0x96, 0x80, 0x01,
   196  				0x44, 0xcd, 0x76, 0xdc, 0x8f, 0xaa, 0xc2, 0xae,
   197  				0xac, 0x44, 0xfb, 0xd1, 0xc3, 0x10, 0x12, 0x4b,
   198  				// UTXO 1 vout 4 bytes
   199  				0x0, 0x0, 0x0, 0x1,
   200  				// UTXO 2 hash 32 bytes
   201  				0x45, 0xb8, 0x21, 0x38, 0xca, 0x90, 0xe6, 0x65,
   202  				0xa1, 0xc8, 0x79, 0x3a, 0xa9, 0x01, 0xaa, 0x23,
   203  				0x2d, 0xd8, 0x2b, 0xe4, 0x1b, 0x8e, 0x63, 0x0d,
   204  				0xd6, 0x21, 0xf2, 0x4e, 0x71, 0x7f, 0xc1, 0x3a,
   205  				// UTXO 2 vout 4 bytes
   206  				0x0, 0x0, 0x0, 0x2,
   207  				// Sell 1 byte
   208  				0x0,
   209  				// Quantity 8 bytes
   210  				0x0, 0x0, 0x0, 0x1e, 0xd4, 0x71, 0xb7, 0xec,
   211  				// Address (variable size)
   212  				0x44, 0x63, 0x71, 0x58, 0x73, 0x77, 0x6a, 0x54, 0x50, 0x6e, 0x55, 0x63,
   213  				0x64, 0x34, 0x46, 0x52, 0x43, 0x6b, 0x58, 0x34, 0x76, 0x52, 0x4a, 0x78,
   214  				0x6d, 0x56, 0x74, 0x66, 0x67, 0x47, 0x56, 0x61, 0x35, 0x75, 0x69},
   215  		},
   216  	}
   217  	for _, tt := range tests {
   218  		t.Run(tt.name, func(t *testing.T) {
   219  			o := &MarketOrder{
   220  				P: tt.fields.Prefix,
   221  				T: Trade{
   222  					Coins:    tt.fields.Coins,
   223  					Sell:     tt.fields.Sell,
   224  					Quantity: tt.fields.Quantity,
   225  					Address:  tt.fields.Address,
   226  				},
   227  			}
   228  			got := o.Serialize()
   229  			if !reflect.DeepEqual(got, tt.want) {
   230  				t.Errorf("MarketOrder.Serialize() = %#v,\n want %#v", got, tt.want)
   231  			}
   232  			sz := o.serializeSize()
   233  			wantSz := len(got)
   234  			if sz != wantSz {
   235  				t.Errorf("MarketOrder.serializeSize() = %d,\n want %d", sz, wantSz)
   236  			}
   237  		})
   238  	}
   239  }
   240  
   241  func TestLimitOrder_Serialize_serializeSize(t *testing.T) {
   242  	tests := []struct {
   243  		name       string
   244  		LimitOrder *LimitOrder
   245  		want       []byte
   246  	}{
   247  		{
   248  			"ok acct0",
   249  			&LimitOrder{
   250  				P: Prefix{
   251  					AccountID:  acct0,
   252  					BaseAsset:  AssetDCR,
   253  					QuoteAsset: AssetBTC,
   254  					OrderType:  LimitOrderType,
   255  					ClientTime: time.Unix(1566497653, 0),
   256  					ServerTime: time.Unix(1566497656, 0),
   257  					Commit:     commit0,
   258  				},
   259  				T: Trade{
   260  					Coins: []CoinID{
   261  						utxoCoinID("d186e4b6625c9c94797cc494f535fc150177e0619e2303887e0a677f29ef1bab", 0),
   262  						utxoCoinID("11d9580e19ad65a875a5bc558d600e96b2916062db9e8b65cbc2bb905207c1ad", 16),
   263  					},
   264  					Sell:     false,
   265  					Quantity: 132413241324,
   266  					Address:  "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui",
   267  				},
   268  				Rate:  13241324,
   269  				Force: StandingTiF,
   270  			},
   271  			[]byte{
   272  				// Prefix - AccountID 32 bytes
   273  				0x22, 0x4c, 0xba, 0xaa, 0xfa, 0x80, 0xbf, 0x3b,
   274  				0xd1, 0xff, 0x73, 0x15, 0x90, 0xbc, 0xbd, 0xda,
   275  				0x5a, 0x76, 0xf9, 0x1e, 0x60, 0xa1, 0x56, 0x99,
   276  				0x46, 0x34, 0xe9, 0x1c, 0xec, 0x25, 0xd5, 0x40,
   277  				// Prefix - BaseAsset 4 bytes
   278  				0x0, 0x0, 0x0, 0x0,
   279  				// Prefix - QuoteAsset 4 bytes
   280  				0x0, 0x0, 0x0, 0x1,
   281  				// Prefix - OrderType 1 byte
   282  				0x1,
   283  				// Prefix - ClientTime 8 bytes
   284  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0x41, 0x8,
   285  				// Prefix - ServerTime 8 bytes
   286  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0x4c, 0xc0,
   287  				// Prefix - Commit, 32 bytes
   288  				0xd9, 0x83, 0xec, 0xdf, 0x34, 0x0f, 0xd9, 0xaf,
   289  				0xda, 0xb8, 0x81, 0x8d, 0x5a, 0x29, 0x36, 0xe0,
   290  				0x71, 0xaf, 0x3c, 0xbb, 0x3d, 0xa8, 0xac, 0xf4,
   291  				0x38, 0xb6, 0xc2, 0x91, 0x65, 0xf2, 0x0d, 0x8d,
   292  				// UTXO count 1 byte
   293  				0x2,
   294  				// UTXO 1 hash 32 bytes
   295  				0xd1, 0x86, 0xe4, 0xb6, 0x62, 0x5c, 0x9c, 0x94,
   296  				0x79, 0x7c, 0xc4, 0x94, 0xf5, 0x35, 0xfc, 0x15,
   297  				0x01, 0x77, 0xe0, 0x61, 0x9e, 0x23, 0x03, 0x88,
   298  				0x7e, 0x0a, 0x67, 0x7f, 0x29, 0xef, 0x1b, 0xab,
   299  				// UTXO 1 vout 4 bytes
   300  				0x0, 0x0, 0x0, 0x0,
   301  				// UTXO 2 hash 32 bytes
   302  				0x11, 0xd9, 0x58, 0x0e, 0x19, 0xad, 0x65, 0xa8,
   303  				0x75, 0xa5, 0xbc, 0x55, 0x8d, 0x60, 0x0e, 0x96,
   304  				0xb2, 0x91, 0x60, 0x62, 0xdb, 0x9e, 0x8b, 0x65,
   305  				0xcb, 0xc2, 0xbb, 0x90, 0x52, 0x07, 0xc1, 0xad,
   306  				// UTXO 2 vout 4 bytes
   307  				0x0, 0x0, 0x0, 0x10,
   308  				// Sell 1 byte
   309  				0x0,
   310  				// Quantity 8 bytes
   311  				0x0, 0x0, 0x0, 0x1e, 0xd4, 0x71, 0xb7, 0xec,
   312  				// Address (variable size)
   313  				0x44, 0x63, 0x71,
   314  				0x58, 0x73, 0x77, 0x6a, 0x54, 0x50, 0x6e, 0x55, 0x63, 0x64, 0x34, 0x46,
   315  				0x52, 0x43, 0x6b, 0x58, 0x34, 0x76, 0x52, 0x4a, 0x78, 0x6d, 0x56, 0x74,
   316  				0x66, 0x67, 0x47, 0x56, 0x61, 0x35, 0x75, 0x69,
   317  				// Rate 8 bytes
   318  				0x0, 0x0, 0x0, 0x0, 0x0, 0xca, 0xb, 0xec,
   319  				// Force 1 byte
   320  				0x1,
   321  			},
   322  		},
   323  	}
   324  	for _, tt := range tests {
   325  		t.Run(tt.name, func(t *testing.T) {
   326  			o := tt.LimitOrder
   327  			got := o.Serialize()
   328  			if !reflect.DeepEqual(got, tt.want) {
   329  				t.Errorf("LimitOrder.Serialize() = %#v, want %#v", got, tt.want)
   330  			}
   331  			sz := o.serializeSize()
   332  			wantSz := len(got)
   333  			if sz != wantSz {
   334  				t.Errorf("LimitOrder.serializeSize() = %d,\n want %d", sz, wantSz)
   335  			}
   336  		})
   337  	}
   338  }
   339  
   340  func TestCancelOrder_Serialize(t *testing.T) {
   341  	type fields struct {
   342  		Prefix        Prefix
   343  		TargetOrderID OrderID
   344  	}
   345  	tests := []struct {
   346  		name   string
   347  		fields fields
   348  		want   []byte
   349  	}{
   350  		{
   351  			"ok",
   352  			fields{
   353  				Prefix: Prefix{
   354  					AccountID:  acct0,
   355  					BaseAsset:  AssetDCR,
   356  					QuoteAsset: AssetBTC,
   357  					OrderType:  CancelOrderType,
   358  					ClientTime: time.Unix(1566497693, 0),
   359  					ServerTime: time.Unix(1566497696, 0),
   360  					Commit:     commit0,
   361  				},
   362  				TargetOrderID: OrderID{
   363  					0xce, 0x8c, 0xc8, 0xd, 0xda, 0x9a, 0x40, 0xbb,
   364  					0x43, 0xba, 0x58, 0x9, 0x75, 0xfd, 0x23, 0x85,
   365  					0x4c, 0x04, 0x4d, 0x8, 0x12, 0x54, 0x1f, 0x88,
   366  					0x25, 0x48, 0xaa, 0x8, 0x78, 0xe5, 0xa2, 0x67},
   367  			},
   368  			[]byte{
   369  				// Prefix - AccountID 32 bytes
   370  				0x22, 0x4c, 0xba, 0xaa, 0xfa, 0x80, 0xbf, 0x3b,
   371  				0xd1, 0xff, 0x73, 0x15, 0x90, 0xbc, 0xbd, 0xda,
   372  				0x5a, 0x76, 0xf9, 0x1e, 0x60, 0xa1, 0x56, 0x99,
   373  				0x46, 0x34, 0xe9, 0x1c, 0xec, 0x25, 0xd5, 0x40,
   374  				// Prefix - BaseAsset 4 bytes
   375  				0x0, 0x0, 0x0, 0x0,
   376  				// Prefix - QuoteAsset 4 bytes
   377  				0x0, 0x0, 0x0, 0x1,
   378  				// Prefix - OrderType 1 byte
   379  				0x3,
   380  				// Prefix - ClientTime 8 bytes
   381  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0xdd, 0x48,
   382  				// Prefix - ServerTime 8 bytes
   383  				0x0, 0x0, 0x1, 0x6c, 0xba, 0x89, 0xe9, 0x0,
   384  				// Prefix - Commit, 32 bytes
   385  				0xd9, 0x83, 0xec, 0xdf, 0x34, 0x0f, 0xd9, 0xaf,
   386  				0xda, 0xb8, 0x81, 0x8d, 0x5a, 0x29, 0x36, 0xe0,
   387  				0x71, 0xaf, 0x3c, 0xbb, 0x3d, 0xa8, 0xac, 0xf4,
   388  				0x38, 0xb6, 0xc2, 0x91, 0x65, 0xf2, 0x0d, 0x8d,
   389  				// Order ID - 32 bytes
   390  				0xce, 0x8c, 0xc8, 0x0d, 0xda, 0x9a, 0x40, 0xbb,
   391  				0x43, 0xba, 0x58, 0x09, 0x75, 0xfd, 0x23, 0x85,
   392  				0x4c, 0x04, 0x4d, 0x08, 0x12, 0x54, 0x1f, 0x88,
   393  				0x25, 0x48, 0xaa, 0x08, 0x78, 0xe5, 0xa2, 0x67,
   394  			},
   395  		},
   396  	}
   397  	for _, tt := range tests {
   398  		t.Run(tt.name, func(t *testing.T) {
   399  			o := &CancelOrder{
   400  				P:             tt.fields.Prefix,
   401  				TargetOrderID: tt.fields.TargetOrderID,
   402  			}
   403  			got := o.Serialize()
   404  			if !reflect.DeepEqual(got, tt.want) {
   405  				t.Errorf("CancelOrder.Serialize() = %#v, want %v", got, tt.want)
   406  			}
   407  			sz := o.serializeSize()
   408  			wantSz := len(got)
   409  			if sz != wantSz {
   410  				t.Errorf("CancelOrder.serializeSize() = %d,\n want %d", sz, wantSz)
   411  			}
   412  		})
   413  	}
   414  }
   415  
   416  func TestMarketOrder_ID(t *testing.T) {
   417  	orderID0, _ := hex.DecodeString("2389e5debf84ad84742845b10adb4b9d74965291a4a0050f31a6992dbdeced4c")
   418  	var orderID OrderID
   419  	copy(orderID[:], orderID0)
   420  
   421  	type fields struct {
   422  		Prefix   Prefix
   423  		Coins    []CoinID
   424  		Sell     bool
   425  		Quantity uint64
   426  		Address  string
   427  	}
   428  	tests := []struct {
   429  		name   string
   430  		fields fields
   431  		want   OrderID
   432  	}{
   433  		{
   434  			"ok",
   435  			fields{
   436  				Prefix: Prefix{
   437  					AccountID:  acct0,
   438  					BaseAsset:  AssetDCR,
   439  					QuoteAsset: AssetBTC,
   440  					OrderType:  MarketOrderType,
   441  					ClientTime: time.Unix(1566497653, 0),
   442  					ServerTime: time.Unix(1566497656, 0),
   443  					Commit:     commit0,
   444  				},
   445  				Coins: []CoinID{
   446  					utxoCoinID("a985d8df97571b130ce30a049a76ffedaa79b6e69b173ff81b1bf9fc07f063c7", 1),
   447  				},
   448  				Sell:     true,
   449  				Quantity: 132413241324,
   450  				Address:  "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui",
   451  			},
   452  			orderID,
   453  		},
   454  	}
   455  	for _, tt := range tests {
   456  		t.Run(tt.name, func(t *testing.T) {
   457  			o := &MarketOrder{
   458  				P: tt.fields.Prefix,
   459  				T: Trade{
   460  					Coins:    tt.fields.Coins,
   461  					Sell:     tt.fields.Sell,
   462  					Quantity: tt.fields.Quantity,
   463  					Address:  tt.fields.Address,
   464  				},
   465  			}
   466  			remaining := o.Remaining()
   467  			if remaining != o.Quantity-o.FillAmt {
   468  				t.Errorf("MarketOrder.Remaining incorrect, got %d, expected %d",
   469  					remaining, o.Quantity-o.FillAmt)
   470  			}
   471  			if got := o.ID(); got != tt.want {
   472  				t.Errorf("MarketOrder.ID() = %v, want %v", got, tt.want)
   473  			}
   474  		})
   475  	}
   476  }
   477  
   478  func TestLimitOrder_ID(t *testing.T) {
   479  	orderID0, _ := hex.DecodeString("8490aca39a672a79a1d93d70b531bee2297c56040e970cac6d2be755c932508a")
   480  	var orderID OrderID
   481  	copy(orderID[:], orderID0)
   482  
   483  	tests := []struct {
   484  		name       string
   485  		LimitOrder *LimitOrder
   486  		want       OrderID
   487  	}{
   488  		{
   489  			"ok",
   490  			&LimitOrder{
   491  				P: Prefix{
   492  					AccountID:  acct0,
   493  					BaseAsset:  AssetDCR,
   494  					QuoteAsset: AssetBTC,
   495  					OrderType:  LimitOrderType,
   496  					ClientTime: time.Unix(1566497653, 0),
   497  					ServerTime: time.Unix(1566497656, 0),
   498  				},
   499  				T: Trade{
   500  					Coins: []CoinID{
   501  						utxoCoinID("01516d9c7ffbe260b811dc04462cedd3f8969ce3a3ffe6231ae870775a92e9b0", 1),
   502  					},
   503  					Sell:     false,
   504  					Quantity: 132413241324,
   505  					Address:  "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui",
   506  				},
   507  				Rate:  13241324,
   508  				Force: StandingTiF,
   509  			},
   510  			orderID,
   511  		},
   512  	}
   513  	for _, tt := range tests {
   514  		t.Run(tt.name, func(t *testing.T) {
   515  			o := tt.LimitOrder
   516  			remaining := o.Remaining()
   517  			if remaining != o.Quantity-o.FillAmt {
   518  				t.Errorf("LimitOrder.Remaining incorrect, got %d, expected %d",
   519  					remaining, o.Quantity-o.FillAmt)
   520  			}
   521  			if got := o.ID(); got != tt.want {
   522  				t.Errorf("LimitOrder.ID() = %v, want %v", got, tt.want)
   523  			}
   524  		})
   525  	}
   526  }
   527  
   528  func TestCancelOrder_ID(t *testing.T) {
   529  	limitOrderID0, _ := hex.DecodeString("8490aca39a672a79a1d93d70b531bee2297c56040e970cac6d2be755c932508a")
   530  	var limitOrderID OrderID
   531  	copy(limitOrderID[:], limitOrderID0)
   532  
   533  	orderID0, _ := hex.DecodeString("9b85bf47fca687baed88d3b854f7a96458067a57802e19af4a14884769f9e342")
   534  	var cancelOrderID OrderID
   535  	copy(cancelOrderID[:], orderID0)
   536  
   537  	type fields struct {
   538  		Prefix        Prefix
   539  		TargetOrderID OrderID
   540  	}
   541  	tests := []struct {
   542  		name   string
   543  		fields fields
   544  		want   OrderID
   545  	}{
   546  		{
   547  			"ok",
   548  			fields{
   549  				Prefix: Prefix{
   550  					AccountID:  acct0,
   551  					BaseAsset:  AssetDCR,
   552  					QuoteAsset: AssetBTC,
   553  					OrderType:  CancelOrderType,
   554  					ClientTime: time.Unix(1566497693, 0),
   555  					ServerTime: time.Unix(1566497696, 0),
   556  				},
   557  				TargetOrderID: limitOrderID,
   558  			},
   559  			cancelOrderID,
   560  		},
   561  	}
   562  	for _, tt := range tests {
   563  		t.Run(tt.name, func(t *testing.T) {
   564  			o := &CancelOrder{
   565  				P:             tt.fields.Prefix,
   566  				TargetOrderID: tt.fields.TargetOrderID,
   567  			}
   568  			if got := o.ID(); got != tt.want {
   569  				t.Errorf("CancelOrder.ID() = %v, want %v", got, tt.want)
   570  			}
   571  		})
   572  	}
   573  }
   574  
   575  // func randomHash() (h [32]byte) {
   576  // 	rand.Read(h[:])
   577  // 	return
   578  // }
   579  
   580  func Test_UTXOtoCoinID(t *testing.T) {
   581  	type args struct {
   582  		id CoinID
   583  	}
   584  	tests := []struct {
   585  		name string
   586  		args args
   587  		want []byte
   588  	}{
   589  		{
   590  			"ok",
   591  			args{
   592  				utxoCoinID("bc4b0ffe3a70cf159657b1f8f12c2d895c5d7e849de6ac1c3358be86842f4549", 4),
   593  			},
   594  			[]byte{
   595  				// Tx hash 32 bytes
   596  				0xbc, 0x4b, 0x0f, 0xfe, 0x3a, 0x70, 0xcf, 0x15,
   597  				0x96, 0x57, 0xb1, 0xf8, 0xf1, 0x2c, 0x2d, 0x89,
   598  				0x5c, 0x5d, 0x7e, 0x84, 0x9d, 0xe6, 0xac, 0x1c,
   599  				0x33, 0x58, 0xbe, 0x86, 0x84, 0x2f, 0x45, 0x49,
   600  				// Vout 4 bytes
   601  				0x0, 0x0, 0x0, 0x4,
   602  			},
   603  		},
   604  	}
   605  	for _, tt := range tests {
   606  		t.Run(tt.name, func(t *testing.T) {
   607  			if !bytes.Equal(tt.args.id, tt.want) {
   608  				t.Errorf("serializeOutpoint() = %#v, want %v", tt.args.id, tt.want)
   609  			}
   610  		})
   611  	}
   612  }
   613  
   614  func TestPreimage_Commit(t *testing.T) {
   615  	tests := []struct {
   616  		name string
   617  		pi   *Preimage
   618  		want Commitment
   619  	}{
   620  		{
   621  			"ok zeros",
   622  			&Preimage{},
   623  			Commitment{
   624  				0x05, 0xf6, 0xac, 0x47, 0xac, 0xcd, 0x33, 0x8d,
   625  				0x32, 0x9c, 0xc1, 0x6f, 0x6d, 0x59, 0xf3, 0x40,
   626  				0x9c, 0xc8, 0xbf, 0xe7, 0x6a, 0x27, 0x2e, 0x1e,
   627  				0xec, 0x61, 0x2e, 0x49, 0xc1, 0x15, 0x14, 0x5d},
   628  		},
   629  		{
   630  			"ok one bit",
   631  			&Preimage{0x01},
   632  			Commitment{
   633  				0x8e, 0xa4, 0x09, 0x18, 0xf0, 0x47, 0x2d, 0xdd,
   634  				0xdd, 0x8e, 0xe0, 0x6f, 0xab, 0xfe, 0xbc, 0xde,
   635  				0xca, 0x0c, 0xad, 0x2f, 0xe4, 0xa0, 0x69, 0xc7,
   636  				0xe4, 0x17, 0x2b, 0x81, 0x9c, 0x2e, 0xe5, 0x07},
   637  		},
   638  		{
   639  			"ok ref",
   640  			&preimage0,
   641  			commit0,
   642  		},
   643  	}
   644  	for _, tt := range tests {
   645  		t.Run(tt.name, func(t *testing.T) {
   646  			if got := tt.pi.Commit(); !reflect.DeepEqual(got, tt.want) {
   647  				t.Errorf("Preimage.Commit() = %#v, want %#v", got, tt.want)
   648  			}
   649  		})
   650  	}
   651  }