diff --git a/state/stage_test.go b/state/stage_test.go index 3aa242950..f747574eb 100644 --- a/state/stage_test.go +++ b/state/stage_test.go @@ -6,6 +6,7 @@ package state import ( + "errors" "math/big" "testing" @@ -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) @@ -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") +} diff --git a/state/state_test.go b/state/state_test.go index 87ea73841..daa5007d7 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -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) diff --git a/state/stater_test.go b/state/stater_test.go new file mode 100644 index 000000000..ebe40ae2d --- /dev/null +++ b/state/stater_test.go @@ -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") + } +}