Skip to content

Commit

Permalink
fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fleandrei committed Jan 16, 2025
1 parent 55ffb9a commit 9eb6e88
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ run:
- api/swagger/server
- resource

issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- funlen

linters-settings:
tagliatelle:
# Check the struck tag name case.
Expand Down
1 change: 0 additions & 1 deletion int/api/cmd/deploySC.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (d *deploySC) Handle(params operations.CmdDeploySCParams) middleware.Respon
&models.Error{
Message: "Smart contract bytecode is required",
})

}

smartContractByteCode, err := base64.StdEncoding.DecodeString(params.Body.SmartContract)
Expand Down
3 changes: 2 additions & 1 deletion pkg/node/sendoperation/executesc/executesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const OpType = 3

type OperationDetails struct {
ByteCode []byte `json:"data"`
ByteCode []byte `json:"byte_code"`
MaxGas uint64 `json:"max_gas"`
MaxCoins uint64 `json:"max_coins"`
//nolint:tagliatelle
Expand Down Expand Up @@ -168,6 +168,7 @@ func DecodeMessage(data []byte) (*MessageContent, error) {
}

dataStoreBytes := make([]byte, remainingBytesLen)

_, err = buf.Read(dataStoreBytes)
if err != nil {
return nil, fmt.Errorf("failed to read dataStore: %w", err)
Expand Down
57 changes: 34 additions & 23 deletions pkg/onchain/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package onchain
import (
"bytes"
"encoding/binary"
"fmt"

"github.com/massalabs/station/pkg/convert"
)
Expand All @@ -18,7 +19,7 @@ type ContractDatastore struct {
Coins uint64
}

type datastoreEntry struct {
type DatastoreEntry struct {
Key []byte
Value []byte
}
Expand All @@ -27,24 +28,23 @@ type datastoreEntry struct {
creates and serializes a datastore for the given contract.
*/
func populateDatastore(contract ContractDatastore) ([]byte, error) {
var datastore []datastoreEntry
var datastore []DatastoreEntry

// number of contracts to deploy
numberOfContractsKey := []byte{0}
numberOfContracts := convert.U64ToBytes(1)
datastore = append(datastore, datastoreEntry{Key: numberOfContractsKey, Value: numberOfContracts})
datastore = append(datastore, DatastoreEntry{Key: numberOfContractsKey, Value: numberOfContracts})

// contract bytecode
datastore = append(datastore, datastoreEntry{Key: getContractByteCodeKey(), Value: contract.ByteCode})

datastore = append(datastore, DatastoreEntry{Key: getContractByteCodeKey(), Value: contract.ByteCode})
// args data
// hardcoded for now, could be dynamix see: https://github.com/massalabs/massa-web3/blob/main/src/dataStore.ts
datastore = append(datastore, datastoreEntry{Key: getArgsKey(), Value: contract.Args})
datastore = append(datastore, DatastoreEntry{Key: getArgsKey(), Value: contract.Args})

// coins data
//nolint: lll
// 12/02/2024 single contract deployement is supported. Multiple not planned. see https://github.com/massalabs/station/issues/1364
datastore = append(datastore, datastoreEntry{Key: getCoinsKey(), Value: convert.U64ToBytes(contract.Coins)})
datastore = append(datastore, DatastoreEntry{Key: getCoinsKey(), Value: convert.U64ToBytes(contract.Coins)})

// Serialize the datastore
serializedDatastore, err := SerializeDatastore(datastore)
Expand All @@ -55,30 +55,37 @@ func populateDatastore(contract ContractDatastore) ([]byte, error) {
return serializedDatastore, nil
}

// getContractByteCodeKey returns the key for the deployed contract bytecode in the datastore
// getContractByteCodeKey returns the key for the deployed contract bytecode in the datastore.
func getContractByteCodeKey() []byte {
return convert.U64ToBytes(contractByteCodeKey)
}

// getArgsKey returns the key for the deployed contract constructor parameters in the datastore
// getArgsKey returns the key for the deployed contract constructor parameters in the datastore.
func getArgsKey() []byte {
lengthPrefix := convert.U32ToBytes(byteArrayLengthPrefix)
argsKeySuffix := []byte{0}

tempKey := append(getContractByteCodeKey(), lengthPrefix...)

return append(tempKey, argsKeySuffix...)
}

// getCoinsKey returns the key in the datastore for the amount of MAS to be sent to the deployed contract
// getCoinsKey returns the key in the datastore for the amount of MAS to be sent to the deployed contract.
func getCoinsKey() []byte {
lengthPrefix := convert.U32ToBytes(byteArrayLengthPrefix)
coinsKeySuffix := []byte{1}
tempKey := append(getContractByteCodeKey(), lengthPrefix...)

return append(tempKey, coinsKeySuffix...)
}

// DatastoreToDeployedContract If the datastore is a valid datastore for a deployed contract, it will return the contract's bytecode, args and coins
// If the datastore is not a valid datastore for a deployed contract, it will return an empty ContractDatastore and success = false
func DatastoreToDeployedContract(datastore []datastoreEntry) (contractDatastore ContractDatastore, success bool) {
/*
DatastoreToDeployedContract If the datastore is a valid datastore for a deployed contract,
it will return the contract's bytecode, args and coins
If the datastore is not a valid datastore for a deployed contract,
it will return an empty ContractDatastore and success = false.
*/
func DatastoreToDeployedContract(datastore []DatastoreEntry) (contractDatastore ContractDatastore, success bool) {
if !isContractDeployDatastore(datastore) {
return ContractDatastore{}, false
}
Expand All @@ -88,6 +95,7 @@ func DatastoreToDeployedContract(datastore []datastoreEntry) (contractDatastore
contract.Args = datastore[2].Value

var err error

contract.Coins, err = convert.BytesToU64(datastore[3].Value)
if err != nil {
return ContractDatastore{}, false
Expand All @@ -97,19 +105,20 @@ func DatastoreToDeployedContract(datastore []datastoreEntry) (contractDatastore
}

// isContractDeployDatastore checks if the provided datastore entries correspond to a contract deployment.
func isContractDeployDatastore(datastore []datastoreEntry) bool {
func isContractDeployDatastore(datastore []DatastoreEntry) bool {
if len(datastore) == 4 &&
bytes.Equal(datastore[0].Key, []byte{0}) &&
bytes.Equal(datastore[1].Key, getContractByteCodeKey()) &&
bytes.Equal(datastore[2].Key, getArgsKey()) &&
bytes.Equal(datastore[3].Key, getCoinsKey()) {
return true
}

return false
}

// SerializeDatastore serializes the datastore into a []byte array.
func SerializeDatastore(datastore []datastoreEntry) ([]byte, error) {
func SerializeDatastore(datastore []DatastoreEntry) ([]byte, error) {
var buffer bytes.Buffer

buf := make([]byte, binary.MaxVarintLen64)
Expand Down Expand Up @@ -139,19 +148,19 @@ func SerializeDatastore(datastore []datastoreEntry) ([]byte, error) {
return buffer.Bytes(), nil
}

func DeSerializeDatastore(datastore []byte) ([]datastoreEntry, error) {
func DeSerializeDatastore(datastore []byte) ([]DatastoreEntry, error) {
if len(datastore) == 0 {
return nil, nil
}

var entries []datastoreEntry
var entries []DatastoreEntry

reader := bytes.NewReader(datastore)

// Decode the number of key-value pairs
datastoreSize, err := binary.ReadUvarint(reader)
if err != nil {
return nil, err
return nil, fmt.Errorf("couldn't deserialize the number of key values pair in the datastore: %w", err)
}

// Decode each key-value pair
Expand All @@ -160,31 +169,33 @@ func DeSerializeDatastore(datastore []byte) ([]datastoreEntry, error) {
// get the key length
keyLength, err := binary.ReadUvarint(reader)
if err != nil {
return nil, err
return nil, fmt.Errorf("couldn't deserialize the length of the %dth datastore key: %w", i+1, err)
}

// get the key
key := make([]byte, keyLength)

_, err = reader.Read(key)
if err != nil {
return nil, err
return nil, fmt.Errorf("couldn't deserialize the %dth datastore key: %w", i+1, err)
}

/* Decode value*/
// get value length
valueLength, err := binary.ReadUvarint(reader)
if err != nil {
return nil, err
return nil, fmt.Errorf("couldn't deserialize the length of the %dth datastore value: %w", i+1, err)
}

// get the value
value := make([]byte, valueLength)

_, err = reader.Read(value)
if err != nil {
return nil, err
return nil, fmt.Errorf("couldn't deserialize the %dth datastore value: %w", i+1, err)
}

entries = append(entries, datastoreEntry{Key: key, Value: value})
entries = append(entries, DatastoreEntry{Key: key, Value: value})
}

return entries, nil
Expand Down
56 changes: 33 additions & 23 deletions pkg/onchain/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
func TestIsContractDeployDatastore(t *testing.T) {
tests := []struct {
name string
datastore []datastoreEntry
datastore []DatastoreEntry
isDeploySC bool
}{
{
name: "Valid contract deploy datastore",
datastore: []datastoreEntry{
datastore: []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: getContractByteCodeKey(), Value: []byte{1, 2, 3}},
{Key: getArgsKey(), Value: []byte{4, 5, 6}},
Expand All @@ -23,15 +23,15 @@ func TestIsContractDeployDatastore(t *testing.T) {
},
{
name: "Invalid contract deploy datastore - wrong length",
datastore: []datastoreEntry{
datastore: []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: getContractByteCodeKey(), Value: []byte{1, 2, 3}},
},
isDeploySC: false,
},
{
name: "Invalid contract deploy datastore - wrong smart contract number key",
datastore: []datastoreEntry{
datastore: []DatastoreEntry{
{Key: []byte{1}, Value: []byte{1}},
{Key: getContractByteCodeKey(), Value: []byte{1, 2, 3}},
{Key: getArgsKey(), Value: []byte{4, 5, 6}},
Expand All @@ -41,7 +41,7 @@ func TestIsContractDeployDatastore(t *testing.T) {
},
{
name: "Invalid contract deploy datastore - wrong bytecode key",
datastore: []datastoreEntry{
datastore: []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: []byte("wrong key"), Value: []byte{1, 2, 3}},
{Key: getArgsKey(), Value: []byte{4, 5, 6}},
Expand All @@ -51,7 +51,7 @@ func TestIsContractDeployDatastore(t *testing.T) {
},
{
name: "Invalid contract deploy datastore - wrong Args key",
datastore: []datastoreEntry{
datastore: []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: getContractByteCodeKey(), Value: []byte{1, 2, 3}},
{Key: []byte("wrong key"), Value: []byte{4, 5, 6}},
Expand All @@ -61,7 +61,7 @@ func TestIsContractDeployDatastore(t *testing.T) {
},
{
name: "Invalid contract deploy datastore - wrong coins key",
datastore: []datastoreEntry{
datastore: []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: getContractByteCodeKey(), Value: []byte{1, 2, 3}},
{Key: getArgsKey(), Value: []byte{4, 5, 6}},
Expand All @@ -85,19 +85,19 @@ func TestDeSerializeDatastore(t *testing.T) {
tests := []struct {
name string
input []byte
expected []datastoreEntry
expected []DatastoreEntry
}{
{
name: "Valid datastore",
input: func() []byte {
datastore := []datastoreEntry{
datastore := []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: []byte{1}, Value: []byte{2, 3}},
}
serialized, _ := SerializeDatastore(datastore)
return serialized
}(),
expected: []datastoreEntry{
expected: []DatastoreEntry{
{Key: []byte{0}, Value: []byte{1}},
{Key: []byte{1}, Value: []byte{2, 3}},
},
Expand All @@ -110,41 +110,51 @@ func TestDeSerializeDatastore(t *testing.T) {
{
name: "Large datastore",
input: func() []byte {
datastore := []datastoreEntry{
{Key: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Value: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},
datastore := []DatastoreEntry{
{
Key: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Value: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
},
}
serialized, _ := SerializeDatastore(datastore)

return serialized
}(),
expected: []datastoreEntry{
{Key: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Value: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}},
expected: []DatastoreEntry{
{
Key: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Value: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DeSerializeDatastore(tt.input)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := DeSerializeDatastore(test.input)
if err != nil {
t.Errorf("Got unexpected error = %v", err)

return
}

if !equalDatastoreEntries(result, tt.expected) {
t.Errorf("DeSerializeDatastore() = %+v, expected: %+v", result, tt.expected)
if !equalDatastoreEntries(result, test.expected) {
t.Errorf("DeSerializeDatastore() = %+v, expected: %+v", result, test.expected)
}
})
}
}

func equalDatastoreEntries(a, b []datastoreEntry) bool {
if len(a) != len(b) {
func equalDatastoreEntries(entries1, entries2 []DatastoreEntry) bool {
if len(entries1) != len(entries2) {
return false
}
for i := range a {
if !bytes.Equal(a[i].Key, b[i].Key) || !bytes.Equal(a[i].Value, b[i].Value) {

for i := range entries1 {
if !bytes.Equal(entries1[i].Key, entries1[i].Key) || !bytes.Equal(entries1[i].Value, entries1[i].Value) {
return false
}
}

return true
}
1 change: 0 additions & 1 deletion pkg/plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
)

//nolint:funlen
func Test_removePlugin(t *testing.T) {
testCases := []struct {
name string
Expand Down
1 change: 0 additions & 1 deletion pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func AssertUpdate(t *testing.T, testPluginUpdate testPluginUpdate) {
assert.Equal(t, isUpdatable, testPluginUpdate.updatable)
}

//nolint:funlen
func TestPlugin_Update(t *testing.T) {
//nolint:exhaustruct
pluginNonUpdatable := Plugin{
Expand Down

0 comments on commit 9eb6e88

Please sign in to comment.