Skip to content

Commit

Permalink
Migrating SHA256Bytes to Checksum256 for consistency.
Browse files Browse the repository at this point in the history
Type alias kept for the time being.  Please alter your code.
  • Loading branch information
abourget committed Nov 9, 2018
1 parent 91879e8 commit 92c2bf6
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 116 deletions.
2 changes: 1 addition & 1 deletion actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Action struct {
ActionData
}

func (a Action) Digest() SHA256Bytes {
func (a Action) Digest() Checksum256 {
toEat := jsonActionToServer{
Account: a.Account,
Name: a.Name,
Expand Down
8 changes: 4 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ func (api *API) GetCode(account AccountName) (out *GetCodeResp, err error) {
return
}

func (api *API) GetCodeHash(account AccountName) (out SHA256Bytes, err error) {
func (api *API) GetCodeHash(account AccountName) (out Checksum256, err error) {
resp := GetCodeHashResp{}
if err = api.call("chain", "get_code_hash", M{"account_name": account}, &resp); err != nil {
return
}

buffer, err := hex.DecodeString(resp.CodeHash)
return SHA256Bytes(buffer), err
return Checksum256(buffer), err
}

func (api *API) GetABI(account AccountName) (out *GetABIResp, err error) {
Expand Down Expand Up @@ -323,7 +323,7 @@ func (api *API) SignPushActionsWithOpts(actions []*Action, opts *TxOptions) (out

// SignPushTransaction will sign a transaction and submit it to the
// chain.
func (api *API) SignPushTransaction(tx *Transaction, chainID SHA256Bytes, compression CompressionType) (out *PushTransactionFullResp, err error) {
func (api *API) SignPushTransaction(tx *Transaction, chainID Checksum256, compression CompressionType) (out *PushTransactionFullResp, err error) {
_, packed, err := api.SignTransaction(tx, chainID, compression)
if err != nil {
return nil, err
Expand All @@ -342,7 +342,7 @@ func (api *API) SignPushTransaction(tx *Transaction, chainID SHA256Bytes, compre
//
// To sign a transaction, you need a Signer defined on the `API`
// object. See SetSigner.
func (api *API) SignTransaction(tx *Transaction, chainID SHA256Bytes, compression CompressionType) (*SignedTransaction, *PackedTransaction, error) {
func (api *API) SignTransaction(tx *Transaction, chainID Checksum256, compression CompressionType) (*SignedTransaction, *PackedTransaction, error) {
if api.Signer == nil {
return nil, nil, fmt.Errorf("no Signer configured")
}
Expand Down
12 changes: 6 additions & 6 deletions bnet/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type Hello struct {
Password string `json:"password"`
Agent string `json:"agent"`
ProtocolVersion string `json:"protocol_version"`
ChainID eos.SHA256Bytes `json:"chain_id"`
ChainID eos.Checksum256 `json:"chain_id"`
RequestTransactions eos.Bool `json:"request_transactions"`
LastIrreversibleBlockNum uint32 `json:"last_irr_block_num"`
PendingBlockIDs []eos.SHA256Bytes `json:"pending_block_ids"`
PendingBlockIDs []eos.Checksum256 `json:"pending_block_ids"`
}

/**
Expand All @@ -41,7 +41,7 @@ type Hello struct {
* and informs a peer not to send this message.
*/
type TransactionNotice struct {
SignedTransactionIDs []eos.SHA256Bytes ///< hash of trx + sigs
SignedTransactionIDs []eos.Checksum256 ///< hash of trx + sigs
}

/**
Expand All @@ -51,18 +51,18 @@ type TransactionNotice struct {
* and informs the remote peer that there is no need to send this block.
*/
type BlockNotice struct {
BlockIDs []eos.SHA256Bytes `json:"block_ids"`
BlockIDs []eos.Checksum256 `json:"block_ids"`
}

type Ping struct {
Sent eos.Tstamp `json:"sent"`
Code eos.SHA256Bytes `json:"code"`
Code eos.Checksum256 `json:"code"`
LastIrreversibleBlock uint32 `json:"lib"` /// last irreversible block
}

type Pong struct {
Sent eos.Tstamp `json:"sent"`
Code eos.SHA256Bytes `json:"code"`
Code eos.Checksum256 `json:"code"`
}

// Also use `eos.SignedBlock`
Expand Down
26 changes: 4 additions & 22 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var TypeSize = struct {
UInt128 int
Float32 int
Float64 int
SHA256Bytes int
Checksum160 int
Checksum256 int
Checksum512 int
Expand All @@ -52,7 +51,6 @@ var TypeSize = struct {
UInt128: 16,
Float32: 4,
Float64: 8,
SHA256Bytes: 32,
Checksum160: 20,
Checksum256: 32,
Checksum512: 64,
Expand Down Expand Up @@ -216,9 +214,9 @@ func (d *Decoder) Decode(v interface{}) (err error) {
data, err = d.ReadByteArray()
rv.SetBytes(data)
return
case *SHA256Bytes:
var s SHA256Bytes
s, err = d.ReadSHA256Bytes()
case *Checksum256:
var s Checksum256
s, err = d.ReadChecksum256()
rv.SetBytes(s)
return
case *ecc.PublicKey:
Expand Down Expand Up @@ -268,7 +266,7 @@ func (d *Decoder) Decode(v interface{}) (err error) {
Logger.Decoder.Print(fmt.Sprintf("Type byte value : %d", t))

if t == 0 {
id, e := d.ReadSHA256Bytes()
id, e := d.ReadChecksum256()
if err != nil {
err = fmt.Errorf("decode: TransactionWithID failed to read id: %s", e)
return
Expand Down Expand Up @@ -632,21 +630,7 @@ func (d *Decoder) ReadString() (out string, err error) {
return
}

func (d *Decoder) ReadSHA256Bytes() (out SHA256Bytes, err error) {

if d.remaining() < TypeSize.SHA256Bytes {
err = fmt.Errorf("sha256 required [%d] bytes, remaining [%d]", TypeSize.SHA256Bytes, d.remaining())
return
}

out = SHA256Bytes(d.data[d.pos : d.pos+TypeSize.SHA256Bytes])
d.pos += TypeSize.SHA256Bytes
Logger.Decoder.Print(fmt.Sprintf("readSHA256Bytes [%s]", hex.EncodeToString(out)))
return
}

func (d *Decoder) ReadChecksum160() (out Checksum160, err error) {

if d.remaining() < TypeSize.Checksum160 {
err = fmt.Errorf("checksum 160 required [%d] bytes, remaining [%d]", TypeSize.Checksum160, d.remaining())
return
Expand All @@ -659,7 +643,6 @@ func (d *Decoder) ReadChecksum160() (out Checksum160, err error) {
}

func (d *Decoder) ReadChecksum256() (out Checksum256, err error) {

if d.remaining() < TypeSize.Checksum256 {
err = fmt.Errorf("checksum 256 required [%d] bytes, remaining [%d]", TypeSize.Checksum256, d.remaining())
return
Expand All @@ -672,7 +655,6 @@ func (d *Decoder) ReadChecksum256() (out Checksum256, err error) {
}

func (d *Decoder) ReadChecksum512() (out Checksum512, err error) {

if d.remaining() < TypeSize.Checksum512 {
err = fmt.Errorf("checksum 512 required [%d] bytes, remaining [%d]", TypeSize.Checksum512, d.remaining())
return
Expand Down
26 changes: 13 additions & 13 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,36 +199,36 @@ func TestDecoder_string(t *testing.T) {
assert.Equal(t, 0, d.remaining())
}

func TestDecoder_SHA256Bytes(t *testing.T) {
func TestDecoder_Checksum256(t *testing.T) {

s := SHA256Bytes(bytes.Repeat([]byte{1}, 32))
s := Checksum256(bytes.Repeat([]byte{1}, 32))

buf := new(bytes.Buffer)
enc := NewEncoder(buf)
enc.writeSHA256Bytes(s)
enc.writeChecksum256(s)

d := NewDecoder(buf.Bytes())

rs, err := d.ReadSHA256Bytes()
rs, err := d.ReadChecksum256()
assert.NoError(t, err)

assert.Equal(t, s, rs)
assert.Equal(t, 0, d.remaining())
}

func TestDecoder_Empty_SHA256Bytes(t *testing.T) {
func TestDecoder_Empty_Checksum256(t *testing.T) {

s := SHA256Bytes([]byte{})
s := Checksum256([]byte{})

buf := new(bytes.Buffer)
enc := NewEncoder(buf)
enc.writeSHA256Bytes(s)
enc.writeChecksum256(s)

d := NewDecoder(buf.Bytes())

s, err := d.ReadSHA256Bytes()
s, err := d.ReadChecksum256()
assert.NoError(t, err)
assert.Equal(t, s, SHA256Bytes(bytes.Repeat([]byte{0}, 32)))
assert.Equal(t, s, Checksum256(bytes.Repeat([]byte{0}, 32)))
assert.Equal(t, 0, d.remaining())
}

Expand Down Expand Up @@ -334,7 +334,7 @@ type EncodeTestStruct struct {
F2 int16
F3 uint16
F4 uint32
F5 SHA256Bytes
F5 Checksum256
F6 []string
F7 [2]string
F8 map[string]string
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestDecoder_Encode(t *testing.T) {
assert.Equal(t, int16(-75), s.F2)
assert.Equal(t, uint16(99), s.F3)
assert.Equal(t, uint32(999), s.F4)
assert.Equal(t, SHA256Bytes(bytes.Repeat([]byte{0}, 32)), s.F5)
assert.Equal(t, Checksum256(bytes.Repeat([]byte{0}, 32)), s.F5)
assert.Equal(t, []string{"def", "789"}, s.F6)
assert.Equal(t, [2]string{"foo", "bar"}, s.F7)
assert.Equal(t, map[string]string{"foo": "bar", "hello": "you"}, s.F8)
Expand Down Expand Up @@ -605,8 +605,8 @@ func TestDecoder_readUint16_missing_data(t *testing.T) {
_, err = NewDecoder([]byte{}).ReadUint64()
assert.EqualError(t, err, "uint64 required [8] bytes, remaining [0]")

_, err = NewDecoder([]byte{}).ReadSHA256Bytes()
assert.EqualError(t, err, "sha256 required [32] bytes, remaining [0]")
_, err = NewDecoder([]byte{}).ReadChecksum256()
assert.EqualError(t, err, "checksum 256 required [32] bytes, remaining [0]")

_, err = NewDecoder([]byte{}).ReadPublicKey()
assert.EqualError(t, err, "publicKey required [34] bytes, remaining [0]")
Expand Down
8 changes: 0 additions & 8 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func (e *Encoder) Encode(v interface{}) (err error) {
case Uint128:
return e.writeUint128(cv)
case Int128:
fmt.Println("CAMILON")
return e.writeUint128(Uint128(cv))
case Float128:
return e.writeUint128(Uint128(cv))
Expand Down Expand Up @@ -344,13 +343,6 @@ func (e *Encoder) writeString(s string) (err error) {
return e.writeByteArray([]byte(s))
}

func (e *Encoder) writeSHA256Bytes(s SHA256Bytes) error {
Logger.Encoder.Printf("Writing SHA256 [%s]\n", hex.EncodeToString(s))
if len(s) == 0 {
return e.toWriter(bytes.Repeat([]byte{0}, TypeSize.SHA256Bytes))
}
return e.toWriter(s)
}
func (e *Encoder) writeChecksum160(checksum Checksum160) error {
Logger.Encoder.Printf("Writing checksum160 [%s]\n", hex.EncodeToString(checksum))
if len(checksum) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ type Peer struct {
}

type HandshakeInfo struct {
ChainID eos.SHA256Bytes
ChainID eos.Checksum256
HeadBlockNum uint32
HeadBlockID eos.SHA256Bytes
HeadBlockID eos.Checksum256
HeadBlockTime time.Time
LastIrreversibleBlockNum uint32
LastIrreversibleBlockID eos.SHA256Bytes
LastIrreversibleBlockID eos.Checksum256
}

func (h *HandshakeInfo) String() string {
Expand Down
36 changes: 18 additions & 18 deletions p2ptypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ type P2PMessage interface {
type HandshakeMessage struct {
// net_plugin/protocol.hpp handshake_message
NetworkVersion uint16 `json:"network_version"`
ChainID SHA256Bytes `json:"chain_id"`
NodeID SHA256Bytes `json:"node_id"` // sha256
ChainID Checksum256 `json:"chain_id"`
NodeID Checksum256 `json:"node_id"` // sha256
Key ecc.PublicKey `json:"key"` // can be empty, producer key, or peer key
Time Tstamp `json:"time"` // time?!
Token SHA256Bytes `json:"token"` // digest of time to prove we own the private `key`
Token Checksum256 `json:"token"` // digest of time to prove we own the private `key`
Signature ecc.Signature `json:"sig"` // can be empty if no key, signature of the digest above
P2PAddress string `json:"p2p_address"`
LastIrreversibleBlockNum uint32 `json:"last_irreversible_block_num"`
LastIrreversibleBlockID SHA256Bytes `json:"last_irreversible_block_id"`
LastIrreversibleBlockID Checksum256 `json:"last_irreversible_block_id"`
HeadNum uint32 `json:"head_num"`
HeadID SHA256Bytes `json:"head_id"`
HeadID Checksum256 `json:"head_id"`
OS string `json:"os"`
Agent string `json:"agent"`
Generation int16 `json:"generation"`
Expand All @@ -40,9 +40,9 @@ func (m *HandshakeMessage) GetType() P2PMessageType {

type ChainSizeMessage struct {
LastIrreversibleBlockNum uint32 `json:"last_irreversible_block_num"`
LastIrreversibleBlockID SHA256Bytes `json:"last_irreversible_block_id"`
LastIrreversibleBlockID Checksum256 `json:"last_irreversible_block_id"`
HeadNum uint32 `json:"head_num"`
HeadID SHA256Bytes `json:"head_id"`
HeadID Checksum256 `json:"head_id"`
}

func (m *ChainSizeMessage) GetType() P2PMessageType {
Expand Down Expand Up @@ -105,7 +105,7 @@ func (r GoAwayReason) String() string {

type GoAwayMessage struct {
Reason GoAwayReason `json:"reason"`
NodeID SHA256Bytes `json:"node_id"`
NodeID Checksum256 `json:"node_id"`
}

func (m *GoAwayMessage) GetType() P2PMessageType {
Expand Down Expand Up @@ -199,7 +199,7 @@ func (s TransactionStatus) String() string {

}

//type TransactionID SHA256Bytes
//type TransactionID Checksum256

type ProducerKey struct {
AccountName AccountName `json:"producer_name"`
Expand All @@ -215,9 +215,9 @@ type BlockHeader struct {
Timestamp BlockTimestamp `json:"timestamp"`
Producer AccountName `json:"producer"`
Confirmed uint16 `json:"confirmed"`
Previous SHA256Bytes `json:"previous"`
TransactionMRoot SHA256Bytes `json:"transaction_mroot"`
ActionMRoot SHA256Bytes `json:"action_mroot"`
Previous Checksum256 `json:"previous"`
TransactionMRoot Checksum256 `json:"transaction_mroot"`
ActionMRoot Checksum256 `json:"action_mroot"`
ScheduleVersion uint32 `json:"schedule_version"`
NewProducers *OptionalProducerSchedule `json:"new_producers" eos:"optional"`
HeaderExtensions []*Extension `json:"header_extensions"`
Expand All @@ -227,7 +227,7 @@ func (b *BlockHeader) BlockNumber() uint32 {
return binary.BigEndian.Uint32(b.Previous[:4]) + 1
}

func (b *BlockHeader) BlockID() (SHA256Bytes, error) {
func (b *BlockHeader) BlockID() (Checksum256, error) {
cereal, err := MarshalBinary(b)
if err != nil {
return nil, err
Expand All @@ -239,7 +239,7 @@ func (b *BlockHeader) BlockID() (SHA256Bytes, error) {

binary.BigEndian.PutUint32(hashed, b.BlockNumber())

return SHA256Bytes(hashed), nil
return Checksum256(hashed), nil
}

type OptionalProducerSchedule struct {
Expand Down Expand Up @@ -277,7 +277,7 @@ type TransactionReceipt struct {
}

type TransactionWithID struct {
ID SHA256Bytes
ID Checksum256
Packed *PackedTransaction
}

Expand Down Expand Up @@ -313,7 +313,7 @@ func (t *TransactionWithID) UnmarshalJSON(data []byte) error {
}

*t = TransactionWithID{
ID: SHA256Bytes(shaID),
ID: Checksum256(shaID),
}

return nil
Expand Down Expand Up @@ -371,12 +371,12 @@ const (
type OrderedTransactionIDs struct {
Mode [4]byte `json:"mode"`
Pending uint32 `json:"pending"`
IDs []SHA256Bytes `json:"ids"`
IDs []Checksum256 `json:"ids"`
}
type OrderedBlockIDs struct {
Mode [4]byte `json:"mode"`
Pending uint32 `json:"pending"`
IDs []SHA256Bytes `json:"ids"`
IDs []Checksum256 `json:"ids"`
}

func (o *OrderedBlockIDs) String() string {
Expand Down
Loading

0 comments on commit 92c2bf6

Please sign in to comment.