Skip to content

Commit 0587003

Browse files
committed
pull develop and fix conflited files
2 parents 4935d37 + c5ec463 commit 0587003

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1960
-578
lines changed

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ run:
7878

7979
issues:
8080
exclude-rules:
81+
- text: "captLocal:"
82+
linters:
83+
- gocritic
8184
- text: "G401: Use of weak cryptographic primitive"
8285
linters:
8386
- gosec

api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func startGrpcServer(
175175
// Set GRPC handler for skipped block smith
176176
rpcService.RegisterSkippedBlockSmithsServiceServer(grpcServer, &handler.SkippedBlockSmithHandler{
177177
Service: service.NewSkippedBlockSmithService(
178-
query.NewSkippedBlocksmithQuery(),
178+
query.NewSkippedBlocksmithQuery(&chaintype.MainChain{}),
179179
queryExecutor,
180180
),
181181
})

api/service/skippedBlockSmithService_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"database/sql"
55
"errors"
6+
chaintype2 "github.com/zoobc/zoobc-core/common/chaintype"
67
"reflect"
78
"testing"
89

@@ -24,11 +25,11 @@ func TestNewSkippedBlockSmithService(t *testing.T) {
2425
{
2526
name: "wantSuccess",
2627
args: args{
27-
skippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(),
28+
skippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(&chaintype2.MainChain{}),
2829
queryExecutor: &query.Executor{},
2930
},
3031
want: &SkippedBlockSmithService{
31-
SkippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(),
32+
SkippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(&chaintype2.MainChain{}),
3233
QueryExecutor: &query.Executor{},
3334
},
3435
},
@@ -62,7 +63,7 @@ func (*mockGetSkippedBlockSmithsSelectFail) ExecuteSelectRow(query string, tx bo
6263
func (*mockGetSkippedBlockSmithsSelectSuccess) ExecuteSelect(string, bool, ...interface{}) (*sql.Rows, error) {
6364
db, mock, _ := sqlmock.New()
6465
defer db.Close()
65-
mockRows := mock.NewRows(query.NewSkippedBlocksmithQuery().Fields)
66+
mockRows := mock.NewRows(query.NewSkippedBlocksmithQuery(&chaintype2.MainChain{}).Fields)
6667
mockRows.AddRow(
6768
[]byte{1},
6869
1,
@@ -97,7 +98,7 @@ func TestSkippedBlockSmithService_GetSkippedBlockSmiths(t *testing.T) {
9798
name: "wantFail:",
9899
fields: fields{
99100
QueryExecutor: &mockGetSkippedBlockSmithsSelectFail{},
100-
SkippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(),
101+
SkippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(&chaintype2.MainChain{}),
101102
},
102103
args: args{
103104
req: &model.GetSkippedBlocksmithsRequest{
@@ -112,7 +113,7 @@ func TestSkippedBlockSmithService_GetSkippedBlockSmiths(t *testing.T) {
112113
name: "WantSuccess",
113114
fields: fields{
114115
QueryExecutor: &mockGetSkippedBlockSmithsSelectSuccess{},
115-
SkippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(),
116+
SkippedBlocksmithQuery: query.NewSkippedBlocksmithQuery(&chaintype2.MainChain{}),
116117
},
117118
args: args{
118119
req: &model.GetSkippedBlocksmithsRequest{

api/service/transactionApiService.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -242,21 +242,29 @@ func (ts *TransactionService) PostTransaction(
242242
// when the node is too busy due to high number of goroutines,
243243
// the network can regulate itself without leading to blockchain splits or hard forks
244244
tpsReceived = ts.FeedbackStrategy.IncrementVarCount("tpsReceivedTmp").(int)
245-
if limitReached, limitLevel := ts.FeedbackStrategy.IsGoroutineLimitReached(constant.FeedbackMinGoroutineSamples); limitReached {
246-
switch limitLevel {
247-
case constant.FeedbackLimitHigh:
248-
ts.Logger.Error("Tx dropped due to network being spammed with too many transactions")
245+
if limitReached, limitLevel := ts.FeedbackStrategy.IsCPULimitReached(constant.FeedbackCPUMinSamples); limitReached {
246+
if limitLevel == constant.FeedbackLimitHigh {
247+
ts.Logger.Error("Tx dropped due to high cpu usage")
249248
monitoring.IncreaseTxFiltered()
250-
return nil, status.Error(codes.Internal, "TooManyTps")
251-
case constant.FeedbackLimitMedium:
252-
if tpsReceived > 1 {
253-
ts.Logger.Error("Tx dropped due to network being spammed with too many transactions")
254-
monitoring.IncreaseTxFiltered()
255-
return nil, status.Error(codes.Internal, "TooManyTps")
256-
}
249+
return nil, status.Error(codes.Unavailable, "Service is currently not available")
257250
}
258251
}
259-
if limitReached, limitLevel := ts.FeedbackStrategy.IsP2PRequestLimitReached(constant.FeedbackMinGoroutineSamples); limitReached {
252+
// STEF removing goroutine limit (only considering CPU usage)
253+
// if limitReached, limitLevel := ts.FeedbackStrategy.IsGoroutineLimitReached(constant.FeedbackMinSamples); limitReached {
254+
// switch limitLevel {
255+
// case constant.FeedbackLimitHigh:
256+
// ts.Logger.Error("Tx dropped due to network being spammed with too many transactions")
257+
// monitoring.IncreaseTxFiltered()
258+
// return nil, status.Error(codes.Internal, "TooManyTps")
259+
// case constant.FeedbackLimitMedium:
260+
// if tpsReceived > 1 {
261+
// ts.Logger.Error("Tx dropped due to network being spammed with too many transactions")
262+
// monitoring.IncreaseTxFiltered()
263+
// return nil, status.Error(codes.Internal, "TooManyTps")
264+
// }
265+
// }
266+
// }
267+
if limitReached, limitLevel := ts.FeedbackStrategy.IsP2PRequestLimitReached(constant.FeedbackMinSamples); limitReached {
260268
switch limitLevel {
261269
case constant.FeedbackLimitHigh:
262270
ts.Logger.Error("Tx dropped due to node being too busy resolving P2P requests")

cmd/account/account.go

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ func (gc *GeneratorCommands) ConvertEncodedAccountAddressToHex() RunCommand {
145145
if err != nil {
146146
panic(err)
147147
}
148+
case int32(model.AccountType_EstoniaEidAccountType):
149+
estoniaEidAccountType := &accounttype.EstoniaEidAccountType{}
150+
accPubKey, err = estoniaEidAccountType.DecodePublicKeyFromAddress(encodedAccountAddress)
151+
if err != nil {
152+
panic(err)
153+
}
148154
}
149155
accType, err := accounttype.NewAccountType(accountTypeInt, accPubKey)
150156
if err != nil {

cmd/block/blockGenerator.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package block
22

33
import (
44
"fmt"
5+
"strings"
6+
"time"
57

68
log "github.com/sirupsen/logrus"
79
"github.com/spf13/cobra"
@@ -21,8 +23,6 @@ import (
2123
"github.com/zoobc/zoobc-core/core/smith/strategy"
2224
coreUtil "github.com/zoobc/zoobc-core/core/util"
2325
"github.com/zoobc/zoobc-core/observer"
24-
"strings"
25-
"time"
2626
)
2727

2828
type (
@@ -127,7 +127,7 @@ func initialize(
127127
MempoolCacheStorage: mempoolStorage,
128128
}
129129
blockStorage := storage.NewBlockStateStorage()
130-
blocksStorage := storage.NewBlocksStorage()
130+
blocksStorage := storage.NewBlocksStorage(monitoring.TypeMainBlocksCacheStorage)
131131
nodeAddressInfoStorage := storage.NewNodeAddressInfoStorage()
132132
receiptService := service.NewReceiptService(
133133
query.NewBatchReceiptQuery(),
@@ -196,7 +196,7 @@ func initialize(
196196
log.New(),
197197
nil,
198198
activeNodeRegistryCacheStorage,
199-
query.NewSkippedBlocksmithQuery(),
199+
query.NewSkippedBlocksmithQuery(&chaintype.MainChain{}),
200200
query.NewBlockQuery(&chaintype.MainChain{}),
201201
nil,
202202
queryExecutor,
@@ -218,7 +218,7 @@ func initialize(
218218
query.NewBlockQuery(chainType),
219219
query.NewMempoolQuery(chainType),
220220
query.NewTransactionQuery(chainType),
221-
query.NewSkippedBlocksmithQuery(),
221+
query.NewSkippedBlocksmithQuery(&chaintype.MainChain{}),
222222
crypto.NewSignature(),
223223
mempoolService,
224224
receiptService,

cmd/genesisblock/cmd.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import (
66
"encoding/hex"
77
"encoding/json"
88
"fmt"
9-
"github.com/zoobc/zoobc-core/common/accounttype"
10-
"github.com/zoobc/zoobc-core/common/crypto"
11-
"github.com/zoobc/zoobc-core/common/signaturetype"
129
"io/ioutil"
1310
"log"
1411
"os"
@@ -18,16 +15,18 @@ import (
1815
"text/template"
1916
"time"
2017

21-
"github.com/zoobc/zoobc-core/common/monitoring"
22-
2318
"github.com/spf13/cobra"
2419
"github.com/zoobc/lib/address"
20+
"github.com/zoobc/zoobc-core/common/accounttype"
2521
"github.com/zoobc/zoobc-core/common/auth"
2622
"github.com/zoobc/zoobc-core/common/chaintype"
2723
"github.com/zoobc/zoobc-core/common/constant"
24+
"github.com/zoobc/zoobc-core/common/crypto"
2825
"github.com/zoobc/zoobc-core/common/database"
2926
"github.com/zoobc/zoobc-core/common/model"
27+
"github.com/zoobc/zoobc-core/common/monitoring"
3028
"github.com/zoobc/zoobc-core/common/query"
29+
"github.com/zoobc/zoobc-core/common/signaturetype"
3130
"github.com/zoobc/zoobc-core/common/storage"
3231
"github.com/zoobc/zoobc-core/common/transaction"
3332
"github.com/zoobc/zoobc-core/common/util"
@@ -591,6 +590,7 @@ func getGenesisBlockID(genesisEntries []genesisEntry) (mainBlockID, spineBlockID
591590
&chaintype.SpineChain{},
592591
nil,
593592
nil,
593+
query.NewSkippedBlocksmithQuery(&chaintype.SpineChain{}),
594594
nil,
595595
nil,
596596
nil,
@@ -602,6 +602,7 @@ func getGenesisBlockID(genesisEntries []genesisEntry) (mainBlockID, spineBlockID
602602
nil,
603603
nil,
604604
bs,
605+
nil,
605606
)
606607
spine, err := sb.GenerateGenesisBlock(genesisConfig)
607608
if err != nil {

cmd/snapshot/snapshot.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func newSnapshotProcess() func(ccmd *cobra.Command, args []string) {
9797
query.NewPendingSignatureQuery(),
9898
query.NewMultisignatureInfoQuery(),
9999
query.NewMultiSignatureParticipantQuery(),
100-
query.NewSkippedBlocksmithQuery(),
100+
query.NewSkippedBlocksmithQuery(&chaintype.MainChain{}),
101101
query.NewFeeScaleQuery(),
102102
query.NewFeeVoteCommitmentVoteQuery(),
103103
query.NewFeeVoteRevealVoteQuery(),
@@ -332,7 +332,7 @@ func storingPayloadProcess() func(ccmd *cobra.Command, args []string) {
332332
query.NewPendingSignatureQuery(),
333333
query.NewMultisignatureInfoQuery(),
334334
query.NewMultiSignatureParticipantQuery(),
335-
query.NewSkippedBlocksmithQuery(),
335+
query.NewSkippedBlocksmithQuery(&chaintype.MainChain{}),
336336
query.NewFeeScaleQuery(),
337337
query.NewFeeVoteCommitmentVoteQuery(),
338338
query.NewFeeVoteRevealVoteQuery(),

common/accounttype/accountTypeUtil.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func NewAccountType(accTypeInt int32, accPubKey []byte) (AccountTypeInterface, e
2424
acc = &EmptyAccountType{}
2525
case int32(model.AccountType_MultiSignatureAccountType):
2626
acc = &MultiSignatureAccountType{}
27+
case int32(model.AccountType_EstoniaEidAccountType):
28+
acc = &EstoniaEidAccountType{}
2729
default:
2830
return nil, errors.New("InvalidAccountType")
2931
}
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package accounttype
2+
3+
import (
4+
"bytes"
5+
"crypto/ecdsa"
6+
"crypto/elliptic"
7+
"encoding/binary"
8+
"encoding/hex"
9+
"errors"
10+
"fmt"
11+
"math/big"
12+
13+
"github.com/zoobc/zoobc-core/common/blocker"
14+
"github.com/zoobc/zoobc-core/common/constant"
15+
"github.com/zoobc/zoobc-core/common/model"
16+
)
17+
18+
// EstoniaEidAccountType the default account type
19+
type EstoniaEidAccountType struct {
20+
publicKey, fullAddress []byte
21+
}
22+
23+
func (acc *EstoniaEidAccountType) SetAccountPublicKey(accountPublicKey []byte) {
24+
if accountPublicKey == nil {
25+
acc.publicKey = make([]byte, 0)
26+
}
27+
acc.publicKey = accountPublicKey
28+
}
29+
30+
func (acc *EstoniaEidAccountType) GetAccountAddress() ([]byte, error) {
31+
if acc.fullAddress != nil {
32+
return acc.fullAddress, nil
33+
}
34+
if acc.GetAccountPublicKey() == nil {
35+
return nil, errors.New("AccountAddressPublicKeyEmpty")
36+
}
37+
buff := bytes.NewBuffer([]byte{})
38+
tmpBuf := make([]byte, 4)
39+
binary.LittleEndian.PutUint32(tmpBuf, uint32(acc.GetTypeInt()))
40+
buff.Write(tmpBuf)
41+
buff.Write(acc.GetAccountPublicKey())
42+
acc.fullAddress = buff.Bytes()
43+
return acc.fullAddress, nil
44+
}
45+
46+
func (acc *EstoniaEidAccountType) GetTypeInt() int32 {
47+
return int32(model.AccountType_EstoniaEidAccountType)
48+
}
49+
50+
func (acc *EstoniaEidAccountType) GetAccountPublicKey() []byte {
51+
return acc.publicKey
52+
}
53+
54+
func (acc *EstoniaEidAccountType) GetAccountPrefix() string {
55+
return ""
56+
}
57+
58+
func (acc *EstoniaEidAccountType) GetName() string {
59+
return "EstoniaEid"
60+
}
61+
62+
func (acc *EstoniaEidAccountType) GetAccountPublicKeyLength() uint32 {
63+
return 97
64+
}
65+
66+
func (acc *EstoniaEidAccountType) IsEqual(acc2 AccountTypeInterface) bool {
67+
return bytes.Equal(acc.GetAccountPublicKey(), acc2.GetAccountPublicKey()) && acc.GetTypeInt() == acc2.GetTypeInt()
68+
}
69+
70+
func (acc *EstoniaEidAccountType) GetSignatureType() model.SignatureType {
71+
return model.SignatureType_EstoniaEidSignature
72+
}
73+
74+
func (acc *EstoniaEidAccountType) GetSignatureLength() uint32 {
75+
return constant.EstoniaEidSignatureLength
76+
}
77+
78+
func (acc *EstoniaEidAccountType) GetEncodedAddress() (string, error) {
79+
if acc.GetAccountPublicKey() == nil || bytes.Equal(acc.GetAccountPublicKey(), []byte{}) {
80+
return "", errors.New("EmptyAccountPublicKey")
81+
}
82+
return hex.EncodeToString(acc.GetAccountPublicKey()), nil
83+
}
84+
85+
func (acc *EstoniaEidAccountType) DecodePublicKeyFromAddress(address string) ([]byte, error) {
86+
return hex.DecodeString(address)
87+
}
88+
89+
func (acc *EstoniaEidAccountType) GenerateAccountFromSeed(seed string, optionalParams ...interface{}) error {
90+
return errors.New("NoImplementation")
91+
}
92+
93+
func (acc *EstoniaEidAccountType) GetAccountPublicKeyString() (string, error) {
94+
return acc.GetEncodedAddress()
95+
}
96+
97+
// GetAccountPrivateKey: we don't store neither generate private key of this account type as the private key resides in the eID
98+
func (acc *EstoniaEidAccountType) GetAccountPrivateKey() ([]byte, error) {
99+
return nil, nil
100+
}
101+
102+
// Sign: this account type and signature comes only from Estonia eID and we don't have private key of the accounts
103+
// so we don't sign sign for this type of account.
104+
func (acc *EstoniaEidAccountType) Sign(payload []byte, seed string, optionalParams ...interface{}) ([]byte, error) {
105+
return []byte{}, nil
106+
}
107+
108+
func (acc *EstoniaEidAccountType) VerifySignature(payload, signature, accountAddress []byte) error {
109+
publicKey := acc.loadPublicKeyFromDer(acc.GetAccountPublicKey())
110+
r, s, _ := acc.decodeSignatureNIST384RS(signature)
111+
if !ecdsa.Verify(&publicKey, payload, r, s) {
112+
return blocker.NewBlocker(
113+
blocker.ValidationErr,
114+
"InvalidSignature",
115+
)
116+
}
117+
return nil
118+
}
119+
120+
// decodeSignatureNIST384RS: decode signature to R and S component
121+
// source: https://github.com/warner/python-ecdsa/blob/master/src/ecdsa/util.py (sigdecode_string)
122+
func (acc *EstoniaEidAccountType) decodeSignatureNIST384RS(signature []byte) (r, s *big.Int, err error) {
123+
// curveOrder "39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643"
124+
curveOrderLen := 48
125+
if len(signature) != curveOrderLen*2 {
126+
return nil, nil, fmt.Errorf("error signature length: %d", len(signature))
127+
}
128+
rBytes := signature[:48]
129+
sBytes := signature[48:]
130+
r = new(big.Int).SetBytes(rBytes)
131+
s = new(big.Int).SetBytes(sBytes)
132+
return r, s, nil
133+
}
134+
135+
// loadPublicKeyFromDer loads public key from DER byte data
136+
func (acc *EstoniaEidAccountType) loadPublicKeyFromDer(publicKeyBytes []byte) (publicKey ecdsa.PublicKey) {
137+
curve := elliptic.P384()
138+
publicKey.Curve = curve
139+
publicKey.X, publicKey.Y = elliptic.Unmarshal(curve, publicKeyBytes)
140+
return publicKey
141+
}

0 commit comments

Comments
 (0)