X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=protocol%2Fbc%2Fasset.go;h=880df75fd1376b3cbc0e1a05f48d84e84d44cc00;hb=ddf068f0b16fbc4cd65fba5c0525a3646267ce72;hp=c330738fbafab60b131bc55762b6f9f841bd0ffd;hpb=8d586403867dfd15629a5bcc58190b1bda74f3b8;p=bytom%2Fbytom-spv.git diff --git a/protocol/bc/asset.go b/protocol/bc/asset.go index c330738f..880df75f 100644 --- a/protocol/bc/asset.go +++ b/protocol/bc/asset.go @@ -1,7 +1,7 @@ package bc import ( - "database/sql/driver" + "encoding/binary" "errors" "io" @@ -9,23 +9,41 @@ import ( "github.com/bytom/encoding/blockchain" ) -// AssetID is the Hash256 of the asset definition. - +// NewAssetID convert byte array to aseet id func NewAssetID(b [32]byte) (a AssetID) { - return AssetID(NewHash(b)) + return AssetID{ + V0: binary.BigEndian.Uint64(b[0:8]), + V1: binary.BigEndian.Uint64(b[8:16]), + V2: binary.BigEndian.Uint64(b[16:24]), + V3: binary.BigEndian.Uint64(b[24:32]), + } } -func (a AssetID) Byte32() (b32 [32]byte) { return Hash(a).Byte32() } -func (a AssetID) MarshalText() ([]byte, error) { return Hash(a).MarshalText() } -func (a *AssetID) UnmarshalText(b []byte) error { return (*Hash)(a).UnmarshalText(b) } -func (a *AssetID) UnmarshalJSON(b []byte) error { return (*Hash)(a).UnmarshalJSON(b) } -func (a AssetID) Bytes() []byte { return Hash(a).Bytes() } -func (a AssetID) Value() (driver.Value, error) { return Hash(a).Value() } -func (a *AssetID) Scan(val interface{}) error { return (*Hash)(a).Scan(val) } -func (a AssetID) WriteTo(w io.Writer) (int64, error) { return Hash(a).WriteTo(w) } +// Byte32 return the byte array representation +func (a AssetID) Byte32() (b32 [32]byte) { return Hash(a).Byte32() } + +// MarshalText satisfies the TextMarshaler interface. +func (a AssetID) MarshalText() ([]byte, error) { return Hash(a).MarshalText() } + +// UnmarshalText satisfies the TextUnmarshaler interface. +func (a *AssetID) UnmarshalText(b []byte) error { return (*Hash)(a).UnmarshalText(b) } + +// UnmarshalJSON satisfies the json.Unmarshaler interface. +func (a *AssetID) UnmarshalJSON(b []byte) error { return (*Hash)(a).UnmarshalJSON(b) } + +// Bytes returns the byte representation. +func (a AssetID) Bytes() []byte { return Hash(a).Bytes() } + +// WriteTo satisfies the io.WriterTo interface. +func (a AssetID) WriteTo(w io.Writer) (int64, error) { return Hash(a).WriteTo(w) } + +// ReadFrom satisfies the io.ReaderFrom interface. func (a *AssetID) ReadFrom(r io.Reader) (int64, error) { return (*Hash)(a).ReadFrom(r) } -func (a *AssetID) IsZero() bool { return (*Hash)(a).IsZero() } +// IsZero tells whether a Asset pointer is nil or points to an all-zero hash. +func (a *AssetID) IsZero() bool { return (*Hash)(a).IsZero() } + +// ComputeAssetID calculate the asset id from AssetDefinition func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) { h := sha3pool.Get256() defer sha3pool.Put256(h) @@ -35,9 +53,9 @@ func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) { return NewAssetID(b) } -func ComputeAssetID(prog []byte, initialBlockID *Hash, vmVersion uint64, data *Hash) AssetID { +// ComputeAssetID implement the assetID calculate logic +func ComputeAssetID(prog []byte, vmVersion uint64, data *Hash) AssetID { def := &AssetDefinition{ - InitialBlockId: initialBlockID, IssuanceProgram: &Program{ VmVersion: vmVersion, Code: prog, @@ -47,10 +65,10 @@ func ComputeAssetID(prog []byte, initialBlockID *Hash, vmVersion uint64, data *H return def.ComputeAssetID() } -func (a *AssetAmount) ReadFrom(r *blockchain.Reader) error { +// ReadFrom read the AssetAmount from the bytes +func (a *AssetAmount) ReadFrom(r *blockchain.Reader) (err error) { var assetID AssetID - _, err := assetID.ReadFrom(r) - if err != nil { + if _, err = assetID.ReadFrom(r); err != nil { return err } a.AssetId = &assetID @@ -58,6 +76,7 @@ func (a *AssetAmount) ReadFrom(r *blockchain.Reader) error { return err } +// WriteTo convert struct to byte and write to io func (a AssetAmount) WriteTo(w io.Writer) (int64, error) { n, err := a.AssetId.WriteTo(w) if err != nil { @@ -67,6 +86,7 @@ func (a AssetAmount) WriteTo(w io.Writer) (int64, error) { return n + int64(n2), err } +// Equal check does two AssetAmount have same assetID and amount func (a *AssetAmount) Equal(other *AssetAmount) (eq bool, err error) { if a == nil || other == nil { return false, errors.New("empty asset amount")