OSDN Git Service

refactor(bcrp): code refactoring (#1901)
authorDeKaiju <longjinglv@gmail.com>
Mon, 19 Apr 2021 07:59:23 +0000 (15:59 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Apr 2021 07:59:23 +0000 (15:59 +0800)
consensus/bcrp/bcrp_test.go
protocol/state/contract_view.go

index 85d9427..4f5b1d9 100644 (file)
@@ -89,3 +89,32 @@ func TestIsCallBCRPScript(t *testing.T) {
        }
 }
 
+func TestParseContractHash(t *testing.T) {
+       tests := []struct {
+               program  string
+               expected string
+       }{
+               {
+                       //call BCRP script format: OP_1 + OP_DATA_32 + SHA3-256(contract)
+                       program:  "5120605f9b8e978bb3956d729047e556cdf4b56238348d4293ad9afef376462063c5",
+                       expected: "605f9b8e978bb3956d729047e556cdf4b56238348d4293ad9afef376462063c5",
+               },
+       }
+
+       for i, test := range tests {
+               program, err := hex.DecodeString(test.program)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               hash, err := ParseContractHash(program)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               expected := hex.EncodeToString(hash[:])
+               if expected != test.expected {
+                       t.Errorf("TestParseContractHash #%d failed: got %v want %v", i, expected, test.expected)
+               }
+       }
+}
index cb7111d..73bbe2e 100644 (file)
@@ -25,12 +25,11 @@ func (view *ContractViewpoint) ApplyBlock(block *types.Block) error {
        for _, tx := range block.Transactions {
                for _, output := range tx.Outputs {
                        program := output.ControlProgram
-                       if !bcrp.IsBCRPScript(program) {
-                               continue
+                       if bcrp.IsBCRPScript(program) {
+                               var hash [32]byte
+                               sha3pool.Sum256(hash[:], program)
+                               view.AttachEntries[hash] = append(tx.ID.Bytes(), program...)
                        }
-                       var hash [32]byte
-                       sha3pool.Sum256(hash[:], program)
-                       view.AttachEntries[hash] = append(tx.ID.Bytes(), program...)
                }
        }
        return nil
@@ -41,12 +40,11 @@ func (view *ContractViewpoint) DetachBlock(block *types.Block) error {
        for _, tx := range block.Transactions {
                for _, output := range tx.Outputs {
                        program := output.ControlProgram
-                       if !bcrp.IsBCRPScript(program) {
-                               continue
+                       if bcrp.IsBCRPScript(program) {
+                               var hash [32]byte
+                               sha3pool.Sum256(hash[:], program)
+                               view.DetachEntries[hash] = append(tx.ID.Bytes(), program...)
                        }
-                       var hash [32]byte
-                       sha3pool.Sum256(hash[:], program)
-                       view.DetachEntries[hash] = append(tx.ID.Bytes(), program...)
                }
        }
        return nil