Fix data race in block
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"io"
+ "sync"
"github.com/icon-project/goloop/chain/base"
"github.com/icon-project/goloop/common/codec"
@@ -60,6 +61,9 @@ type blockV2 struct {
nextValidatorsHash []byte
_nextValidators module.ValidatorList
votes module.CommitVoteSet
+
+ // mutable data
+ mu sync.Mutex
_id []byte
}
@@ -68,6 +72,9 @@ func (b *blockV2) Version() int {
}
func (b *blockV2) ID() []byte {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
if b._id == nil {
bs := v2Codec.MustMarshalToBytes(b._headerFormat())
b._id = crypto.SHA3Sum256(bs)
