diff --git a/monero/client/client.go b/monero/client/client.go index b9a2054..ad0620c 100644 --- a/monero/client/client.go +++ b/monero/client/client.go @@ -10,7 +10,6 @@ import ( "git.gammaspectra.live/P2Pool/consensus/v3/types" "git.gammaspectra.live/P2Pool/consensus/v3/utils" "github.com/floatdrop/lru" - fasthex "github.com/tmthrgd/go-hex" "sync" "sync/atomic" "time" @@ -105,14 +104,10 @@ func (c *Client) GetTransactions(txIds ...types.Hash) (data [][]byte, jsonTx []* return data, jsonTx, nil } <-c.throttler - hs := make([]string, 0, len(txIds)) - for _, h := range txIds { - hs = append(hs, h.String()) - } - if result, err := c.d.GetTransactions(context.Background(), hs); err != nil { + if result, err := c.d.GetTransactions(context.Background(), txIds); err != nil { return nil, nil, err } else { - if len(result.Txs) != len(hs) { + if len(result.Txs) != len(txIds) { return nil, nil, errors.New("invalid transaction count") } @@ -123,11 +118,7 @@ func (c *Client) GetTransactions(txIds ...types.Hash) (data [][]byte, jsonTx []* } for _, tx := range result.Txs { - if buf, err := fasthex.DecodeString(tx.PrunedAsHex); err != nil { - return nil, nil, err - } else { - data = append(data, buf) - } + data = append(data, tx.PrunedAsHex) } } @@ -137,29 +128,25 @@ func (c *Client) GetTransactions(txIds ...types.Hash) (data [][]byte, jsonTx []* func (c *Client) GetCoinbaseTransaction(txId types.Hash) (*transaction.CoinbaseTransaction, error) { if tx := c.coinbaseTransactionCache.Get(txId); tx == nil { <-c.throttler - if result, err := c.d.GetTransactions(context.Background(), []string{txId.String()}); err != nil { + if result, err := c.d.GetTransactions(context.Background(), []types.Hash{txId}); err != nil { return nil, err } else { if len(result.Txs) != 1 { return nil, errors.New("invalid transaction count") } - if buf, err := fasthex.DecodeString(result.Txs[0].PrunedAsHex); err != nil { + tx := &transaction.CoinbaseTransaction{} + if err = tx.UnmarshalBinary(result.Txs[0].PrunedAsHex); err != nil { return nil, err - } else { - tx := &transaction.CoinbaseTransaction{} - if err = tx.UnmarshalBinary(buf); err != nil { - return nil, err - } - - if tx.CalculateId() != txId { - return nil, fmt.Errorf("expected %s, got %s", txId.String(), tx.CalculateId().String()) - } - - c.coinbaseTransactionCache.Set(txId, tx) - - return tx, nil } + + if tx.CalculateId() != txId { + return nil, fmt.Errorf("expected %s, got %s", txId.String(), tx.CalculateId().String()) + } + + c.coinbaseTransactionCache.Set(txId, tx) + + return tx, nil } } else { return *tx, nil @@ -182,13 +169,7 @@ type TransactionInput struct { func (c *Client) GetTransactionInputs(ctx context.Context, hashes ...types.Hash) ([]TransactionInputResult, error) { <-c.throttler - if result, err := c.d.GetTransactions(ctx, func() []string { - result := make([]string, 0, len(hashes)) - for _, h := range hashes { - result = append(result, h.String()) - } - return result - }()); err != nil { + if result, err := c.d.GetTransactions(ctx, hashes); err != nil { return nil, err } else { if len(result.Txs) != len(hashes) { @@ -208,7 +189,7 @@ func (c *Client) GetTransactionInputs(ctx context.Context, hashes ...types.Hash) s[ix].Inputs = make([]TransactionInput, len(tx.Vin)) for i, input := range tx.Vin { s[ix].Inputs[i].Amount = uint64(input.Key.Amount) - s[ix].Inputs[i].KeyImage, _ = types.HashFromString(input.Key.KImage) + s[ix].Inputs[i].KeyImage = input.Key.KImage s[ix].Inputs[i].KeyOffsets = make([]uint64, len(input.Key.KeyOffsets)) for j, o := range input.Key.KeyOffsets { s[ix].Inputs[i].KeyOffsets[j] = uint64(o) @@ -237,7 +218,7 @@ type Output struct { func (c *Client) GetOutputIndexes(id types.Hash) (indexes []uint64, err error) { <-c.throttler - return c.d.GetOIndexes(context.Background(), id.String()) + return c.d.GetOIndexes(context.Background(), id) } func (c *Client) GetOuts(inputs ...uint64) ([]Output, error) { @@ -261,9 +242,9 @@ func (c *Client) GetOuts(inputs ...uint64) ([]Output, error) { o := &result.Outs[i] s[i].GlobalOutputIndex = inputs[i] s[i].Height = o.Height - s[i].Key, _ = types.HashFromString(o.Key) - s[i].Mask, _ = types.HashFromString(o.Mask) - s[i].TransactionId, _ = types.HashFromString(o.Txid) + s[i].Key = o.Key + s[i].Mask = o.Mask + s[i].TransactionId = o.Txid s[i].Unlocked = o.Unlocked } @@ -300,7 +281,7 @@ func (c *Client) GetInfo() (*daemon.GetInfoResult, error) { func (c *Client) GetBlockHeaderByHash(hash types.Hash, ctx context.Context) (*daemon.BlockHeader, error) { <-c.throttler - if result, err := c.d.GetBlockHeaderByHash(ctx, []string{hash.String()}); err != nil { + if result, err := c.d.GetBlockHeaderByHash(ctx, []types.Hash{hash}); err != nil { return nil, err } else if result != nil && len(result.BlockHeaders) > 0 { return &result.BlockHeaders[0], nil @@ -312,7 +293,7 @@ func (c *Client) GetBlockHeaderByHash(hash types.Hash, ctx context.Context) (*da func (c *Client) GetBlock(hash types.Hash, ctx context.Context) (*daemon.GetBlockResult, error) { <-c.throttler if result, err := c.d.GetBlock(ctx, daemon.GetBlockRequestParameters{ - Hash: hash.String(), + Hash: hash, }); err != nil { return nil, err } else { diff --git a/monero/client/rpc/daemon/binary_endpoints.go b/monero/client/rpc/daemon/binary_endpoints.go index 7202aed..a4b1e41 100644 --- a/monero/client/rpc/daemon/binary_endpoints.go +++ b/monero/client/rpc/daemon/binary_endpoints.go @@ -5,7 +5,7 @@ import ( "context" "errors" "git.gammaspectra.live/P2Pool/consensus/v3/monero/client/levin" - fasthex "github.com/tmthrgd/go-hex" + "git.gammaspectra.live/P2Pool/consensus/v3/types" "io" ) @@ -14,18 +14,13 @@ const ( ) func (c *Client) GetOIndexes( - ctx context.Context, txid string, + ctx context.Context, txid types.Hash, ) (indexes []uint64, finalError error) { - binaryTxId, err := fasthex.DecodeString(txid) - if err != nil { - return nil, err - } - storage := levin.PortableStorage{Entries: levin.Entries{ levin.Entry{ Name: "txid", - Serializable: levin.BoostString(binaryTxId), + Serializable: levin.BoostString(txid[:]), }, }} diff --git a/monero/client/rpc/daemon/jsonrpc.go b/monero/client/rpc/daemon/jsonrpc.go index 1640daf..4aec478 100644 --- a/monero/client/rpc/daemon/jsonrpc.go +++ b/monero/client/rpc/daemon/jsonrpc.go @@ -3,8 +3,8 @@ package daemon import ( "context" "fmt" + "git.gammaspectra.live/P2Pool/consensus/v3/types" "git.gammaspectra.live/P2Pool/consensus/v3/utils" - fasthex "github.com/tmthrgd/go-hex" ) const ( @@ -189,20 +189,20 @@ func (c *Client) GetBlockCount( func (c *Client) OnGetBlockHash( ctx context.Context, height uint64, -) (string, error) { +) (types.Hash, error) { resp := "" params := []uint64{height} err := c.JSONRPC(ctx, methodOnGetBlockHash, params, &resp) if err != nil { - return "", fmt.Errorf("jsonrpc: %w", err) + return types.ZeroHash, fmt.Errorf("jsonrpc: %w", err) } - return resp, nil + return types.HashFromString(resp) } func (c *Client) RelayTx( - ctx context.Context, txns []string, + ctx context.Context, txns []types.Hash, ) (*RelayTxResult, error) { resp := &RelayTxResult{} params := map[string]interface{}{ @@ -246,15 +246,10 @@ func (c *Client) GetMinerData(ctx context.Context) (*GetMinerDataResult, error) return resp, nil } -func (c *Client) SubmitBlock(ctx context.Context, blobs ...[]byte) (*SubmitBlockResult, error) { +func (c *Client) SubmitBlock(ctx context.Context, blobs ...types.Bytes) (*SubmitBlockResult, error) { resp := &SubmitBlockResult{} - params := make([]string, 0, len(blobs)) - for _, blob := range blobs { - params = append(params, fasthex.EncodeToString(blob)) - } - - err := c.JSONRPC(ctx, methodSubmitBlock, params, resp) + err := c.JSONRPC(ctx, methodSubmitBlock, blobs, resp) if err != nil { return nil, fmt.Errorf("jsonrpc: %w", err) } @@ -367,7 +362,7 @@ func (c *Client) GetBlockHeaderByHeight( // GetBlockHeaderByHash retrieves block header information for either one or // multiple blocks. func (c *Client) GetBlockHeaderByHash( - ctx context.Context, hashes []string, + ctx context.Context, hashes []types.Hash, ) (*GetBlockHeaderByHashResult, error) { resp := &GetBlockHeaderByHashResult{} params := map[string]interface{}{ @@ -385,8 +380,8 @@ func (c *Client) GetBlockHeaderByHash( // GetBlockRequestParameters represents the set of possible parameters that can // be used for submitting a call to the `get_block` jsonrpc method. type GetBlockRequestParameters struct { - Height uint64 `json:"height,omitempty"` - Hash string `json:"hash,omitempty"` + Height uint64 `json:"height,omitempty"` + Hash types.Hash `json:"hash,omitempty"` } // GetBlock fetches full block information from a block at a particular hash OR diff --git a/monero/client/rpc/daemon/raw_endpoints.go b/monero/client/rpc/daemon/raw_endpoints.go index 43518c0..430ff49 100644 --- a/monero/client/rpc/daemon/raw_endpoints.go +++ b/monero/client/rpc/daemon/raw_endpoints.go @@ -3,6 +3,7 @@ package daemon import ( "context" "fmt" + "git.gammaspectra.live/P2Pool/consensus/v3/types" "git.gammaspectra.live/P2Pool/consensus/v3/utils" ) @@ -242,7 +243,7 @@ func (r *GetTransactionsResult) GetTransactions() ([]*TransactionJSON, error) { } func (c *Client) GetTransactions( - ctx context.Context, txns []string, + ctx context.Context, txns []types.Hash, ) (*GetTransactionsResult, error) { resp := &GetTransactionsResult{} params := map[string]interface{}{ diff --git a/monero/client/rpc/daemon/types.go b/monero/client/rpc/daemon/types.go index 721f3db..d27360a 100644 --- a/monero/client/rpc/daemon/types.go +++ b/monero/client/rpc/daemon/types.go @@ -1,5 +1,10 @@ package daemon +import ( + "git.gammaspectra.live/P2Pool/consensus/v3/p2pool/mempool" + "git.gammaspectra.live/P2Pool/consensus/v3/types" +) + // RPCResultFooter contains the set of fields that every RPC result message // will contain. type RPCResultFooter struct { @@ -33,11 +38,11 @@ type GetAlternateChainsResult struct { // BlockHash is the hash of the first diverging block of this // alternative chain. // - BlockHash string `json:"block_hash"` + BlockHash types.Hash `json:"block_hash"` // BlockHashes TODO // - BlockHashes []string `json:"block_hashes"` + BlockHashes []types.Hash `json:"block_hashes"` // Difficulty is the cumulative difficulty of all blocks in the // alternative chain. @@ -61,7 +66,7 @@ type GetAlternateChainsResult struct { // MainChainParentBlock TODO // - MainChainParentBlock string `json:"main_chain_parent_block"` + MainChainParentBlock types.Hash `json:"main_chain_parent_block"` // WideDifficulty is the network difficulty as a hexadecimal // string representing a 128-bit number. @@ -194,44 +199,44 @@ type GetFeeEstimateResult struct { // GetInfoResult is the result of a call to the GetInfo RPC method. type GetInfoResult struct { - AdjustedTime uint64 `json:"adjusted_time"` - AltBlocksCount int `json:"alt_blocks_count"` - BlockSizeLimit uint64 `json:"block_size_limit"` - BlockSizeMedian uint64 `json:"block_size_median"` - BlockWeightLimit uint64 `json:"block_weight_limit"` - BlockWeightMedian uint64 `json:"block_weight_median"` - BootstrapDaemonAddress string `json:"bootstrap_daemon_address"` - BusySyncing bool `json:"busy_syncing"` - CumulativeDifficulty int64 `json:"cumulative_difficulty"` - CumulativeDifficultyTop64 uint64 `json:"cumulative_difficulty_top64"` - DatabaseSize uint64 `json:"database_size"` - Difficulty uint64 `json:"difficulty"` - DifficultyTop64 uint64 `json:"difficulty_top64"` - FreeSpace uint64 `json:"free_space"` - GreyPeerlistSize uint `json:"grey_peerlist_size"` - Height uint64 `json:"height"` - HeightWithoutBootstrap uint64 `json:"height_without_bootstrap"` - IncomingConnectionsCount uint `json:"incoming_connections_count"` - Mainnet bool `json:"mainnet"` - Nettype string `json:"nettype"` - Offline bool `json:"offline"` - OutgoingConnectionsCount uint `json:"outgoing_connections_count"` - RPCConnectionsCount uint `json:"rpc_connections_count"` - Stagenet bool `json:"stagenet"` - StartTime uint64 `json:"start_time"` - Synchronized bool `json:"synchronized"` - Target uint64 `json:"target"` - TargetHeight uint64 `json:"target_height"` - Testnet bool `json:"testnet"` - TopBlockHash string `json:"top_block_hash"` - TxCount uint64 `json:"tx_count"` - TxPoolSize uint64 `json:"tx_pool_size"` - UpdateAvailable bool `json:"update_available"` - Version string `json:"version"` - WasBootstrapEverUsed bool `json:"was_bootstrap_ever_used"` - WhitePeerlistSize uint `json:"white_peerlist_size"` - WideCumulativeDifficulty string `json:"wide_cumulative_difficulty"` - WideDifficulty string `json:"wide_difficulty"` + AdjustedTime uint64 `json:"adjusted_time"` + AltBlocksCount int `json:"alt_blocks_count"` + BlockSizeLimit uint64 `json:"block_size_limit"` + BlockSizeMedian uint64 `json:"block_size_median"` + BlockWeightLimit uint64 `json:"block_weight_limit"` + BlockWeightMedian uint64 `json:"block_weight_median"` + BootstrapDaemonAddress string `json:"bootstrap_daemon_address"` + BusySyncing bool `json:"busy_syncing"` + CumulativeDifficulty int64 `json:"cumulative_difficulty"` + CumulativeDifficultyTop64 uint64 `json:"cumulative_difficulty_top64"` + DatabaseSize uint64 `json:"database_size"` + Difficulty uint64 `json:"difficulty"` + DifficultyTop64 uint64 `json:"difficulty_top64"` + FreeSpace uint64 `json:"free_space"` + GreyPeerlistSize uint `json:"grey_peerlist_size"` + Height uint64 `json:"height"` + HeightWithoutBootstrap uint64 `json:"height_without_bootstrap"` + IncomingConnectionsCount uint `json:"incoming_connections_count"` + Mainnet bool `json:"mainnet"` + Nettype string `json:"nettype"` + Offline bool `json:"offline"` + OutgoingConnectionsCount uint `json:"outgoing_connections_count"` + RPCConnectionsCount uint `json:"rpc_connections_count"` + Stagenet bool `json:"stagenet"` + StartTime uint64 `json:"start_time"` + Synchronized bool `json:"synchronized"` + Target uint64 `json:"target"` + TargetHeight uint64 `json:"target_height"` + Testnet bool `json:"testnet"` + TopBlockHash types.Hash `json:"top_block_hash"` + TxCount uint64 `json:"tx_count"` + TxPoolSize uint64 `json:"tx_pool_size"` + UpdateAvailable bool `json:"update_available"` + Version string `json:"version"` + WasBootstrapEverUsed bool `json:"was_bootstrap_ever_used"` + WhitePeerlistSize uint `json:"white_peerlist_size"` + WideCumulativeDifficulty string `json:"wide_cumulative_difficulty"` + WideDifficulty string `json:"wide_difficulty"` RPCResultFooter `json:",inline"` } @@ -241,11 +246,11 @@ type GetInfoResult struct { type GetBlockTemplateResult struct { // BlockhashingBlob is the blob on which to try to find a valid nonce. // - BlockhashingBlob string `json:"blockhashing_blob"` + BlockhashingBlob types.Bytes `json:"blockhashing_blob"` // BlocktemplateBlob is the blob on which to try to mine a new block. // - BlocktemplateBlob string `json:"blocktemplate_blob"` + BlocktemplateBlob types.Bytes `json:"blocktemplate_blob"` // Difficulty is the difficulty of the next block. Difficulty int64 `json:"difficulty"` @@ -262,7 +267,7 @@ type GetBlockTemplateResult struct { // PrevHash is the hash of the most recent block on which to mine the // next block. // - PrevHash string `json:"prev_hash"` + PrevHash types.Hash `json:"prev_hash"` // ReservedOffset TODO // @@ -274,20 +279,15 @@ type GetBlockTemplateResult struct { // GetMinerDataResult is the result of a call to the GetMinerData RPC // method. type GetMinerDataResult struct { - MajorVersion uint8 `json:"major_version"` - Height uint64 `json:"height"` - PrevId string `json:"prev_id"` - SeedHash string `json:"seed_hash"` - Difficulty string `json:"difficulty"` - MedianWeight uint64 `json:"median_weight"` - AlreadyGeneratedCoins uint64 `json:"already_generated_coins"` - MedianTimestamp uint64 `json:"median_timestamp"` - TxBacklog []struct { - Id string `json:"id"` - BlobSize uint64 `json:"blob_size"` - Weight uint64 `json:"weight"` - Fee uint64 `json:"fee"` - } `json:"tx_backlog"` + MajorVersion uint8 `json:"major_version"` + Height uint64 `json:"height"` + PrevId types.Hash `json:"prev_id"` + SeedHash types.Hash `json:"seed_hash"` + Difficulty types.Difficulty `json:"difficulty"` + MedianWeight uint64 `json:"median_weight"` + AlreadyGeneratedCoins uint64 `json:"already_generated_coins"` + MedianTimestamp uint64 `json:"median_timestamp"` + TxBacklog mempool.Mempool `json:"tx_backlog"` } // SubmitBlockResult is the result of a call to the SubmitBlock RPC @@ -352,11 +352,11 @@ type GetConnectionsResult struct { type GetOutsResult struct { Outs []struct { - Height uint64 `json:"height"` - Key string `json:"key"` - Mask string `json:"mask"` - Txid string `json:"txid"` - Unlocked bool `json:"unlocked"` + Height uint64 `json:"height"` + Key types.Hash `json:"key"` + Mask types.Hash `json:"mask"` + Txid types.Hash `json:"txid"` + Unlocked bool `json:"unlocked"` } `json:"outs"` RPCResultFooter `json:",inline"` @@ -364,8 +364,8 @@ type GetOutsResult struct { // GetHeightResult is the result of a call to the GetHeight RPC method. type GetHeightResult struct { - Hash string `json:"hash"` - Height uint64 `json:"height"` + Hash types.Hash `json:"hash"` + Height uint64 `json:"height"` RPCResultFooter `json:",inline"` } @@ -393,8 +393,8 @@ type GetPublicNodesResult struct { // GenerateBlocksResult is the result of a call to the GenerateBlocks RPC // method. type GenerateBlocksResult struct { - Blocks []string `json:"blocks"` - Height int `json:"height"` + Blocks []types.Hash `json:"blocks"` + Height int `json:"height"` RPCResultFooter `json:",inline"` } @@ -460,7 +460,7 @@ type BlockHeader struct { // Hash is the hash of this block. // - Hash string `json:"hash"` + Hash types.Hash `json:"hash"` // Height is the number of blocks preceding this block on the // blockchain. @@ -478,7 +478,7 @@ type BlockHeader struct { // MinerTxHash TODO // - MinerTxHash string `json:"miner_tx_hash"` + MinerTxHash types.Hash `json:"miner_tx_hash"` // MinorVersion is the minor version of the monero protocol at // this block height. @@ -502,12 +502,12 @@ type BlockHeader struct { // PowHash TODO // - PowHash string `json:"pow_hash"` + PowHash types.Hash `json:"pow_hash"` // PrevHash is the hash of the block immediately preceding this // block in the chain. // - PrevHash string `json:"prev_hash"` + PrevHash types.Hash `json:"prev_hash,omitempty"` // Reward the amount of new atomic-units generated in this // block and rewarded to the miner (1XMR = 1e12 atomic units). @@ -535,7 +535,7 @@ type BlockHeader struct { type GetBlockResult struct { // Blob is a hexadecimal representation of the block. // - Blob string `json:"blob"` + Blob types.Bytes `json:"blob"` // BlockHeader contains the details from the block header. // @@ -548,7 +548,7 @@ type GetBlockResult struct { // MinerTxHash is the hash of the coinbase transaction // - MinerTxHash string `json:"miner_tx_hash"` + MinerTxHash types.Hash `json:"miner_tx_hash"` RPCResultFooter `json:",inline"` } @@ -569,7 +569,7 @@ type GetBlockResultJSON struct { // PrevID (same as `block_hash` in the block header) // - PrevID string `json:"prev_id"` + PrevID types.Hash `json:"prev_id"` // Nonce (same as in the block header) // @@ -600,7 +600,7 @@ type GetBlockResultJSON struct { Vout []struct { Amount uint64 `json:"amount"` Target struct { - Key string `json:"key"` + Key types.Hash `json:"key"` } `json:"target"` } `json:"vout"` // Extra (aka the transaction id) can be used to include any @@ -620,7 +620,7 @@ type GetBlockResultJSON struct { // TxHashes is the list of hashes of non-coinbase transactions in the // block. // - TxHashes []string `json:"tx_hashes"` + TxHashes []types.Hash `json:"tx_hashes"` } func (c *GetBlockResultJSON) MinerOutputs() uint64 { @@ -637,13 +637,13 @@ func (c *GetBlockResultJSON) MinerOutputs() uint64 { type SyncInfoResult struct { Credits uint64 `json:"credits"` - Height uint64 `json:"height"` - NextNeededPruningSeed uint64 `json:"next_needed_pruning_seed"` - Overview string `json:"overview"` - Status string `json:"status"` - TargetHeight uint64 `json:"target_height"` - TopHash string `json:"top_hash"` - Untrusted bool `json:"untrusted"` + Height uint64 `json:"height"` + NextNeededPruningSeed uint64 `json:"next_needed_pruning_seed"` + Overview string `json:"overview"` + Status string `json:"status"` + TargetHeight uint64 `json:"target_height"` + TopHash types.Hash `json:"top_hash"` + Untrusted bool `json:"untrusted"` Peers []struct { Info struct { Address string `json:"address"` @@ -756,25 +756,25 @@ type GetTransactionPoolStatsResult struct { } type GetTransactionsResultTransaction struct { - AsHex string `json:"as_hex"` - AsJSON string `json:"as_json"` - BlockHeight uint64 `json:"block_height"` - BlockTimestamp int64 `json:"block_timestamp"` - DoubleSpendSeen bool `json:"double_spend_seen"` - InPool bool `json:"in_pool"` - OutputIndices []int `json:"output_indices"` - PrunableAsHex string `json:"prunable_as_hex"` - PrunableHash string `json:"prunable_hash"` - PrunedAsHex string `json:"pruned_as_hex"` - TxHash string `json:"tx_hash"` + AsHex types.Bytes `json:"as_hex"` + AsJSON string `json:"as_json"` + BlockHeight uint64 `json:"block_height"` + BlockTimestamp int64 `json:"block_timestamp"` + DoubleSpendSeen bool `json:"double_spend_seen"` + InPool bool `json:"in_pool"` + OutputIndices []int `json:"output_indices"` + PrunableAsHex types.Bytes `json:"prunable_as_hex"` + PrunableHash types.Hash `json:"prunable_hash"` + PrunedAsHex types.Bytes `json:"pruned_as_hex"` + TxHash types.Hash `json:"tx_hash"` } type GetTransactionsResult struct { Credits int `json:"credits"` Status string `json:"status"` - TopHash string `json:"top_hash"` + TopHash types.Hash `json:"top_hash"` Txs []GetTransactionsResultTransaction `json:"txs"` - TxsAsHex []string `json:"txs_as_hex"` + TxsAsHex []types.Bytes `json:"txs_as_hex"` Untrusted bool `json:"untrusted"` } @@ -783,15 +783,15 @@ type TransactionJSON struct { UnlockTime int `json:"unlock_time"` Vin []struct { Key struct { - Amount int `json:"amount"` - KeyOffsets []uint `json:"key_offsets"` - KImage string `json:"k_image"` + Amount int `json:"amount"` + KeyOffsets []uint `json:"key_offsets"` + KImage types.Hash `json:"k_image"` } `json:"key"` } `json:"vin"` Vout []struct { Amount uint64 `json:"amount"` Target struct { - Key string `json:"key"` + Key types.Hash `json:"key"` } `json:"target"` } `json:"vout"` Extra []byte `json:"extra"` @@ -840,28 +840,28 @@ type TransactionJSON struct { type GetTransactionPoolResult struct { Credits int `json:"credits"` SpentKeyImages []struct { - IDHash string `json:"id_hash"` - TxsHashes []string `json:"txs_hashes"` + IDHash types.Hash `json:"id_hash"` + TxsHashes []types.Hash `json:"txs_hashes"` } `json:"spent_key_images"` - Status string `json:"status"` - TopHash string `json:"top_hash"` + Status string `json:"status"` + TopHash types.Hash `json:"top_hash"` Transactions []struct { - BlobSize uint64 `json:"blob_size"` - DoNotRelay bool `json:"do_not_relay"` - DoubleSpendSeen bool `json:"double_spend_seen"` - Fee uint64 `json:"fee"` - IDHash string `json:"id_hash"` - KeptByBlock bool `json:"kept_by_block"` - LastFailedHeight uint64 `json:"last_failed_height"` - LastFailedIDHash string `json:"last_failed_id_hash"` - LastRelayedTime uint64 `json:"last_relayed_time"` - MaxUsedBlockHeight uint64 `json:"max_used_block_height"` - MaxUsedBlockIDHash string `json:"max_used_block_id_hash"` - ReceiveTime int64 `json:"receive_time"` - Relayed bool `json:"relayed"` - TxBlob string `json:"tx_blob"` - TxJSON string `json:"tx_json"` - Weight uint64 `json:"weight"` + BlobSize uint64 `json:"blob_size"` + DoNotRelay bool `json:"do_not_relay"` + DoubleSpendSeen bool `json:"double_spend_seen"` + Fee uint64 `json:"fee"` + IDHash types.Hash `json:"id_hash"` + KeptByBlock bool `json:"kept_by_block"` + LastFailedHeight uint64 `json:"last_failed_height"` + LastFailedIDHash string `json:"last_failed_id_hash"` + LastRelayedTime uint64 `json:"last_relayed_time"` + MaxUsedBlockHeight uint64 `json:"max_used_block_height"` + MaxUsedBlockIDHash string `json:"max_used_block_id_hash"` + ReceiveTime int64 `json:"receive_time"` + Relayed bool `json:"relayed"` + TxBlob string `json:"tx_blob"` + TxJSON string `json:"tx_json"` + Weight uint64 `json:"weight"` } `json:"transactions"` Untrusted bool `json:"untrusted"` } diff --git a/p2pool/mainchain/mainchain.go b/p2pool/mainchain/mainchain.go index eca0672..ef52ab3 100644 --- a/p2pool/mainchain/mainchain.go +++ b/p2pool/mainchain/mainchain.go @@ -356,18 +356,16 @@ func (c *MainChain) DownloadBlockHeaders(currentHeight uint64) error { return fmt.Errorf("couldn't download block headers range for height %d to %d: %s", startHeight, currentHeight-1, err) } else { for _, header := range rangeResult.Headers { - prevHash, _ := types.HashFromString(header.PrevHash) - h, _ := types.HashFromString(header.Hash) c.HandleMainHeader(&mainblock.Header{ MajorVersion: uint8(header.MajorVersion), MinorVersion: uint8(header.MinorVersion), Timestamp: uint64(header.Timestamp), - PreviousId: prevHash, + PreviousId: header.PrevHash, Height: header.Height, Nonce: uint32(header.Nonce), Reward: header.Reward, - Id: h, - Difficulty: types.DifficultyFrom64(header.Difficulty), + Id: header.Hash, + Difficulty: types.NewDifficulty(header.Difficulty, header.DifficultyTop64), }) } utils.Logf("MainChain", "Downloaded headers for range %d to %d", startHeight, currentHeight-1) @@ -457,18 +455,16 @@ func (c *MainChain) getBlockHeader(height uint64) error { if header, err := c.p2pool.ClientRPC().GetBlockHeaderByHeight(height, c.p2pool.Context()); err != nil { return fmt.Errorf("couldn't download block header for height %d: %s", height, err) } else { - prevHash, _ := types.HashFromString(header.BlockHeader.PrevHash) - h, _ := types.HashFromString(header.BlockHeader.Hash) c.HandleMainHeader(&mainblock.Header{ MajorVersion: uint8(header.BlockHeader.MajorVersion), MinorVersion: uint8(header.BlockHeader.MinorVersion), Timestamp: uint64(header.BlockHeader.Timestamp), - PreviousId: prevHash, + PreviousId: header.BlockHeader.PrevHash, Height: header.BlockHeader.Height, Nonce: uint32(header.BlockHeader.Nonce), Reward: header.BlockHeader.Reward, - Id: h, - Difficulty: types.DifficultyFrom64(header.BlockHeader.Difficulty), + Id: header.BlockHeader.Hash, + Difficulty: types.NewDifficulty(header.BlockHeader.Difficulty, header.BlockHeader.DifficultyTop64), }) } diff --git a/p2pool/sidechain/fake_server.go b/p2pool/sidechain/fake_server.go index f0cd59f..c7a391f 100644 --- a/p2pool/sidechain/fake_server.go +++ b/p2pool/sidechain/fake_server.go @@ -63,12 +63,12 @@ func (s *FakeServer) GetMinimalBlockHeaderByHeight(height uint64) *mainblock.Hea MajorVersion: uint8(h.BlockHeader.MajorVersion), MinorVersion: uint8(h.BlockHeader.MinorVersion), Timestamp: uint64(h.BlockHeader.Timestamp), - PreviousId: types.MustHashFromString(h.BlockHeader.PrevHash), + PreviousId: h.BlockHeader.PrevHash, Height: h.BlockHeader.Height, Nonce: uint32(h.BlockHeader.Nonce), Reward: h.BlockHeader.Reward, - Difficulty: types.DifficultyFrom64(h.BlockHeader.Difficulty), - Id: types.MustHashFromString(h.BlockHeader.Hash), + Difficulty: types.NewDifficulty(h.BlockHeader.Difficulty, h.BlockHeader.DifficultyTop64), + Id: h.BlockHeader.Hash, } s.headers[height] = header return header diff --git a/p2pool/sidechain/poolblock_test.go b/p2pool/sidechain/poolblock_test.go index f9dcf9c..2ef27a1 100644 --- a/p2pool/sidechain/poolblock_test.go +++ b/p2pool/sidechain/poolblock_test.go @@ -20,7 +20,7 @@ func testPoolBlock(b *PoolBlock, t *testing.T, expectedBufferLength int, majorVe if h, err := client.GetDefaultClient().GetBlockByHeight(seedHeight, context.Background()); err != nil { return types.ZeroHash } else { - return types.MustHashFromString(h.BlockHeader.Hash) + return h.BlockHeader.Hash } }) @@ -58,7 +58,7 @@ func testPoolBlock(b *PoolBlock, t *testing.T, expectedBufferLength int, majorVe if h, err := client.GetDefaultClient().GetBlockByHeight(seedHeight, context.Background()); err != nil { return types.ZeroHash } else { - return types.MustHashFromString(h.BlockHeader.Hash) + return h.BlockHeader.Hash } }) { t.Fatal() diff --git a/p2pool/stratum/server_test.go b/p2pool/stratum/server_test.go index c2d7a3e..36d7e5a 100644 --- a/p2pool/stratum/server_test.go +++ b/p2pool/stratum/server_test.go @@ -47,9 +47,9 @@ func getMinerData() *p2pooltypes.MinerData { return &p2pooltypes.MinerData{ MajorVersion: d.MajorVersion, Height: d.Height, - PrevId: types.MustHashFromString(d.PrevId), - SeedHash: types.MustHashFromString(d.SeedHash), - Difficulty: types.MustDifficultyFromString(d.Difficulty), + PrevId: d.PrevId, + SeedHash: d.SeedHash, + Difficulty: d.Difficulty, MedianWeight: d.MedianWeight, AlreadyGeneratedCoins: d.AlreadyGeneratedCoins, MedianTimestamp: d.MedianTimestamp,