19201dcf52
* Added utility function GetVarSize * 1) Added Size method: this implied that Fixed8 implements now the serializable interface. 2) Added few arithmetic operation (Add, Sub, div): this will be used to calculated networkfeeand feePerByte. Changed return value of the Value() method to int instead of int64. Modified fixed8_test accordingly. * Implemented Size or MarshalJSON method. - Structs accepting the Size method implement the serializable interface. - Structs accepting the MarshalJSON method implements the customized json marshaller interface. * Added fee calculation * Implemented rcp server method GetRawTransaction * Updated Tests * Fixed: 1) NewFixed8 will accept as input int64 2) race condition affecting configDeafault, blockchainDefault * Simplified Size calculation * 1) Removed global variable blockchainDefault, configDefault 2) Extended Blockchainer interface to include the methods: References, FeePerByte, SystemFee, NetworkFee 3) Deleted fee_test.go, fee.go. Moved corresponding methods to blockchain_test.go and blockchain.go respectively 4) Amended tx_raw_output.go * Simplified GetVarSize Method * Replaced ValueAtAndType with ValueWithType * Cosmetic changes + Added test case getrawtransaction_7 * Clean up Print statement * Filled up keys * Aligned verbose logic with the C#-neo implementation * Implemented @Kim requests Refactor server_test.go * Small fixes * Fixed verbose logic Added more tests Cosmetic changes * Replaced assert.NoError with require.NoError * Fixed tests by adding context.Background() as argument * Fixed tests
104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package transaction
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
// Attribute represents a Transaction attribute.
|
|
type Attribute struct {
|
|
Usage AttrUsage
|
|
Data []byte
|
|
}
|
|
|
|
// DecodeBinary implements the Payload interface.
|
|
func (attr *Attribute) DecodeBinary(r io.Reader) error {
|
|
if err := binary.Read(r, binary.LittleEndian, &attr.Usage); err != nil {
|
|
return err
|
|
}
|
|
if attr.Usage == ContractHash ||
|
|
attr.Usage == Vote ||
|
|
(attr.Usage >= Hash1 && attr.Usage <= Hash15) {
|
|
attr.Data = make([]byte, 32)
|
|
return binary.Read(r, binary.LittleEndian, attr.Data)
|
|
}
|
|
if attr.Usage == ECDH02 || attr.Usage == ECDH03 {
|
|
attr.Data = make([]byte, 33)
|
|
attr.Data[0] = byte(attr.Usage)
|
|
return binary.Read(r, binary.LittleEndian, attr.Data[1:])
|
|
}
|
|
if attr.Usage == Script {
|
|
attr.Data = make([]byte, 20)
|
|
return binary.Read(r, binary.LittleEndian, attr.Data)
|
|
}
|
|
if attr.Usage == DescriptionURL {
|
|
attr.Data = make([]byte, 1)
|
|
return binary.Read(r, binary.LittleEndian, attr.Data)
|
|
}
|
|
if attr.Usage == Description || attr.Usage >= Remark {
|
|
lenData := util.ReadVarUint(r)
|
|
attr.Data = make([]byte, lenData)
|
|
return binary.Read(r, binary.LittleEndian, attr.Data)
|
|
}
|
|
return fmt.Errorf("failed decoding TX attribute usage: 0x%2x", attr.Usage)
|
|
}
|
|
|
|
// EncodeBinary implements the Payload interface.
|
|
func (attr *Attribute) EncodeBinary(w io.Writer) error {
|
|
if err := binary.Write(w, binary.LittleEndian, &attr.Usage); err != nil {
|
|
return err
|
|
}
|
|
if attr.Usage == ContractHash ||
|
|
attr.Usage == Vote ||
|
|
(attr.Usage >= Hash1 && attr.Usage <= Hash15) {
|
|
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
}
|
|
if attr.Usage == ECDH02 || attr.Usage == ECDH03 {
|
|
attr.Data[0] = byte(attr.Usage)
|
|
return binary.Write(w, binary.LittleEndian, attr.Data[1:33])
|
|
}
|
|
if attr.Usage == Script {
|
|
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
}
|
|
if attr.Usage == DescriptionURL {
|
|
if err := util.WriteVarUint(w, uint64(len(attr.Data))); err != nil {
|
|
return err
|
|
}
|
|
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
}
|
|
if attr.Usage == Description || attr.Usage >= Remark {
|
|
if err := util.WriteVarUint(w, uint64(len(attr.Data))); err != nil {
|
|
return err
|
|
}
|
|
return binary.Write(w, binary.LittleEndian, attr.Data)
|
|
}
|
|
return fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Usage)
|
|
}
|
|
|
|
// Size returns the size in number bytes of the Attribute
|
|
func (attr *Attribute) Size() int {
|
|
switch attr.Usage {
|
|
case ContractHash, ECDH02, ECDH03, Vote,
|
|
Hash1, Hash2, Hash3, Hash4, Hash5, Hash6, Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15:
|
|
return 33 // uint8 + 32 = size(attrUsage) + 32
|
|
case Script:
|
|
return 21 // uint8 + 20 = size(attrUsage) + 20
|
|
case Description:
|
|
return 2 + len(attr.Data) // uint8 + uint8+ len of data = size(attrUsage) + size(byte) + len of data
|
|
default:
|
|
return 1 + len(attr.Data) // uint8 + len of data = size(attrUsage) + len of data
|
|
}
|
|
}
|
|
|
|
// MarshalJSON implements the json Marschaller interface
|
|
func (attr *Attribute) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(map[string]string{
|
|
"usage": attr.Usage.String(),
|
|
"data": hex.EncodeToString(attr.Data),
|
|
})
|
|
}
|