@@ -3,22 +3,43 @@ package db
33import (
44 "encoding/binary"
55 "errors"
6+ "fmt"
67 "reflect"
78
89 "github.com/NilFoundation/nil/nil/common"
910 "github.com/NilFoundation/nil/nil/internal/serialization"
1011 "github.com/NilFoundation/nil/nil/internal/types"
1112)
1213
13- // todo: return errors
14+ type prettyKey interface {
15+ fmt.Stringer
16+ Bytes () []byte
17+ }
18+
19+ func Get (tx RoTx , table TableName , key prettyKey ) ([]byte , error ) {
20+ data , err := tx .Get (table , key .Bytes ())
21+ if err != nil {
22+ return nil , fmt .Errorf ("%w: key=%s" , err , key )
23+ }
24+ return data , nil
25+ }
26+
27+ func GetFromShard (tx RoTx , shardId types.ShardId , table ShardedTableName , key prettyKey ) ([]byte , error ) {
28+ data , err := tx .GetFromShard (shardId , table , key .Bytes ())
29+ if err != nil {
30+ return nil , fmt .Errorf ("%w: key=%s" , err , key )
31+ }
32+ return data , nil
33+ }
34+
1435func readDecodable [
1536 T interface {
1637 ~ * S
1738 serialization.NilUnmarshaler
1839 },
1940 S any ,
2041](tx RoTx , table ShardedTableName , shardId types.ShardId , hash common.Hash ) (* S , error ) {
21- data , err := tx . GetFromShard (shardId , table , hash . Bytes () )
42+ data , err := GetFromShard (tx , shardId , table , hash )
2243 if err != nil {
2344 return nil , err
2445 }
@@ -30,25 +51,15 @@ func readDecodable[
3051 return decoded , nil
3152}
3253
33- func writeRawKeyEncodable [
34- T interface {
35- serialization.NilMarshaler
36- },
37- ](tx RwTx , tableName ShardedTableName , shardId types.ShardId , key []byte , value T ) error {
38- data , err := value .MarshalNil ()
54+ func writeEncodable [T serialization.NilMarshaler ](
55+ tx RwTx , tableName ShardedTableName , shardId types.ShardId , hash common.Hash , obj T ,
56+ ) error {
57+ data , err := obj .MarshalNil ()
3958 if err != nil {
4059 return err
4160 }
4261
43- return tx .PutToShard (shardId , tableName , key , data )
44- }
45-
46- func writeEncodable [
47- T interface {
48- serialization.NilMarshaler
49- },
50- ](tx RwTx , tableName ShardedTableName , shardId types.ShardId , hash common.Hash , obj T ) error {
51- return writeRawKeyEncodable (tx , tableName , shardId , hash .Bytes (), obj )
62+ return tx .PutToShard (shardId , tableName , hash .Bytes (), data )
5263}
5364
5465func ReadVersionInfo (tx RoTx ) (* types.VersionInfo , error ) {
@@ -88,7 +99,7 @@ func ReadBlock(tx RoTx, shardId types.ShardId, hash common.Hash) (*types.Block,
8899}
89100
90101func ReadBlockBytes (tx RoTx , shardId types.ShardId , hash common.Hash ) ([]byte , error ) {
91- return tx . GetFromShard (shardId , blockTable , hash . Bytes () )
102+ return GetFromShard (tx , shardId , blockTable , hash )
92103}
93104
94105func ReadLastBlock (tx RoTx , shardId types.ShardId ) (* types.Block , common.Hash , error ) {
@@ -105,7 +116,7 @@ func ReadLastBlock(tx RoTx, shardId types.ShardId) (*types.Block, common.Hash, e
105116
106117func ReadCollatorState (tx RoTx , shardId types.ShardId ) (types.CollatorState , error ) {
107118 res := types.CollatorState {}
108- buf , err := tx . Get (collatorStateTable , shardId . Bytes () )
119+ buf , err := Get (tx , collatorStateTable , shardId )
109120 if err != nil {
110121 return res , err
111122 }
@@ -125,7 +136,7 @@ func WriteCollatorState(tx RwTx, shardId types.ShardId, state types.CollatorStat
125136}
126137
127138func ReadLastBlockHash (tx RoTx , shardId types.ShardId ) (common.Hash , error ) {
128- h , err := tx . Get (LastBlockTable , shardId . Bytes () )
139+ h , err := Get (tx , LastBlockTable , shardId )
129140 return common .BytesToHash (h ), err
130141}
131142
@@ -140,7 +151,7 @@ func WriteBlockTimestamp(tx RwTx, shardId types.ShardId, blockHash common.Hash,
140151}
141152
142153func ReadBlockTimestamp (tx RoTx , shardId types.ShardId , blockHash common.Hash ) (uint64 , error ) {
143- value , err := tx . GetFromShard (shardId , blockTimestampTable , blockHash . Bytes () )
154+ value , err := GetFromShard (tx , shardId , blockTimestampTable , blockHash )
144155 if err != nil {
145156 return 0 , err
146157 }
@@ -156,7 +167,7 @@ func WriteError(tx RwTx, txnHash common.Hash, errMsg string) error {
156167}
157168
158169func ReadError (tx RoTx , txnHash common.Hash ) (string , error ) {
159- res , err := tx . Get (errorByTransactionHashTable , txnHash . Bytes () )
170+ res , err := Get (tx , errorByTransactionHashTable , txnHash )
160171 if err != nil {
161172 return "" , err
162173 }
@@ -168,14 +179,14 @@ func WriteCode(tx RwTx, shardId types.ShardId, hash common.Hash, code types.Code
168179}
169180
170181func ReadCode (tx RoTx , shardId types.ShardId , hash common.Hash ) (types.Code , error ) {
171- if hash == types . EmptyCodeHash {
182+ if hash . Empty () {
172183 return types.Code {}, nil
173184 }
174- return tx . GetFromShard (shardId , codeTable , hash . Bytes () )
185+ return GetFromShard (tx , shardId , codeTable , hash )
175186}
176187
177188func ReadBlockHashByNumber (tx RoTx , shardId types.ShardId , blockNumber types.BlockNumber ) (common.Hash , error ) {
178- blockHash , err := tx . GetFromShard (shardId , BlockHashByNumberIndex , blockNumber . Bytes () )
189+ blockHash , err := GetFromShard (tx , shardId , BlockHashByNumberIndex , blockNumber )
179190 return common .BytesToHash (blockHash ), err
180191}
181192
0 commit comments