Skip to content

Commit

Permalink
Merge pull request #36 from joeqian10/dev
Browse files Browse the repository at this point in the history
Sort imports and clean code
  • Loading branch information
joeqian10 authored Sep 11, 2024
2 parents 7ec5de8 + 46223ff commit 60a70fb
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 67 deletions.
1 change: 0 additions & 1 deletion block/block.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package block

import (
//"github.com/joeqian10/neo-gogogo/rpc/models"
"github.com/joeqian10/neo-gogogo/tx"
)

Expand Down
5 changes: 3 additions & 2 deletions block/blockHeader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"

"github.com/joeqian10/neo-gogogo/crypto"
"github.com/joeqian10/neo-gogogo/helper"
"github.com/joeqian10/neo-gogogo/helper/io"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (bh *BlockHeader) Deserialize(br *io.BinaryReader) {
}
}

//DeserializeUnsigned deserialize blockheader without witness
// DeserializeUnsigned deserialize blockheader without witness
func (bh *BlockHeader) DeserializeUnsigned(br *io.BinaryReader) {
br.ReadLE(&bh.Version)
br.ReadLE(&bh.PrevHash)
Expand All @@ -99,7 +100,7 @@ func (bh *BlockHeader) Serialize(bw *io.BinaryWriter) {
bw.WriteLE(byte(0))
}

//SerializeUnsigned serialize blockheader without witness
// SerializeUnsigned serialize blockheader without witness
func (bh *BlockHeader) SerializeUnsigned(bw *io.BinaryWriter) {
bw.WriteLE(bh.Version)
bw.WriteLE(bh.PrevHash)
Expand Down
8 changes: 5 additions & 3 deletions block/blockHeader_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package block

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/joeqian10/neo-gogogo/helper"
"github.com/joeqian10/neo-gogogo/helper/io"
"github.com/joeqian10/neo-gogogo/rpc/models"
"github.com/joeqian10/neo-gogogo/sc"
"github.com/joeqian10/neo-gogogo/tx"
"github.com/stretchr/testify/assert"
"testing"
)

func SetupBlockHeaderWithValues() *BlockHeader {
Expand Down Expand Up @@ -59,7 +61,7 @@ func TestBlockHeader_DeserializeUnsigned(t *testing.T) {
0, 0, 0, 0, // Index
30, 0, 0, 0, 0, 0, 0, 0, // ConsensusData
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // NextConsensus
}
}

br := io.NewBinaryReaderFromBuf(rawBlock)
bh := &BlockHeader{}
Expand Down
12 changes: 6 additions & 6 deletions blockchain/storagekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"github.com/joeqian10/neo-gogogo/helper/io"
)

//Storagekey key use to store StorageItem on blockchain
type Storagekey struct {
// StorageKey key use to store StorageItem on blockchain
type StorageKey struct {
ScriptHash helper.UInt160
Key []byte
}

//Deserialize deserialize from byte array
func (sk *Storagekey) Deserialize(reader *io.BinaryReader) {
// Deserialize deserializes from byte array
func (sk *StorageKey) Deserialize(reader *io.BinaryReader) {
reader.ReadLE(&sk.ScriptHash)
sk.Key, _ = reader.ReadBytesWithGrouping()
}

//Serialize serialize to byte array
func (sk *Storagekey) Serialize(writer *io.BinaryWriter) {
// Serialize serializes to byte array
func (sk *StorageKey) Serialize(writer *io.BinaryWriter) {
writer.WriteLE(sk.ScriptHash)
writer.WriteBytesWithGrouping(sk.Key)
}
3 changes: 2 additions & 1 deletion crypto/base58_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package crypto

import (
"encoding/hex"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBase58CheckEncodeDecode(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions helper/fixed8_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package helper

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewFixed8(t *testing.T) {
Expand All @@ -12,7 +13,7 @@ func TestNewFixed8(t *testing.T) {

func TestFixed8FromInt64(t *testing.T) {
f := Fixed8FromInt64(100000000)
assert.Equal(t, int64(100000000 * D), f.Value)
assert.Equal(t, int64(100000000*D), f.Value)
}

func TestFixed8FromFloat64(t *testing.T) {
Expand Down Expand Up @@ -90,4 +91,4 @@ func TestFixed8_LessThan(t *testing.T) {
func TestFixed8_String(t *testing.T) {
s := NewFixed8(100000000).String()
assert.Equal(t, "1", s)
}
}
5 changes: 3 additions & 2 deletions helper/io/binaryReader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package io
import (
"bytes"
"encoding/hex"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewBinaryReaderFromIO(t *testing.T) {
Expand Down Expand Up @@ -77,4 +78,4 @@ func TestBinaryReader_ReadVarString(t *testing.T) {
br := NewBinaryReaderFromBuf(bin)
result = br.ReadVarString()
assert.Equal(t, val, result)
}
}
6 changes: 3 additions & 3 deletions helper/io/binaryWriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package io

import (
"bytes"
"github.com/joeqian10/neo-gogogo/helper"
"testing"

//"github.com/joeqian10/neo-gogogo/helper"
"github.com/stretchr/testify/assert"
"testing"

"github.com/joeqian10/neo-gogogo/helper"
)

func TestNewBinaryWriterFromIO(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions helper/io/serializable_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io

import (
"github.com/joeqian10/neo-gogogo/helper"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

"github.com/joeqian10/neo-gogogo/helper"
)

type TestSerializable struct {
Expand Down
9 changes: 5 additions & 4 deletions helper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/joeqian10/neo-gogogo/crypto"
"math/big"

"github.com/joeqian10/neo-gogogo/crypto"
)

//BytesToHex bytes to hex string
// BytesToHex bytes to hex string
func BytesToHex(b []byte) string {
return hex.EncodeToString(b)
}

//HexToBytes Simple hex string to bytes
// HexToBytes Simple hex string to bytes
func HexToBytes(hexstring string) (b []byte) {
b, _ = hex.DecodeString(hexstring)
return b
Expand Down Expand Up @@ -43,7 +44,7 @@ func BytesToScriptHash(bs []byte) (UInt160, error) {
return UInt160FromBytes(crypto.Hash160(bs))
}

//ToNibbles ..
// ToNibbles ..
func ToNibbles(data []byte) []byte {
r := make([]byte, len(data)*2)
for i := 0; i < len(data); i++ {
Expand Down
7 changes: 4 additions & 3 deletions helper/util_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package helper

import (
"github.com/joeqian10/neo-gogogo/crypto"
"testing"

"github.com/stretchr/testify/assert"

"github.com/joeqian10/neo-gogogo/crypto"
)

func TestReverseBytes(t *testing.T) {
Expand All @@ -27,7 +28,7 @@ func TestReverseBytes(t *testing.T) {
}

func TestBytesToScriptHash(t *testing.T) {
script := []byte{ 0x01, 0x02, 0x03, 0x04 }
script := []byte{0x01, 0x02, 0x03, 0x04}
hash := crypto.Hash160(script)
scriptHash, _ := BytesToScriptHash(script)
assert.Equal(t, "ecd2cbd8262d2c361b93bf89c4f0a78d76a16e70", BytesToHex(hash))
Expand All @@ -54,4 +55,4 @@ func TestBytesToScriptHash(t *testing.T) {
// bi := HashToInt(hash)
//
// assert.Equal(t, 0, bi)
//}
//}
5 changes: 3 additions & 2 deletions helper/varInt_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package helper

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestVarIntFromUInt64(t *testing.T) {
Expand All @@ -18,4 +19,4 @@ func TestVarInt_Length(t *testing.T) {
func TestVarInt_Bytes(t *testing.T) {
v := VarIntFromUInt64(100000000)
assert.Equal(t, "fe00e1f505", BytesToHex(v.Bytes()))
}
}
10 changes: 5 additions & 5 deletions mpt/proofdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"github.com/joeqian10/neo-gogogo/helper"
)

//ProofDb a db to use for verify
// ProofDb a db to use for verify
type ProofDb struct {
nodes map[string]([]byte)
nodes map[string][]byte
}

//NewProofDb new instance of ProofDb from a string list
// NewProofDb new instance of ProofDb from a string list
func NewProofDb(proof [][]byte) *ProofDb {
p := &ProofDb{}
p.nodes = make(map[string]([]byte), len(proof))
p.nodes = make(map[string][]byte, len(proof))
for _, v := range proof {
data := v
hashstr := helper.BytesToHex(crypto.Hash256(data))
Expand All @@ -24,7 +24,7 @@ func NewProofDb(proof [][]byte) *ProofDb {
return p
}

//Get for TrieDb
// Get for TrieDb
func (pd *ProofDb) Get(key []byte) ([]byte, error) {
keystr := helper.BytesToHex(key)
if v, ok := pd.nodes[keystr]; ok {
Expand Down
14 changes: 7 additions & 7 deletions mpt/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
nio "github.com/joeqian10/neo-gogogo/helper/io"
)

//Trie mpt tree
// Trie mpt tree
type Trie struct {
db *trieDb
root node
}

//NewTrie new a trie instance
// NewTrie new a trie instance
func NewTrie(root []byte, db IKVReadOnlyDb) (*Trie, error) {
if db == nil {
return nil, errors.New("failed initialize Trie, invalid db")
Expand All @@ -36,7 +36,7 @@ func (t *Trie) resolve(hash hashNode) (node, error) {
return t.db.node(hash)
}

//Get try get value
// Get try get value
func (t *Trie) Get(path []byte) ([]byte, error) {
path = helper.ToNibbles(path)
vn, err := t.get(t.root, path)
Expand Down Expand Up @@ -76,9 +76,9 @@ func (t *Trie) get(n node, path []byte) (node, error) {
return nil, errors.New("trie cant find the path")
}

//VerifyProof directly verify proof
// VerifyProof directly verify proof
func VerifyProof(root []byte, scriptHash helper.UInt160, key []byte, proof [][]byte) ([]byte, error) {
sKey := blockchain.Storagekey{
sKey := blockchain.StorageKey{
ScriptHash: scriptHash,
Key: key,
}
Expand All @@ -98,7 +98,7 @@ func VerifyProof(root []byte, scriptHash helper.UInt160, key []byte, proof [][]b
return resolveValue(value)
}

//ResolveProof get key and proofs from proofdata
// ResolveProof get key and proofs from proofdata
func ResolveProof(proofBytes []byte) (scriptHash helper.UInt160, key []byte, proof [][]byte, err error) {
buffer := bytes.NewBuffer(proofBytes)
reader := nio.NewBinaryReaderFromIO(io.Reader(buffer))
Expand All @@ -122,7 +122,7 @@ func resolveValue(value []byte) ([]byte, error) {
}

func resolveKey(key []byte) (scriptHash helper.UInt160, kk []byte, err error) {
sKey := blockchain.Storagekey{}
sKey := blockchain.StorageKey{}
err = nio.AsSerializable(&sKey, key)
return sKey.ScriptHash, sKey.Key, err
}
5 changes: 3 additions & 2 deletions nep5/nep5Helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package nep5
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/joeqian10/neo-gogogo/helper"
"github.com/joeqian10/neo-gogogo/rpc"
"github.com/joeqian10/neo-gogogo/rpc/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestNewNep5Helper(t *testing.T) {
Expand Down
17 changes: 9 additions & 8 deletions rpc/rpcClient_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
//go:build integration
// +build integration

package rpc_integration

import (
"crypto/rand"
"log"
"net/rpc"
"testing"

"github.com/stretchr/testify/assert"

"github.com/joeqian10/neo-gogogo/crypto"
"github.com/joeqian10/neo-gogogo/helper"
"github.com/joeqian10/neo-gogogo/sc"
"github.com/joeqian10/neo-gogogo/tx"
"github.com/stretchr/testify/assert"
"log"
"net/rpc"

//"math/rand"
"crypto/rand"
"testing"
)

/*
If you want to run all the tests, you'd better have a private net prepared for testing,
since a private net is more flexible and you will have plenty of neo and gas to spend.
since a private net is more flexible, and you will have plenty of neo and gas to spend.
You need to change the LocalEndPoint to your node, and install all required plugins.
*/

Expand Down
Loading

0 comments on commit 60a70fb

Please sign in to comment.