Make nonce on block uint32 instead of bytes
This commit is contained in:
parent
ea97ca98b9
commit
135fbd8973
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
@ -397,10 +398,16 @@ func main() {
|
|||
return s.String()
|
||||
} else if s, ok := val.(types.Hash); ok {
|
||||
return s.String()
|
||||
} else if s, ok := val.(types.Nonce); ok {
|
||||
return hex.EncodeToString(s[:])
|
||||
} else if s, ok := val.([]byte); ok {
|
||||
return hex.EncodeToString(s)
|
||||
} else if s, ok := val.(uint32); ok {
|
||||
var buf [4]byte
|
||||
binary.BigEndian.PutUint32(buf[:], s)
|
||||
return hex.EncodeToString(buf[:])
|
||||
} else if s, ok := val.(uint64); ok {
|
||||
var buf [8]byte
|
||||
binary.BigEndian.PutUint64(buf[:], s)
|
||||
return hex.EncodeToString(buf[:])
|
||||
}
|
||||
|
||||
return val
|
||||
|
|
|
@ -17,7 +17,7 @@ type Block struct {
|
|||
MinorVersion uint8
|
||||
Timestamp uint64
|
||||
PreviousId types.Hash
|
||||
Nonce types.Nonce
|
||||
Nonce uint32
|
||||
|
||||
Coinbase *transaction.CoinbaseTransaction
|
||||
|
||||
|
@ -34,12 +34,12 @@ func (b *Block) MarshalBinary() (buf []byte, err error) {
|
|||
if txBuf, err = b.Coinbase.MarshalBinary(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf = make([]byte, 0, 1+1+binary.MaxVarintLen64+types.HashSize+types.NonceSize+len(txBuf)+binary.MaxVarintLen64+types.HashSize*len(b.Transactions))
|
||||
buf = make([]byte, 0, 1+1+binary.MaxVarintLen64+types.HashSize+4+len(txBuf)+binary.MaxVarintLen64+types.HashSize*len(b.Transactions))
|
||||
buf = append(buf, b.MajorVersion)
|
||||
buf = append(buf, b.MinorVersion)
|
||||
buf = binary.AppendUvarint(buf, b.Timestamp)
|
||||
buf = append(buf, b.PreviousId[:]...)
|
||||
buf = append(buf, b.Nonce[:]...)
|
||||
buf = binary.LittleEndian.AppendUint32(buf, b.Nonce)
|
||||
|
||||
buf = append(buf, txBuf[:]...)
|
||||
|
||||
|
@ -57,10 +57,10 @@ func (b *Block) FromReader(reader readerAndByteReader) (err error) {
|
|||
transactionHash types.Hash
|
||||
)
|
||||
|
||||
if err = binary.Read(reader, binary.BigEndian, &b.MajorVersion); err != nil {
|
||||
if b.MajorVersion, err = reader.ReadByte(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = binary.Read(reader, binary.BigEndian, &b.MinorVersion); err != nil {
|
||||
if b.MinorVersion, err = reader.ReadByte(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ func (b *Block) FromReader(reader readerAndByteReader) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
if _, err = io.ReadFull(reader, b.Nonce[:]); err != nil {
|
||||
if err = binary.Read(reader, binary.LittleEndian, &b.Nonce); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -110,12 +110,12 @@ func (b *Block) UnmarshalBinary(data []byte) error {
|
|||
|
||||
func (b *Block) Header() []byte {
|
||||
//TODO: cache
|
||||
buf := make([]byte, 0, 1+1+binary.MaxVarintLen64+types.HashSize+types.NonceSize+types.HashSize+binary.MaxVarintLen64) //predict its use on HashingBlob
|
||||
buf := make([]byte, 0, 1+1+binary.MaxVarintLen64+types.HashSize+4+types.HashSize+binary.MaxVarintLen64) //predict its use on HashingBlob
|
||||
buf = append(buf, b.MajorVersion)
|
||||
buf = append(buf, b.MinorVersion)
|
||||
buf = binary.AppendUvarint(buf, b.Timestamp)
|
||||
buf = append(buf, b.PreviousId[:]...)
|
||||
buf = append(buf, b.Nonce[:]...)
|
||||
buf = binary.LittleEndian.AppendUint32(buf, b.Nonce)
|
||||
|
||||
return buf
|
||||
}
|
||||
|
@ -125,12 +125,12 @@ func (b *Block) SideChainHashingBlob() (buf []byte, err error) {
|
|||
if txBuf, err = b.Coinbase.SideChainHashingBlob(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf = make([]byte, 0, 1+1+binary.MaxVarintLen64+types.HashSize+types.NonceSize+len(txBuf)+binary.MaxVarintLen64+types.HashSize*len(b.Transactions))
|
||||
buf = make([]byte, 0, 1+1+binary.MaxVarintLen64+types.HashSize+4+len(txBuf)+binary.MaxVarintLen64+types.HashSize*len(b.Transactions))
|
||||
buf = append(buf, b.MajorVersion)
|
||||
buf = append(buf, b.MinorVersion)
|
||||
buf = binary.AppendUvarint(buf, b.Timestamp)
|
||||
buf = append(buf, b.PreviousId[:]...)
|
||||
buf = append(buf, make([]byte, len(b.Nonce[:]))...) //replaced
|
||||
buf = binary.LittleEndian.AppendUint32(buf, 0) //replaced
|
||||
|
||||
buf = append(buf, txBuf[:]...)
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ func (c *CoinbaseTransaction) FromReader(reader readerAndByteReader) (err error)
|
|||
txExtraSize uint64
|
||||
)
|
||||
|
||||
if err = binary.Read(reader, binary.BigEndian, &c.Version); err != nil {
|
||||
if c.Version, err = reader.ReadByte(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -59,11 +59,11 @@ func (c *CoinbaseTransaction) FromReader(reader readerAndByteReader) (err error)
|
|||
return err
|
||||
}
|
||||
|
||||
if err = binary.Read(reader, binary.BigEndian, &c.InputCount); err != nil {
|
||||
if c.InputCount, err = reader.ReadByte(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = binary.Read(reader, binary.BigEndian, &c.InputType); err != nil {
|
||||
if c.InputType, err = reader.ReadByte(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ const TxExtraPaddingMaxCount = 255
|
|||
const TxExtraNonceMaxCount = 255
|
||||
const TxExtraAdditionalPubKeysMaxCount = 4096
|
||||
|
||||
const TxExtraTemplateNonceSize = 4
|
||||
|
||||
type ExtraTags []ExtraTag
|
||||
|
||||
type ExtraTag struct {
|
||||
|
@ -103,8 +105,9 @@ func (t *ExtraTag) SideChainHashingBlob() ([]byte, error) {
|
|||
buf = append(buf, make([]byte, len(t.Data))...)
|
||||
} else if t.Tag == TxExtraTagNonce {
|
||||
b := make([]byte, len(t.Data))
|
||||
if len(t.Data) > 4 {
|
||||
copy(b[4:], t.Data[4:])
|
||||
//Replace only the first four bytes
|
||||
if len(t.Data) > TxExtraTemplateNonceSize {
|
||||
copy(b[TxExtraTemplateNonceSize:], t.Data[TxExtraTemplateNonceSize:])
|
||||
}
|
||||
buf = append(buf, b...)
|
||||
} else {
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
|
||||
const HashSize = 32
|
||||
const DifficultySize = 16
|
||||
const NonceSize = 4
|
||||
|
||||
type Hash [HashSize]byte
|
||||
|
||||
|
@ -105,5 +104,3 @@ func (d Difficulty) String() string {
|
|||
d.ReverseBytes().PutBytes(buf[:])
|
||||
return hex.EncodeToString(buf[:])
|
||||
}
|
||||
|
||||
type Nonce [NonceSize]byte
|
||||
|
|
Loading…
Reference in a new issue