Skip to content

Commit e1fb50d

Browse files
author
Jiri Malek
committed
Compress transaction data in memory.
1 parent 91460a6 commit e1fb50d

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/graph-gophers/graphql-transport-ws v0.0.0-20200904065757-c681d7e1b135
2020
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
2121
github.com/karalabe/usb v0.0.0-20210518091819-4ea20957c210 // indirect
22-
github.com/klauspost/compress v1.11.12 // indirect
22+
github.com/klauspost/compress v1.13.0 // indirect
2323
github.com/kr/text v0.2.0 // indirect
2424
github.com/magiconair/properties v1.8.4 // indirect
2525
github.com/mattn/go-runewidth v0.0.10 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
375375
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
376376
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
377377
github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
378-
github.com/klauspost/compress v1.11.12 h1:famVnQVu7QwryBN4jNseQdUKES71ZAOnB6UQQJPZvqk=
379-
github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
378+
github.com/klauspost/compress v1.13.0 h1:2T7tUoQrQT+fQWdaY5rjWztFGAFwbGD04iPJg90ZiOs=
379+
github.com/klauspost/compress v1.13.0/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
380380
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
381381
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
382382
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=

internal/repository/cache/transaction.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cache
44
import (
55
"fantom-api-graphql/internal/types"
66
"github.com/ethereum/go-ethereum/common"
7+
"github.com/klauspost/compress/s2"
78
)
89

910
// PullTransaction extracts transaction information from the in-memory cache if available.
@@ -15,6 +16,12 @@ func (b *MemBridge) PullTransaction(hash *common.Hash) *types.Transaction {
1516
return nil
1617
}
1718

19+
// decode compressed transaction data from Snappy S2
20+
data, err = s2.Decode(nil, data)
21+
if err != nil {
22+
return nil
23+
}
24+
1825
// do we have the data?
1926
trx := new(types.Transaction)
2027
if err := trx.UnmarshalBSON(data); err != nil {
@@ -39,8 +46,15 @@ func (b *MemBridge) PushTransaction(trx *types.Transaction) {
3946
return
4047
}
4148

49+
// recover from s2 encoder panic
50+
defer func() {
51+
if r := recover(); r != nil {
52+
b.log.Criticalf("can not encode transaction")
53+
}
54+
}()
55+
4256
// set the data to cache by block number
43-
if err := b.cache.Set(trx.Hash.String(), data); err != nil {
57+
if err := b.cache.Set(trx.Hash.String(), s2.Encode(nil, data)); err != nil {
4458
b.log.Criticalf("can not cache transaction %s; %s", trx.Hash.String(), err.Error())
4559
}
4660
}

0 commit comments

Comments
 (0)