github.com/cryptohub-digital/blockbook-fork@v0.0.0-20230713133354-673c927af7f1/bchain/coins/xcb/cvm.go (about) 1 package xcb 2 3 import ( 4 "context" 5 "math/big" 6 7 "github.com/core-coin/go-core/v2" 8 "github.com/core-coin/go-core/v2/common" 9 "github.com/core-coin/go-core/v2/core/types" 10 "github.com/core-coin/go-core/v2/rpc" 11 "github.com/core-coin/go-core/v2/xcbclient" 12 "github.com/cryptohub-digital/blockbook-fork/bchain" 13 ) 14 15 // CoreblockchainClient wraps a client to implement the CVMClient interface 16 type CoreblockchainClient struct { 17 *xcbclient.Client 18 } 19 20 // HeaderByNumber returns a block header that implements the CVMHeader interface 21 func (c *CoreblockchainClient) HeaderByNumber(ctx context.Context, number *big.Int) (CVMHeader, error) { 22 h, err := c.Client.HeaderByNumber(ctx, number) 23 if err != nil { 24 return nil, err 25 } 26 27 return &CoreCoinHeader{Header: h}, nil 28 } 29 30 // EstimateEnergy returns the current estimated energy cost for executing a transaction 31 func (c *CoreblockchainClient) EstimateEnergy(ctx context.Context, msg interface{}) (uint64, error) { 32 return c.Client.EstimateEnergy(ctx, msg.(core.CallMsg)) 33 } 34 35 // BalanceAt returns the balance for the given account at a specific block, or latest known block if no block number is provided 36 func (c *CoreblockchainClient) BalanceAt(ctx context.Context, addrDesc bchain.AddressDescriptor, blockNumber *big.Int) (*big.Int, error) { 37 return c.Client.BalanceAt(ctx, common.BytesToAddress(addrDesc), blockNumber) 38 } 39 40 // NonceAt returns the nonce for the given account at a specific block, or latest known block if no block number is provided 41 func (c *CoreblockchainClient) NonceAt(ctx context.Context, addrDesc bchain.AddressDescriptor, blockNumber *big.Int) (uint64, error) { 42 return c.Client.NonceAt(ctx, common.BytesToAddress(addrDesc), blockNumber) 43 } 44 45 // CoreCoinRPCClient wraps an rpc client to implement the CVMRPCClient interface 46 type CoreCoinRPCClient struct { 47 *rpc.Client 48 } 49 50 // XcbSubscribe subscribes to events and returns a client subscription that implements the EVMClientSubscription interface 51 func (c *CoreCoinRPCClient) XcbSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (CVMClientSubscription, error) { 52 sub, err := c.Client.XcbSubscribe(ctx, channel, args...) 53 if err != nil { 54 return nil, err 55 } 56 57 return &CoreCoinClientSubscription{ClientSubscription: sub}, nil 58 } 59 60 // CoreCoinHeader wraps a block header to implement the CVMHeader interface 61 type CoreCoinHeader struct { 62 *types.Header 63 } 64 65 // Hash returns the block hash as a hex string 66 func (h *CoreCoinHeader) Hash() string { 67 return h.Header.Hash().Hex() 68 } 69 70 // Number returns the block number 71 func (h *CoreCoinHeader) Number() *big.Int { 72 return h.Header.Number 73 } 74 75 // Difficulty returns the block difficulty 76 func (h *CoreCoinHeader) Difficulty() *big.Int { 77 return h.Header.Difficulty 78 } 79 80 // CoreCoinHash wraps a transaction hash to implement the CVMHash interface 81 type CoreCoinHash struct { 82 common.Hash 83 } 84 85 // CoreCoinClientSubscription wraps a client subcription to implement the CVMClientSubscription interface 86 type CoreCoinClientSubscription struct { 87 *rpc.ClientSubscription 88 } 89 90 // CoreCoinNewBlock wraps a block header channel to implement the CVMNewBlockSubscriber interface 91 type CoreCoinNewBlock struct { 92 channel chan *types.Header 93 } 94 95 // NewCoreCoinNewBlock returns an initialized CoreCoinNewBlock struct 96 func NewCoreCoinNewBlock() *CoreCoinNewBlock { 97 return &CoreCoinNewBlock{channel: make(chan *types.Header)} 98 } 99 100 // Channel returns the underlying channel as an empty interface 101 func (s *CoreCoinNewBlock) Channel() interface{} { 102 return s.channel 103 } 104 105 // Read from the underlying channel and return a block header that implements the CVMHeader interface 106 func (s *CoreCoinNewBlock) Read() (CVMHeader, bool) { 107 h, ok := <-s.channel 108 return &CoreCoinHeader{Header: h}, ok 109 } 110 111 // Close the underlying channel 112 func (s *CoreCoinNewBlock) Close() { 113 close(s.channel) 114 } 115 116 // CoreCoinNewTx wraps a transaction hash channel to implement the CVMNewTxSubscriber interface 117 type CoreCoinNewTx struct { 118 channel chan common.Hash 119 } 120 121 // NewCoreCoinNewTx returns an initialized CoreCoinNewTx struct 122 func NewCoreCoinNewTx() *CoreCoinNewTx { 123 return &CoreCoinNewTx{channel: make(chan common.Hash)} 124 } 125 126 // Channel returns the underlying channel as an empty interface 127 func (s *CoreCoinNewTx) Channel() interface{} { 128 return s.channel 129 } 130 131 // Read from the underlying channel and return a transaction hash that implements the CVMHash interface 132 func (s *CoreCoinNewTx) Read() (CVMHash, bool) { 133 h, ok := <-s.channel 134 return &CoreCoinHash{Hash: h}, ok 135 } 136 137 // Close the underlying channel 138 func (s *CoreCoinNewTx) Close() { 139 close(s.channel) 140 }