@@ -40,7 +40,11 @@ func (s *Service) UpdateAndSaveHeadWithBalances(ctx context.Context) error {
40
40
if err != nil {
41
41
return errors .Wrap (err , "could not update head" )
42
42
}
43
- return s .saveHead (ctx , headRoot )
43
+ headBlock , err := s .cfg .BeaconDB .Block (ctx , headRoot )
44
+ if err != nil {
45
+ return err
46
+ }
47
+ return s .saveHead (ctx , headRoot , headBlock )
44
48
}
45
49
46
50
// This defines the current chain service's view of head.
@@ -97,7 +101,7 @@ func (s *Service) updateHead(ctx context.Context, balances []uint64) ([32]byte,
97
101
98
102
// This saves head info to the local service cache, it also saves the
99
103
// new head root to the DB.
100
- func (s * Service ) saveHead (ctx context.Context , headRoot [32 ]byte ) error {
104
+ func (s * Service ) saveHead (ctx context.Context , headRoot [32 ]byte , headBlock block. SignedBeaconBlock ) error {
101
105
ctx , span := trace .StartSpan (ctx , "blockChain.saveHead" )
102
106
defer span .End ()
103
107
@@ -109,22 +113,16 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
109
113
if headRoot == bytesutil .ToBytes32 (r ) {
110
114
return nil
111
115
}
116
+ if err := helpers .BeaconBlockIsNil (headBlock ); err != nil {
117
+ return err
118
+ }
112
119
113
120
// If the head state is not available, just return nil.
114
121
// There's nothing to cache
115
122
if ! s .cfg .BeaconDB .HasStateSummary (ctx , headRoot ) {
116
123
return nil
117
124
}
118
125
119
- // Get the new head block from DB.
120
- newHeadBlock , err := s .cfg .BeaconDB .Block (ctx , headRoot )
121
- if err != nil {
122
- return err
123
- }
124
- if err := helpers .BeaconBlockIsNil (newHeadBlock ); err != nil {
125
- return err
126
- }
127
-
128
126
// Get the new head state from cached state or DB.
129
127
newHeadState , err := s .cfg .StateGen .StateByRoot (ctx , headRoot )
130
128
if err != nil {
@@ -136,11 +134,11 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
136
134
137
135
// A chain re-org occurred, so we fire an event notifying the rest of the services.
138
136
headSlot := s .HeadSlot ()
139
- newHeadSlot := newHeadBlock .Block ().Slot ()
137
+ newHeadSlot := headBlock .Block ().Slot ()
140
138
oldHeadRoot := s .headRoot ()
141
139
oldStateRoot := s .headBlock ().Block ().StateRoot ()
142
- newStateRoot := newHeadBlock .Block ().StateRoot ()
143
- if bytesutil .ToBytes32 (newHeadBlock .Block ().ParentRoot ()) != bytesutil .ToBytes32 (r ) {
140
+ newStateRoot := headBlock .Block ().StateRoot ()
141
+ if bytesutil .ToBytes32 (headBlock .Block ().ParentRoot ()) != bytesutil .ToBytes32 (r ) {
144
142
log .WithFields (logrus.Fields {
145
143
"newSlot" : fmt .Sprintf ("%d" , newHeadSlot ),
146
144
"oldSlot" : fmt .Sprintf ("%d" , headSlot ),
@@ -172,7 +170,7 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
172
170
}
173
171
174
172
// Cache the new head info.
175
- s .setHead (headRoot , newHeadBlock , newHeadState )
173
+ s .setHead (headRoot , headBlock , newHeadState )
176
174
177
175
// Save the new head root to DB.
178
176
if err := s .cfg .BeaconDB .SaveHeadBlockRoot (ctx , headRoot ); err != nil {
0 commit comments