mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 03:38:35 +00:00
920d7c610c
Commit 578ac414d4
was wrong in that it saved
only a part of the block, so depending on how you use blockchain, you may
still see that the block was not really processed properly. To really fix it
this commit introduces intermediate storage layer in form of memStore, which
actually is a MemoryStore that supports full Store API (thus easily fitting
into the existing code) and one extension that allows it to flush its data to
some other Store.
It also changes AddBlock() semantics in that it only accepts now successive
blocks, but when it does it guarantees that they're properly added into the
Blockchain and can be referred to in any way. Pending block queing is now
moved into the server (see 8c0c055ac657813fe3ed10257bce199e9527d5ed).
So the only thing done with persist() now is just a move from memStore to
Store which probably should've always been the case (notice also that
previously headers and some other metadata was written into the Store
bypassing caching/batching mechanism thus leading to some inefficiency).
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
// UnspentCoins is mapping between transactions and their unspent
|
|
// coin state.
|
|
type UnspentCoins map[util.Uint256]*UnspentCoinState
|
|
|
|
// getAndUpdate retreives UnspentCoinState from temporary or persistent Store
|
|
// and return it. If it's not present in both stores, returns a new
|
|
// UnspentCoinState.
|
|
func (u UnspentCoins) getAndUpdate(ts storage.Store, ps storage.Store, hash util.Uint256) (*UnspentCoinState, error) {
|
|
if unspent, ok := u[hash]; ok {
|
|
return unspent, nil
|
|
}
|
|
|
|
unspent, err := getUnspentCoinStateFromStore(ts, hash)
|
|
if err != nil {
|
|
if err != storage.ErrKeyNotFound {
|
|
return nil, err
|
|
}
|
|
unspent, err = getUnspentCoinStateFromStore(ps, hash)
|
|
if err != nil {
|
|
if err != storage.ErrKeyNotFound {
|
|
return nil, err
|
|
}
|
|
unspent = &UnspentCoinState{
|
|
states: []CoinState{},
|
|
}
|
|
}
|
|
}
|
|
|
|
u[hash] = unspent
|
|
return unspent, nil
|
|
}
|
|
|
|
// getUnspentCoinStateFromStore retrieves UnspentCoinState from the given store
|
|
func getUnspentCoinStateFromStore(s storage.Store, hash util.Uint256) (*UnspentCoinState, error) {
|
|
unspent := &UnspentCoinState{}
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesReverse())
|
|
if b, err := s.Get(key); err == nil {
|
|
r := io.NewBinReaderFromBuf(b)
|
|
unspent.DecodeBinary(r)
|
|
if r.Err != nil {
|
|
return nil, fmt.Errorf("failed to decode (UnspentCoinState): %s", r.Err)
|
|
}
|
|
} else {
|
|
return nil, err
|
|
}
|
|
return unspent, nil
|
|
}
|
|
|
|
// UnspentCoinState hold the state of a unspent coin.
|
|
type UnspentCoinState struct {
|
|
states []CoinState
|
|
}
|
|
|
|
// NewUnspentCoinState returns a new unspent coin state with N confirmed states.
|
|
func NewUnspentCoinState(n int) *UnspentCoinState {
|
|
u := &UnspentCoinState{
|
|
states: make([]CoinState, n),
|
|
}
|
|
for i := 0; i < n; i++ {
|
|
u.states[i] = CoinStateConfirmed
|
|
}
|
|
return u
|
|
}
|
|
|
|
// commit writes all unspent coin states to the given Batch.
|
|
func (u UnspentCoins) commit(b storage.Batch) error {
|
|
buf := io.NewBufBinWriter()
|
|
for hash, state := range u {
|
|
state.EncodeBinary(buf.BinWriter)
|
|
if buf.Err != nil {
|
|
return buf.Err
|
|
}
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesReverse())
|
|
b.Put(key, buf.Bytes())
|
|
buf.Reset()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeBinary encodes UnspentCoinState to the given BinWriter.
|
|
func (s *UnspentCoinState) EncodeBinary(bw *io.BinWriter) {
|
|
bw.WriteVarUint(uint64(len(s.states)))
|
|
for _, state := range s.states {
|
|
bw.WriteLE(byte(state))
|
|
}
|
|
}
|
|
|
|
// DecodeBinary decodes UnspentCoinState from the given BinReader.
|
|
func (s *UnspentCoinState) DecodeBinary(br *io.BinReader) {
|
|
lenStates := br.ReadVarUint()
|
|
s.states = make([]CoinState, lenStates)
|
|
for i := 0; i < int(lenStates); i++ {
|
|
var state uint8
|
|
br.ReadLE(&state)
|
|
s.states[i] = CoinState(state)
|
|
}
|
|
}
|
|
|
|
// IsDoubleSpend verifies that the input transactions are not double spent.
|
|
func IsDoubleSpend(s storage.Store, tx *transaction.Transaction) bool {
|
|
if len(tx.Inputs) == 0 {
|
|
return false
|
|
}
|
|
|
|
for prevHash, inputs := range tx.GroupInputsByPrevHash() {
|
|
unspent := &UnspentCoinState{}
|
|
key := storage.AppendPrefix(storage.STCoin, prevHash.BytesReverse())
|
|
if b, err := s.Get(key); err == nil {
|
|
r := io.NewBinReaderFromBuf(b)
|
|
unspent.DecodeBinary(r)
|
|
if r.Err != nil {
|
|
return false
|
|
}
|
|
if unspent == nil {
|
|
return true
|
|
}
|
|
|
|
for _, input := range inputs {
|
|
if int(input.PrevIndex) >= len(unspent.states) || unspent.states[input.PrevIndex] == CoinStateSpent {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
}
|