core: renames entities-> state and removed State prefix

This commit is contained in:
Vsevolod Brekelov 2019-11-28 19:06:09 +03:00
parent 8809fe437d
commit 2d42b14a1d
31 changed files with 221 additions and 222 deletions

View file

@ -11,7 +11,7 @@ import (
"time"
"github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -378,7 +378,7 @@ func (bc *Blockchain) storeBlock(block *Block) error {
if err != nil {
return err
}
unspent.states[input.PrevIndex] = entities.CoinStateSpent
unspent.states[input.PrevIndex] = state.CoinSpent
if err = cache.PutUnspentCoinState(input.PrevHash, unspent); err != nil {
return err
}
@ -439,7 +439,7 @@ func (bc *Blockchain) storeBlock(block *Block) error {
// Process the underlying type of the TX.
switch t := tx.Data.(type) {
case *transaction.RegisterTX:
err := cache.PutAssetState(&entities.AssetState{
err := cache.PutAssetState(&state.Asset{
ID: tx.Hash(),
AssetType: t.AssetType,
Name: t.Name,
@ -499,7 +499,7 @@ func (bc *Blockchain) storeBlock(block *Block) error {
if t.NeedStorage {
properties |= smartcontract.HasStorage
}
contract := &entities.ContractState{
contract := &state.Contract{
Script: t.Script,
ParamList: t.ParamList,
ReturnType: t.ReturnType,
@ -555,7 +555,7 @@ func (bc *Blockchain) storeBlock(block *Block) error {
"err": err,
}).Warn("contract invocation failed")
}
aer := &entities.AppExecResult{
aer := &state.AppExecResult{
TxHash: tx.Hash(),
Trigger: trigger.Application,
VMState: v.State(),
@ -588,7 +588,7 @@ func processOutputs(tx *transaction.Transaction, dao *dao) error {
if err != nil {
return err
}
account.Balances[output.AssetID] = append(account.Balances[output.AssetID], entities.UnspentBalance{
account.Balances[output.AssetID] = append(account.Balances[output.AssetID], state.UnspentBalance{
Tx: tx.Hash(),
Index: uint16(index),
Value: output.Amount,
@ -739,12 +739,12 @@ func (bc *Blockchain) GetTransaction(hash util.Uint256) (*transaction.Transactio
}
// GetStorageItem returns an item from storage.
func (bc *Blockchain) GetStorageItem(scripthash util.Uint160, key []byte) *entities.StorageItem {
func (bc *Blockchain) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
return bc.dao.GetStorageItem(scripthash, key)
}
// GetStorageItems returns all storage items for a given scripthash.
func (bc *Blockchain) GetStorageItems(hash util.Uint160) (map[string]*entities.StorageItem, error) {
func (bc *Blockchain) GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error) {
return bc.dao.GetStorageItems(hash)
}
@ -830,7 +830,7 @@ func (bc *Blockchain) HeaderHeight() uint32 {
}
// GetAssetState returns asset state from its assetID.
func (bc *Blockchain) GetAssetState(assetID util.Uint256) *entities.AssetState {
func (bc *Blockchain) GetAssetState(assetID util.Uint256) *state.Asset {
asset, err := bc.dao.GetAssetState(assetID)
if asset == nil && err != storage.ErrKeyNotFound {
log.Warnf("failed to get asset state %s : %s", assetID, err)
@ -839,7 +839,7 @@ func (bc *Blockchain) GetAssetState(assetID util.Uint256) *entities.AssetState {
}
// GetContractState returns contract by its script hash.
func (bc *Blockchain) GetContractState(hash util.Uint160) *entities.ContractState {
func (bc *Blockchain) GetContractState(hash util.Uint160) *state.Contract {
contract, err := bc.dao.GetContractState(hash)
if contract == nil && err != storage.ErrKeyNotFound {
log.Warnf("failed to get contract state: %s", err)
@ -848,7 +848,7 @@ func (bc *Blockchain) GetContractState(hash util.Uint160) *entities.ContractStat
}
// GetAccountState returns the account state from its script hash.
func (bc *Blockchain) GetAccountState(scriptHash util.Uint160) *entities.AccountState {
func (bc *Blockchain) GetAccountState(scriptHash util.Uint160) *state.Account {
as, err := bc.dao.GetAccountState(scriptHash)
if as == nil && err != storage.ErrKeyNotFound {
log.Warnf("failed to get account state: %s", err)
@ -1166,7 +1166,7 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P
if err != nil {
return nil, err
}
accountState.Balances[output.AssetID] = append(accountState.Balances[output.AssetID], entities.UnspentBalance{
accountState.Balances[output.AssetID] = append(accountState.Balances[output.AssetID], state.UnspentBalance{
Tx: tx.Hash(),
Index: uint16(index),
Value: output.Amount,
@ -1250,7 +1250,7 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P
validators := cache.GetValidators()
count := entities.GetValidatorsWeightedAverage(validators)
count := state.GetValidatorsWeightedAverage(validators)
standByValidators, err := bc.GetStandByValidators()
if err != nil {
return nil, err

View file

@ -2,7 +2,7 @@ package core
import (
"github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -20,19 +20,19 @@ type Blockchainer interface {
Close()
HeaderHeight() uint32
GetBlock(hash util.Uint256) (*Block, error)
GetContractState(hash util.Uint160) *entities.ContractState
GetContractState(hash util.Uint160) *state.Contract
GetHeaderHash(int) util.Uint256
GetHeader(hash util.Uint256) (*Header, error)
CurrentHeaderHash() util.Uint256
CurrentBlockHash() util.Uint256
HasBlock(util.Uint256) bool
HasTransaction(util.Uint256) bool
GetAssetState(util.Uint256) *entities.AssetState
GetAccountState(util.Uint160) *entities.AccountState
GetAssetState(util.Uint256) *state.Asset
GetAccountState(util.Uint160) *state.Account
GetValidators(txes... *transaction.Transaction) ([]*keys.PublicKey, error)
GetScriptHashesForVerifying(*transaction.Transaction) ([]util.Uint160, error)
GetStorageItem(scripthash util.Uint160, key []byte) *entities.StorageItem
GetStorageItems(hash util.Uint160) (map[string]*entities.StorageItem, error)
GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem
GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error)
GetTestVM() (*vm.VM, storage.Store)
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
GetUnspentCoinState(util.Uint256) *UnspentCoinState

View file

@ -6,7 +6,7 @@ import (
"fmt"
"sort"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -42,15 +42,15 @@ func (dao *dao) Put(entity io.Serializable, key []byte) error {
// -- start accounts.
// GetAccountStateOrNew retrieves AccountState from temporary or persistent Store
// GetAccountStateOrNew retrieves Account from temporary or persistent Store
// or creates a new one if it doesn't exist and persists it.
func (dao *dao) GetAccountStateOrNew(hash util.Uint160) (*entities.AccountState, error) {
func (dao *dao) GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) {
account, err := dao.GetAccountState(hash)
if err != nil {
if err != storage.ErrKeyNotFound {
return nil, err
}
account = entities.NewAccountState(hash)
account = state.NewAccount(hash)
if err = dao.PutAccountState(account); err != nil {
return nil, err
}
@ -58,10 +58,10 @@ func (dao *dao) GetAccountStateOrNew(hash util.Uint160) (*entities.AccountState,
return account, nil
}
// GetAccountState returns AccountState from the given Store if it's
// GetAccountState returns Account from the given Store if it's
// present there. Returns nil otherwise.
func (dao *dao) GetAccountState(hash util.Uint160) (*entities.AccountState, error) {
account := &entities.AccountState{}
func (dao *dao) GetAccountState(hash util.Uint160) (*state.Account, error) {
account := &state.Account{}
key := storage.AppendPrefix(storage.STAccount, hash.BytesBE())
err := dao.GetAndDecode(account, key)
if err != nil {
@ -70,8 +70,7 @@ func (dao *dao) GetAccountState(hash util.Uint160) (*entities.AccountState, erro
return account, err
}
// PutAccountState puts given AccountState into the given store.
func (dao *dao) PutAccountState(as *entities.AccountState) error {
func (dao *dao) PutAccountState(as *state.Account) error {
key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.BytesBE())
return dao.Put(as, key)
}
@ -81,8 +80,8 @@ func (dao *dao) PutAccountState(as *entities.AccountState) error {
// -- start assets.
// GetAssetState returns given asset state as recorded in the given store.
func (dao *dao) GetAssetState(assetID util.Uint256) (*entities.AssetState, error) {
asset := &entities.AssetState{}
func (dao *dao) GetAssetState(assetID util.Uint256) (*state.Asset, error) {
asset := &state.Asset{}
key := storage.AppendPrefix(storage.STAsset, assetID.BytesBE())
err := dao.GetAndDecode(asset, key)
if err != nil {
@ -95,7 +94,7 @@ func (dao *dao) GetAssetState(assetID util.Uint256) (*entities.AssetState, error
}
// PutAssetState puts given asset state into the given store.
func (dao *dao) PutAssetState(as *entities.AssetState) error {
func (dao *dao) PutAssetState(as *state.Asset) error {
key := storage.AppendPrefix(storage.STAsset, as.ID.BytesBE())
return dao.Put(as, key)
}
@ -106,8 +105,8 @@ func (dao *dao) PutAssetState(as *entities.AssetState) error {
// GetContractState returns contract state as recorded in the given
// store by the given script hash.
func (dao *dao) GetContractState(hash util.Uint160) (*entities.ContractState, error) {
contract := &entities.ContractState{}
func (dao *dao) GetContractState(hash util.Uint160) (*state.Contract, error) {
contract := &state.Contract{}
key := storage.AppendPrefix(storage.STContract, hash.BytesBE())
err := dao.GetAndDecode(contract, key)
if err != nil {
@ -121,7 +120,7 @@ func (dao *dao) GetContractState(hash util.Uint160) (*entities.ContractState, er
}
// PutContractState puts given contract state into the given store.
func (dao *dao) PutContractState(cs *entities.ContractState) error {
func (dao *dao) PutContractState(cs *state.Contract) error {
key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().BytesBE())
return dao.Put(cs, key)
}
@ -146,7 +145,7 @@ func (dao *dao) GetUnspentCoinStateOrNew(hash util.Uint256) (*UnspentCoinState,
return nil, err
}
unspent = &UnspentCoinState{
states: []entities.CoinState{},
states: []state.Coin{},
}
if err = dao.PutUnspentCoinState(hash, unspent); err != nil {
return nil, err
@ -221,13 +220,13 @@ func (dao *dao) DeleteSpentCoinState(hash util.Uint256) error {
// -- start validator.
// GetValidatorStateOrNew gets validator from store or created new one in case of error.
func (dao *dao) GetValidatorStateOrNew(publicKey *keys.PublicKey) (*entities.ValidatorState, error) {
func (dao *dao) GetValidatorStateOrNew(publicKey *keys.PublicKey) (*state.Validator, error) {
validatorState, err := dao.GetValidatorState(publicKey)
if err != nil {
if err != storage.ErrKeyNotFound {
return nil, err
}
validatorState = &entities.ValidatorState{PublicKey: publicKey}
validatorState = &state.Validator{PublicKey: publicKey}
if err = dao.PutValidatorState(validatorState); err != nil {
return nil, err
}
@ -237,11 +236,11 @@ func (dao *dao) GetValidatorStateOrNew(publicKey *keys.PublicKey) (*entities.Val
}
// GetValidators returns all validators from store.
func (dao *dao) GetValidators() []*entities.ValidatorState {
var validators []*entities.ValidatorState
func (dao *dao) GetValidators() []*state.Validator {
var validators []*state.Validator
dao.store.Seek(storage.STValidator.Bytes(), func(k, v []byte) {
r := io.NewBinReaderFromBuf(v)
validator := &entities.ValidatorState{}
validator := &state.Validator{}
validator.DecodeBinary(r)
if r.Err != nil {
return
@ -252,8 +251,8 @@ func (dao *dao) GetValidators() []*entities.ValidatorState {
}
// GetValidatorState returns validator by publicKey.
func (dao *dao) GetValidatorState(publicKey *keys.PublicKey) (*entities.ValidatorState, error) {
validatorState := &entities.ValidatorState{}
func (dao *dao) GetValidatorState(publicKey *keys.PublicKey) (*state.Validator, error) {
validatorState := &state.Validator{}
key := storage.AppendPrefix(storage.STValidator, publicKey.Bytes())
err := dao.GetAndDecode(validatorState, key)
if err != nil {
@ -262,14 +261,14 @@ func (dao *dao) GetValidatorState(publicKey *keys.PublicKey) (*entities.Validato
return validatorState, nil
}
// PutValidatorState puts given ValidatorState into the given store.
func (dao *dao) PutValidatorState(vs *entities.ValidatorState) error {
// PutValidatorState puts given Validator into the given store.
func (dao *dao) PutValidatorState(vs *state.Validator) error {
key := storage.AppendPrefix(storage.STValidator, vs.PublicKey.Bytes())
return dao.Put(vs, key)
}
// DeleteValidatorState deletes given ValidatorState into the given store.
func (dao *dao) DeleteValidatorState(vs *entities.ValidatorState) error {
// DeleteValidatorState deletes given Validator into the given store.
func (dao *dao) DeleteValidatorState(vs *state.Validator) error {
key := storage.AppendPrefix(storage.STValidator, vs.PublicKey.Bytes())
return dao.store.Delete(key)
}
@ -280,8 +279,8 @@ func (dao *dao) DeleteValidatorState(vs *entities.ValidatorState) error {
// GetAppExecResult gets application execution result from the
// given store.
func (dao *dao) GetAppExecResult(hash util.Uint256) (*entities.AppExecResult, error) {
aer := &entities.AppExecResult{}
func (dao *dao) GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) {
aer := &state.AppExecResult{}
key := storage.AppendPrefix(storage.STNotification, hash.BytesBE())
err := dao.GetAndDecode(aer, key)
if err != nil {
@ -292,7 +291,7 @@ func (dao *dao) GetAppExecResult(hash util.Uint256) (*entities.AppExecResult, er
// PutAppExecResult puts given application execution result into the
// given store.
func (dao *dao) PutAppExecResult(aer *entities.AppExecResult) error {
func (dao *dao) PutAppExecResult(aer *state.AppExecResult) error {
key := storage.AppendPrefix(storage.STNotification, aer.TxHash.BytesBE())
return dao.Put(aer, key)
}
@ -302,14 +301,14 @@ func (dao *dao) PutAppExecResult(aer *entities.AppExecResult) error {
// -- start storage item.
// GetStorageItem returns StorageItem if it exists in the given Store.
func (dao *dao) GetStorageItem(scripthash util.Uint160, key []byte) *entities.StorageItem {
func (dao *dao) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
b, err := dao.store.Get(makeStorageItemKey(scripthash, key))
if err != nil {
return nil
}
r := io.NewBinReaderFromBuf(b)
si := &entities.StorageItem{}
si := &state.StorageItem{}
si.DecodeBinary(r)
if r.Err != nil {
return nil
@ -320,7 +319,7 @@ func (dao *dao) GetStorageItem(scripthash util.Uint160, key []byte) *entities.St
// PutStorageItem puts given StorageItem for given script with given
// key into the given Store.
func (dao *dao) PutStorageItem(scripthash util.Uint160, key []byte, si *entities.StorageItem) error {
func (dao *dao) PutStorageItem(scripthash util.Uint160, key []byte, si *state.StorageItem) error {
return dao.Put(si, makeStorageItemKey(scripthash, key))
}
@ -331,8 +330,8 @@ func (dao *dao) DeleteStorageItem(scripthash util.Uint160, key []byte) error {
}
// GetStorageItems returns all storage items for a given scripthash.
func (dao *dao) GetStorageItems(hash util.Uint160) (map[string]*entities.StorageItem, error) {
var siMap = make(map[string]*entities.StorageItem)
func (dao *dao) GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error) {
var siMap = make(map[string]*state.StorageItem)
var err error
saveToMap := func(k, v []byte) {
@ -340,7 +339,7 @@ func (dao *dao) GetStorageItems(hash util.Uint160) (map[string]*entities.Storage
return
}
r := io.NewBinReaderFromBuf(v)
si := &entities.StorageItem{}
si := &state.StorageItem{}
si.DecodeBinary(r)
if r.Err != nil {
err = r.Err
@ -546,7 +545,7 @@ func (dao *dao) IsDoubleSpend(tx *transaction.Transaction) bool {
return false
}
for _, input := range inputs {
if int(input.PrevIndex) >= len(unspent.states) || unspent.states[input.PrevIndex] == entities.CoinStateSpent {
if int(input.PrevIndex) >= len(unspent.states) || unspent.states[input.PrevIndex] == state.CoinSpent {
return true
}
}

View file

@ -3,7 +3,7 @@ package core
import (
"testing"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/testutil"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -53,7 +53,7 @@ func TestGetAccountStateOrNew_New(t *testing.T) {
func TestPutAndGetAccountStateOrNew(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160()
accountState := &entities.AccountState{ScriptHash: hash}
accountState := &state.Account{ScriptHash: hash}
err := dao.PutAccountState(accountState)
require.NoError(t, err)
gotAccount, err := dao.GetAccountStateOrNew(hash)
@ -64,7 +64,7 @@ func TestPutAndGetAccountStateOrNew(t *testing.T) {
func TestPutAndGetAssetState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
id := testutil.RandomUint256()
assetState := &entities.AssetState{ID: id, Owner: keys.PublicKey{}}
assetState := &state.Asset{ID: id, Owner: keys.PublicKey{}}
err := dao.PutAssetState(assetState)
require.NoError(t, err)
gotAssetState, err := dao.GetAssetState(id)
@ -74,7 +74,7 @@ func TestPutAndGetAssetState(t *testing.T) {
func TestPutAndGetContractState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
contractState := &entities.ContractState{Script: []byte{}, ParamList:[]smartcontract.ParamType{}}
contractState := &state.Contract{Script: []byte{}, ParamList:[]smartcontract.ParamType{}}
hash := contractState.ScriptHash()
err := dao.PutContractState(contractState)
require.NoError(t, err)
@ -85,7 +85,7 @@ func TestPutAndGetContractState(t *testing.T) {
func TestDeleteContractState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
contractState := &entities.ContractState{Script: []byte{}, ParamList:[]smartcontract.ParamType{}}
contractState := &state.Contract{Script: []byte{}, ParamList:[]smartcontract.ParamType{}}
hash := contractState.ScriptHash()
err := dao.PutContractState(contractState)
require.NoError(t, err)
@ -118,7 +118,7 @@ func TestGetUnspentCoinState_Err(t *testing.T) {
func TestPutGetUnspentCoinState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256()
unspentCoinState := &UnspentCoinState{states:[]entities.CoinState{}}
unspentCoinState := &UnspentCoinState{states:[]state.Coin{}}
err := dao.PutUnspentCoinState(hash, unspentCoinState)
require.NoError(t, err)
gotUnspentCoinState, err := dao.GetUnspentCoinState(hash)
@ -183,7 +183,7 @@ func TestGetValidatorStateOrNew_New(t *testing.T) {
func TestPutGetValidatorState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
publicKey := &keys.PublicKey{}
validatorState := &entities.ValidatorState{
validatorState := &state.Validator{
PublicKey: publicKey,
Registered: false,
Votes: 0,
@ -198,7 +198,7 @@ func TestPutGetValidatorState(t *testing.T) {
func TestDeleteValidatorState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
publicKey := &keys.PublicKey{}
validatorState := &entities.ValidatorState{
validatorState := &state.Validator{
PublicKey: publicKey,
Registered: false,
Votes: 0,
@ -215,7 +215,7 @@ func TestDeleteValidatorState(t *testing.T) {
func TestGetValidators(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
publicKey := &keys.PublicKey{}
validatorState := &entities.ValidatorState{
validatorState := &state.Validator{
PublicKey: publicKey,
Registered: false,
Votes: 0,
@ -230,7 +230,7 @@ func TestGetValidators(t *testing.T) {
func TestPutGetAppExecResult(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256()
appExecResult := &entities.AppExecResult{TxHash: hash, Events:[]entities.NotificationEvent{}}
appExecResult := &state.AppExecResult{TxHash: hash, Events:[]state.NotificationEvent{}}
err := dao.PutAppExecResult(appExecResult)
require.NoError(t, err)
gotAppExecResult, err := dao.GetAppExecResult(hash)
@ -242,7 +242,7 @@ func TestPutGetStorageItem(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160()
key := []byte{0}
storageItem := &entities.StorageItem{Value:[]uint8{}}
storageItem := &state.StorageItem{Value: []uint8{}}
err := dao.PutStorageItem(hash, key, storageItem)
require.NoError(t, err)
gotStorageItem := dao.GetStorageItem(hash, key)
@ -253,7 +253,7 @@ func TestDeleteStorageItem(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160()
key := []byte{0}
storageItem := &entities.StorageItem{Value:[]uint8{}}
storageItem := &state.StorageItem{Value: []uint8{}}
err := dao.PutStorageItem(hash, key, storageItem)
require.NoError(t, err)
err = dao.DeleteStorageItem(hash, key)

View file

@ -1,12 +0,0 @@
package entities
// CoinState represents the state of a coin.
type CoinState uint8
// Viable CoinState constants.
const (
CoinStateConfirmed CoinState = 0
CoinStateSpent CoinState = 1 << 1
CoinStateClaimed CoinState = 1 << 2
CoinStateFrozen CoinState = 1 << 5
)

View file

@ -5,7 +5,7 @@ import (
"fmt"
"math"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/smartcontract"
@ -317,7 +317,7 @@ func (ic *interopContext) bcGetAccount(v *vm.VM) error {
}
acc := ic.bc.GetAccountState(acchash)
if acc == nil {
acc = entities.NewAccountState(acchash)
acc = state.NewAccount(acchash)
}
v.Estack().PushVal(vm.NewInteropItem(acc))
return nil
@ -341,7 +341,7 @@ func (ic *interopContext) bcGetAsset(v *vm.VM) error {
// accountGetBalance returns balance for a given account.
func (ic *interopContext) accountGetBalance(v *vm.VM) error {
accInterface := v.Estack().Pop().Value()
acc, ok := accInterface.(*entities.AccountState)
acc, ok := accInterface.(*state.Account)
if !ok {
return fmt.Errorf("%T is not an account state", acc)
}
@ -361,7 +361,7 @@ func (ic *interopContext) accountGetBalance(v *vm.VM) error {
// accountGetScriptHash returns script hash of a given account.
func (ic *interopContext) accountGetScriptHash(v *vm.VM) error {
accInterface := v.Estack().Pop().Value()
acc, ok := accInterface.(*entities.AccountState)
acc, ok := accInterface.(*state.Account)
if !ok {
return fmt.Errorf("%T is not an account state", acc)
}
@ -372,7 +372,7 @@ func (ic *interopContext) accountGetScriptHash(v *vm.VM) error {
// accountGetVotes returns votes of a given account.
func (ic *interopContext) accountGetVotes(v *vm.VM) error {
accInterface := v.Estack().Pop().Value()
acc, ok := accInterface.(*entities.AccountState)
acc, ok := accInterface.(*state.Account)
if !ok {
return fmt.Errorf("%T is not an account state", acc)
}
@ -428,9 +428,9 @@ func (ic *interopContext) storageFind(v *vm.VM) error {
}
*/
// createContractStateFromVM pops all contract state elements from the VM
// evaluation stack, does a lot of checks and returns ContractState if it
// evaluation stack, does a lot of checks and returns Contract if it
// succeeds.
func (ic *interopContext) createContractStateFromVM(v *vm.VM) (*entities.ContractState, error) {
func (ic *interopContext) createContractStateFromVM(v *vm.VM) (*state.Contract, error) {
if ic.trigger != trigger.Application {
return nil, errors.New("can't create contract when not triggered by an application")
}
@ -468,7 +468,7 @@ func (ic *interopContext) createContractStateFromVM(v *vm.VM) (*entities.Contrac
if len(desc) > MaxContractStringLen {
return nil, errors.New("too big description")
}
contract := &entities.ContractState{
contract := &state.Contract{
Script: script,
ParamList: paramList,
ReturnType: retType,
@ -503,7 +503,7 @@ func (ic *interopContext) contractCreate(v *vm.VM) error {
// contractGetScript returns a script associated with a contract.
func (ic *interopContext) contractGetScript(v *vm.VM) error {
csInterface := v.Estack().Pop().Value()
cs, ok := csInterface.(*entities.ContractState)
cs, ok := csInterface.(*state.Contract)
if !ok {
return fmt.Errorf("%T is not a contract state", cs)
}
@ -514,7 +514,7 @@ func (ic *interopContext) contractGetScript(v *vm.VM) error {
// contractIsPayable returns whether contract is payable.
func (ic *interopContext) contractIsPayable(v *vm.VM) error {
csInterface := v.Estack().Pop().Value()
cs, ok := csInterface.(*entities.ContractState)
cs, ok := csInterface.(*state.Contract)
if !ok {
return fmt.Errorf("%T is not a contract state", cs)
}
@ -610,7 +610,7 @@ func (ic *interopContext) assetCreate(v *vm.VM) error {
if err != nil {
return gherr.Wrap(err, "failed to get issuer")
}
asset := &entities.AssetState{
asset := &state.Asset{
ID: ic.tx.Hash(),
AssetType: atype,
Name: name,
@ -632,7 +632,7 @@ func (ic *interopContext) assetCreate(v *vm.VM) error {
// assetGetAdmin returns asset admin.
func (ic *interopContext) assetGetAdmin(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -643,7 +643,7 @@ func (ic *interopContext) assetGetAdmin(v *vm.VM) error {
// assetGetAmount returns the overall amount of asset available.
func (ic *interopContext) assetGetAmount(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -654,7 +654,7 @@ func (ic *interopContext) assetGetAmount(v *vm.VM) error {
// assetGetAssetId returns the id of an asset.
func (ic *interopContext) assetGetAssetID(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -665,7 +665,7 @@ func (ic *interopContext) assetGetAssetID(v *vm.VM) error {
// assetGetAssetType returns type of an asset.
func (ic *interopContext) assetGetAssetType(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -676,7 +676,7 @@ func (ic *interopContext) assetGetAssetType(v *vm.VM) error {
// assetGetAvailable returns available (not yet issued) amount of asset.
func (ic *interopContext) assetGetAvailable(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -687,7 +687,7 @@ func (ic *interopContext) assetGetAvailable(v *vm.VM) error {
// assetGetIssuer returns issuer of an asset.
func (ic *interopContext) assetGetIssuer(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -698,7 +698,7 @@ func (ic *interopContext) assetGetIssuer(v *vm.VM) error {
// assetGetOwner returns owner of an asset.
func (ic *interopContext) assetGetOwner(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -709,7 +709,7 @@ func (ic *interopContext) assetGetOwner(v *vm.VM) error {
// assetGetPrecision returns precision used to measure this asset.
func (ic *interopContext) assetGetPrecision(v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}
@ -723,7 +723,7 @@ func (ic *interopContext) assetRenew(v *vm.VM) error {
return errors.New("can't create asset when not triggered by an application")
}
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*entities.AssetState)
as, ok := asInterface.(*state.Asset)
if !ok {
return fmt.Errorf("%T is not an asset state", as)
}

View file

@ -4,7 +4,7 @@ import (
"math/big"
"testing"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/testutil"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -323,7 +323,7 @@ func TestAssetGetPrecision(t *testing.T) {
require.Equal(t, big.NewInt(int64(assetState.Precision)), precision)
}
// Helper functions to create VM, InteropContext, TX, AccountState, ContractState, AssetState.
// Helper functions to create VM, InteropContext, TX, Account, Contract, Asset.
func createVMAndPushBlock(t *testing.T) (*vm.VM, *Block, *interopContext) {
v := vm.New()
@ -339,9 +339,9 @@ func createVMAndPushTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop
return v, tx, context
}
func createVMAndAssetState(t *testing.T) (*vm.VM, *entities.AssetState, *interopContext) {
func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interopContext) {
v := vm.New()
assetState := &entities.AssetState{
assetState := &state.Asset{
ID: util.Uint256{},
AssetType: transaction.GoverningToken,
Name: "TestAsset",
@ -361,9 +361,9 @@ func createVMAndAssetState(t *testing.T) (*vm.VM, *entities.AssetState, *interop
return v, assetState, context
}
func createVMAndContractState(t *testing.T) (*vm.VM, *entities.ContractState, *interopContext) {
func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interopContext) {
v := vm.New()
contractState := &entities.ContractState{
contractState := &state.Contract{
Script: []byte("testscript"),
ParamList: []smartcontract.ParamType{smartcontract.StringType, smartcontract.IntegerType, smartcontract.Hash160Type},
ReturnType: smartcontract.ArrayType,
@ -379,11 +379,11 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *entities.ContractState, *i
return v, contractState, context
}
func createVMAndAccState(t *testing.T) (*vm.VM, *entities.AccountState, *interopContext) {
func createVMAndAccState(t *testing.T) (*vm.VM, *state.Account, *interopContext) {
v := vm.New()
rawHash := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
hash, err := util.Uint160DecodeStringBE(rawHash)
accountState := entities.NewAccountState(hash)
accountState := state.NewAccount(hash)
key := &keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)}
accountState.Votes = []*keys.PublicKey{key}

View file

@ -5,7 +5,7 @@ import (
"fmt"
"math"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -342,7 +342,7 @@ func (ic *interopContext) runtimeCheckWitness(v *vm.VM) error {
func (ic *interopContext) runtimeNotify(v *vm.VM) error {
// It can be just about anything.
e := v.Estack().Pop()
ne := entities.NotificationEvent{ScriptHash:getContextScriptHash(v, 0), Item:e.Item()}
ne := state.NotificationEvent{ScriptHash: getContextScriptHash(v, 0), Item: e.Item()}
ic.notifications = append(ic.notifications, ne)
return nil
}
@ -475,7 +475,7 @@ func (ic *interopContext) putWithContextAndFlags(stc *StorageContext, key []byte
}
si := ic.dao.GetStorageItem(stc.ScriptHash, key)
if si == nil {
si = &entities.StorageItem{}
si = &state.StorageItem{}
}
if si.IsConst {
return errors.New("storage item exists and is read-only")
@ -558,7 +558,7 @@ func (ic *interopContext) contractDestroy(v *vm.VM) error {
// contractGetStorageContext retrieves StorageContext of a contract.
func (ic *interopContext) contractGetStorageContext(v *vm.VM) error {
csInterface := v.Estack().Pop().Value()
cs, ok := csInterface.(*entities.ContractState)
cs, ok := csInterface.(*state.Contract)
if !ok {
return fmt.Errorf("%T is not a contract state", cs)
}

View file

@ -8,7 +8,7 @@ package core
*/
import (
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/vm"
@ -20,12 +20,12 @@ type interopContext struct {
block *Block
tx *transaction.Transaction
dao *dao
notifications []entities.NotificationEvent
notifications []state.NotificationEvent
}
func newInteropContext(trigger byte, bc Blockchainer, s storage.Store, block *Block, tx *transaction.Transaction) *interopContext {
dao := &dao{store: storage.NewMemCachedStore(s)}
nes := make([]entities.NotificationEvent, 0)
nes := make([]state.NotificationEvent, 0)
return &interopContext{bc, trigger, block, tx, dao, nes}
}

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -17,8 +17,8 @@ type UnspentBalance struct {
// UnspentBalances is a slice of UnspentBalance (mostly needed to sort them).
type UnspentBalances []UnspentBalance
// AccountState represents the state of a NEO account.
type AccountState struct {
// Account represents the state of a NEO account.
type Account struct {
Version uint8
ScriptHash util.Uint160
IsFrozen bool
@ -26,9 +26,9 @@ type AccountState struct {
Balances map[util.Uint256][]UnspentBalance
}
// NewAccountState returns a new AccountState object.
func NewAccountState(scriptHash util.Uint160) *AccountState {
return &AccountState{
// NewAccount returns a new Account object.
func NewAccount(scriptHash util.Uint160) *Account {
return &Account{
Version: 0,
ScriptHash: scriptHash,
IsFrozen: false,
@ -37,8 +37,8 @@ func NewAccountState(scriptHash util.Uint160) *AccountState {
}
}
// DecodeBinary decodes AccountState from the given BinReader.
func (s *AccountState) DecodeBinary(br *io.BinReader) {
// DecodeBinary decodes Account from the given BinReader.
func (s *Account) DecodeBinary(br *io.BinReader) {
br.ReadLE(&s.Version)
br.ReadBytes(s.ScriptHash[:])
br.ReadLE(&s.IsFrozen)
@ -55,8 +55,8 @@ func (s *AccountState) DecodeBinary(br *io.BinReader) {
}
}
// EncodeBinary encodes AccountState to the given BinWriter.
func (s *AccountState) EncodeBinary(bw *io.BinWriter) {
// EncodeBinary encodes Account to the given BinWriter.
func (s *Account) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(s.Version)
bw.WriteBytes(s.ScriptHash[:])
bw.WriteLE(s.IsFrozen)
@ -85,7 +85,7 @@ func (u *UnspentBalance) EncodeBinary(w *io.BinWriter) {
// GetBalanceValues sums all unspent outputs and returns a map of asset IDs to
// overall balances.
func (s *AccountState) GetBalanceValues() map[util.Uint256]util.Fixed8 {
func (s *Account) GetBalanceValues() map[util.Uint256]util.Fixed8 {
res := make(map[util.Uint256]util.Fixed8)
for k, v := range s.Balances {
balance := util.Fixed8(0)

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"testing"
@ -30,7 +30,7 @@ func TestDecodeEncodeAccountState(t *testing.T) {
votes[i] = k.PublicKey()
}
a := &AccountState{
a := &Account{
Version: 0,
ScriptHash: testutil.RandomUint160(),
IsFrozen: true,
@ -42,7 +42,7 @@ func TestDecodeEncodeAccountState(t *testing.T) {
a.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
aDecode := &AccountState{}
aDecode := &Account{}
r := io.NewBinReaderFromBuf(buf.Bytes())
aDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
@ -60,7 +60,7 @@ func TestDecodeEncodeAccountState(t *testing.T) {
func TestAccountStateBalanceValues(t *testing.T) {
asset1 := testutil.RandomUint256()
asset2 := testutil.RandomUint256()
as := AccountState{Balances: make(map[util.Uint256][]UnspentBalance)}
as := Account{Balances: make(map[util.Uint256][]UnspentBalance)}
ref := 0
for i := 0; i < 10; i++ {
ref += i

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"github.com/CityOfZion/neo-go/pkg/core/transaction"
@ -9,8 +9,8 @@ import (
const feeMode = 0x0
// AssetState represents the state of an NEO registered Asset.
type AssetState struct {
// Asset represents the state of an NEO registered Asset.
type Asset struct {
ID util.Uint256
AssetType transaction.AssetType
Name string
@ -27,7 +27,7 @@ type AssetState struct {
}
// DecodeBinary implements Serializable interface.
func (a *AssetState) DecodeBinary(br *io.BinReader) {
func (a *Asset) DecodeBinary(br *io.BinReader) {
br.ReadBytes(a.ID[:])
br.ReadLE(&a.AssetType)
@ -47,7 +47,7 @@ func (a *AssetState) DecodeBinary(br *io.BinReader) {
}
// EncodeBinary implements Serializable interface.
func (a *AssetState) EncodeBinary(bw *io.BinWriter) {
func (a *Asset) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(a.ID[:])
bw.WriteLE(a.AssetType)
bw.WriteString(a.Name)
@ -66,7 +66,7 @@ func (a *AssetState) EncodeBinary(bw *io.BinWriter) {
}
// GetName returns the asset name based on its type.
func (a *AssetState) GetName() string {
func (a *Asset) GetName() string {
if a.AssetType == transaction.GoverningToken {
return "NEO"

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"testing"
@ -12,7 +12,7 @@ import (
)
func TestEncodeDecodeAssetState(t *testing.T) {
asset := &AssetState{
asset := &Asset{
ID: testutil.RandomUint256(),
AssetType: transaction.Token,
Name: "super cool token",
@ -30,7 +30,7 @@ func TestEncodeDecodeAssetState(t *testing.T) {
buf := io.NewBufBinWriter()
asset.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assetDecode := &AssetState{}
assetDecode := &Asset{}
r := io.NewBinReaderFromBuf(buf.Bytes())
assetDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
@ -38,11 +38,11 @@ func TestEncodeDecodeAssetState(t *testing.T) {
}
func TestAssetState_GetName_NEO(t *testing.T) {
asset := &AssetState{AssetType: transaction.GoverningToken}
asset := &Asset{AssetType: transaction.GoverningToken}
assert.Equal(t, "NEO", asset.GetName())
}
func TestAssetState_GetName_NEOGas(t *testing.T) {
asset := &AssetState{AssetType: transaction.UtilityToken}
asset := &Asset{AssetType: transaction.UtilityToken}
assert.Equal(t, "NEOGas", asset.GetName())
}

12
pkg/core/state/coin.go Normal file
View file

@ -0,0 +1,12 @@
package state
// Coin represents the state of a coin.
type Coin uint8
// Viable Coin constants.
const (
CoinConfirmed Coin = 0
CoinSpent Coin = 1 << 1
CoinClaimed Coin = 1 << 2
CoinFrozen Coin = 1 << 5
)

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
@ -7,8 +7,8 @@ import (
"github.com/CityOfZion/neo-go/pkg/util"
)
// ContractState holds information about a smart contract in the NEO blockchain.
type ContractState struct {
// Contract holds information about a smart contract in the NEO blockchain.
type Contract struct {
Script []byte
ParamList []smartcontract.ParamType
ReturnType smartcontract.ParamType
@ -23,7 +23,7 @@ type ContractState struct {
}
// DecodeBinary implements Serializable interface.
func (cs *ContractState) DecodeBinary(br *io.BinReader) {
func (cs *Contract) DecodeBinary(br *io.BinReader) {
cs.Script = br.ReadVarBytes()
br.ReadArray(&cs.ParamList)
br.ReadLE(&cs.ReturnType)
@ -37,7 +37,7 @@ func (cs *ContractState) DecodeBinary(br *io.BinReader) {
}
// EncodeBinary implements Serializable interface.
func (cs *ContractState) EncodeBinary(bw *io.BinWriter) {
func (cs *Contract) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarBytes(cs.Script)
bw.WriteArray(cs.ParamList)
bw.WriteLE(cs.ReturnType)
@ -50,7 +50,7 @@ func (cs *ContractState) EncodeBinary(bw *io.BinWriter) {
}
// ScriptHash returns a contract script hash.
func (cs *ContractState) ScriptHash() util.Uint160 {
func (cs *Contract) ScriptHash() util.Uint160 {
if cs.scriptHash.Equals(util.Uint160{}) {
cs.createHash()
}
@ -58,21 +58,21 @@ func (cs *ContractState) ScriptHash() util.Uint160 {
}
// createHash creates contract script hash.
func (cs *ContractState) createHash() {
func (cs *Contract) createHash() {
cs.scriptHash = hash.Hash160(cs.Script)
}
// HasStorage checks whether the contract has storage property set.
func (cs *ContractState) HasStorage() bool {
func (cs *Contract) HasStorage() bool {
return (cs.Properties & smartcontract.HasStorage) != 0
}
// HasDynamicInvoke checks whether the contract has dynamic invoke property set.
func (cs *ContractState) HasDynamicInvoke() bool {
func (cs *Contract) HasDynamicInvoke() bool {
return (cs.Properties & smartcontract.HasDynamicInvoke) != 0
}
// IsPayable checks whether the contract has payable property set.
func (cs *ContractState) IsPayable() bool {
func (cs *Contract) IsPayable() bool {
return (cs.Properties & smartcontract.IsPayable) != 0
}

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"testing"
@ -12,7 +12,7 @@ import (
func TestEncodeDecodeContractState(t *testing.T) {
script := []byte("testscript")
contract := &ContractState{
contract := &Contract{
Script: script,
ParamList: []smartcontract.ParamType{smartcontract.StringType, smartcontract.IntegerType, smartcontract.Hash160Type},
ReturnType: smartcontract.BoolType,
@ -28,7 +28,7 @@ func TestEncodeDecodeContractState(t *testing.T) {
buf := io.NewBufBinWriter()
contract.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
contractDecoded := &ContractState{}
contractDecoded := &Contract{}
r := io.NewBinReaderFromBuf(buf.Bytes())
contractDecoded.DecodeBinary(r)
assert.Nil(t, r.Err)
@ -37,10 +37,10 @@ func TestEncodeDecodeContractState(t *testing.T) {
}
func TestContractStateProperties(t *testing.T) {
flaggedContract := ContractState{
flaggedContract := Contract{
Properties: smartcontract.HasStorage | smartcontract.HasDynamicInvoke | smartcontract.IsPayable,
}
nonFlaggedContract := ContractState{
nonFlaggedContract := Contract{
ReturnType: smartcontract.BoolType,
}
assert.Equal(t, true, flaggedContract.HasStorage())

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"github.com/CityOfZion/neo-go/pkg/io"

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"testing"

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"github.com/CityOfZion/neo-go/pkg/io"

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"testing"

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -6,27 +6,27 @@ import (
"github.com/CityOfZion/neo-go/pkg/util"
)
// ValidatorState holds the state of a validator.
type ValidatorState struct {
// Validator holds the state of a validator.
type Validator struct {
PublicKey *keys.PublicKey
Registered bool
Votes util.Fixed8
}
// RegisteredAndHasVotes returns true or false whether Validator is registered and has votes.
func (vs *ValidatorState) RegisteredAndHasVotes() bool {
func (vs *Validator) RegisteredAndHasVotes() bool {
return vs.Registered && vs.Votes > util.Fixed8(0)
}
// EncodeBinary encodes ValidatorState to the given BinWriter.
func (vs *ValidatorState) EncodeBinary(bw *io.BinWriter) {
// EncodeBinary encodes Validator to the given BinWriter.
func (vs *Validator) EncodeBinary(bw *io.BinWriter) {
vs.PublicKey.EncodeBinary(bw)
bw.WriteLE(vs.Registered)
bw.WriteLE(vs.Votes)
}
// DecodeBinary decodes ValidatorState from the given BinReader.
func (vs *ValidatorState) DecodeBinary(reader *io.BinReader) {
// DecodeBinary decodes Validator from the given BinReader.
func (vs *Validator) DecodeBinary(reader *io.BinReader) {
vs.PublicKey = &keys.PublicKey{}
vs.PublicKey.DecodeBinary(reader)
reader.ReadLE(&vs.Registered)
@ -35,17 +35,17 @@ func (vs *ValidatorState) DecodeBinary(reader *io.BinReader) {
// GetValidatorsWeightedAverage applies weighted filter based on votes for validator and returns number of validators.
// Get back to it with further investigation in https://github.com/nspcc-dev/neo-go/issues/512.
func GetValidatorsWeightedAverage(validators []*ValidatorState) int {
func GetValidatorsWeightedAverage(validators []*Validator) int {
return int(weightedAverage(applyWeightedFilter(validators)))
}
// applyWeightedFilter is an implementation of the filter for validators votes.
// C# reference https://github.com/neo-project/neo/blob/41caff115c28d6c7665b2a7ac72967e7ce82e921/neo/Helper.cs#L273
func applyWeightedFilter(validators []*ValidatorState) map[*ValidatorState]float64 {
var validatorsWithVotes []*ValidatorState
func applyWeightedFilter(validators []*Validator) map[*Validator]float64 {
var validatorsWithVotes []*Validator
var amount float64
weightedVotes := make(map[*ValidatorState]float64)
weightedVotes := make(map[*Validator]float64)
start := 0.25
end := 0.75
sum := float64(0)
@ -85,7 +85,7 @@ func applyWeightedFilter(validators []*ValidatorState) map[*ValidatorState]float
return weightedVotes
}
func weightedAverage(weightedVotes map[*ValidatorState]float64) float64 {
func weightedAverage(weightedVotes map[*Validator]float64) float64 {
sumWeight := float64(0)
sumValue := float64(0)
for vState, weight := range weightedVotes {

View file

@ -1,4 +1,4 @@
package entities
package state
import (
"math/big"
@ -11,7 +11,7 @@ import (
)
func TestValidatorState_DecodeEncodeBinary(t *testing.T) {
state := &ValidatorState{
state := &Validator{
PublicKey: &keys.PublicKey{},
Registered: false,
Votes: util.Fixed8(10),
@ -20,7 +20,7 @@ func TestValidatorState_DecodeEncodeBinary(t *testing.T) {
state.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
decodedState := &ValidatorState{}
decodedState := &Validator{}
reader := io.NewBinReaderFromBuf(buf.Bytes())
decodedState.DecodeBinary(reader)
require.NoError(t, reader.Err)
@ -28,7 +28,7 @@ func TestValidatorState_DecodeEncodeBinary(t *testing.T) {
}
func TestRegisteredAndHasVotes_Registered(t *testing.T) {
state := &ValidatorState{
state := &Validator{
PublicKey: &keys.PublicKey{
X: big.NewInt(1),
Y: big.NewInt(1),
@ -40,7 +40,7 @@ func TestRegisteredAndHasVotes_Registered(t *testing.T) {
}
func TestRegisteredAndHasVotes_RegisteredWithVotes(t *testing.T) {
state := &ValidatorState{
state := &Validator{
PublicKey: &keys.PublicKey{
X: big.NewInt(1),
Y: big.NewInt(1),
@ -52,7 +52,7 @@ func TestRegisteredAndHasVotes_RegisteredWithVotes(t *testing.T) {
}
func TestRegisteredAndHasVotes_NotRegisteredWithVotes(t *testing.T) {
state := &ValidatorState{
state := &Validator{
PublicKey: &keys.PublicKey{
X: big.NewInt(1),
Y: big.NewInt(1),

View file

@ -1,22 +1,22 @@
package core
import (
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/io"
)
// UnspentCoinState hold the state of a unspent coin.
type UnspentCoinState struct {
states []entities.CoinState
states []state.Coin
}
// NewUnspentCoinState returns a new unspent coin state with N confirmed states.
func NewUnspentCoinState(n int) *UnspentCoinState {
u := &UnspentCoinState{
states: make([]entities.CoinState, n),
states: make([]state.Coin, n),
}
for i := 0; i < n; i++ {
u.states[i] = entities.CoinStateConfirmed
u.states[i] = state.CoinConfirmed
}
return u
}
@ -32,10 +32,10 @@ func (s *UnspentCoinState) EncodeBinary(bw *io.BinWriter) {
// DecodeBinary decodes UnspentCoinState from the given BinReader.
func (s *UnspentCoinState) DecodeBinary(br *io.BinReader) {
lenStates := br.ReadVarUint()
s.states = make([]entities.CoinState, lenStates)
s.states = make([]state.Coin, lenStates)
for i := 0; i < int(lenStates); i++ {
var state uint8
br.ReadLE(&state)
s.states[i] = entities.CoinState(state)
var coinState uint8
br.ReadLE(&coinState)
s.states[i] = state.Coin(coinState)
}
}

View file

@ -3,19 +3,19 @@ package core
import (
"testing"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert"
)
func TestDecodeEncodeUnspentCoinState(t *testing.T) {
unspent := &UnspentCoinState{
states: []entities.CoinState{
entities.CoinStateConfirmed,
entities.CoinStateSpent,
entities.CoinStateSpent,
entities.CoinStateSpent,
entities.CoinStateConfirmed,
states: []state.Coin{
state.CoinConfirmed,
state.CoinSpent,
state.CoinSpent,
state.CoinSpent,
state.CoinConfirmed,
},
}

View file

@ -9,7 +9,7 @@ import (
"github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -63,7 +63,7 @@ func (chain testChain) HeaderHeight() uint32 {
func (chain testChain) GetBlock(hash util.Uint256) (*core.Block, error) {
panic("TODO")
}
func (chain testChain) GetContractState(hash util.Uint160) *entities.ContractState {
func (chain testChain) GetContractState(hash util.Uint160) *state.Contract {
panic("TODO")
}
func (chain testChain) GetHeaderHash(int) util.Uint256 {
@ -73,10 +73,10 @@ func (chain testChain) GetHeader(hash util.Uint256) (*core.Header, error) {
panic("TODO")
}
func (chain testChain) GetAssetState(util.Uint256) *entities.AssetState {
func (chain testChain) GetAssetState(util.Uint256) *state.Asset {
panic("TODO")
}
func (chain testChain) GetAccountState(util.Uint160) *entities.AccountState {
func (chain testChain) GetAccountState(util.Uint160) *state.Account {
panic("TODO")
}
func (chain testChain) GetValidators(...*transaction.Transaction) ([]*keys.PublicKey, error) {
@ -85,13 +85,13 @@ func (chain testChain) GetValidators(...*transaction.Transaction) ([]*keys.Publi
func (chain testChain) GetScriptHashesForVerifying(*transaction.Transaction) ([]util.Uint160, error) {
panic("TODO")
}
func (chain testChain) GetStorageItem(scripthash util.Uint160, key []byte) *entities.StorageItem {
func (chain testChain) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
panic("TODO")
}
func (chain testChain) GetTestVM() (*vm.VM, storage.Store) {
panic("TODO")
}
func (chain testChain) GetStorageItems(hash util.Uint160) (map[string]*entities.StorageItem, error) {
func (chain testChain) GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error) {
panic("TODO")
}
func (chain testChain) CurrentHeaderHash() util.Uint256 {

View file

@ -11,7 +11,7 @@ import (
"sync"
"time"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/util"
@ -158,7 +158,7 @@ func (c *Client) SetClient(cli *http.Client) {
// asset belonging to specified address. This implementation uses GetUnspents
// JSON-RPC call internally, so make sure your RPC server suppors that.
func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
var utxos entities.UnspentBalances
var utxos state.UnspentBalances
resp, err := c.GetUnspents(address)
if err != nil || resp.Error != nil {

View file

@ -6,7 +6,7 @@ import (
"net/http"
"sort"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
"github.com/CityOfZion/neo-go/pkg/util"
@ -68,7 +68,7 @@ func (s NeoScanServer) CalculateInputs(address string, assetIDUint util.Uint256,
// unspentsToInputs uses UnspentBalances to create a slice of inputs for a new
// transcation containing the required amount of asset.
func unspentsToInputs(utxos entities.UnspentBalances, required util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
func unspentsToInputs(utxos state.UnspentBalances, required util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
var (
num, i uint16
selected = util.Fixed8(0)

View file

@ -1,7 +1,7 @@
package rpc
import (
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/util"
)
@ -20,7 +20,7 @@ type (
// Unspent stores Unspents per asset
Unspent struct {
Unspent entities.UnspentBalances
Unspent state.UnspentBalances
Asset string // "NEO" / "GAS"
Amount util.Fixed8 // total unspent of this asset
}

View file

@ -2,7 +2,7 @@ package wrappers
import (
"bytes"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"sort"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
@ -10,7 +10,7 @@ import (
)
// AccountState wrapper used for the representation of
// core.AccountState on the RPC Server.
// state.Account on the RPC Server.
type AccountState struct {
Version uint8 `json:"version"`
ScriptHash util.Uint160 `json:"script_hash"`
@ -32,8 +32,8 @@ type Balance struct {
Value util.Fixed8 `json:"value"`
}
// NewAccountState creates a new AccountState wrapper.
func NewAccountState(a *entities.AccountState) AccountState {
// NewAccountState creates a new Account wrapper.
func NewAccountState(a *state.Account) AccountState {
balances := make(Balances, 0, len(a.Balances))
for k, v := range a.GetBalanceValues() {
balances = append(balances, Balance{

View file

@ -1,14 +1,14 @@
package wrappers
import (
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/util"
)
// AssetState wrapper used for the representation of
// core.AssetState on the RPC Server.
// state.Asset on the RPC Server.
type AssetState struct {
ID util.Uint256 `json:"assetID"`
AssetType transaction.AssetType `json:"assetType"`
@ -25,8 +25,8 @@ type AssetState struct {
IsFrozen bool `json:"is_frozen"`
}
// NewAssetState creates a new AssetState wrapper.
func NewAssetState(a *entities.AssetState) AssetState {
// NewAssetState creates a new Asset wrapper.
func NewAssetState(a *state.Asset) AssetState {
return AssetState{
ID: a.ID,
AssetType: a.AssetType,

View file

@ -2,14 +2,14 @@ package wrappers
import (
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/entities"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/util"
)
// UnspentBalanceInfo wrapper is used to represent single unspent asset entry
// in `getunspents` output.
type UnspentBalanceInfo struct {
Unspents []entities.UnspentBalance `json:"unspent"`
Unspents []state.UnspentBalance `json:"unspent"`
AssetHash util.Uint256 `json:"asset_hash"`
Asset string `json:"asset"`
AssetSymbol string `json:"asset_symbol"`
@ -28,8 +28,8 @@ var GlobalAssets = map[string]string{
"602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7": "GAS",
}
// NewUnspents creates a new AccountState wrapper using given Blockchainer.
func NewUnspents(a *entities.AccountState, chain core.Blockchainer, addr string) Unspents {
// NewUnspents creates a new Account wrapper using given Blockchainer.
func NewUnspents(a *state.Account, chain core.Blockchainer, addr string) Unspents {
res := Unspents{
Address: addr,
Balance: make([]UnspentBalanceInfo, 0, len(a.Balances)),