Skip to content

Commit

Permalink
State intefaces refactoring and state keys reduction (#561)
Browse files Browse the repository at this point in the history
* Address structure renamed to WavesAddress. New interface Address introduced. New type AddressID added and used to identify Address in state.

* Keys of assets' data in state reduced to 20 bytes.

* Tail of asset ID digest is stored in state and full AssetID digest is reconstructed on retrieval by functions that get AssetInfo

* Added switch between types of functions (#528)

* Function IsDataStorageUntouched returns false in case of no data were stored for given account. XXHash lib updated. (#530)

* Limit new outbound connections (#512)

* Module goleveldb updated to latest master.

* Remote address added to log message in receive function.

* Simplification of file descriptors options. Last attempt timestamp stored for known peers and used to sort them while establishing new connection.

* Fixed max FDs limits for macOS

* Log messages level changed

* Fixed not returning bytes to pool after network message header reading.

* MassTransfer V3 enabled

* Improved warning message on failure to resend message to FSM.
Cancel peer's context before closing network connection handler.

* Peer's channels capacities reduced by 3 times and FSM channels capacities reduced by 10.

* Channels capacity adjusted. Warnings on message drops converted to debug messages.

* Function SetMaxFDs renamed to RaiseMaxFDs

* Network bytes channels capacity increased

* Homegrown bytespool replaced with bytebufferpool package.

* Separate bytepool removed, the global one is used everywhere.

* Added version information to peers storage. (#532)

* Two small patches to let gowaves build on 32 bit systems (#533) (#536)

* specify uint64 for constants

* tag uint32s for 32 bit builds

* camelCase for variables

Co-authored-by: Lindsey Gray <lindsey.gray@gmail.com>

* Updated CI build job. Added 32bit and 64bit architectures. (#535)

* Added a change of balance for reissue and burn actions (#541)

* Decimal library updated. Test on error in pow function then x was 0.5 added. (#542)

Go 1.17 derictives supported.

* Turned off payments validation (#539)

* Turned off payments validation

* Settings for temporary disabling payments validaion added. Recipient alias resolution fixed.

* Fixed reissue

* Tests on issue, burn and reissue in invoke added.
Test on using aliases whlie doing invokes added.
Test on activation of internal transfers and payments added.

* Error messages fixed

Co-authored-by: Alexey Kiselev <alexey.kiselev@gmail.com>

* Signature of function AssetInfoByID improved by returning errs.UnknownAsset error for non-existent asset.

* New version of limit for total size of data entries produced by script invocation added. (#543)

* Fixed bug in 'bytes' method of 'assetScriptKey' structure. Incremented 'StateVersion' constant.

* Fixed bug with 'accountScriptHash' calculation.

* Fixed bug with 'accountsDataStorage' on TestNet height 1638728, transaction 'BWqhPWQfa2u7bZ8SsB6P9MvKDNmFgkjBude2RbeusAa4'.

* Changed state internal methods. Now this methods accept 'crypto.Digest' instead of '[]bytes' when asset needs.

* Changed 'NFTList' method in 'StateInfo' interface. Also changed 'NewestAccountBalance' method in 'SmartState' interface.

* Changed 'AccountBalance' method in 'StateInfo' and 'State' interfaces.

* Fixed bug in 'extractAsset' function.

* WIP: crypto.Digest to proto.AssetID.

* Changed 'StateInfo' and 'SmartState' interfaces. Now 'AccountBalance' and 'NewestAccountBalance' can be retrieved by '*proto.AssetID'.

* Removed 'crypto.Digest' from general purpose state interfaces.

* Deleted 'String' method of 'AssetID'.

* Reverted changes for 'SmartState' interface.

* Removed 'proto.WavesAddress' usage as state key. Now keys uses 'proto.AddressID'.

* Changed 'byteKey' function signature.

* Incremented 'StateVersion' constant.

* Issues fixed. PR #548.

* Changed 'byteKey' utility function in 'state' package.

Co-authored-by: Alexey Kiselev <alexey.kiselev@gmail.com>
Co-authored-by: Alexandr Dolgavin <41806871+esuwu@users.noreply.github.com>
Co-authored-by: Lindsey Gray <lindsey.gray@gmail.com>
  • Loading branch information
4 people authored Nov 10, 2021
1 parent 9997601 commit df751bc
Show file tree
Hide file tree
Showing 96 changed files with 2,061 additions and 1,969 deletions.
2 changes: 1 addition & 1 deletion cmd/chaincmp/chaincmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ type nodeBlockInfo struct {
id int
blockID proto.BlockID
height uint64
generator proto.Address
generator proto.WavesAddress
blockTime uint64
err error
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/wmd/internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ func (a *DataFeedAPI) convertToTickerInfo(aa, pa *data.AssetInfo, aaBalance, paB
return data.NewTickerInfo(sb.String(), *aa, *pa, aaBalance, paBalance, c)
}

func (a *DataFeedAPI) getIssuerBalance(issuer proto.Address, asset crypto.Digest) (uint64, error) {
func (a *DataFeedAPI) getIssuerBalance(issuer proto.WavesAddress, asset crypto.Digest) (uint64, error) {
if bytes.Equal(issuer[:], data.WavesIssuerAddress[:]) {
return 0, nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/wmd/internal/data/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

var (
WavesID = crypto.Digest{}
WavesIssuerAddress = proto.Address{}
WavesIssuerAddress = proto.WavesAddress{}
WavesAssetInfo = AssetInfo{ID: WavesID, Name: "WAVES", IssuerAddress: WavesIssuerAddress, Decimals: 8, Reissuable: false, Supply: 10000000000000000}
)

type AssetInfo struct {
ID crypto.Digest
Name string
IssuerAddress proto.Address
IssuerAddress proto.WavesAddress
Decimals uint8
Reissuable bool
Supply uint64
Expand Down
4 changes: 2 additions & 2 deletions cmd/wmd/internal/data/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ type Symbols struct {
tickers map[string]crypto.Digest
tokens map[crypto.Digest]string
mu sync.RWMutex
oracle proto.Address
oracle proto.WavesAddress
scheme byte
}

func NewSymbolsFromFile(name string, oracle proto.Address, scheme byte) (*Symbols, error) {
func NewSymbolsFromFile(name string, oracle proto.WavesAddress, scheme byte) (*Symbols, error) {
wrapError := func(err error) error {
return errors.Wrapf(err, "failed to import symbols from file '%s'", name)
}
Expand Down
26 changes: 13 additions & 13 deletions cmd/wmd/internal/data/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type Trade struct {
PriceAsset crypto.Digest
TransactionID crypto.Digest
OrderType proto.OrderType
Buyer proto.Address
Seller proto.Address
Matcher proto.Address
Buyer proto.WavesAddress
Seller proto.WavesAddress
Matcher proto.WavesAddress
Price uint64
Amount uint64
Timestamp uint64
Expand Down Expand Up @@ -60,7 +60,7 @@ func NewTradeFromExchangeWithSig(scheme byte, tx *proto.ExchangeWithSig) (Trade,
func NewTradeFromExchangeWithProofs(scheme byte, tx *proto.ExchangeWithProofs) (Trade, error) {
wrapError := func(err error) error { return errors.Wrap(err, "failed to convert ExchangeWithProofs to Trade") }
var buyTS, sellTS uint64
var buyer, seller, matcher proto.Address
var buyer, seller, matcher proto.WavesAddress
var err error
var amountAsset, priceAsset crypto.Digest

Expand Down Expand Up @@ -166,15 +166,15 @@ func (t *Trade) UnmarshalBinary(data []byte) error {

// TradeInfo is an API representation of the Trade
type TradeInfo struct {
Timestamp uint64 `json:"timestamp"`
ID crypto.Digest `json:"id"`
Confirmed bool `json:"confirmed"`
OrderType proto.OrderType `json:"type"`
Price Decimal `json:"price"`
Amount Decimal `json:"amount"`
Buyer proto.Address `json:"buyer"`
Seller proto.Address `json:"seller"`
Matcher proto.Address `json:"matcher"`
Timestamp uint64 `json:"timestamp"`
ID crypto.Digest `json:"id"`
Confirmed bool `json:"confirmed"`
OrderType proto.OrderType `json:"type"`
Price Decimal `json:"price"`
Amount Decimal `json:"amount"`
Buyer proto.WavesAddress `json:"buyer"`
Seller proto.WavesAddress `json:"seller"`
Matcher proto.WavesAddress `json:"matcher"`
}

func NewTradeInfo(trade Trade, amountAssetPrecision, priceAssetPrecision uint) TradeInfo {
Expand Down
4 changes: 2 additions & 2 deletions cmd/wmd/internal/data/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

type AliasBind struct {
Alias proto.Alias
Address proto.Address
Address proto.WavesAddress
}

type Account struct {
Address proto.Address
Address proto.WavesAddress
Alias proto.Alias
}

Expand Down
38 changes: 19 additions & 19 deletions cmd/wmd/internal/state/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,42 @@ import (
)

type assetBalanceKey struct {
address proto.Address
address proto.WavesAddress
asset crypto.Digest
}

func (k *assetBalanceKey) bytes() []byte {
buf := make([]byte, 1+proto.AddressSize+crypto.DigestSize)
buf := make([]byte, 1+proto.WavesAddressSize+crypto.DigestSize)
buf[0] = assetBalanceKeyPrefix
copy(buf[1:], k.address[:])
copy(buf[1+proto.AddressSize:], k.asset[:])
copy(buf[1+proto.WavesAddressSize:], k.asset[:])
return buf
}

type assetBalanceHistoryKey struct {
height uint32
address proto.Address
address proto.WavesAddress
asset crypto.Digest
}

func (k assetBalanceHistoryKey) bytes() []byte {
buf := make([]byte, 1+4+proto.AddressSize+crypto.DigestSize)
buf := make([]byte, 1+4+proto.WavesAddressSize+crypto.DigestSize)
buf[0] = assetBalanceHistoryKeyPrefix
binary.BigEndian.PutUint32(buf[1:], k.height)
copy(buf[1+4:], k.address[:])
copy(buf[1+4+proto.AddressSize:], k.asset[:])
copy(buf[1+4+proto.WavesAddressSize:], k.asset[:])
return buf
}

func (k *assetBalanceHistoryKey) fromBytes(data []byte) error {
if l := len(data); l < 1+4+proto.AddressSize+crypto.DigestSize {
if l := len(data); l < 1+4+proto.WavesAddressSize+crypto.DigestSize {
return errors.Errorf("%d bytes is not enough for assetBalanceHistoryKey", l)
}
data = data[1:]
k.height = binary.BigEndian.Uint32(data)
data = data[4:]
copy(k.address[:], data[:proto.AddressSize])
data = data[proto.AddressSize:]
copy(k.address[:], data[:proto.WavesAddressSize])
data = data[proto.WavesAddressSize:]
copy(k.asset[:], data[:crypto.DigestSize])
return nil
}
Expand Down Expand Up @@ -78,15 +78,15 @@ func (c *balanceDiff) fromBytes(data []byte) error {
}

type assetIssuerKey struct {
address proto.Address
address proto.WavesAddress
asset crypto.Digest
}

func (k *assetIssuerKey) bytes() []byte {
buf := make([]byte, 1+proto.AddressSize+crypto.DigestSize)
buf := make([]byte, 1+proto.WavesAddressSize+crypto.DigestSize)
buf[0] = assetIssuerKeyPrefix
copy(buf[1:], k.address[:])
copy(buf[1+proto.AddressSize:], k.asset[:])
copy(buf[1+proto.WavesAddressSize:], k.asset[:])
return buf
}

Expand Down Expand Up @@ -128,14 +128,14 @@ func (k *assetHistoryKey) fromBytes(data []byte) error {

type asset struct {
name string
issuer proto.Address
issuer proto.WavesAddress
decimals uint8
reissuable bool
sponsored bool
supply uint64
}

const assetInfoSize = 2 + proto.AddressSize + 1 + 1 + 1 + 8
const assetInfoSize = 2 + proto.WavesAddressSize + 1 + 1 + 1 + 8

func newAssetInfoFromIssueChange(scheme byte, ch data.IssueChange) (asset, error) {
a, err := proto.NewAddressFromPublicKey(scheme, ch.Issuer)
Expand All @@ -152,7 +152,7 @@ func (a asset) bytes() []byte {
proto.PutStringWithUInt16Len(buf[p:], a.name)
p += 2 + nl
copy(buf[p:], a.issuer[:])
p += proto.AddressSize
p += proto.WavesAddressSize
buf[p] = a.decimals
p++
proto.PutBool(buf[p:], a.reissuable)
Expand All @@ -173,8 +173,8 @@ func (a *asset) fromBytes(data []byte) error {
}
a.name = s
data = data[2+len(s):]
copy(a.issuer[:], data[:proto.AddressSize])
data = data[proto.AddressSize:]
copy(a.issuer[:], data[:proto.WavesAddressSize])
data = data[proto.WavesAddressSize:]
a.decimals = data[0]
data = data[1:]
a.reissuable, err = proto.Bool(data)
Expand Down Expand Up @@ -338,7 +338,7 @@ func rollbackAssets(snapshot *leveldb.Snapshot, batch *leveldb.Batch, removeHeig
return nil
}

func updateBalanceAndHistory(bs *blockState, batch *leveldb.Batch, height uint32, addr proto.Address, asset crypto.Digest, in, out uint64) error {
func updateBalanceAndHistory(bs *blockState, batch *leveldb.Batch, height uint32, addr proto.WavesAddress, asset crypto.Digest, in, out uint64) error {
//get current state of balance
balance, k, err := bs.balance(addr, asset)
if err != nil {
Expand All @@ -362,7 +362,7 @@ func updateBalanceAndHistory(bs *blockState, batch *leveldb.Batch, height uint32
func putAccounts(bs *blockState, batch *leveldb.Batch, height uint32, accountChanges []data.AccountChange) error {
for _, u := range accountChanges {
// get the address bytes from the account or from state
var addr proto.Address
var addr proto.WavesAddress
if !bytes.Equal(u.Account.Address[:], emptyAddress[:]) {
addr = u.Account.Address
} else {
Expand Down
23 changes: 12 additions & 11 deletions cmd/wmd/internal/state/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package state
import (
"bytes"
"encoding/binary"
"math"

"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
"github.com/wavesplatform/gowaves/cmd/wmd/internal/data"
"github.com/wavesplatform/gowaves/pkg/proto"
"math"
)

var (
emptyAddress = proto.Address{}
emptyAddress = proto.WavesAddress{}
)

type aliasKey struct {
Expand Down Expand Up @@ -44,23 +45,23 @@ func (k aliasHistoryKey) bytes() []byte {
}

type aliasChange struct {
prev proto.Address
curr proto.Address
prev proto.WavesAddress
curr proto.WavesAddress
}

func (c aliasChange) bytes() []byte {
buf := make([]byte, 2*proto.AddressSize)
buf := make([]byte, 2*proto.WavesAddressSize)
copy(buf, c.prev[:])
copy(buf[proto.AddressSize:], c.curr[:])
copy(buf[proto.WavesAddressSize:], c.curr[:])
return buf
}

func (c *aliasChange) fromBytes(data []byte) error {
if l := len(data); l < 2*proto.AddressSize {
return errors.Errorf("%d is not enough bytes for aliasChange, expected %d", l, 2*proto.AddressSize)
if l := len(data); l < 2*proto.WavesAddressSize {
return errors.Errorf("%d is not enough bytes for aliasChange, expected %d", l, 2*proto.WavesAddressSize)
}
copy(c.prev[:], data[:proto.AddressSize])
copy(c.curr[:], data[proto.AddressSize:2*proto.AddressSize])
copy(c.prev[:], data[:proto.WavesAddressSize])
copy(c.curr[:], data[proto.WavesAddressSize:2*proto.WavesAddressSize])
return nil
}

Expand Down Expand Up @@ -88,7 +89,7 @@ func rollbackAliases(snapshot *leveldb.Snapshot, batch *leveldb.Batch, removeHei
l := uint32Key{aliasHistoryKeyPrefix, math.MaxInt32}
it := snapshot.NewIterator(&util.Range{Start: s.bytes(), Limit: l.bytes()}, nil)
remove := make([]proto.Alias, 0)
downgrade := make(map[proto.Alias]proto.Address)
downgrade := make(map[proto.Alias]proto.WavesAddress)
if it.Last() {
for {
key := it.Key()
Expand Down
12 changes: 6 additions & 6 deletions cmd/wmd/internal/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type blockState struct {
snapshot *leveldb.Snapshot
aliasBindings map[proto.Alias]proto.Address
aliasBindings map[proto.Alias]proto.WavesAddress
balances map[assetBalanceKey]uint64
issuers map[assetIssuerKey]struct{}
assets map[assetKey]asset
Expand All @@ -24,7 +24,7 @@ type blockState struct {
func newBlockState(snapshot *leveldb.Snapshot) *blockState {
return &blockState{
snapshot: snapshot,
aliasBindings: make(map[proto.Alias]proto.Address),
aliasBindings: make(map[proto.Alias]proto.WavesAddress),
balances: make(map[assetBalanceKey]uint64),
issuers: make(map[assetIssuerKey]struct{}),
assets: make(map[assetKey]asset),
Expand All @@ -34,8 +34,8 @@ func newBlockState(snapshot *leveldb.Snapshot) *blockState {
}
}

func (s *blockState) addressByAlias(alias proto.Alias) (proto.Address, bool, error) {
var a proto.Address
func (s *blockState) addressByAlias(alias proto.Alias) (proto.WavesAddress, bool, error) {
var a proto.WavesAddress
a, ok := s.aliasBindings[alias]
if !ok {
k := aliasKey{prefix: aliasToAddressKeyPrefix, alias: alias}
Expand All @@ -54,7 +54,7 @@ func (s *blockState) addressByAlias(alias proto.Alias) (proto.Address, bool, err
return a, true, nil
}

func (s *blockState) isIssuer(address proto.Address, asset crypto.Digest) (bool, error) {
func (s *blockState) isIssuer(address proto.WavesAddress, asset crypto.Digest) (bool, error) {
k := assetIssuerKey{address: address, asset: asset}
_, ok := s.issuers[k]
if !ok {
Expand All @@ -69,7 +69,7 @@ func (s *blockState) isIssuer(address proto.Address, asset crypto.Digest) (bool,
return true, nil
}

func (s *blockState) balance(address proto.Address, asset crypto.Digest) (uint64, assetBalanceKey, error) {
func (s *blockState) balance(address proto.WavesAddress, asset crypto.Digest) (uint64, assetBalanceKey, error) {
k := assetBalanceKey{address: address, asset: asset}
var b uint64
b, ok := s.balances[k]
Expand Down
4 changes: 2 additions & 2 deletions cmd/wmd/internal/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (s *Storage) TradesRange(amountAsset, priceAsset crypto.Digest, from, to ui
return trades(snapshot, amountAsset, priceAsset, from, to, maxLimit)
}

func (s *Storage) TradesByAddress(amountAsset, priceAsset crypto.Digest, address proto.Address, limit int) ([]data.Trade, error) {
func (s *Storage) TradesByAddress(amountAsset, priceAsset crypto.Digest, address proto.WavesAddress, limit int) ([]data.Trade, error) {
snapshot, err := s.db.GetSnapshot()
if err != nil {
return nil, err
Expand Down Expand Up @@ -298,7 +298,7 @@ func (s *Storage) BlockID(height int) (proto.BlockID, error) {
return b, nil
}

func (s *Storage) IssuerBalance(issuer proto.Address, asset crypto.Digest) (uint64, error) {
func (s *Storage) IssuerBalance(issuer proto.WavesAddress, asset crypto.Digest) (uint64, error) {
snapshot, err := s.db.GetSnapshot()
if err != nil {
return 0, err
Expand Down
Loading

0 comments on commit df751bc

Please sign in to comment.