Skip to content

Commit 3099b4f

Browse files
authored
Delete dock and fix potential corruption state issue (#4581)
1 parent a04b48c commit 3099b4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+743
-877
lines changed

action/protocol/dock.go

-64
This file was deleted.

action/protocol/dock_test.go

-86
This file was deleted.

action/protocol/kvstorefortrie_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (imsm *inMemStateManager) States(...StateOption) (uint64, state.Iterator, e
4646
return 0, nil, nil
4747
}
4848

49-
func (imsm *inMemStateManager) ReadView(string) (interface{}, error) {
49+
func (imsm *inMemStateManager) ReadView(string) (View, error) {
5050
return nil, nil
5151
}
5252

@@ -88,7 +88,7 @@ func (imsm *inMemStateManager) DelState(opts ...StateOption) (uint64, error) {
8888
return 0, nil
8989
}
9090

91-
func (imsm *inMemStateManager) WriteView(string, interface{}) error {
91+
func (imsm *inMemStateManager) WriteView(string, View) error {
9292
return nil
9393
}
9494

action/protocol/managers.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type (
7070
Height() (uint64, error)
7171
State(interface{}, ...StateOption) (uint64, error)
7272
States(...StateOption) (uint64, state.Iterator, error)
73-
ReadView(string) (interface{}, error)
73+
ReadView(string) (View, error)
7474
}
7575

7676
// StateManager defines the stateDB interface atop IoTeX blockchain
@@ -82,17 +82,7 @@ type (
8282
// General state
8383
PutState(interface{}, ...StateOption) (uint64, error)
8484
DelState(...StateOption) (uint64, error)
85-
WriteView(string, interface{}) error
86-
Dock
87-
}
88-
89-
// Dock defines an interface for protocol to read/write their private data in StateReader/Manager
90-
// data are stored as interface{}, user needs to type-assert on their own upon Unload()
91-
Dock interface {
92-
ProtocolDirty(string) bool
93-
Load(string, string, interface{}) error
94-
Unload(string, string, interface{}) error
95-
Reset()
85+
WriteView(string, View) error
9686
}
9787
)
9888

action/protocol/mock_protocol.go

+81-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/protocol.go

+45-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
var (
2222
// ErrUnimplemented indicates a method is not implemented yet
2323
ErrUnimplemented = errors.New("method is unimplemented")
24+
// ErrNoName indicates the name is not found
25+
ErrNoName = errors.New("name is not found")
2426
)
2527

2628
const (
@@ -39,7 +41,7 @@ type Protocol interface {
3941

4042
// Starter starts the protocol
4143
type Starter interface {
42-
Start(context.Context, StateReader) (interface{}, error)
44+
Start(context.Context, StateReader) (View, error)
4345
}
4446

4547
// GenesisStateCreator creates some genesis states
@@ -100,22 +102,55 @@ func BlobGasFeeOption(blobGasFee *big.Int) DepositOption {
100102
}
101103
}
102104

103-
// DepositGas deposits gas to rewarding pool and burns baseFee
104-
type DepositGas func(context.Context, StateManager, *big.Int, ...DepositOption) ([]*action.TransactionLog, error)
105+
type (
106+
// DepositGas deposits gas to rewarding pool and burns baseFee
107+
DepositGas func(context.Context, StateManager, *big.Int, ...DepositOption) ([]*action.TransactionLog, error)
108+
109+
View interface {
110+
Clone() View
111+
Snapshot() int
112+
Revert(int) error
113+
Commit(context.Context, StateReader) error
114+
}
115+
116+
// Views stores the view for all protocols
117+
Views struct {
118+
vm map[string]View
119+
}
120+
)
105121

106-
// View stores the view for all protocols
107-
type View map[string]interface{}
122+
func NewViews() *Views {
123+
return &Views{
124+
vm: make(map[string]View),
125+
}
126+
}
108127

109-
func (view View) Read(name string) (interface{}, error) {
110-
if v, hit := view[name]; hit {
128+
func (views *Views) Clone() *Views {
129+
clone := NewViews()
130+
for key, view := range views.vm {
131+
clone.vm[key] = view.Clone()
132+
}
133+
return clone
134+
}
135+
136+
func (views *Views) Commit(ctx context.Context, sr StateReader) error {
137+
for _, view := range views.vm {
138+
if err := view.Commit(ctx, sr); err != nil {
139+
return err
140+
}
141+
}
142+
return nil
143+
}
144+
145+
func (views *Views) Read(name string) (View, error) {
146+
if v, hit := views.vm[name]; hit {
111147
return v, nil
112148
}
113149
return nil, ErrNoName
114150
}
115151

116-
func (view View) Write(name string, v interface{}) error {
117-
view[name] = v
118-
return nil
152+
func (views *Views) Write(name string, v View) {
153+
views.vm[name] = v
119154
}
120155

121156
// HashStringToAddress generates the contract address from the protocolID of each protocol

0 commit comments

Comments
 (0)