Skip to content

Commit a8fd536

Browse files
authored
Merge pull request #71 from dedis/scenario-blockchain
Scenario blockchain
2 parents 7cac687 + 6c9f49a commit a8fd536

40 files changed

+452
-322
lines changed

android/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ buildscript {
1515
}
1616

1717
plugins {
18-
id 'com.android.application' version '8.2.1' apply false
19-
id 'com.android.library' version '8.2.1' apply false
18+
id 'com.android.application' version '8.3.1' apply false
19+
id 'com.android.library' version '8.3.1' apply false
2020
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
2121
}
2222

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Wed Jun 08 11:24:29 CEST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

server/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ build: tidy
2626
# Building executable
2727
@go build ./smc/smccli
2828
@go build ./blockchain/chaincli
29-
@go build ./registration
29+
@go build ./registry/registrycli
3030

3131
install: tidy
3232
# Building executable
3333
@go install ./smc/smccli
3434
@go install ./blockchain/chaincli
35-
@go install ./registration
35+
@go install ./registry/registrycli
3636

3737
coverage: tidy
3838
# Test and generate a coverage output usable by sonarcloud

server/blockchain/calypso/controller/controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func (m miniController) OnStart(_ cli.Flags, inj node.Injector) error {
4444

4545
calypso.RegisterContract(exec, contract)
4646

47+
inj.Inject(contract)
48+
4749
return nil
4850
}
4951

server/blockchain/chaincli/chaincli.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"go.dedis.ch/dela/cli/node"
1111
access "go.dedis.ch/dela/contracts/access/controller"
1212
cosipbft "go.dedis.ch/dela/core/ordering/cosipbft/controller"
13-
db "go.dedis.ch/dela/core/store/kv/controller"
13+
kv "go.dedis.ch/dela/core/store/kv/controller"
1414
pool "go.dedis.ch/dela/core/txn/pool/controller"
1515
signed "go.dedis.ch/dela/core/txn/signed/controller"
16-
mino "go.dedis.ch/dela/mino/minogrpc/controller"
16+
minogrpc "go.dedis.ch/dela/mino/minogrpc/controller"
1717
proxy "go.dedis.ch/dela/mino/proxy/http/controller"
18+
"go.dedis.ch/hbt/server/blockchain/web"
19+
purbkv "go.dedis.ch/purb-db/store/kv/controller"
1820
)
1921

2022
func main() {
@@ -37,13 +39,15 @@ func runWithCfg(args []string, cfg config) error {
3739
builder := node.NewBuilderWithCfg(
3840
cfg.Channel,
3941
cfg.Writer,
40-
db.NewController(),
41-
mino.NewController(),
42+
purbkv.NewController(),
43+
proxy.NewController(),
44+
web.NewController(),
45+
minogrpc.NewController(),
46+
kv.NewController(),
4247
cosipbft.NewController(),
4348
signed.NewManagerController(),
4449
pool.NewController(),
4550
access.NewController(),
46-
proxy.NewController(),
4751
calypso.NewController(),
4852
)
4953

server/blockchain/web/action.go

+79-31
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ import (
66
"net/http"
77

88
"github.com/gorilla/mux"
9+
"github.com/rs/zerolog/log"
910
"go.dedis.ch/dela"
1011
"go.dedis.ch/dela/cli/node"
12+
"go.dedis.ch/dela/core/execution"
13+
"go.dedis.ch/dela/core/txn"
14+
"go.dedis.ch/dela/core/txn/signed"
15+
"go.dedis.ch/dela/crypto/bls"
1116
"go.dedis.ch/dela/mino/proxy"
12-
"go.dedis.ch/kyber/v3/suites"
17+
"go.dedis.ch/hbt/server/blockchain/calypso"
18+
purbkv "go.dedis.ch/purb-db/store/kv"
1319

1420
"golang.org/x/xerrors"
1521
)
1622

17-
// suite is the Kyber suite for Pedersen.
18-
var suite = suites.MustFind("Ed25519")
19-
20-
const separator = ":"
21-
const malformedEncoded = "malformed encoded: %s"
22-
2323
// RegisterAction is an action to register the HTTP handlers
2424
//
2525
// - implements node.ActionTemplate
2626
type RegisterAction struct{}
2727

2828
// Execute implements node.ActionTemplate. It registers the handlers using the
29-
// default proxy from the the injector.
29+
// default proxy from the injector.
3030
func (a *RegisterAction) Execute(ctx node.Context) error {
3131
var p proxy.Proxy
3232
err := ctx.Injector.Resolve(&p)
@@ -36,11 +36,10 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
3636

3737
router := mux.NewRouter()
3838

39-
s := &secretHandler{&ctx}
39+
s := &secretHandler{ctx}
4040
router.HandleFunc("/secret", s.addSecret).Methods("POST")
4141
router.HandleFunc("/secret/list", s.listSecrets).Methods("GET")
4242
router.HandleFunc("/secret", s.getSecret).Methods("GET")
43-
router.HandleFunc("/secret/reveal", s.revealSecret).Methods("POST")
4443

4544
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
4645
router.MethodNotAllowedHandler = http.HandlerFunc(notAllowedHandler)
@@ -54,49 +53,74 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
5453

5554
type DocID []byte
5655

57-
// a secretData is a struct to hold the secret data: the document ID and the
58-
// encrypted key to access the document
59-
type secretData struct {
60-
docID DocID `json:"docid"`
61-
encryptedKey string `json:"secret"`
62-
}
63-
6456
type secretHandler struct {
65-
ctx *node.Context
57+
ctx node.Context
6658
}
6759

6860
// addSecret adds a new secret in the blockchain
6961
func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
70-
ctx := *(s.ctx)
62+
err := r.ParseMultipartForm(32 << 20)
63+
if err != nil {
64+
log.Fatal().Err(err)
65+
}
7166

72-
// Decode the request
73-
var sec secretData
74-
err := json.NewDecoder(r.Body).Decode(&sec)
67+
secret := r.FormValue("secret")
68+
id := r.FormValue("id")
69+
dela.Logger.Info().Msgf("received doc ID=%v with secret=%v", id, secret)
70+
71+
// get the calypso contract
72+
var c calypso.Contract
73+
err = s.ctx.Injector.Resolve(&c)
7574
if err != nil {
76-
http.Error(w, err.Error(), http.StatusBadRequest)
75+
dela.Logger.Error().Err(err).Msg("failed to resolve calypso contract")
76+
http.Error(w, fmt.Sprintf("failed to resolve calypso contract: %v", err),
77+
http.StatusInternalServerError)
78+
return
79+
}
80+
81+
var db purbkv.DB
82+
err = s.ctx.Injector.Resolve(&db)
83+
if err != nil {
84+
dela.Logger.Error().Err(err).Msg("failed to resolve database")
85+
http.Error(w, fmt.Sprintf("failed to resolve database: %v", err),
86+
http.StatusInternalServerError)
7787
return
7888
}
7989

8090
// add the secret to the blockchain
8191
// the secret is added to the blockchain with the document ID as the key
8292
// and the encrypted key as the value
83-
// TODO add it to the blockchain
84-
dela.Logger.Info().Msgf("secret added to the blockchain: ID=%v secret=%v", sec.docID,
85-
sec.encryptedKey)
93+
94+
err = db.Update(func(txn purbkv.WritableTx) error {
95+
b, err := txn.GetBucketOrCreate([]byte("bucket:secret"))
96+
if err != nil {
97+
return err
98+
}
99+
100+
err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdCreateSecret),
101+
calypso.SecretNameArg, id, calypso.SecretArg, secret))
102+
103+
return err
104+
})
105+
106+
if err != nil {
107+
dela.Logger.Error().Err(err).Msg("failed to add secret to the blockchain")
108+
http.Error(w, fmt.Sprintf("failed to add secret to the blockchain: %v", err),
109+
http.StatusInternalServerError)
110+
return
111+
}
112+
113+
dela.Logger.Info().Msgf("secret added to the blockchain: ID=%v secret=%v", id, secret)
86114
}
87115

88116
// listSecrets lists all secrets in the blockchain
89-
func (s *secretHandler) listSecrets(w http.ResponseWriter, r *http.Request) {
90-
ctx := *(s.ctx)
91-
117+
func (s *secretHandler) listSecrets(_ http.ResponseWriter, _ *http.Request) {
92118
// list all secrets from the blockchain
93119

94120
}
95121

96122
// getSecret gets a secret from the blockchain
97123
func (s *secretHandler) getSecret(w http.ResponseWriter, r *http.Request) {
98-
ctx := *(s.ctx)
99-
100124
// Decode the request
101125
var id DocID
102126
err := json.NewDecoder(r.Body).Decode(&id)
@@ -160,3 +184,27 @@ func notAllowedHandler(w http.ResponseWriter, r *http.Request) {
160184
w.WriteHeader(http.StatusMethodNotAllowed)
161185
fmt.Fprintln(w, string(buf))
162186
}
187+
188+
// -----------------------------------------------------------------------------
189+
// Utility functions
190+
var nounce uint64
191+
192+
func makeStep(args ...string) execution.Step {
193+
return execution.Step{Current: makeTx(args...)}
194+
}
195+
196+
func makeTx(args ...string) txn.Transaction {
197+
signer := bls.NewSigner()
198+
199+
options := []signed.TransactionOption{}
200+
for i := 0; i < len(args)-1; i += 2 {
201+
options = append(options, signed.WithArg(args[i], []byte(args[i+1])))
202+
}
203+
204+
tx, err := signed.NewTransaction(nounce, signer.GetPublicKey(), options...)
205+
if err != nil {
206+
nounce++
207+
}
208+
209+
return tx
210+
}

server/go.mod

+16-17
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ module go.dedis.ch/hbt/server
22

33
go 1.21
44

5-
// replace go.dedis.ch/dela => ../../dela
6-
75
require (
86
github.com/gorilla/mux v1.8.1
97
github.com/rs/zerolog v1.31.0
10-
github.com/spf13/viper v1.18.1
11-
github.com/steinfletcher/apitest v1.5.15
8+
github.com/spf13/viper v1.18.2
129
github.com/stretchr/testify v1.8.4
13-
go.dedis.ch/dela v0.0.0-20230814162536-4bcfa7981c82
14-
go.dedis.ch/kyber/v3 v3.1.0
15-
go.mongodb.org/mongo-driver v1.13.1
16-
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
10+
go.dedis.ch/dela v0.0.0-20240330091609-33fd2a361d2d
11+
go.dedis.ch/kyber/v3 v3.1.1-0.20231024084410-31ea167adbbb
12+
go.dedis.ch/purb-db v0.0.1
13+
go.mongodb.org/mongo-driver v1.13.2
14+
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
1715
)
1816

1917
require (
@@ -29,18 +27,17 @@ require (
2927
github.com/klauspost/compress v1.17.0 // indirect
3028
github.com/magiconair/properties v1.8.7 // indirect
3129
github.com/mattn/go-colorable v0.1.13 // indirect
32-
github.com/mattn/go-isatty v0.0.19 // indirect
33-
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
30+
github.com/mattn/go-isatty v0.0.20 // indirect
3431
github.com/mitchellh/mapstructure v1.5.0 // indirect
3532
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
3633
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
3734
github.com/opentracing/opentracing-go v1.2.0 // indirect
38-
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
35+
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
3936
github.com/pkg/errors v0.9.1 // indirect
4037
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
41-
github.com/prometheus/client_golang v1.17.0 // indirect
38+
github.com/prometheus/client_golang v1.19.0 // indirect
4239
github.com/prometheus/client_model v0.5.0 // indirect
43-
github.com/prometheus/common v0.44.0 // indirect
40+
github.com/prometheus/common v0.48.0 // indirect
4441
github.com/prometheus/procfs v0.12.0 // indirect
4542
github.com/rs/xid v1.5.0 // indirect
4643
github.com/russross/blackfriday/v2 v2.1.0 // indirect
@@ -50,6 +47,7 @@ require (
5047
github.com/spf13/afero v1.11.0 // indirect
5148
github.com/spf13/cast v1.6.0 // indirect
5249
github.com/spf13/pflag v1.0.5 // indirect
50+
github.com/stretchr/objx v0.5.2 // indirect
5351
github.com/subosito/gotenv v1.6.0 // indirect
5452
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
5553
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
@@ -60,15 +58,16 @@ require (
6058
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
6159
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
6260
go.dedis.ch/fixbuf v1.0.3 // indirect
61+
go.dedis.ch/libpurb v0.0.0-20231108133532-c70e1b84b632 // indirect
6362
go.dedis.ch/protobuf v1.0.11 // indirect
6463
go.etcd.io/bbolt v1.3.7 // indirect
6564
go.uber.org/atomic v1.11.0 // indirect
6665
go.uber.org/multierr v1.9.0 // indirect
67-
golang.org/x/crypto v0.16.0 // indirect
68-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
69-
golang.org/x/net v0.19.0 // indirect
66+
golang.org/x/crypto v0.21.0 // indirect
67+
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 // indirect
68+
golang.org/x/net v0.21.0 // indirect
7069
golang.org/x/sync v0.5.0 // indirect
71-
golang.org/x/sys v0.15.0 // indirect
70+
golang.org/x/sys v0.18.0 // indirect
7271
golang.org/x/text v0.14.0 // indirect
7372
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
7473
google.golang.org/grpc v1.59.0 // indirect

0 commit comments

Comments
 (0)