@@ -9,6 +9,7 @@ package blockchain
9
9
import (
10
10
"context"
11
11
12
+ "github.com/boltdb/bolt"
12
13
"github.com/pkg/errors"
13
14
14
15
"github.com/iotexproject/iotex-core/action"
@@ -86,33 +87,44 @@ func (dao *blockDAO) Start(ctx context.Context) error {
86
87
}
87
88
88
89
// set init height value
89
- if err := dao .kvstore .PutIfNotExists (blockNS , topHeightKey , make ([]byte , 8 )); err != nil {
90
- // ok on none-fresh db
91
- if err == db .ErrAlreadyExist {
92
- return nil
90
+ // TODO: not working with badger, we shouldn't expose detailed db error (e.g., bolt.ErrBucketExists) to application
91
+ if _ , err = dao .kvstore .Get (blockNS , topHeightKey ); err != nil &&
92
+ (errors .Cause (err ) == db .ErrNotExist || errors .Cause (err ) == bolt .ErrBucketNotFound ) {
93
+ if err := dao .kvstore .Put (blockNS , topHeightKey , make ([]byte , 8 )); err != nil {
94
+ return errors .Wrap (err , "failed to write initial value for top height" )
93
95
}
94
-
95
- return errors .Wrap (err , "failed to write initial value for top height" )
96
96
}
97
97
98
98
// set init total transfer to be 0
99
- if err = dao .kvstore .PutIfNotExists (blockNS , totalTransfersKey , make ([]byte , 8 )); err != nil {
100
- return errors .Wrap (err , "failed to write initial value for total transfers" )
99
+ if _ , err := dao .kvstore .Get (blockNS , totalTransfersKey ); err != nil &&
100
+ (errors .Cause (err ) == db .ErrNotExist || errors .Cause (err ) == bolt .ErrBucketNotFound ) {
101
+ if err = dao .kvstore .Put (blockNS , totalTransfersKey , make ([]byte , 8 )); err != nil {
102
+ return errors .Wrap (err , "failed to write initial value for total transfers" )
103
+ }
101
104
}
102
105
103
106
// set init total vote to be 0
104
- if err = dao .kvstore .PutIfNotExists (blockNS , totalVotesKey , make ([]byte , 8 )); err != nil {
105
- return errors .Wrap (err , "failed to write initial value for total votes" )
107
+ if _ , err := dao .kvstore .Get (blockNS , totalVotesKey ); err != nil &&
108
+ (errors .Cause (err ) == db .ErrNotExist || errors .Cause (err ) == bolt .ErrBucketNotFound ) {
109
+ if err = dao .kvstore .Put (blockNS , totalVotesKey , make ([]byte , 8 )); err != nil {
110
+ return errors .Wrap (err , "failed to write initial value for total votes" )
111
+ }
106
112
}
107
113
108
114
// set init total executions to be 0
109
- if err = dao .kvstore .PutIfNotExists (blockNS , totalExecutionsKey , make ([]byte , 8 )); err != nil {
110
- return errors .Wrap (err , "failed to write initial value for total executions" )
115
+ if _ , err := dao .kvstore .Get (blockNS , totalExecutionsKey ); err != nil &&
116
+ (errors .Cause (err ) == db .ErrNotExist || errors .Cause (err ) == bolt .ErrBucketNotFound ) {
117
+ if err = dao .kvstore .Put (blockNS , totalExecutionsKey , make ([]byte , 8 )); err != nil {
118
+ return errors .Wrap (err , "failed to write initial value for total executions" )
119
+ }
111
120
}
112
121
113
122
// set init total actions to be 0
114
- if err = dao .kvstore .PutIfNotExists (blockNS , totalActionsKey , make ([]byte , 8 )); err != nil {
115
- return errors .Wrap (err , "failed to write initial value for total actions" )
123
+ if _ , err := dao .kvstore .Get (blockNS , totalActionsKey ); err != nil &&
124
+ (errors .Cause (err ) == db .ErrNotExist || errors .Cause (err ) == bolt .ErrBucketNotFound ) {
125
+ if err = dao .kvstore .Put (blockNS , totalActionsKey , make ([]byte , 8 )); err != nil {
126
+ return errors .Wrap (err , "failed to write initial value for total actions" )
127
+ }
116
128
}
117
129
118
130
return nil
@@ -633,7 +645,7 @@ func (dao *blockDAO) putBlock(blk *Block) error {
633
645
return errors .Wrap (err , "failed to serialize block" )
634
646
}
635
647
hash := blk .HashBlock ()
636
- batch .PutIfNotExists (blockNS , hash [:], serialized , "failed to put block" )
648
+ batch .Put (blockNS , hash [:], serialized , "failed to put block" )
637
649
638
650
hashKey := append (hashPrefix , hash [:]... )
639
651
batch .Put (blockHashHeightMappingNS , hashKey , height , "failed to put hash -> height mapping" )
@@ -770,7 +782,7 @@ func putTransfers(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
770
782
// put new transfer to sender
771
783
senderKey := append (transferFromPrefix , transfer .Sender ()... )
772
784
senderKey = append (senderKey , byteutil .Uint64ToBytes (senderTransferCount )... )
773
- batch .PutIfNotExists (blockAddressTransferMappingNS , senderKey , transferHash [:],
785
+ batch .Put (blockAddressTransferMappingNS , senderKey , transferHash [:],
774
786
"failed to put transfer hash %x for sender %x" , transfer .Hash (), transfer .Sender ())
775
787
776
788
// update sender transfers count
@@ -795,7 +807,7 @@ func putTransfers(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
795
807
recipientKey := append (transferToPrefix , transfer .Recipient ()... )
796
808
recipientKey = append (recipientKey , byteutil .Uint64ToBytes (recipientTransferCount )... )
797
809
798
- batch .PutIfNotExists (blockAddressTransferMappingNS , recipientKey , transferHash [:],
810
+ batch .Put (blockAddressTransferMappingNS , recipientKey , transferHash [:],
799
811
"failed to put transfer hash %x for recipient %x" , transfer .Hash (), transfer .Recipient ())
800
812
801
813
// update recipient transfers count
@@ -834,7 +846,7 @@ func putVotes(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
834
846
// put new vote to sender
835
847
senderKey := append (voteFromPrefix , Sender ... )
836
848
senderKey = append (senderKey , byteutil .Uint64ToBytes (senderVoteCount )... )
837
- batch .PutIfNotExists (blockAddressVoteMappingNS , senderKey , voteHash [:],
849
+ batch .Put (blockAddressVoteMappingNS , senderKey , voteHash [:],
838
850
"failed to put vote hash %x for sender %x" , voteHash , Sender )
839
851
840
852
// update sender votes count
@@ -858,7 +870,7 @@ func putVotes(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
858
870
// put new vote to recipient
859
871
recipientKey := append (voteToPrefix , Recipient ... )
860
872
recipientKey = append (recipientKey , byteutil .Uint64ToBytes (recipientVoteCount )... )
861
- batch .PutIfNotExists (blockAddressVoteMappingNS , recipientKey , voteHash [:],
873
+ batch .Put (blockAddressVoteMappingNS , recipientKey , voteHash [:],
862
874
"failed to put vote hash %x for recipient %x" , voteHash , Recipient )
863
875
864
876
// update recipient votes count
@@ -895,7 +907,7 @@ func putExecutions(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
895
907
// put new execution to executor
896
908
executorKey := append (executionFromPrefix , execution .Executor ()... )
897
909
executorKey = append (executorKey , byteutil .Uint64ToBytes (executorExecutionCount )... )
898
- batch .PutIfNotExists (blockAddressExecutionMappingNS , executorKey , executionHash [:],
910
+ batch .Put (blockAddressExecutionMappingNS , executorKey , executionHash [:],
899
911
"failed to put execution hash %x for executor %x" , execution .Hash (), execution .Executor ())
900
912
901
913
// update executor executions count
@@ -919,7 +931,7 @@ func putExecutions(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
919
931
// put new execution to contract
920
932
contractKey := append (executionToPrefix , execution .Contract ()... )
921
933
contractKey = append (contractKey , byteutil .Uint64ToBytes (contractExecutionCount )... )
922
- batch .PutIfNotExists (blockAddressExecutionMappingNS , contractKey , executionHash [:],
934
+ batch .Put (blockAddressExecutionMappingNS , contractKey , executionHash [:],
923
935
"failed to put execution hash %x for contract %x" , execution .Hash (), execution .Contract ())
924
936
925
937
// update contract executions count
@@ -962,7 +974,7 @@ func putActions(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
962
974
// put new action to sender
963
975
senderKey := append (actionFromPrefix , act .SrcAddr ()... )
964
976
senderKey = append (senderKey , byteutil .Uint64ToBytes (senderActionCount )... )
965
- batch .PutIfNotExists (blockAddressActionMappingNS , senderKey , actHash [:],
977
+ batch .Put (blockAddressActionMappingNS , senderKey , actHash [:],
966
978
"failed to put action hash %x for sender %s" , actHash , act .SrcAddr ())
967
979
968
980
// update sender action count
@@ -986,7 +998,7 @@ func putActions(dao *blockDAO, blk *Block, batch db.KVStoreBatch) error {
986
998
// put new action to recipient
987
999
recipientKey := append (actionToPrefix , act .DstAddr ()... )
988
1000
recipientKey = append (recipientKey , byteutil .Uint64ToBytes (recipientActionCount )... )
989
- batch .PutIfNotExists (blockAddressActionMappingNS , recipientKey , actHash [:],
1001
+ batch .Put (blockAddressActionMappingNS , recipientKey , actHash [:],
990
1002
"failed to put action hash %x for recipient %s" , actHash , act .DstAddr ())
991
1003
992
1004
// update recipient action count
0 commit comments