github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/asset/asset_test.go (about) 1 package asset 2 3 import ( 4 "context" 5 "io/ioutil" 6 "os" 7 "reflect" 8 "sort" 9 "strings" 10 "testing" 11 12 "github.com/bytom/bytom/consensus" 13 "github.com/bytom/bytom/crypto/ed25519/chainkd" 14 "github.com/bytom/bytom/database" 15 dbm "github.com/bytom/bytom/database/leveldb" 16 "github.com/bytom/bytom/event" 17 "github.com/bytom/bytom/protocol" 18 "github.com/bytom/bytom/testutil" 19 ) 20 21 func TestDefineAssetWithLowercase(t *testing.T) { 22 reg := mockNewRegistry(t) 23 alias := "lower" 24 asset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, alias, nil) 25 if err != nil { 26 t.Fatal(err) 27 } 28 if *asset.Alias != strings.ToUpper(alias) { 29 t.Fatal("created asset alias should be uppercase") 30 } 31 } 32 33 func TestDefineAssetWithSpaceTrimed(t *testing.T) { 34 reg := mockNewRegistry(t) 35 alias := " WITH SPACE " 36 asset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, alias, nil) 37 if err != nil { 38 t.Fatal(err) 39 } 40 if *asset.Alias != strings.TrimSpace(alias) { 41 t.Fatal("created asset alias should be uppercase") 42 } 43 } 44 45 func TestDefineAsset(t *testing.T) { 46 ctx := context.Background() 47 reg := mockNewRegistry(t) 48 asset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, "asset-alias", nil) 49 if err != nil { 50 testutil.FatalErr(t, err) 51 } 52 53 limitAsset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 100, "limit-asset", nil) 54 if err != nil { 55 testutil.FatalErr(t, err) 56 } 57 58 assets := []*Asset{asset, limitAsset} 59 for _, ass := range assets { 60 found, err := reg.FindByID(ctx, &ass.AssetID) 61 if err != nil { 62 t.Errorf("unexpected error %v", err) 63 } 64 65 if !testutil.DeepEqual(ass, found) { 66 t.Errorf("expected asset %v to be recorded as %v", ass, found) 67 } 68 } 69 } 70 71 func TestDefineBtmAsset(t *testing.T) { 72 reg := mockNewRegistry(t) 73 _, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, consensus.BTMAlias, nil) 74 if err == nil { 75 testutil.FatalErr(t, err) 76 } 77 } 78 79 func TestFindAssetByID(t *testing.T) { 80 ctx := context.Background() 81 reg := mockNewRegistry(t) 82 keys := []chainkd.XPub{testutil.TestXPub} 83 asset, err := reg.Define(keys, 1, nil, 0, "TESTASSET", nil) 84 if err != nil { 85 testutil.FatalErr(t, err) 86 87 } 88 found, err := reg.FindByID(ctx, &asset.AssetID) 89 if err != nil { 90 testutil.FatalErr(t, err) 91 } 92 93 if !testutil.DeepEqual(asset, found) { 94 t.Errorf("expected %v and %v to match", asset, found) 95 } 96 } 97 98 func TestUpdateAssetAlias(t *testing.T) { 99 reg := mockNewRegistry(t) 100 101 oldAlias := "OLD_ALIAS" 102 newAlias := "NEW_ALIAS" 103 104 asset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, oldAlias, nil) 105 if err != nil { 106 testutil.FatalErr(t, err) 107 } 108 109 if reg.UpdateAssetAlias(asset.AssetID.String(), newAlias) != nil { 110 testutil.FatalErr(t, err) 111 } 112 113 asset1, err := reg.FindByAlias(newAlias) 114 if err != nil { 115 testutil.FatalErr(t, err) 116 } 117 118 gotAlias := *asset1.Alias 119 if !reflect.DeepEqual(gotAlias, newAlias) { 120 t.Fatalf("alias:\ngot: %v\nwant: %v", gotAlias, newAlias) 121 } 122 } 123 124 type SortByAssetsAlias []*Asset 125 126 func (a SortByAssetsAlias) Len() int { return len(a) } 127 func (a SortByAssetsAlias) Less(i, j int) bool { 128 return strings.Compare(*a[i].Alias, *a[j].Alias) <= 0 129 } 130 func (a SortByAssetsAlias) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 131 132 func TestListAssets(t *testing.T) { 133 reg := mockNewRegistry(t) 134 135 firstAlias := "FIRST_ALIAS" 136 secondAlias := "SECOND_ALIAS" 137 138 firstAsset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, firstAlias, nil) 139 if err != nil { 140 testutil.FatalErr(t, err) 141 } 142 143 secondAsset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, 0, secondAlias, nil) 144 if err != nil { 145 testutil.FatalErr(t, err) 146 } 147 148 wantAssets := []*Asset{DefaultNativeAsset, firstAsset, secondAsset} 149 150 gotAssets, err := reg.ListAssets("") 151 if err != nil { 152 testutil.FatalErr(t, err) 153 } 154 sort.Sort(SortByAssetsAlias(wantAssets)) 155 sort.Sort(SortByAssetsAlias(gotAssets)) 156 if !testutil.DeepEqual(gotAssets, wantAssets) { 157 t.Fatalf("got:\ngot: %v\nwant: %v", gotAssets, wantAssets) 158 } 159 } 160 161 func mockChain(testDB dbm.DB) (*protocol.Chain, error) { 162 store := database.NewStore(testDB) 163 dispatcher := event.NewDispatcher() 164 txPool := protocol.NewTxPool(store, dispatcher) 165 chain, err := protocol.NewChain(store, txPool, dispatcher) 166 if err != nil { 167 return nil, err 168 } 169 return chain, nil 170 } 171 172 func mockNewRegistry(t *testing.T) *Registry { 173 dirPath, err := ioutil.TempDir(".", "") 174 if err != nil { 175 t.Fatal(err) 176 } 177 defer os.RemoveAll(dirPath) 178 179 testDB := dbm.NewDB("testdb", "leveldb", "temp") 180 defer os.RemoveAll("temp") 181 182 chain, err := mockChain(testDB) 183 if err != nil { 184 t.Fatal(err) 185 } 186 187 return NewRegistry(testDB, chain) 188 }