MCPcopy Create free account
hub / github.com/CPChain/chain / TestWritePrivateReceipt

Function TestWritePrivateReceipt

core/private_database_test.go:110–169  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

108}
109
110func TestWritePrivateReceipt(t *testing.T) {
111 type args struct {
112 receipt *types.Receipt
113 txHash common.Hash
114 db database.Database
115 }
116
117 db := getTestDB()
118 receipt := getTestReceipt()
119
120 tests := []struct {
121 name string
122 args args
123 wantErr bool
124 check func() error
125 }{
126 {
127 name: "Write a normal receipt to normal database",
128 args: args{
129 receipt: receipt,
130 txHash: common.Hash{},
131 db: db,
132 },
133 wantErr: false,
134 check: func() error {
135 keys := db.Keys()
136 if len(keys) == 0 {
137 return errors.New("No data is written into db.")
138 }
139
140 contentToHash := bytes.Join([][]byte{
141 privateReceiptPrefix,
142 common.Hash{}.Bytes(),
143 }, []byte{})
144 hasher := sha3.NewKeccak256()
145 hasher.Write(contentToHash)
146 hashBytes := hasher.Sum(nil)
147 hash := common.BytesToHash(hashBytes)
148
149 content, err := db.Get(hash.Bytes())
150 if err != nil || len(content) == 0 {
151 return errors.New("The data written has wrong hash and content.")
152 }
153 return nil
154 },
155 },
156 }
157 for _, tt := range tests {
158 t.Run(tt.name, func(t *testing.T) {
159 if err := WritePrivateReceipt(tt.args.receipt, tt.args.txHash, tt.args.db); (err != nil) != tt.wantErr {
160 t.Errorf("WritePrivateReceipt() error = %v, wantErr %v", err, tt.wantErr)
161 }
162 if tt.check != nil {
163 if err := tt.check(); err != nil {
164 t.Errorf("Checking the result fails for test case <%s>, error is %s", tt.name, err.Error())
165 }
166 }
167 })

Callers

nothing calls this directly

Calls 9

getTestDBFunction · 0.85
getTestReceiptFunction · 0.85
WritePrivateReceiptFunction · 0.85
BytesMethod · 0.65
WriteMethod · 0.65
GetMethod · 0.65
RunMethod · 0.65
ErrorMethod · 0.65
KeysMethod · 0.45

Tested by

no test coverage detected