OSDN Git Service

add test case (#1636)
authormuscle_boy <shenao.78@163.com>
Wed, 13 Mar 2019 12:07:41 +0000 (20:07 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 13 Mar 2019 12:07:41 +0000 (20:07 +0800)
protocol/bc/asset_test.go
protocol/bc/types/merkle_test.go
version/version.go
version/version_test.go

index a311efe..422e1a3 100644 (file)
@@ -2,6 +2,7 @@ package bc
 
 import (
        "testing"
+       "encoding/hex"
 
        "golang.org/x/crypto/sha3"
 )
@@ -21,6 +22,48 @@ func TestComputeAssetID(t *testing.T) {
        }
 }
 
+func TestComputeAssetIDReally(t *testing.T) {
+       cases := []struct {
+               program       string
+               rawDefinition string
+               wantAssetID   string
+       }{
+               {
+                       program: "ae2039294f652632eee970765550c245f0b0314256b4b93aadc86279fdb45db3b70e5151ad",
+                       rawDefinition: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d",
+                       wantAssetID: "07c7ced3f37f48ea39da6971c89f90e9cff3202d54b0a911f12ace8501f3834e",
+               },
+               {
+                       program: "ae20620b1755451738b04f42822f4b37186563f824c9c30d485987298918f96395fe5151ad",
+                       rawDefinition: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d6f626f6c223a2022220a7d",
+                       wantAssetID: "0dafd0f0e42f06f3bf9a8cf5787519d3860650f27a2b3393d34e1fe06e89b469",
+               },
+               {
+                       program: "ae20db11f9dfa39c9e66421c530fe027218edd3d5b1cd98f24c826f4d9c0cd131a475151ad",
+                       rawDefinition: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d",
+                       wantAssetID: "a5bc30d8d0ad051e6e352ebc21d79ba798cd8c436e89f4149969c2c562371791",
+               },
+       }
+
+       for _, c := range cases {
+               progBytes, err := hex.DecodeString(c.program)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               defBytes, err := hex.DecodeString(c.rawDefinition)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               defHash := NewHash(sha3.Sum256(defBytes))
+               assetID := ComputeAssetID(progBytes, 1, &defHash)
+               if assetID.String() != c.wantAssetID {
+                       t.Errorf("got asset id:%s, want asset id:%s", assetID.String(), c.wantAssetID)
+               }
+       }
+}
+
 var assetIDSink AssetID
 
 func BenchmarkComputeAssetID(b *testing.B) {
index c3c0f32..deeb222 100644 (file)
@@ -81,6 +81,36 @@ func TestMerkleRoot(t *testing.T) {
        }
 }
 
+func TestMerkleRootRealTx(t *testing.T) {
+       rawTxs := []string{
+               "070100010160015e5ac79a73db78e5c9215b37cb752f0147d1157c542bb4884908ceb97abc33fe0affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa0f280d42b0001160014085a02ecdf934a56343aa59a3dec9d9feb86ee43630240035e1ef422b4901997ad3c20c50d82e726d03cb6e8ccb5dddc20e0c09e0a6f2e0055331e2b54d9ec52cffb1c47d8fdf2f8887d55c336753637cbf8f832c7af0b20a29601468f08c57ca9c383d28736a9d5c7737cd483126d8db3d85490fe497b3502013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa0aad1b30601160014991b78d1bf731390e2dd838c05ff37ec5146886b00013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8086d8f024011600145ade29df622cc68d0473aa1a20fb89690451c66e00",
+               "070100020160015e4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c480c1240201160014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e630240d96b8f31519c5e34ef983bb7dfb92e807df7fc1ae5a4c08846d00d4f84ebd2f8634b9e0b0374eb2508d0f989520f622aef051862c26daba0e466944e3d55d00b201381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed901300008ede605460cacbf107b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402b001467b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d0125ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad01403a54a3ca0210d005cc9bce490478b518c405ba72e0bc1d134b739f29a73e008345229f0e061c420aa3c56a48bc1c9bf592914252ab9100e69252deeac532430f03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80e0e8f011011600144ab5249140ca4630729030941f59f75e507bd4d500013e7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402011600145ade29df622cc68d0473aa1a20fb89690451c66e00013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80a2c0a012011600145ade29df622cc68d0473aa1a20fb89690451c66e00",
+               "07010001016c016acf24f1471d67c25a01ac84482ecdd8550229180171cae22321f87fe43d4f6a13ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80b4c4c32101012200200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66ef02044059c7a12d006fd34bf8b9b2cb2f99756e5c3c3fdca4c928b830c014819e933b01c92a99bfeb6add73a5087870a3de3465cfed2c99f736b5f77d5fbdc69d91ff0040b95d110d118b873a8232104a6613f0e8c6a791efa3a695c02108cebd5239c8a8471551a48f18ab8ea05d10900b485af5e95b74cd3c01044c1742e71854099c0b40a1b63dae273e3b5b757b7c61286088a934e7282e837d08d62e60d7f75eb739529cd8c6cfef2254d47a546bf8b789657ce0944fec2f7e130c8498e28cae2a9108a901ae20d441b6f375659325a04eede4fc3b74579bb08ccd05b41b99776501e22d6dca7320af6d98ca2c3cd10bf0affbfa6e86609b750523cfadb662ec963c164f05798a49209820b9f1553b03aaebe7e3f9e9222ed7db73b5079b18564042fd3b2cef74156a20271b52de5f554aa4a6f1358f1c2193617bfb3fed4546d13c4af773096a429f9420eeb4a78d8b5cb8283c221ca2d3fd96b8946b3cddee02b7ceffb8f605932588595355ad020149ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80a0d9e61d012200206e8060ef3daca62841802dd9660b24b7dca81c1662b2d68ba8884ecbcd3e1e2200013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80d293ad0301160014ed7d3c466dbc6cc1f3a9af21267ac162f11b30a200",
+               "070100020161015f4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea0dafd0f0e42f06f3bf9a8cf5787519d3860650f27a2b3393d34e1fe06e89b469ddc3f8c2f40200011600141da7f908979e521bf2ba12d280b2c84fc1d024416302409524d0d817176eeb718ce45671d95831cdb138d27289aa8a920104e38a8cab8a7dc8cc3fb60d65aa337b719aed0f696fb12610bfe68add89169a47ac1241e0002033444e1b57524161af3899e50fdfe270a90a1ea97fe38e86019a1e252667fb2d0161015fed3181c99ca80db720231aee6948e1183bfe29c64208c1769afa7f938d3b2cf0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff809cd2b0f4020101160014cfbccfac5018ad4b4bfbcb1fab834e3c8503746063024065beb1da2f0840188af0e3c0127b158f7a2a36f1612499694a731df1e3a9d1abe6694c42986b8700aa9856f59cb3692ee88d68b20d1278f05592fb253c58bd0520e5966eee4092eeefdd805b06f2ad368bb9392edec20998993ebe2a929052c1ce03013e0dafd0f0e42f06f3bf9a8cf5787519d3860650f27a2b3393d34e1fe06e89b469ddfbc8a2cf0201160014583c0323603dd397ba5414255adc80b076cf232b00013d0dafd0f0e42f06f3bf9a8cf5787519d3860650f27a2b3393d34e1fe06e89b46980c8afa02501160014fdb3e6abf7f430fdabb53484ca2469103b2af1b500013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80dafa80f4020116001408e75789f47d2a39622e5a940fa918260bf44c5400",
+               "07010001016d016b1f134a47da4f6df00822935e02a07514718ea99ce5ac4e07bd6c204e098eb525ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff808a858fa70200012200206205ec178dc1ac6ea05ea01bb0fcda6aa978173026fa75204a101bdad7bd6b4889010240d8d5bbf4969fba52df8fba06f75c5de0f51b2bd5f902bf234591f90e78bae20bfb5b7904cb83a1d6577c431f644d37722b432df9d64718b8300e3ab74a871a0046ae2068003e53d467b6d81beaf1e7bd9b60a5ffedc79b36ce14ecd1f30a2dcbcd0551200449030407a3a1fa0731f7f784a72c325b5ce4d534fc3cf8fb7140536ba928605152ad02014affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f699b2a302012200209a0b4b27fde7d29d3b465d20eb2e19f4bda3a873d19d11f4cba53958bde92ed000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80b3ffc40301160014ed7d3c466dbc6cc1f3a9af21267ac162f11b30a200",
+       }
+       wantMerkleRoot := "0f07b8a453771c2dc628f3895ebb33fea82a8de42e11aa588bec26419af22065"
+
+       var txs []*bc.Tx
+       for _, rawTx := range rawTxs {
+               tx := Tx{}              
+               if err := tx.UnmarshalText([]byte(rawTx)); err != nil {
+                       t.Fatal(err)
+               }
+
+               txs = append(txs, tx.Tx)
+       }
+
+       gotMerkleRoot, err := TxMerkleRoot(txs)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       if wantMerkleRoot != gotMerkleRoot.String() {
+               t.Errorf("got merkle root:%s, want merkle root:%s", gotMerkleRoot.String(), wantMerkleRoot)
+       }
+}
+
 func TestDuplicateLeaves(t *testing.T) {
        trueProg := []byte{byte(vm.OP_TRUE)}
        assetID := bc.ComputeAssetID(trueProg, 1, &bc.EmptyStringHash)
index 5295a3e..cb65372 100644 (file)
@@ -106,7 +106,14 @@ func (s *UpdateStatus) CheckUpdate(localVerStr string, remoteVerStr string, remo
        }
        if remoteVersion.GreaterThan(localVersion) {
                s.versionStatus = hasUpdate
-               s.maxVerSeen = remoteVerStr
+               maxVersion, err := gover.NewVersion(s.maxVerSeen)
+               if err != nil {
+                       return err
+               }
+               
+               if remoteVersion.GreaterThan(maxVersion) {
+                       s.maxVerSeen = remoteVerStr
+               }
        }
        if remoteVersion.Segments()[0] > localVersion.Segments()[0] {
                s.versionStatus = hasMUpdate
index 833b492..cb57918 100644 (file)
@@ -3,6 +3,7 @@ package version
 import (
        "testing"
 
+       "gopkg.in/fatih/set.v0"
        gover "github.com/hashicorp/go-version"
 )
 
@@ -71,6 +72,85 @@ func TestCompatibleWith(t *testing.T) {
        }
 }
 
+func TestCheckUpdate(t *testing.T) {
+       cases := []struct {
+               desc           string
+               localVer       string
+               remotePeers    map[string]string
+               wantStatus     uint16
+               wantmaxVerSeen string
+               wantNotified   bool
+       }{
+               {
+                       desc:           "has large version number update",
+                       localVer:       "1.0",
+                       remotePeers:    map[string]string{"peer1": "1.0", "peer2": "2.0", "peer3": "1.0.3"},
+                       wantStatus:     hasMUpdate,
+                       wantmaxVerSeen: "2.0",
+                       wantNotified:   true,
+               },
+               {
+                       desc:           "some remote version less than local version, but some remote verison larger than local version",
+                       localVer:       "1.0",
+                       remotePeers:    map[string]string{"peer1": "0.8", "peer2": "1.1", "peer3": "1.0.3", "peer4": "0.9"},
+                       wantStatus:     hasUpdate,
+                       wantmaxVerSeen: "1.1",
+                       wantNotified:   true,
+               },
+               {
+                       desc:           "has small version number update",
+                       localVer:       "1.0",
+                       remotePeers:    map[string]string{"peer1": "1.0", "peer2": "1.0.3", "peer3": "1.0.2"},
+                       wantStatus:     hasUpdate,
+                       wantmaxVerSeen: "1.0.3",
+                       wantNotified:   true,
+               },
+               {
+                       desc:           "the remote equals to local version",
+                       localVer:       "1.0",
+                       remotePeers:    map[string]string{"peer1": "1.0", "peer2": "1.0", "peer3": "1.0"},
+                       wantStatus:     noUpdate,
+                       wantmaxVerSeen: "1.0",
+                       wantNotified:   false,
+               },
+               {
+                       desc:           "the remote version less than local version",
+                       localVer:       "1.0",
+                       remotePeers:    map[string]string{"peer1": "0.8", "peer2": "0.8", "peer3": "0.8"},
+                       wantStatus:     noUpdate,
+                       wantmaxVerSeen: "1.0",
+                       wantNotified:   false,
+               },
+       }
+
+       for i, c := range cases {
+               status := &UpdateStatus{
+                       maxVerSeen:    c.localVer,
+                       notified:      false,
+                       seedSet:       set.New(),
+                       versionStatus: noUpdate,
+               }
+               for peer, remoteVer := range c.remotePeers {
+                       status.seedSet.Add(peer)
+                       if err := status.CheckUpdate(c.localVer, remoteVer, peer); err != nil {
+                               t.Fatal(err)
+                       }
+               }
+
+               if status.versionStatus != c.wantStatus {
+                       t.Errorf("got version status:%d, want version status:%d", status.versionStatus, c.wantStatus)
+               }
+
+               if status.notified != c.wantNotified {
+                       t.Errorf("got notified:%t, want notified:%t", status.notified, c.wantNotified)
+               }
+
+               if status.maxVerSeen != c.wantmaxVerSeen {
+                       t.Errorf("#%d(%s) got max version seen%s, want max version seen%s", i, c.desc, status.maxVerSeen, c.wantmaxVerSeen)
+               }
+       }
+}
+
 // In case someone edit the iota part and have the mapping changed:
 // noUpdate: 0
 // hasUpdate: 1