aboutsummaryrefslogtreecommitdiff
path: root/core/state_backend.go
diff options
context:
space:
mode:
authorsangtran <tranquangsang12.7@gmail.com>2026-09-05 17:06:30 +0700
committersangtran <tranquangsang12.7@gmail.com>2026-09-05 17:06:30 +0700
commite626cee8f7de0dd3ffca4c7297e59a87f790cc02 (patch)
tree1b57f5e3134459872890e3df7484be5cf3cb3ba6 /core/state_backend.go
parent67be8b5e2b5188d4291ed0a3c3a91b5e40bfbebe (diff)
signatureVerified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust
downloadflux-e626cee8f7de0dd3ffca4c7297e59a87f790cc02.tar.gz
flux-e626cee8f7de0dd3ffca4c7297e59a87f790cc02.zip
Verified feat: add on pebble state backend and using sync Pool to reuse buffer []byte
Diffstat (limited to 'core/state_backend.go')
-rw-r--r--core/state_backend.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/core/state_backend.go b/core/state_backend.go
index 5870739..ae9fe1d 100644
--- a/core/state_backend.go
+++ b/core/state_backend.go
@@ -2,6 +2,9 @@ package core
2 2
3import ( 3import (
4 "context" 4 "context"
5 "encoding/json"
6 "fmt"
7
5) 8)
6 9
7type StateBackend interface { 10type StateBackend interface {
@@ -22,6 +25,7 @@ type stateBackendKeyType struct{}
22 25
23var stateBackendKey stateBackendKeyType 26var stateBackendKey stateBackendKeyType
24 27
28// type adapter
25type typedValueState[T any] struct { 29type typedValueState[T any] struct {
26 inner ValueState[any] 30 inner ValueState[any]
27} 31}
@@ -32,14 +36,24 @@ func CastValueType[T any](inner ValueState[any]) ValueState[T] {
32 36
33func (s *typedValueState[T]) Value(ctx context.Context) (T, error) { 37func (s *typedValueState[T]) Value(ctx context.Context) (T, error) {
34 val, err := s.inner.Value(ctx) 38 val, err := s.inner.Value(ctx)
39 var zero T
35 if err != nil { 40 if err != nil {
36 var zero T
37 return zero, err 41 return zero, err
38 } 42 }
39 if val == nil { 43 if val == nil {
40 var zero T
41 return zero, nil 44 return zero, nil
42 } 45 }
46
47 if rawBytes, ok := val.([]byte); ok {
48 defer PutBuffer(rawBytes)
49
50 var result T
51 if err := json.Unmarshal(rawBytes, &result); err != nil {
52 return zero, fmt.Errorf("failed to unmarshal state: %w", err)
53 }
54 return result, nil
55 }
56
43 return val.(T), nil 57 return val.(T), nil
44} 58}
45 59