Skip to content

Commit

Permalink
feat: add methods for first and last block (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored Mar 4, 2020
1 parent a3b3034 commit b2a1cfb
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
24 changes: 24 additions & 0 deletions client/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ func (s *BlocksService) Get(ctx context.Context, id int64) (*GetBlock, *http.Res
return responseStruct, resp, err
}

// Get the first block.
func (s *BlocksService) First(ctx context.Context) (*GetBlock, *http.Response, error) {
var responseStruct *GetBlock
resp, err := s.client.SendRequest(ctx, "GET", "blocks/first", nil, nil, &responseStruct)

if err != nil {
return nil, resp, err
}

return responseStruct, resp, err
}

// Get the last block.
func (s *BlocksService) Last(ctx context.Context) (*GetBlock, *http.Response, error) {
var responseStruct *GetBlock
resp, err := s.client.SendRequest(ctx, "GET", "blocks/last", nil, nil, &responseStruct)

if err != nil {
return nil, resp, err
}

return responseStruct, resp, err
}

// Get all transactions by the given block.
func (s *BlocksService) Transactions(ctx context.Context, id int64, query *Pagination) (*GetBlockTransactions, *http.Response, error) {
uri := fmt.Sprintf("blocks/%v/transactions", id)
Expand Down
2 changes: 1 addition & 1 deletion client/blocks_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type BlockForged struct {

type BlockPayload struct {
Hash string `json:"hash,omitempty"`
Length byte `json:"length,omitempty"`
Length uint32 `json:"length,omitempty"`
}

type BlockGenerator struct {
Expand Down
152 changes: 152 additions & 0 deletions client/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,158 @@ func TestBlocksService_Get(t *testing.T) {
})
}

// Get the first block.
func TestBlocksService_First(t *testing.T) {
client, mux, _, teardown := setupTest()
defer teardown()

mux.HandleFunc("/blocks/first", func(writer http.ResponseWriter, request *http.Request) {
testMethod(t, request, "GET")
fmt.Fprint(writer,
`{
"data": {
"id": "13114381566690093367",
"version": 0,
"height": 1,
"previous": "0",
"forged": {
"reward": "0",
"fee": "0",
"total": "0",
"amount": "12500000000000004"
},
"payload": {
"hash": "2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867",
"length": 11395
},
"generator": {
"address": "D6Z26L69gdk9qYmTv5uzk3uGepigtHY4ax",
"publicKey": "03d3fdad9c5b25bf8880e6b519eb3611a5c0b31adebc8455f0e096175b28321aff"
},
"signature": "3044022035694a9b99a9236655c658eb07fc3b02ce5edcc24b76424a7287c54ed3822b0602203621e92defb360490610f763d85e94c2db2807a4bd7756cc8a6a585463ef7bae",
"confirmations": 4347586,
"transactions": 52,
"timestamp": {
"epoch": 0,
"unix": 1490101200,
"human": "2017-03-21T13:00:00.000Z"
}
}
}`)
})

responseStruct, response, err := client.Blocks.First(context.Background())
testGeneralError(t, "Blocks.First", err)
testResponseUrl(t, "Blocks.First", response, "/blocks/first")
testResponseStruct(t, "Blocks.First", responseStruct, &GetBlock{
Data: Block{
Id: "13114381566690093367",
Version: 0,
Height: 1,
Previous: "0",
Forged: BlockForged{
Reward: 0,
Fee: 0,
Total: 0,
Amount: 12500000000000004,
},
Payload: BlockPayload{
Hash: "2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867",
Length: 11395,
},
Generator: BlockGenerator{
Address: "D6Z26L69gdk9qYmTv5uzk3uGepigtHY4ax",
PublicKey: "03d3fdad9c5b25bf8880e6b519eb3611a5c0b31adebc8455f0e096175b28321aff",
},
Signature: "3044022035694a9b99a9236655c658eb07fc3b02ce5edcc24b76424a7287c54ed3822b0602203621e92defb360490610f763d85e94c2db2807a4bd7756cc8a6a585463ef7bae",
Confirmations: 4347586,
Transactions: 52,
Timestamp: Timestamp{
Epoch: 0,
Unix: 1490101200,
Human: "2017-03-21T13:00:00.000Z",
},
},
})
}

// Get the last block.
func TestBlocksService_Last(t *testing.T) {
client, mux, _, teardown := setupTest()
defer teardown()

mux.HandleFunc("/blocks/last", func(writer http.ResponseWriter, request *http.Request) {
testMethod(t, request, "GET")
fmt.Fprint(writer,
`{
"data": {
"id": "dummy",
"version": 0,
"height": 10,
"previous": "dummy",
"forged": {
"reward": "200000000",
"fee": "0",
"total": "200000000",
"amount": "0"
},
"payload": {
"hash": "dummy",
"length": 0
},
"generator": {
"username": "dummy",
"address": "dummy",
"publicKey": "dummy"
},
"signature": "dummy",
"confirmations": 0,
"transactions": 0,
"timestamp": {
"epoch": 40678848,
"unix": 1530780048,
"human": "2018-07-05T08:40:48Z"
}
}
}`)
})

responseStruct, response, err := client.Blocks.Last(context.Background())
testGeneralError(t, "Blocks.Last", err)
testResponseUrl(t, "Blocks.Last", response, "/blocks/last")
testResponseStruct(t, "Blocks.Last", responseStruct, &GetBlock{
Data: Block{
Id: "dummy",
Version: 0,
Height: 10,
Previous: "dummy",
Forged: BlockForged{
Reward: 200000000,
Fee: 0,
Total: 200000000,
Amount: 0,
},
Payload: BlockPayload{
Hash: "dummy",
Length: 0,
},
Generator: BlockGenerator{
Username: "dummy",
Address: "dummy",
PublicKey: "dummy",
},
Signature: "dummy",
Confirmations: 0,
Transactions: 0,
Timestamp: Timestamp{
Epoch: 40678848,
Unix: 1530780048,
Human: "2018-07-05T08:40:48Z",
},
},
})
}

// Get all transactions by the given block.
func TestBlocksService_Transactions(t *testing.T) {
client, mux, _, teardown := setupTest()
Expand Down

0 comments on commit b2a1cfb

Please sign in to comment.