Skip to content

Commit 5218938

Browse files
bluesignpeterarguejanezpodhostnik
authored
Transaction logs API endpoint (#438)
Co-authored-by: Peter Argue <89119817+peterargue@users.noreply.github.com> Co-authored-by: Janez Podhostnik <67895329+janezpodhostnik@users.noreply.github.com>
1 parent c8bd062 commit 5218938

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

emulator/blockchain.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,3 +1641,12 @@ func (b *Blockchain) GetTransactionResultsByBlockID(blockID flowgo.Identifier) (
16411641
}
16421642
return results, nil
16431643
}
1644+
1645+
func (b *Blockchain) GetLogs(identifier flowgo.Identifier) ([]string, error) {
1646+
txResult, err := b.storage.TransactionResultByID(context.Background(), identifier)
1647+
if err != nil {
1648+
return nil, err
1649+
1650+
}
1651+
return txResult.Logs, nil
1652+
}

emulator/emulator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ type ExecutionCapable interface {
162162
CommitBlock() (*flowgo.Block, error)
163163
}
164164

165+
type LogProvider interface {
166+
GetLogs(flowgo.Identifier) ([]string, error)
167+
}
168+
165169
// Emulator defines the method set of an emulated emulator.
166170
type Emulator interface {
167171
ServiceKey() ServiceKey
@@ -174,4 +178,5 @@ type Emulator interface {
174178
RollbackCapable
175179
AutoMineCapable
176180
ExecutionCapable
181+
LogProvider
177182
}

emulator/mocks/emulator.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/utils/emulator.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ func NewEmulatorAPIServer(emulator emulator.Emulator, adapter *adapters.AccessAd
6161
router.HandleFunc("/emulator/snapshots", r.SnapshotList).Methods("GET")
6262
router.HandleFunc("/emulator/snapshots/{name}", r.SnapshotJump).Methods("PUT")
6363

64+
router.HandleFunc("/emulator/logs/{id}", r.Logs).Methods("GET")
65+
6466
router.HandleFunc("/emulator/storages/{address}", r.Storage)
6567

6668
router.HandleFunc("/emulator/config", r.Config)
@@ -271,3 +273,32 @@ func (m EmulatorAPIServer) ResetCodeCoverage(w http.ResponseWriter, r *http.Requ
271273
m.emulator.ResetCoverageReport()
272274
w.WriteHeader(http.StatusOK)
273275
}
276+
277+
func (m EmulatorAPIServer) Logs(w http.ResponseWriter, r *http.Request) {
278+
w.Header().Set("Content-Type", "application/json")
279+
vars := mux.Vars(r)
280+
id := vars["id"]
281+
282+
identifier, err := flowgo.HexStringToIdentifier(id)
283+
if err != nil {
284+
w.WriteHeader(http.StatusBadRequest)
285+
return
286+
}
287+
288+
logs, err := m.emulator.GetLogs(identifier)
289+
if err != nil {
290+
w.WriteHeader(http.StatusNotFound)
291+
return
292+
}
293+
294+
if len(logs) == 0 {
295+
err = json.NewEncoder(w).Encode([]string{})
296+
} else {
297+
err = json.NewEncoder(w).Encode(logs)
298+
}
299+
300+
if err != nil {
301+
w.WriteHeader(http.StatusInternalServerError)
302+
return
303+
}
304+
}

0 commit comments

Comments
 (0)