github.com/iotexproject/iotex-core@v1.14.1-rc1/state/factory/historyfactory.go (about)

     1  // Copyright (c) 2020 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package factory
     7  
     8  import (
     9  	"github.com/iotexproject/iotex-core/action/protocol"
    10  	"github.com/iotexproject/iotex-core/state"
    11  )
    12  
    13  // historyStateReader implements state reader interface, wrap factory with archive height
    14  type historyStateReader struct {
    15  	height uint64
    16  	sf     Factory
    17  }
    18  
    19  // NewHistoryStateReader creates new history state reader by given state factory and height
    20  func NewHistoryStateReader(sf Factory, h uint64) protocol.StateReader {
    21  	return &historyStateReader{
    22  		sf:     sf,
    23  		height: h,
    24  	}
    25  }
    26  
    27  // Height returns archive height
    28  func (hReader *historyStateReader) Height() (uint64, error) {
    29  	return hReader.height, nil
    30  }
    31  
    32  // State returns history state in the archive mode state factory
    33  func (hReader *historyStateReader) State(s interface{}, opts ...protocol.StateOption) (uint64, error) {
    34  	return hReader.height, hReader.sf.StateAtHeight(hReader.height, s, opts...)
    35  }
    36  
    37  // States returns history states in the archive mode state factory
    38  func (hReader *historyStateReader) States(opts ...protocol.StateOption) (uint64, state.Iterator, error) {
    39  	iterator, err := hReader.sf.StatesAtHeight(hReader.height, opts...)
    40  	if err != nil {
    41  		return 0, nil, err
    42  	}
    43  	return hReader.height, iterator, nil
    44  }
    45  
    46  // ReadView reads the view
    47  func (hReader *historyStateReader) ReadView(name string) (interface{}, error) {
    48  	return hReader.sf.ReadView(name)
    49  }