Skip to content

Commit

Permalink
Get last config block API (#180)
Browse files Browse the repository at this point in the history
Signed-off-by: May Rosenbaum <[email protected]>
  • Loading branch information
MayRosenbaum authored Nov 30, 2023
1 parent a28c0c2 commit 40e0e0a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/bcdb/config_tx_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type ConfigTxContext interface {
// pending config was set is permitted. Those methods are applied to the pending config (e.g. AddAdmin() will add
// an admin, etc.).
SetClusterConfig(newConfig *types.ClusterConfig) error

// GetLastConfigBlock returns the last config block.
GetLastConfigBlock() ([]byte, error)
}

type configTxContext struct {
Expand Down Expand Up @@ -347,6 +350,26 @@ func (c *configTxContext) SetClusterConfig(newConfig *types.ClusterConfig) error
return nil
}

func (c *configTxContext) GetLastConfigBlock() ([]byte, error) {
configResponseEnv := &types.GetConfigBlockResponseEnvelope{}
path := constants.GetLastConfigBlock
err := c.handleRequest(
path,
&types.GetConfigBlockQuery{
UserId: c.userID,
},
configResponseEnv,
)
if err != nil {
c.logger.Errorf("failed to execute cluster config query path %s, due to %s", path, err)
return nil, err
}

confResp := configResponseEnv.GetResponse()

return confResp.GetBlock(), nil
}

func (c *configTxContext) queryClusterConfig() error {
if c.oldConfig != nil {
return nil
Expand Down
94 changes: 94 additions & 0 deletions pkg/bcdb/config_tx_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"github.com/golang/protobuf/proto"

sdkConfig "github.com/hyperledger-labs/orion-sdk-go/pkg/config"
"github.com/hyperledger-labs/orion-server/pkg/server/testutils"
"github.com/hyperledger-labs/orion-server/pkg/types"
Expand Down Expand Up @@ -762,3 +764,95 @@ func TestConfigTx_CommitAbortFinality(t *testing.T) {
}
}
}

func TestGetLastConfigBlock(t *testing.T) {
cryptoDir := testutils.GenerateTestCrypto(t, []string{"admin", "server"})
testServer, _, _, err := SetupTestServer(t, cryptoDir)
defer func() {
if testServer != nil {
_ = testServer.Stop()
}
}()
require.NoError(t, err)
StartTestServer(t, testServer)

serverPort, err := testServer.Port()
require.NoError(t, err)

bcdb := createDBInstance(t, cryptoDir, serverPort)
session := openUserSession(t, bcdb, "admin", cryptoDir)

t.Run("GetLastConfigBlock returns genesis config block", func(t *testing.T) {
tx, err := session.ConfigTx()
require.NoError(t, err)

txBlk, err := tx.GetLastConfigBlock()
require.NoError(t, err)
require.NotNil(t, txBlk)

var block = &types.Block{}
err = proto.Unmarshal(txBlk, block)
require.NoError(t, err)
require.NotNil(t, block)
require.Equal(t, uint64(1), block.GetHeader().GetBaseHeader().GetNumber())
require.Equal(t, 1, len(block.GetConfigTxEnvelope().GetPayload().GetNewConfig().GetNodes()))
})

t.Run("GetLastConfigBlock returns the last config block", func(t *testing.T) {
// 1. tx1 - set raft config MaxInflightBlocks param
tx, err := session.ConfigTx()
require.NoError(t, err)

clusterConfig, version, err := tx.GetClusterConfig()
require.NoError(t, err)
require.NotNil(t, version)
raftConf := clusterConfig.GetConsensusConfig().GetRaftConfig()
raftConf.MaxInflightBlocks++
err = tx.UpdateRaftConfig(raftConf)
require.NoError(t, err)

txID, receiptEnv, err := tx.Commit(true)
require.NoError(t, err)
require.True(t, txID != "")
require.NotNil(t, receiptEnv)

txBlk, err := tx.GetLastConfigBlock()
require.NoError(t, err)
require.NotNil(t, txBlk)

// check the last config block
var block = &types.Block{}
err = proto.Unmarshal(txBlk, block)
require.NoError(t, err)
require.NotNil(t, block)
require.Equal(t, uint64(2), block.GetHeader().GetBaseHeader().GetNumber())
require.Equal(t, 1, len(block.GetConfigTxEnvelope().GetPayload().GetNewConfig().GetNodes()))

// 2. tx2 - set raft config HeartbeatTicks param
tx, err = session.ConfigTx()
require.NoError(t, err)

clusterConfig, version, err = tx.GetClusterConfig()
require.NoError(t, err)
require.NotNil(t, version)
raftConf = clusterConfig.GetConsensusConfig().GetRaftConfig()
raftConf.HeartbeatTicks++
err = tx.UpdateRaftConfig(raftConf)
require.NoError(t, err)

txID, receiptEnv, err = tx.Commit(true)
require.NoError(t, err)
require.True(t, txID != "")
require.NotNil(t, receiptEnv)

txBlk, err = tx.GetLastConfigBlock()
require.NoError(t, err)
require.NotNil(t, txBlk)

// check the last config block
err = proto.Unmarshal(txBlk, block)
require.NoError(t, err)
require.NotNil(t, block)
require.Equal(t, uint64(3), block.GetHeader().GetBaseHeader().GetNumber())
})
}
2 changes: 2 additions & 0 deletions pkg/bcdb/tx_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ func ResponseSelector(envelop ResponseEnvelop) (ResponseWithHeader, error) {
return envelop.(*types.GetDataRangeResponseEnvelope).GetResponse(), nil
case *types.GetTxResponseEnvelope:
return envelop.(*types.GetTxResponseEnvelope).GetResponse(), nil
case *types.GetConfigBlockResponseEnvelope:
return envelop.(*types.GetConfigBlockResponseEnvelope).GetResponse(), nil

default:
return nil, errors.Errorf("unknown response type %T", t)
Expand Down

0 comments on commit 40e0e0a

Please sign in to comment.