github.com/amazechain/amc@v0.1.3/common/interfaces.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package common
    18  
    19  import (
    20  	"context"
    21  	"github.com/amazechain/amc/common/block"
    22  	"github.com/amazechain/amc/common/message"
    23  	"github.com/amazechain/amc/common/transaction"
    24  	"github.com/amazechain/amc/common/types"
    25  	"github.com/holiman/uint256"
    26  	pubsub "github.com/libp2p/go-libp2p-pubsub"
    27  	"github.com/libp2p/go-libp2p/core/host"
    28  	"github.com/libp2p/go-libp2p/core/peer"
    29  	"google.golang.org/protobuf/proto"
    30  )
    31  
    32  // Service is a struct that can be registered into a ServiceRegistry for
    33  // easy dependency management.
    34  type Service interface {
    35  	// Start spawns any goroutines required by the service.
    36  	//Start()
    37  	// Stop terminates all goroutines belonging to the service,
    38  	// blocking until they are all terminated.
    39  	Stop() error
    40  	// Status returns error if the service is not considered healthy.
    41  	//Status() error
    42  }
    43  
    44  type IDownloader interface {
    45  	SyncHeader() error
    46  	SyncBody() error
    47  	SyncTx() error
    48  	Start() error
    49  	Close() error
    50  	IsDownloading() bool
    51  	ConnHandler([]byte, peer.ID) error
    52  }
    53  
    54  type ConnHandler func([]byte, peer.ID) error
    55  
    56  type ProtocolHandshakeFn func(peer IPeer, genesisHash types.Hash, currentHeight *uint256.Int) (Peer, bool)
    57  type ProtocolHandshakeInfo func() (types.Hash, *uint256.Int, error)
    58  
    59  type INetwork interface {
    60  	WriterMessage(messageType message.MessageType, payload []byte, peer peer.ID) error
    61  	//BroadcastMessage(messageType message.MessageType, payload []byte) (int, error)
    62  	SetHandler(message.MessageType, ConnHandler) error
    63  	ClosePeer(id peer.ID) error
    64  	Start() error
    65  	Host() host.Host
    66  	PeerCount() int
    67  	Bootstrapped() bool
    68  }
    69  
    70  type IPeer interface {
    71  	ID() peer.ID
    72  	Write(msg message.IMessage) error
    73  	WriteMsg(messageType message.MessageType, payload []byte) error
    74  	SetHandler(message.MessageType, ConnHandler) error
    75  	ClearHandler(message.MessageType) error
    76  	Close() error
    77  }
    78  
    79  type IPubSub interface {
    80  	JoinTopic(topic string) (*pubsub.Topic, error)
    81  	Publish(topic string, msg proto.Message) error
    82  	GetTopics() []string
    83  	Start() error
    84  }
    85  
    86  type IStateDB interface {
    87  	CreateAccount(types.Address)
    88  
    89  	SubBalance(addr types.Address, amount uint256.Int)
    90  	AddBalance(addr types.Address, amount uint256.Int)
    91  	GetBalance(addr types.Address) uint256.Int
    92  
    93  	GetNonce(addr types.Address) uint64
    94  	SetNonce(addr types.Address, nonce uint64)
    95  
    96  	GetCodeHash(addr types.Address) types.Hash
    97  	GetCode(addr types.Address) []byte
    98  	SetCode(addr types.Address, code []byte)
    99  	GetCodeSize(addr types.Address) int
   100  
   101  	AddRefund(uint64)
   102  	SubRefund(uint64)
   103  	GetRefund() uint64
   104  
   105  	GetCommittedState(types.Address, types.Hash) types.Hash
   106  	GetState(types.Address, types.Hash) types.Hash
   107  	SetState(types.Address, types.Hash, types.Hash)
   108  
   109  	Suicide(types.Address) bool
   110  	HasSuicided(types.Address) bool
   111  
   112  	Exist(types.Address) bool
   113  	Empty(types.Address) bool
   114  
   115  	PrepareAccessList(sender types.Address, dest *types.Address, precompiles []types.Address, list transaction.AccessList)
   116  	AddressInAccessList(addr types.Address) bool
   117  	SlotInAccessList(addr types.Address, slot types.Hash) (addressOk bool, slotOk bool)
   118  	AddAddressToAccessList(addr types.Address)
   119  	AddSlotToAccessList(addr types.Address, slot types.Hash)
   120  
   121  	RevertToSnapshot(int)
   122  	Snapshot() int
   123  
   124  	AddLog(*block.Log)
   125  	GetLogs(hash types.Hash, blockHash types.Hash) []*block.Log
   126  
   127  	TxIndex() int
   128  	Prepare(thash types.Hash, ti int)
   129  
   130  	Error() error
   131  }
   132  type ChainStateReader interface {
   133  	BalanceAt(ctx context.Context, account types.Address, blockNumber uint256.Int) (uint256.Int, error)
   134  	StorageAt(ctx context.Context, account types.Address, key types.Hash, blockNumber uint256.Int) ([]byte, error)
   135  	CodeAt(ctx context.Context, account types.Address, blockNumber uint256.Int) ([]byte, error)
   136  	NonceAt(ctx context.Context, account types.Address, blockNumber uint256.Int) (uint64, error)
   137  }
   138  
   139  type ITxsPool interface {
   140  	Service
   141  	Has(hash types.Hash) bool
   142  	Pending(enforceTips bool) map[types.Address][]*transaction.Transaction
   143  	GetTransaction() ([]*transaction.Transaction, error)
   144  	GetTx(hash types.Hash) *transaction.Transaction
   145  	AddRemotes(txs []*transaction.Transaction) []error
   146  	AddLocal(tx *transaction.Transaction) error
   147  	Stats() (int, int, int, int)
   148  	Nonce(addr types.Address) uint64
   149  	Content() (map[types.Address][]*transaction.Transaction, map[types.Address][]*transaction.Transaction)
   150  }