mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
5bf00db2c9
The logic here is that we'll have all binary encoding/decoding done via our io package, which simplifies error handling. This functionality doesn't belong to util, so it's moved. This also expands BufBinWriter with Reset() method to fit the needs of core package.
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
// SpentCoins is mapping between transactions and their spent
|
|
// coin state.
|
|
type SpentCoins map[util.Uint256]*SpentCoinState
|
|
|
|
func (s SpentCoins) getAndUpdate(store storage.Store, hash util.Uint256) (*SpentCoinState, error) {
|
|
if spent, ok := s[hash]; ok {
|
|
return spent, nil
|
|
}
|
|
|
|
spent := &SpentCoinState{}
|
|
key := storage.AppendPrefix(storage.STSpentCoin, hash.BytesReverse())
|
|
if b, err := store.Get(key); err == nil {
|
|
if err := spent.DecodeBinary(io.NewBinReaderFromBuf(b)); err != nil {
|
|
return nil, fmt.Errorf("failed to decode (UnspentCoinState): %s", err)
|
|
}
|
|
} else {
|
|
spent = &SpentCoinState{
|
|
items: make(map[uint16]uint32),
|
|
}
|
|
}
|
|
|
|
s[hash] = spent
|
|
return spent, nil
|
|
}
|
|
|
|
func (s SpentCoins) commit(b storage.Batch) error {
|
|
buf := io.NewBufBinWriter()
|
|
for hash, state := range s {
|
|
if err := state.EncodeBinary(buf.BinWriter); err != nil {
|
|
return err
|
|
}
|
|
key := storage.AppendPrefix(storage.STSpentCoin, hash.BytesReverse())
|
|
b.Put(key, buf.Bytes())
|
|
buf.Reset()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SpentCoinState represents the state of a spent coin.
|
|
type SpentCoinState struct {
|
|
txHash util.Uint256
|
|
txHeight uint32
|
|
|
|
// A mapping between the index of the prevIndex and block height.
|
|
items map[uint16]uint32
|
|
}
|
|
|
|
// NewSpentCoinState returns a new SpentCoinState object.
|
|
func NewSpentCoinState(hash util.Uint256, height uint32) *SpentCoinState {
|
|
return &SpentCoinState{
|
|
txHash: hash,
|
|
txHeight: height,
|
|
items: make(map[uint16]uint32),
|
|
}
|
|
}
|
|
|
|
// DecodeBinary implements the Payload interface.
|
|
func (s *SpentCoinState) DecodeBinary(br *io.BinReader) error {
|
|
br.ReadLE(&s.txHash)
|
|
br.ReadLE(&s.txHeight)
|
|
|
|
s.items = make(map[uint16]uint32)
|
|
lenItems := br.ReadVarUint()
|
|
for i := 0; i < int(lenItems); i++ {
|
|
var (
|
|
key uint16
|
|
value uint32
|
|
)
|
|
br.ReadLE(&key)
|
|
br.ReadLE(&value)
|
|
s.items[key] = value
|
|
}
|
|
return br.Err
|
|
}
|
|
|
|
// EncodeBinary implements the Payload interface.
|
|
func (s *SpentCoinState) EncodeBinary(bw *io.BinWriter) error {
|
|
bw.WriteLE(s.txHash)
|
|
bw.WriteLE(s.txHeight)
|
|
bw.WriteVarUint(uint64(len(s.items)))
|
|
for k, v := range s.items {
|
|
bw.WriteLE(k)
|
|
bw.WriteLE(v)
|
|
}
|
|
return bw.Err
|
|
}
|