mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-30 19:33:36 +00:00
095653af23
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold' * Added new files: * Added new method: CompareTo * Fixed empty Slice case * Added new methods: LessThan, GreaterThan, Equal, CompareTo * Added new method: InputIntersection * Added MaxTransactionSize, GroupOutputByAssetID * Added ned method: ScriptHash * Added new method: IsDoubleSpend * Refactor blockchainer, Added Feer interface, Verify and GetMemPool method * 1) Added MemPool 2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool * Added new methods: RelayTxn, RelayDirectly * Fixed tests * Implemented RPC server method sendrawtransaction * Refactor getrawtransaction, sendrawtransaction in separate methods * Moved 'secondsPerBlock' to config file * Implemented Kim suggestions: 1) Fixed data race issues 2) refactor Verify method 3) Get rid of unused InputIntersection method due to refactoring Verify method 4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135 5) minor simplications of the code * Fixed minor issues related to 1) space 2) getter methods do not need pointer on the receiver 3) error message 4) refactoring CompareTo method in uint256.go * Fixed small issues * Use sync.RWMutex instead of sync.Mutex * Refined (R)Lock/(R)Unlock * return error instead of bool in Verify methods
123 lines
3.1 KiB
Go
123 lines
3.1 KiB
Go
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
// UnspentCoins is mapping between transactions and their unspent
|
|
// coin state.
|
|
type UnspentCoins map[util.Uint256]*UnspentCoinState
|
|
|
|
func (u UnspentCoins) getAndUpdate(s storage.Store, hash util.Uint256) (*UnspentCoinState, error) {
|
|
if unspent, ok := u[hash]; ok {
|
|
return unspent, nil
|
|
}
|
|
|
|
unspent := &UnspentCoinState{}
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesReverse())
|
|
if b, err := s.Get(key); err == nil {
|
|
if err := unspent.DecodeBinary(bytes.NewReader(b)); err != nil {
|
|
return nil, fmt.Errorf("failed to decode (UnspentCoinState): %s", err)
|
|
}
|
|
} else {
|
|
unspent = &UnspentCoinState{
|
|
states: []CoinState{},
|
|
}
|
|
}
|
|
|
|
u[hash] = unspent
|
|
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 := new(bytes.Buffer)
|
|
for hash, state := range u {
|
|
if err := state.EncodeBinary(buf); err != nil {
|
|
return err
|
|
}
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesReverse())
|
|
b.Put(key, buf.Bytes())
|
|
buf.Reset()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeBinary encodes UnspentCoinState to the given io.Writer.
|
|
func (s *UnspentCoinState) EncodeBinary(w io.Writer) error {
|
|
if err := util.WriteVarUint(w, uint64(len(s.states))); err != nil {
|
|
return err
|
|
}
|
|
for _, state := range s.states {
|
|
if err := binary.Write(w, binary.LittleEndian, byte(state)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DecodeBinary decodes UnspentCoinState from the given io.Reader.
|
|
func (s *UnspentCoinState) DecodeBinary(r io.Reader) error {
|
|
lenStates := util.ReadVarUint(r)
|
|
s.states = make([]CoinState, lenStates)
|
|
for i := 0; i < int(lenStates); i++ {
|
|
var state uint8
|
|
if err := binary.Read(r, binary.LittleEndian, &state); err != nil {
|
|
return err
|
|
}
|
|
s.states[i] = CoinState(state)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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 {
|
|
if err := unspent.DecodeBinary(bytes.NewReader(b)); 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
|
|
}
|