Skip to content

Commit

Permalink
Add tests for state package
Browse files Browse the repository at this point in the history
  • Loading branch information
MakisChristou committed Dec 14, 2023
1 parent 138d4f6 commit 7506cca
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
43 changes: 41 additions & 2 deletions state/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package state

import (
"errors"
"math/big"
"testing"

Expand All @@ -17,9 +18,7 @@ import (

func TestStage(t *testing.T) {
db := muxdb.NewMem()

state := New(db, thor.Bytes32{}, 0, 0, 0)

addr := thor.BytesToAddress([]byte("acc1"))

balance := big.NewInt(10)
Expand Down Expand Up @@ -56,3 +55,43 @@ func TestStage(t *testing.T) {
assert.Equal(t, M(v, nil), M(state.GetStorage(addr, k)))
}
}

func TestStageCommitError(t *testing.T) {
db := muxdb.NewMem()
state := New(db, thor.Bytes32{}, 0, 0, 0)

// Set up the state with an account, balance, code, and storage.
addr := thor.BytesToAddress([]byte("acc1"))
balance := big.NewInt(10)
code := []byte{1, 2, 3}
storage := map[thor.Bytes32]thor.Bytes32{
thor.BytesToBytes32([]byte("s1")): thor.BytesToBytes32([]byte("v1")),
thor.BytesToBytes32([]byte("s2")): thor.BytesToBytes32([]byte("v2")),
thor.BytesToBytes32([]byte("s3")): thor.BytesToBytes32([]byte("v3")),
}

state.SetBalance(addr, balance)
state.SetCode(addr, code)
for k, v := range storage {
state.SetStorage(addr, k, v)
}

// Prepare the stage with the current state.
stage, err := state.Stage(1, 0)
assert.Nil(t, err, "Stage should not return an error")

// Mock a commit function to simulate an error.
commitFuncWithError := func() error {
return errors.New("commit error")
}

// Include the error-producing commit function in the stage's commits.
stage.commits = append(stage.commits, commitFuncWithError)

// Attempt to commit changes.
_, err = stage.Commit()

// Assert that an error is returned.
assert.NotNil(t, err, "Commit should return an error")
assert.EqualError(t, err, "state: commit error", "The error message should match the mock error")
}
57 changes: 57 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,63 @@ func TestEnergy(t *testing.T) {
assert.Equal(t, x, bal1)
}

func TestEncodeDecodeStorage(t *testing.T) {
db := muxdb.NewMem()
state := New(db, thor.Bytes32{}, 0, 0, 0)

// Create an account and key
addr := thor.BytesToAddress([]byte("account1"))
key := thor.BytesToBytes32([]byte("key"))

// Original value to be encoded and then decoded
originalValue := []byte("value")

// Encoding function
encodingFunc := func() ([]byte, error) {
return rlp.EncodeToBytes(originalValue)
}

// Encode and store the value
err := state.EncodeStorage(addr, key, encodingFunc)
assert.Nil(t, err, "EncodeStorage should not return an error")

// Function to decode the storage value
var decodedValue []byte
decodeFunc := func(b []byte) error {
var err error
err = rlp.DecodeBytes(b, &decodedValue)
return err
}

// Decode the stored value
err = state.DecodeStorage(addr, key, decodeFunc)
assert.Nil(t, err, "DecodeStorage should not return an error")

// Verify that the decoded value matches the original value
assert.Equal(t, originalValue, decodedValue, "decoded value should match the original value")
}

func TestBuildStorageTrie(t *testing.T) {
db := muxdb.NewMem()
state := New(db, thor.Bytes32{}, 0, 0, 0)

// Create an account and set storage values
addr := thor.BytesToAddress([]byte("account1"))
key1 := thor.BytesToBytes32([]byte("key1"))
value1 := thor.BytesToBytes32([]byte("value1"))
key2 := thor.BytesToBytes32([]byte("key2"))
value2 := thor.BytesToBytes32([]byte("value2"))

state.SetRawStorage(addr, key1, value1[:])
state.SetRawStorage(addr, key2, value2[:])

assert.Equal(t, M(rlp.RawValue(value1[:]), nil), M(state.GetRawStorage(addr, key1)))

// Build the storage trie
_, err := state.BuildStorageTrie(addr)
assert.Nil(t, err, "error should be nil")
}

func TestStorage(t *testing.T) {
db := muxdb.NewMem()
st := New(db, thor.Bytes32{}, 0, 0, 0)
Expand Down
25 changes: 25 additions & 0 deletions state/stater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package state

import (
"testing"

"github.com/vechain/thor/muxdb"
"github.com/vechain/thor/thor"
)

func TestStater(t *testing.T) {
db := muxdb.NewMem()
stater := NewStater(db)

// Example State
root := thor.Bytes32{}
blockNum := uint32(1)
blockConflicts := uint32(0)
steadyBlockNum := uint32(1)

state := stater.NewState(root, blockNum, blockConflicts, steadyBlockNum)

if state == nil {
t.Errorf("NewState returned nil")
}
}

0 comments on commit 7506cca

Please sign in to comment.