Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b76b6c0

Browse files
committed
Added more receipt tests, fixed receipt wsapi.
1 parent 56dca91 commit b76b6c0

5 files changed

Lines changed: 67 additions & 27 deletions

File tree

receipts/receipts.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type Receipt struct {
2323
}
2424

2525
func (e *Receipt) TrimReceipt() {
26+
if e == nil {
27+
return
28+
}
2629
entry, _ := primitives.NewShaHashFromStr(e.Entry.Key)
2730
for i := range e.MerkleBranch {
2831
if entry.IsSameAs(e.MerkleBranch[i].Left) {
@@ -38,6 +41,9 @@ func (e *Receipt) TrimReceipt() {
3841
}
3942

4043
func (e *Receipt) Validate() error {
44+
if e == nil {
45+
return fmt.Errorf("No receipt provided")
46+
}
4147
if e.Entry == nil {
4248
return fmt.Errorf("Receipt has no entry")
4349
}
@@ -403,7 +409,7 @@ func VerifyFullReceipt(dbo interfaces.DBOverlay, receiptStr string) error {
403409
return err
404410
}
405411

406-
//fmt.Printf("receipt - %v\n", receipt.CustomMarshalString())
412+
fmt.Printf("receipt - %v\n", receipt.CustomMarshalString())
407413

408414
err = receipt.Validate()
409415
if err != nil {

receipts/receipts_test.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,33 @@ import (
1212

1313
func TestReceipts(t *testing.T) {
1414
dbo := CreateAndPopulateTestDatabaseOverlay()
15-
entry := CreateFirstTestEntry()
16-
receipt, err := CreateFullReceipt(dbo, entry.DatabasePrimaryIndex())
17-
if err != nil {
18-
t.Error(err)
19-
}
20-
t.Logf("\n\n%v\n", receipt.CustomMarshalString())
21-
22-
err = VerifyFullReceipt(dbo, receipt.CustomMarshalString())
23-
if err != nil {
24-
t.Error(err)
25-
}
26-
27-
receipt.TrimReceipt()
28-
t.Logf("\n\n%v\n", receipt.CustomMarshalString())
29-
30-
err = VerifyFullReceipt(dbo, receipt.CustomMarshalString())
31-
if err == nil {
32-
t.Errorf("\n\nError is nil when it shouldn't be for receipt\n\n%v\n\n", receipt)
33-
}
34-
35-
err = VerifyMinimalReceipt(dbo, receipt.CustomMarshalString())
36-
if err != nil {
37-
t.Error(err)
15+
blocks := CreateFullTestBlockSet()
16+
for _, block := range blocks[:len(blocks)-2] {
17+
for _, entry := range block.Entries {
18+
receipt, err := CreateFullReceipt(dbo, entry.DatabasePrimaryIndex())
19+
if err != nil {
20+
t.Error(err)
21+
}
22+
t.Logf("\n\n%v\n", receipt.CustomMarshalString())
23+
24+
err = VerifyFullReceipt(dbo, receipt.CustomMarshalString())
25+
if err != nil {
26+
t.Error(err)
27+
}
28+
29+
receipt.TrimReceipt()
30+
t.Logf("\n\n%v\n", receipt.CustomMarshalString())
31+
32+
err = VerifyFullReceipt(dbo, receipt.CustomMarshalString())
33+
if err == nil {
34+
t.Errorf("\n\nError is nil when it shouldn't be for receipt\n\n%v\n\n", receipt)
35+
}
36+
37+
err = VerifyMinimalReceipt(dbo, receipt.CustomMarshalString())
38+
if err != nil {
39+
t.Error(err)
40+
}
41+
}
3842
}
3943

4044
//t.Fail()

wsapi/wsapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func HandleGetReceipt(ctx *web.Context, hashkey string) {
267267
state := ctx.Server.Env["state"].(interfaces.IState)
268268

269269
param := HashRequest{Hash: hashkey}
270-
req := primitives.NewJSON2Request("get-receipt", 1, param)
270+
req := primitives.NewJSON2Request("receipt", 1, param)
271271

272272
jsonResp, jsonError := HandleV2Request(state, req)
273273
returnV1(ctx, jsonResp, jsonError)

wsapi/wsapiV2_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/FactomProject/factomd/common/interfaces"
1414
"github.com/FactomProject/factomd/common/primitives"
15+
"github.com/FactomProject/factomd/receipts"
1516
"github.com/FactomProject/factomd/testHelper"
1617
. "github.com/FactomProject/factomd/wsapi"
1718
)
@@ -209,3 +210,32 @@ func TestHandleV2CommitChain(t *testing.T) {
209210
t.Error("Error: TxID returned during Commit Chain is incorrect")
210211
}
211212
}
213+
214+
func TestHandleV2GetReceipt(t *testing.T) {
215+
state := testHelper.CreateAndPopulateTestState()
216+
//Start(state)
217+
218+
hashkey := new(HashRequest)
219+
hashkey.Hash = "8c35266c406e5a42fc3ca93f2d850b954bdfa79f49b2ceaf7f7086b691ffc022"
220+
221+
resp, jErr := HandleV2Receipt(state, hashkey)
222+
if jErr != nil {
223+
t.Errorf("%v", jErr)
224+
return
225+
}
226+
227+
dbo := state.GetAndLockDB()
228+
defer state.UnlockDB()
229+
230+
marshalled, err := json.Marshal(resp.(*ReceiptResponse).Receipt)
231+
if err != nil {
232+
t.Error(err)
233+
}
234+
t.Logf("Resp - %s", marshalled)
235+
236+
err = receipts.VerifyFullReceipt(dbo, string(marshalled))
237+
if err != nil {
238+
t.Logf("receipt - %s", marshalled)
239+
t.Error(err)
240+
}
241+
}

wsapi/wsapi_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func TestBlockIteration(t *testing.T) {
414414

415415
func TestHandleGetReceipt(t *testing.T) {
416416
context := testHelper.CreateWebContext()
417-
hash := "f6dc70879a5c5733e60b1d391915e39aa0fed79869e87b9a51473a54c9427ff5"
417+
hash := "8c35266c406e5a42fc3ca93f2d850b954bdfa79f49b2ceaf7f7086b691ffc022"
418418

419419
HandleGetReceipt(context, hash)
420420

@@ -428,7 +428,7 @@ func TestHandleGetReceipt(t *testing.T) {
428428
dbo := context.Server.Env["state"].(interfaces.IState).GetAndLockDB()
429429
defer context.Server.Env["state"].(interfaces.IState).UnlockDB()
430430

431-
receipt := j["Receipt"].(map[string]interface{})
431+
receipt := j["receipt"].(map[string]interface{})
432432
marshalled, err := json.Marshal(receipt)
433433
if err != nil {
434434
t.Error(err)

0 commit comments

Comments
 (0)