github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/orderer/ledger/json_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ledger_test
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  
    23  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    24  	. "github.com/hyperledger/fabric/orderer/ledger"
    25  	jsonledger "github.com/hyperledger/fabric/orderer/ledger/json"
    26  	cb "github.com/hyperledger/fabric/protos/common"
    27  )
    28  
    29  var genesisBlock = cb.NewBlock(0, nil)
    30  
    31  func init() {
    32  	testables = append(testables, &jsonLedgerTestEnv{})
    33  }
    34  
    35  type jsonLedgerTestFactory struct {
    36  	location string
    37  }
    38  
    39  type jsonLedgerTestEnv struct {
    40  }
    41  
    42  func (env *jsonLedgerTestEnv) Initialize() (ledgerTestFactory, error) {
    43  	var err error
    44  	location, err := ioutil.TempDir("", "hyperledger")
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return &jsonLedgerTestFactory{location: location}, nil
    49  }
    50  
    51  func (env *jsonLedgerTestEnv) Name() string {
    52  	return "jsonledger"
    53  }
    54  
    55  func (env *jsonLedgerTestFactory) Destroy() error {
    56  	err := os.RemoveAll(env.location)
    57  	return err
    58  }
    59  
    60  func (env *jsonLedgerTestFactory) Persistent() bool {
    61  	return true
    62  }
    63  
    64  func (env *jsonLedgerTestFactory) New() (Factory, ReadWriter) {
    65  	flf := jsonledger.New(env.location)
    66  	fl, err := flf.GetOrCreate(provisional.TestChainID)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  	if fl.Height() == 0 {
    71  		if err = fl.Append(genesisBlock); err != nil {
    72  			panic(err)
    73  		}
    74  	}
    75  	return flf, fl
    76  }