Finalized size calculation methodology (#215)

- Simplified Transactioner interface
-  Added size calculation test
- Added utility methods in the address pkg: FromUint160, Uint160Decode
This commit is contained in:
dauTT 2019-03-25 02:04:54 +01:00 committed by decentralisedkev
parent beab4d186f
commit ce1fe72607
4 changed files with 118 additions and 20 deletions

View file

@ -98,7 +98,7 @@ func (c *Chaindb) saveTx(tx transaction.Transactioner, txIndex uint32, blockHash
if err != nil { if err != nil {
return err return err
} }
err = txTable.Put(txHash.Bytes(), tx.Bytes()) err = txTable.Put(txHash.Bytes(), tx.BaseTx().Bytes())
if err != nil { if err != nil {
return err return err
} }
@ -118,7 +118,7 @@ func (c *Chaindb) saveTx(tx transaction.Transactioner, txIndex uint32, blockHash
// Save all of the utxos in a transaction // Save all of the utxos in a transaction
// We do this additional save so that we can form a utxo database // We do this additional save so that we can form a utxo database
// and know when a transaction is a double spend. // and know when a transaction is a double spend.
utxos := tx.UTXOs() utxos := tx.BaseTx().Outputs
for utxoIndex, utxo := range utxos { for utxoIndex, utxo := range utxos {
err := c.saveUTXO(utxo, uint16(utxoIndex), txHash.Bytes(), blockHash) err := c.saveUTXO(utxo, uint16(utxoIndex), txHash.Bytes(), blockHash)
if err != nil { if err != nil {
@ -135,7 +135,7 @@ func (c *Chaindb) saveTx(tx transaction.Transactioner, txIndex uint32, blockHash
// We do this so that once an output has been spent // We do this so that once an output has been spent
// It will be removed from the utxo database and cannot be spent again // It will be removed from the utxo database and cannot be spent again
// If the output was never in the utxo database, this function will return an error // If the output was never in the utxo database, this function will return an error
txos := tx.TXOs() txos := tx.BaseTx().Inputs
for _, txo := range txos { for _, txo := range txos {
err := c.removeUTXO(txo) err := c.removeUTXO(txo)
if err != nil { if err != nil {

File diff suppressed because one or more lines are too long

View file

@ -18,11 +18,8 @@ type decodeExclusiveFields func(br *util.BinReader)
type Transactioner interface { type Transactioner interface {
Encode(w io.Writer) error Encode(w io.Writer) error
Decode(r io.Reader) error Decode(r io.Reader) error
BaseTx() *Base
ID() (util.Uint256, error) ID() (util.Uint256, error)
Bytes() []byte
UTXOs() []*Output
TXOs() []*Input
Witness() []*Witness
} }
// Base transaction is the template for all other transactions // Base transaction is the template for all other transactions
@ -199,17 +196,7 @@ func (b *Base) Bytes() []byte {
return buf.Bytes() return buf.Bytes()
} }
// UTXOs returns the outputs in the tx // BaseTx returns the Base object in a transaction
func (b *Base) UTXOs() []*Output { func (b *Base) BaseTx() *Base {
return b.Outputs return b
}
// TXOs returns the inputs in the tx
func (b *Base) TXOs() []*Input {
return b.Inputs
}
// Witness returns the witnesses in the tx
func (b *Base) Witness() []*Witness {
return b.Witnesses
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"github.com/CityOfZion/neo-go/pkg/crypto/base58" "github.com/CityOfZion/neo-go/pkg/crypto/base58"
"github.com/CityOfZion/neo-go/pkg/wire/util"
) )
// ToScriptHash converts an address to a script hash // ToScriptHash converts an address to a script hash
@ -17,3 +18,21 @@ func ToScriptHash(address string) string {
scriptHash := (decodedAddressAsHex[2:42]) scriptHash := (decodedAddressAsHex[2:42])
return scriptHash return scriptHash
} }
// FromUint160 returns the "NEO address" from the given
// Uint160.
func FromUint160(u util.Uint160) (string, error) {
// Dont forget to prepend the Address version 0x17 (23) A
b := append([]byte{0x17}, u.Bytes()...)
return base58.CheckEncode(b)
}
// Uint160Decode attempts to decode the given NEO address string
// into an Uint160.
func Uint160Decode(s string) (u util.Uint160, err error) {
b, err := base58.CheckDecode(s)
if err != nil {
return u, err
}
return util.Uint160DecodeBytes(b[1:21])
}