Skip to content

Commit 5c790cc

Browse files
committed
online: server: store ceremony log to file
Ceremony log is necessary for ceremony atestation. It documents the course of contributions and contains beacon, which is necessary for regenerating keys out of original contributions in the offline mode. The log is stored in persistent storage in a structured JSON format. It is also pretty-printed to stdout during the ceremony. Signed-off-by: Wojciech Zmuda <[email protected]>
1 parent 4819350 commit 5c790cc

File tree

5 files changed

+77
-28
lines changed

5 files changed

+77
-28
lines changed

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/consensys/gnark v0.13.0
77
github.com/consensys/gnark-crypto v0.18.0
88
github.com/drand/go-clients v0.2.3
9-
github.com/golang/protobuf v1.5.4
9+
github.com/rs/zerolog v1.34.0
1010
github.com/stretchr/testify v1.10.0
1111
github.com/urfave/cli/v3 v3.3.8
1212
github.com/worldcoin/ptau-deserializer v0.2.0
@@ -43,8 +43,6 @@ require (
4343
github.com/prometheus/common v0.65.0 // indirect
4444
github.com/prometheus/procfs v0.17.0 // indirect
4545
github.com/ronanh/intcomp v1.1.1 // indirect
46-
github.com/rs/zerolog v1.34.0 // indirect
47-
github.com/stretchr/objx v0.5.2 // indirect
4846
github.com/x448/float16 v0.8.4 // indirect
4947
go.dedis.ch/fixbuf v1.0.3 // indirect
5048
go.uber.org/multierr v1.11.0 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ github.com/ronanh/intcomp v1.1.1/go.mod h1:7FOLy3P3Zj3er/kVrU/pl+Ql7JFZj7bwliMGk
113113
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
114114
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
115115
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
116-
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
117-
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
118116
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
119117
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
120118
github.com/urfave/cli/v3 v3.3.8 h1:BzolUExliMdet9NlJ/u4m5vHSotJ3PzEqSAZ1oPMa/E=

online/actions/server.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package actions
22

33
import (
44
"context"
5+
"io"
56
"log"
67
"os"
78
"os/signal"
89
"syscall"
910

11+
"github.com/rs/zerolog"
1012
"github.com/urfave/cli/v3"
1113

1214
"github.com/reilabs/trusted-setup/offline/phase1"
@@ -40,12 +42,25 @@ func Server(_ context.Context, cmd *cli.Command) error {
4042
return err
4143
}
4244

45+
logFile, err := os.CreateTemp("", "")
46+
if err != nil {
47+
return err
48+
}
49+
defer func(logFile *os.File) {
50+
err = logFile.Close()
51+
if err != nil {
52+
log.Printf("Error closing log file writer: %v", err)
53+
}
54+
}(logFile)
55+
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006/01/02 15:04:05"}
56+
ceremonyLogger := zerolog.New(io.MultiWriter(consoleWriter, logFile)).With().Timestamp().Logger()
57+
4358
beaconProvider, err := randomness.New()
4459
if err != nil {
4560
return err
4661
}
4762
beacon := beaconProvider.GetBeacon()
48-
log.Printf("Beacon: %x", beacon)
63+
ceremonyLogger.Info().Hex("beacon", beacon).Send()
4964

5065
store := storage.NewTmpfs(config.CeremonyName)
5166

@@ -61,6 +76,7 @@ func Server(_ context.Context, cmd *cli.Command) error {
6176
last,
6277
contributors_manager.New(),
6378
),
79+
&ceremonyLogger,
6480
)
6581

6682
s := server.New(service)
@@ -83,6 +99,14 @@ func Server(_ context.Context, cmd *cli.Command) error {
8399
log.Printf("No contributions received")
84100
}
85101

102+
_, err = logFile.Seek(0, 0)
103+
if err != nil {
104+
log.Printf("Rewinding log file failed")
105+
}
106+
if _, err = store.Save("log", logFile); err != nil {
107+
log.Printf("Storing ceremony log failed")
108+
}
109+
86110
log.Println("Artifacts generated in the ceremony:")
87111
files, err := store.List()
88112
if err != nil {

online/server/ceremony_service/ceremony_service.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package ceremony_service
33

44
import (
55
"context"
6-
"log"
76

7+
"github.com/rs/zerolog"
88
"google.golang.org/grpc/peer"
99

1010
"github.com/reilabs/trusted-setup/online/api"
@@ -17,17 +17,25 @@ type ceremonyService struct {
1717

1818
name string
1919
coordinator coordinator.Coordinator
20+
log *zerolog.Logger
2021
}
2122

2223
// New returns a new instance of CeremonyServiceServer.
2324
//
24-
// # The returned object can be passed to the gRPC server constructor
25+
// Accepts a name of the ceremony, ceremony coordinator instance and a logger.
26+
// The ceremony name is sent to contributors after they connect.
27+
// The coordinator keeps track of incoming contributions, accepts new contribution
28+
// candidates and validates them.
29+
// The logger will accept log entries with crucial steps of the ceremony, that can
30+
// be useful during attestation or keys recovery.
2531
//
26-
// Accepts a name of the ceremony and a ceremony coordinator instance.
32+
// The returned object can be passed to the gRPC server constructor
2733
func New(
28-
name string, coordinator coordinator.Coordinator,
34+
name string, coordinator coordinator.Coordinator, log *zerolog.Logger,
2935
) api.CeremonyServiceServer {
30-
return &ceremonyService{name: name, coordinator: coordinator}
36+
log.Info().Str("name", name).Msg("new ceremony started")
37+
38+
return &ceremonyService{name: name, coordinator: coordinator, log: log}
3139
}
3240

3341
func clientAddressFromContext(ctx context.Context) string {
@@ -40,47 +48,65 @@ func clientAddressFromContext(ctx context.Context) string {
4048
return clientIP
4149
}
4250

43-
func onContributorPositionUpdate(newPosition int, clientIp string, stream api.CeremonyService_ContributeServer) {
44-
log.Printf("contributor %s got slot %d in the queue", clientIp, newPosition)
45-
if err := stream.Send(api.NewTurnNotification(newPosition)); err != nil {
46-
log.Printf("failed to send position update to %s: %v", clientIp, err)
47-
}
48-
}
49-
5051
// Contribute implements the flow of a single contribution coming from a contributor client.
5152
func (s *ceremonyService) Contribute(
5253
stream api.CeremonyService_ContributeServer,
5354
) error {
55+
clientIp := clientAddressFromContext(stream.Context())
56+
s.log.Info().
57+
Str("ip", clientIp).
58+
Msg("new contributor connected")
59+
5460
err := stream.Send(api.NewHello(s.name))
5561
if err != nil {
5662
return err
5763
}
5864

59-
clientIp := clientAddressFromContext(stream.Context())
6065
waitForThisContributorsTurn := s.coordinator.AddContributor(
6166
func(newPosition int) {
62-
onContributorPositionUpdate(newPosition, clientIp, stream)
67+
err = stream.Send(api.NewTurnNotification(newPosition))
68+
s.log.Info().
69+
Int("newQueuePosition", newPosition).
70+
Str("ip", clientIp).
71+
Err(err).
72+
Msg("contributor position update")
6373
},
6474
)
6575

6676
waitForThisContributorsTurn()
6777

68-
log.Printf("Sending last contribution to %s", clientIp)
78+
s.log.Info().
79+
Str("ip", clientIp).
80+
Msg("sending last accepted contribution")
6981
n, err := s.coordinator.WriteLastContribution(stream_utils.NewStreamWriter(stream))
7082
if err != nil {
71-
log.Printf("error sending last contribution to %s", clientIp)
83+
s.log.Info().
84+
Str("ip", clientIp).
85+
Err(err)
7286
return err
7387
}
74-
log.Printf("Sent %d bytes", n)
88+
s.log.Info().
89+
Str("ip", clientIp).
90+
Int64("size", n).
91+
Msg("sent last accepted contribution")
7592

76-
log.Printf("Contribution to be received from %s", clientIp)
93+
s.log.Info().
94+
Str("ip", clientIp).
95+
Msg("receiving new contribution candidate")
7796
n, err = s.coordinator.ReadNextContribution(stream_utils.NewStreamReader(stream))
78-
log.Printf("Received %d bytes", n)
7997
if err != nil {
80-
log.Printf("%s: %v", clientIp, err)
98+
s.log.Info().
99+
Str("ip", clientIp).
100+
Int64("size", n).
101+
Err(err).
102+
Msg("new contribution candidate rejected")
81103
return stream.Send(api.NewValidationResponse(err))
82104
}
83-
log.Printf("Contribution %d from %s accepted", s.coordinator.GetContributionsCount(), clientIp)
105+
106+
s.log.Info().
107+
Str("ip", clientIp).
108+
Int64("size", n).
109+
Msg("new contribution candidate accepted")
84110

85111
return stream.Send(api.NewValidationResponse(nil))
86112
}

online/test/online_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package online_test
22

33
import (
44
"bytes"
5+
"io"
56
"strconv"
67
"strings"
78
"sync"
89
"testing"
910

1011
"github.com/consensys/gnark/backend/groth16/bn254/mpcsetup"
12+
"github.com/rs/zerolog"
1113
"github.com/stretchr/testify/assert"
1214

1315
"github.com/reilabs/trusted-setup/offline/phase1"
@@ -61,7 +63,8 @@ func testStartServer(t *testing.T) {
6163
last, err = contribution.New(p1, ccs, store, beacon)
6264
assert.NoError(t, err)
6365

64-
service := ceremony_service.New(config.CeremonyName, coordinator.New(last, contributors_manager.New()))
66+
muteLogger := zerolog.New(io.Discard)
67+
service := ceremony_service.New(config.CeremonyName, coordinator.New(last, contributors_manager.New()), &muteLogger)
6568

6669
serv = server.New(service)
6770

0 commit comments

Comments
 (0)