Skip to content

Commit

Permalink
Merge pull request #261 from dedis/extend_crypto
Browse files Browse the repository at this point in the history
Extend crypto to support SHA3-224
  • Loading branch information
jbsv authored Aug 7, 2023
2 parents f932657 + ceea23f commit 19cf20a
Show file tree
Hide file tree
Showing 25 changed files with 205 additions and 90 deletions.
25 changes: 20 additions & 5 deletions contracts/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const (
// credentialAllCommand defines the credential command that is allowed to
// perform all commands.
credentialAllCommand = "all"

// contractKeyPrefix is used to prefix keys in the K/V store.
contractKeyPrefix = "VALU" // intentionally 4 bytes only, not a typo!
)

// Command defines a type of command for the value contract
Expand Down Expand Up @@ -172,14 +175,17 @@ func (c valueCommand) write(snap store.Snapshot, step execution.Step) error {
return xerrors.Errorf("'%s' not found in tx arg", ValueArg)
}

err := snap.Set(key, value)
snapKey := prefix(key)
err := snap.Set(snapKey, value)
if err != nil {
return xerrors.Errorf("failed to set value: %v", err)
}

c.index[string(key)] = struct{}{}

dela.Logger.Info().Str("contract", ContractName).Msgf("setting %x=%s", key, value)
dela.Logger.Info().
Str("contract", ContractName).
Msgf("setting value %x=%s", snapKey, value)

return nil
}
Expand All @@ -191,7 +197,7 @@ func (c valueCommand) read(snap store.Snapshot, step execution.Step) error {
return xerrors.Errorf("'%s' not found in tx arg", KeyArg)
}

val, err := snap.Get(key)
val, err := snap.Get(prefix(key))
if err != nil {
return xerrors.Errorf("failed to get key '%s': %v", key, err)
}
Expand All @@ -208,13 +214,18 @@ func (c valueCommand) delete(snap store.Snapshot, step execution.Step) error {
return xerrors.Errorf("'%s' not found in tx arg", KeyArg)
}

err := snap.Delete(key)
snapKey := prefix(key)
err := snap.Delete(snapKey)
if err != nil {
return xerrors.Errorf("failed to delete key '%x': %v", key, err)
}

delete(c.index, string(key))

dela.Logger.Info().
Str("contract", ContractName).
Msgf("deleting value %x", snapKey)

return nil
}

Expand All @@ -223,7 +234,7 @@ func (c valueCommand) list(snap store.Snapshot) error {
res := []string{}

for k := range c.index {
v, err := snap.Get([]byte(k))
v, err := snap.Get(prefix([]byte(k)))
if err != nil {
return xerrors.Errorf("failed to get key '%s': %v", k, err)
}
Expand All @@ -247,3 +258,7 @@ func (h infoLog) Write(p []byte) (int, error) {

return len(p), nil
}

func prefix(key []byte) []byte {
return append([]byte(contractKeyPrefix), key...)
}
12 changes: 6 additions & 6 deletions contracts/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCommand_Write(t *testing.T) {
_, found = contract.index["dummy"]
require.True(t, found)

res, err := snap.Get([]byte("dummy"))
res, err := snap.Get(prefix([]byte("dummy")))
require.NoError(t, err)
require.Equal(t, "value", string(res))
}
Expand All @@ -98,7 +98,7 @@ func TestCommand_Read(t *testing.T) {
require.EqualError(t, err, fake.Err("failed to get key 'dummy'"))

snap := fake.NewSnapshot()
snap.Set(key, []byte("value"))
snap.Set(prefix(key), []byte("value"))

buf := &bytes.Buffer{}
cmd.Contract.printer = buf
Expand Down Expand Up @@ -127,13 +127,13 @@ func TestCommand_Delete(t *testing.T) {
require.EqualError(t, err, fake.Err("failed to delete key '"+keyHex+"'"))

snap := fake.NewSnapshot()
snap.Set(key, []byte("value"))
snap.Set(prefix(key), []byte("value"))
contract.index[keyStr] = struct{}{}

err = cmd.delete(snap, makeStep(t, KeyArg, keyStr))
require.NoError(t, err)

res, err := snap.Get(key)
res, err := snap.Get(prefix(key))
require.Nil(t, err)
require.Nil(t, res)

Expand All @@ -158,8 +158,8 @@ func TestCommand_List(t *testing.T) {
}

snap := fake.NewSnapshot()
snap.Set([]byte(key1), []byte("value1"))
snap.Set([]byte(key2), []byte("value2"))
snap.Set(prefix([]byte(key1)), []byte("value1"))
snap.Set(prefix([]byte(key2)), []byte("value2"))

err := cmd.list(snap)
require.NoError(t, err)
Expand Down
35 changes: 25 additions & 10 deletions core/ordering/cosipbft/cosipbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type ServiceParam struct {
// NewService starts a new ordering service.
func NewService(param ServiceParam, opts ...ServiceOption) (*Service, error) {
tmpl := serviceTemplate{
hashFac: crypto.NewSha256Factory(),
hashFac: crypto.NewHashFactory(crypto.Sha256),
genesis: blockstore.NewGenesisStore(),
blocks: blockstore.NewInMemory(),
}
Expand All @@ -173,7 +173,8 @@ func NewService(param ServiceParam, opts ...ServiceOption) (*Service, error) {
proc.blocks = tmpl.blocks
proc.genesis = tmpl.genesis
proc.pool = param.Pool
proc.rosterFac = authority.NewFactory(param.Mino.GetAddressFactory(), param.Cosi.GetPublicKeyFactory())
proc.rosterFac = authority.NewFactory(param.Mino.GetAddressFactory(),
param.Cosi.GetPublicKeyFactory())
proc.tree = blockstore.NewTreeCache(param.Tree)
proc.access = param.Access
proc.logger = dela.Logger.With().Str("addr", param.Mino.GetAddress().String()).Logger()
Expand All @@ -193,7 +194,8 @@ func NewService(param ServiceParam, opts ...ServiceOption) (*Service, error) {
proc.pbftsm = pbft.NewStateMachine(pcparam)

blockFac := types.NewBlockFactory(param.Validation.GetFactory())
csFac := authority.NewChangeSetFactory(param.Mino.GetAddressFactory(), param.Cosi.GetPublicKeyFactory())
csFac := authority.NewChangeSetFactory(param.Mino.GetAddressFactory(),
param.Cosi.GetPublicKeyFactory())
linkFac := types.NewLinkFactory(blockFac, param.Cosi.GetSignatureFactory(), csFac)
chainFac := types.NewChainFactory(linkFac)

Expand Down Expand Up @@ -491,23 +493,30 @@ func (s *Service) doRound(ctx context.Context) error {
}

if s.me.Equal(leader) {
s.logger.Debug().Msgf("Starting a leader round with a %.1f seconds timeout", timeout.Seconds())
s.logger.Debug().Msgf("Starting a leader round with a %.1f seconds timeout",
timeout.Seconds())
return s.doLeaderRound(ctx, roster, timeout)
}

s.logger.Debug().Msgf("Starting a follower round with a %.1f seconds timeout", timeout.Seconds())
s.logger.Debug().Msgf("Starting a follower round with a %.1f seconds timeout",
timeout.Seconds())
return s.doFollowerRound(ctx, roster)
}

func (s *Service) doLeaderRound(ctx context.Context, roster authority.Authority, timeout time.Duration) error {
func (s *Service) doLeaderRound(
ctx context.Context,
roster authority.Authority,
timeout time.Duration,
) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

s.logger.Debug().Uint64("index", s.blocks.Len()).Msg("round has started")

// Send a synchronization to the roster so that they can learn about the
// latest block of the chain.
err := s.sync.Sync(ctx, roster, blocksync.Config{MinHard: threshold.ByzantineThreshold(roster.Len())})
err := s.sync.Sync(ctx, roster,
blocksync.Config{MinHard: threshold.ByzantineThreshold(roster.Len())})
if err != nil {
return xerrors.Errorf("sync failed: %v", err)
}
Expand Down Expand Up @@ -556,7 +565,8 @@ func (s *Service) doFollowerRound(ctx context.Context, roster authority.Authorit
for resp := range resps {
_, err = resp.GetMessageOrError()
if err != nil {
s.logger.Warn().Err(err).Str("to", resp.GetFrom().String()).Msg("view propagation failure")
s.logger.Warn().Err(err).Str("to",
resp.GetFrom().String()).Msg("view propagation failure")
}
}

Expand Down Expand Up @@ -712,7 +722,11 @@ func (s *Service) prepareViews() map[mino.Address]types.ViewMessage {
return msgs
}

func (s *Service) prepareData(txs []txn.Transaction) (data validation.Result, id types.Digest, err error) {
func (s *Service) prepareData(txs []txn.Transaction) (
data validation.Result,
id types.Digest,
err error,
) {
var stageTree hashtree.StagingTree

stageTree, err = s.tree.Get().Stage(func(snap store.Snapshot) error {
Expand Down Expand Up @@ -747,7 +761,8 @@ func (s *Service) wakeUp(ctx context.Context, ro authority.Authority) error {
return xerrors.Errorf("read genesis failed: %v", err)
}

resps, err := s.rpc.Call(ctx, types.NewGenesisMessage(genesis), mino.NewAddresses(changeset.GetNewAddresses()...))
resps, err := s.rpc.Call(ctx, types.NewGenesisMessage(genesis),
mino.NewAddresses(changeset.GetNewAddresses()...))
if err != nil {
return xerrors.Errorf("rpc failed: %v", err)
}
Expand Down
16 changes: 8 additions & 8 deletions core/ordering/cosipbft/cosipbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestService_Setup(t *testing.T) {

srvc := &Service{processor: newProcessor()}
srvc.rpc = rpc
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.tree = blockstore.NewTreeCache(fakeTree{})
srvc.genesis = blockstore.NewGenesisStore()
srvc.access = fakeAccess{}
Expand Down Expand Up @@ -623,7 +623,7 @@ func TestService_DoPBFT(t *testing.T) {
srvc.val = fakeValidation{}
srvc.blocks = blockstore.NewInMemory()
srvc.genesis = blockstore.NewGenesisStore()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.pbftsm = fakeSM{}
srvc.rosterFac = authority.NewFactory(fake.AddressFactory{}, fake.PublicKeyFactory{})
srvc.actor = fakeCosiActor{}
Expand Down Expand Up @@ -707,7 +707,7 @@ func TestService_FailPrepare_DoPBFT(t *testing.T) {
srvc.tree = blockstore.NewTreeCache(fakeTree{})
srvc.pbftsm = fakeSM{err: fake.GetError()}
srvc.pool = mem.NewPool()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.blocks = blockstore.NewInMemory()

srvc.pool.Add(makeTx(t, 0, fake.NewSigner()))
Expand All @@ -725,7 +725,7 @@ func TestService_FailReadRoster_DoPBFT(t *testing.T) {
srvc.tree = blockstore.NewTreeCache(fakeTree{err: fake.GetError()})
srvc.pbftsm = fakeSM{}
srvc.pool = mem.NewPool()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.blocks = blockstore.NewInMemory()

srvc.pool.Add(makeTx(t, 0, fake.NewSigner()))
Expand All @@ -743,7 +743,7 @@ func TestService_FailPrepareSig_DoPBFT(t *testing.T) {
srvc.tree = blockstore.NewTreeCache(fakeTree{})
srvc.pbftsm = fakeSM{}
srvc.pool = mem.NewPool()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.blocks = blockstore.NewInMemory()
srvc.actor = fakeCosiActor{err: fake.GetError()}
srvc.rosterFac = authority.NewFactory(fake.AddressFactory{}, fake.PublicKeyFactory{})
Expand All @@ -763,7 +763,7 @@ func TestService_FailCommitSign_DoPBFT(t *testing.T) {
srvc.tree = blockstore.NewTreeCache(fakeTree{})
srvc.pbftsm = fakeSM{}
srvc.pool = mem.NewPool()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.blocks = blockstore.NewInMemory()
srvc.actor = fakeCosiActor{
err: fake.GetError(),
Expand All @@ -786,7 +786,7 @@ func TestService_FailPropagation_DoPBFT(t *testing.T) {
srvc.tree = blockstore.NewTreeCache(fakeTree{})
srvc.pbftsm = fakeSM{}
srvc.pool = mem.NewPool()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.blocks = blockstore.NewInMemory()
srvc.actor = fakeCosiActor{}
srvc.rosterFac = authority.NewFactory(fake.AddressFactory{}, fake.PublicKeyFactory{})
Expand All @@ -810,7 +810,7 @@ func TestService_FailWakeUp_DoPBFT(t *testing.T) {
srvc.tree = blockstore.NewTreeCache(fakeTree{})
srvc.pbftsm = fakeSM{}
srvc.pool = mem.NewPool()
srvc.hashFactory = crypto.NewSha256Factory()
srvc.hashFactory = crypto.NewHashFactory(crypto.Sha256)
srvc.blocks = blockstore.NewInMemory()
srvc.actor = fakeCosiActor{}
srvc.rosterFac = authority.NewFactory(fake.AddressFactory{}, fake.PublicKeyFactory{})
Expand Down
9 changes: 7 additions & 2 deletions core/ordering/cosipbft/pbft/pbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func NewStateMachine(param StateMachineParam) StateMachine {
return &pbftsm{
logger: param.Logger,
watcher: core.NewWatcher(),
hashFac: crypto.NewSha256Factory(),
hashFac: crypto.NewHashFactory(crypto.Sha256),
val: param.Validation,
verifierFac: param.VerifierFactory,
signer: param.Signer,
Expand Down Expand Up @@ -622,7 +622,12 @@ func (m *pbftsm) Watch(ctx context.Context) <-chan State {
return ch
}

func (m *pbftsm) verifyPrepare(tree hashtree.Tree, block types.Block, r *round, ro authority.Authority) error {
func (m *pbftsm) verifyPrepare(
tree hashtree.Tree,
block types.Block,
r *round,
ro authority.Authority,
) error {
stageTree, err := tree.Stage(func(snap store.Snapshot) error {
txs := block.GetTransactions()
rejected := 0
Expand Down
4 changes: 2 additions & 2 deletions core/ordering/cosipbft/pbft/pbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestStateMachine_FailReadRosterInStageTree_Prepare(t *testing.T) {
},
genesis: blockstore.NewGenesisStore(),
blocks: blockstore.NewInMemory(),
hashFac: crypto.NewSha256Factory(),
hashFac: crypto.NewHashFactory(crypto.Sha256),
watcher: core.NewWatcher(),
}

Expand Down Expand Up @@ -564,7 +564,7 @@ func TestStateMachine_FailStoreBlock_Finalize(t *testing.T) {
genesis: blockstore.NewGenesisStore(),
blocks: badBlockStore{},
db: db,
hashFac: crypto.NewSha256Factory(),
hashFac: crypto.NewHashFactory(crypto.Sha256),
round: round{
prepareSig: fake.Signature{},
tree: tree.(hashtree.StagingTree),
Expand Down
4 changes: 2 additions & 2 deletions core/ordering/cosipbft/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewGenesis(ro authority.Authority, opts ...GenesisOption) (Genesis, error)
roster: ro,
treeRoot: Digest{},
},
hashFactory: crypto.NewSha256Factory(),
hashFactory: crypto.NewHashFactory(crypto.Sha256),
}

for _, opt := range opts {
Expand Down Expand Up @@ -231,7 +231,7 @@ func NewBlock(data validation.Result, opts ...BlockOption) (Block, error) {
data: data,
treeRoot: Digest{},
},
hashFactory: crypto.NewSha256Factory(),
hashFactory: crypto.NewHashFactory(crypto.Sha256),
}

for _, opt := range opts {
Expand Down
8 changes: 5 additions & 3 deletions core/ordering/cosipbft/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewForwardLink(from, to Digest, opts ...LinkOption) (Link, error) {
to: to,
changeset: authority.NewChangeSet(),
},
hashFac: crypto.NewSha256Factory(),
hashFac: crypto.NewHashFactory(crypto.Sha256),
}

for _, opt := range opts {
Expand Down Expand Up @@ -230,8 +230,10 @@ type linkFac struct {
}

// NewLinkFactory creates a new block link factory.
func NewLinkFactory(blockFac serde.Factory,
sigFac crypto.SignatureFactory, csFac authority.ChangeSetFactory) LinkFactory {
func NewLinkFactory(
blockFac serde.Factory,
sigFac crypto.SignatureFactory, csFac authority.ChangeSetFactory,
) LinkFactory {

return linkFac{
blockFac: blockFac,
Expand Down
2 changes: 1 addition & 1 deletion core/ordering/pow/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewBlock(ctx context.Context, data validation.Result, opts ...BlockOption)
Block: Block{
data: data,
},
hashFactory: crypto.NewSha256Factory(),
hashFactory: crypto.NewHashFactory(crypto.Sha256),
difficulty: Difficulty,
}

Expand Down
Loading

0 comments on commit 19cf20a

Please sign in to comment.