The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/rosetta/services/block_service.go

159 lines
3.7 KiB

Node API Refactor - pt3 (Stage 3.1 of Node API Overhaul) (#3297) * [rosetta] Add server with example block & network services * Update go.mod for rosetta SDK Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal/configs] Add RosettaServer to node config Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Add rosetta port flag Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Add rosetta server start & rename api.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Change server start to use nodeconfig of rosetta Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Cleanup debugging prints Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix stdout print & document placeholders Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix lint & make StartSevers more consistent Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Disable rosetta by default Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Make explorer deploy rosetta server Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Use direct http server for start * Make go.mod changes minimal Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix fmt Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix fmt of go.mod Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Use port 9700 instead of 10000 Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Bump config version Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Add v1.0.0 config backwards compatibility test * Included update message if old config is loaded Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Do not broadcast invalid tx on localnet Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Correct for invalid port when loading old config Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Make rosetta variable names consistent Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
package services
import (
"context"
"fmt"
"time"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/harmony-one/harmony/hmy"
)
// BlockAPIService implements the server.BlockAPIServicer interface.
type BlockAPIService struct {
hmy *hmy.Harmony
network *types.NetworkIdentifier
}
// NewBlockAPIService creates a new instance of a BlockAPIService.
func NewBlockAPIService(
network *types.NetworkIdentifier, hmy *hmy.Harmony,
) server.BlockAPIServicer {
return &BlockAPIService{
hmy: hmy,
network: network,
}
}
// Block implements the /block endpoint (placeholder)
// FIXME: remove placeholder & implement block endpoint
func (s *BlockAPIService) Block(
ctx context.Context,
request *types.BlockRequest,
) (*types.BlockResponse, *types.Error) {
if *request.BlockIdentifier.Index != 1000 {
previousBlockIndex := *request.BlockIdentifier.Index - 1
if previousBlockIndex < 0 {
previousBlockIndex = 0
}
return &types.BlockResponse{
Block: &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Index: *request.BlockIdentifier.Index,
Hash: fmt.Sprintf("block %d", *request.BlockIdentifier.Index),
},
ParentBlockIdentifier: &types.BlockIdentifier{
Index: previousBlockIndex,
Hash: fmt.Sprintf("block %d", previousBlockIndex),
},
Timestamp: time.Now().UnixNano() / 1000000,
Transactions: []*types.Transaction{},
},
}, nil
}
return &types.BlockResponse{
Block: &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Index: 1000,
Hash: "block 1000",
},
ParentBlockIdentifier: &types.BlockIdentifier{
Index: 999,
Hash: "block 999",
},
Timestamp: 1586483189000,
Transactions: []*types.Transaction{
{
TransactionIdentifier: &types.TransactionIdentifier{
Hash: "transaction 0",
},
Operations: []*types.Operation{
{
OperationIdentifier: &types.OperationIdentifier{
Index: 0,
},
Type: "Transfer",
Status: "Success",
Account: &types.AccountIdentifier{
Address: "account 0",
},
Amount: &types.Amount{
Value: "-1000",
Currency: &types.Currency{
Symbol: "ROS",
Decimals: 2,
},
},
},
{
OperationIdentifier: &types.OperationIdentifier{
Index: 1,
},
RelatedOperations: []*types.OperationIdentifier{
{
Index: 0,
},
},
Type: "Transfer",
Status: "Reverted",
Account: &types.AccountIdentifier{
Address: "account 1",
},
Amount: &types.Amount{
Value: "1000",
Currency: &types.Currency{
Symbol: "ROS",
Decimals: 2,
},
},
},
},
},
},
},
OtherTransactions: []*types.TransactionIdentifier{
{
Hash: "transaction 1",
},
},
}, nil
}
// BlockTransaction implements the /block/transaction endpoint (placeholder)
// FIXME: remove placeholder & implement block endpoint
func (s *BlockAPIService) BlockTransaction(
ctx context.Context,
request *types.BlockTransactionRequest,
) (*types.BlockTransactionResponse, *types.Error) {
return &types.BlockTransactionResponse{
Transaction: &types.Transaction{
TransactionIdentifier: &types.TransactionIdentifier{
Hash: "transaction 1",
},
Operations: []*types.Operation{
{
OperationIdentifier: &types.OperationIdentifier{
Index: 0,
},
Type: "Reward",
Status: "Success",
Account: &types.AccountIdentifier{
Address: "account 2",
},
Amount: &types.Amount{
Value: "1000",
Currency: &types.Currency{
Symbol: "ROS",
Decimals: 2,
},
},
},
},
},
}, nil
}