Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: SIKFromAddress data race #1199

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions vochain/state/sik.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ type SIK []byte
// SIKFromAddress function return the current SIK value associated to the provided
// address.
func (v *State) SIKFromAddress(address common.Address) (SIK, error) {
sik, err := v.mainTreeViewer(false).DeepGet(address.Bytes(), StateTreeCfg(TreeSIK))
siksTree, err := v.tx.DeepSubTree(StateTreeCfg(TreeSIK))
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrSIKSubTree, err)
}
v.tx.Lock()
defer v.tx.Unlock()

sik, err := siksTree.Get(address.Bytes())
if err != nil {
if errors.Is(err, arbo.ErrKeyNotFound) {
return nil, fmt.Errorf("%w: %w", ErrSIKNotFound, err)
Expand Down Expand Up @@ -110,8 +117,14 @@ func (v *State) SetAddressSIK(address common.Address, newSIK SIK) error {
// prevent it from being updated until all processes created before that height
// have finished.
func (v *State) InvalidateSIK(address common.Address) error {
siksTree, err := v.tx.DeepSubTree(StateTreeCfg(TreeSIK))
if err != nil {
return fmt.Errorf("%w: %w", ErrSIKSubTree, err)
}
// if the sik does not exists or something fails querying return the error
rawSIK, err := v.mainTreeViewer(false).DeepGet(address.Bytes(), StateTreeCfg(TreeSIK))
v.tx.Lock()
p4u marked this conversation as resolved.
Show resolved Hide resolved
rawSIK, err := siksTree.Get(address.Bytes())
v.tx.Unlock()
if err != nil {
return fmt.Errorf("%w: %w", ErrSIKGet, err)
}
Expand All @@ -121,7 +134,7 @@ func (v *State) InvalidateSIK(address common.Address) error {
}
v.tx.Lock()
invalidatedSIK := make(SIK, sikLeafValueLen).InvalidateAt(v.CurrentHeight())
err = v.tx.DeepSet(address.Bytes(), invalidatedSIK, StateTreeCfg(TreeSIK))
err = siksTree.Set(address.Bytes(), invalidatedSIK)
v.tx.Unlock()
if err != nil {
return fmt.Errorf("%w: %w", ErrSIKDelete, err)
Expand Down
50 changes: 50 additions & 0 deletions vochain/state/sik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package state
import (
"bytes"
"encoding/hex"
"errors"
"sync"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -205,3 +207,51 @@ func Test_validSIK(t *testing.T) {
input, _ = hex.DecodeString("F3668000B66c61aAa08aBC559a8C78Ae7E007C2e")
qt.Assert(t, SIK(input).Valid(), qt.IsTrue)
}

func TestSIKDataRace(t *testing.T) {
c := qt.New(t)
// create a state for testing
dir := t.TempDir()
s, err := NewState(db.TypePebble, dir)
qt.Assert(t, err, qt.IsNil)

// create some siks
addrs := []common.Address{}
siks := map[common.Address]SIK{}
for i := 0; i < 10000; i++ {
s := ethereum.NewSignKeys()
c.Assert(s.Generate(), qt.IsNil)
sik, err := s.AccountSIK(nil)
c.Assert(err, qt.IsNil)
addrs = append(addrs, s.Address())
siks[s.Address()] = sik
}

wg := &sync.WaitGroup{}
iterations := len(addrs) * 10

wg.Add(2)
go func() {
defer wg.Done()

for i := 0; i < iterations; i++ {
idx := util.RandomInt(0, len(addrs)-1)
addr := addrs[idx]
if err := s.SetAddressSIK(addr, siks[addr]); err != nil {
c.Assert(errors.Is(err, ErrRegisteredValidSIK), qt.IsTrue)
}
}
}()
go func() {
defer wg.Done()

idx := util.RandomInt(0, len(addrs)-1)
addr := addrs[idx]
if _, err := s.SIKFromAddress(addr); err != nil {
c.Assert(errors.Is(err, ErrSIKNotFound), qt.IsTrue)
return
}
c.Assert(s.InvalidateSIK(addr), qt.IsNil)
}()
wg.Wait()
}